Skip to content

Commit

Permalink
Merge pull request #2 from taoweiji/develop
Browse files Browse the repository at this point in the history
add auto gc
  • Loading branch information
taoweiji authored May 31, 2021
2 parents 0c52870 + 3587f15 commit 075c9ba
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 145 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ quickjs-android 是 QuickJS JavaScript 引擎的 Andoroid 接口框架,实现
##### 引入依赖

```groovy
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.0.0'
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.1.2'
```

##### 简单示例

```java
QuickJS quickJS = QuickJS.createV8Runtime();
JSContext jsContext = quickJS.createContext();
int result = jsContext.executeIntegerScript("var a = 2+10;\n a;", "file.js");
jsContext.close();
QuickJS quickJS = QuickJS.createRuntime();
JSContext context = quickJS.createContext();
int result = context.executeIntegerScript("var a = 2+10;\n a;", "file.js");
context.close();
quickJS.close();
```

Expand All @@ -31,7 +31,7 @@ quickJS.close();
运行环境,可以创建多个运行时环境,不同的环境之间不能共享对象

```java
QuickJS quickJS = QuickJS.createV8Runtime();
QuickJS quickJS = QuickJS.createRuntime();
```

##### JSContext
Expand Down Expand Up @@ -94,17 +94,17 @@ Log.e("QuickJS", String.valueOf(arr.length()));

| 方法 | 说明 |
| -------------------------------- | ---------- |
| static QuickJS createV8Runtime() | 创建运行时 |
| static QuickJS createRuntime() | 创建运行时 |
| JSContext createContext() | 创建上下文 |
| void close() | 销毁运行时 |
| void close() | 销毁引擎 |

#### JSValue

一切JS元素的基类

| 方法 | 说明 |
| -------------------------------- | ---------- |
| void close() | 销毁运行时 |


#### JSObject

Expand Down
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ android {

buildTypes {
release {
minifyEnabled false
minifyEnabled true
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug{

minifyEnabled false
}
}
compileOptions {
Expand Down
60 changes: 18 additions & 42 deletions example/src/main/java/com/quickjs/android/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,38 @@
package com.quickjs.android.example;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.quickjs.android.JSArray;
import com.quickjs.android.JSContext;
import com.quickjs.android.JSObject;
import com.quickjs.android.QuickJS;

import java.util.Arrays;
import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSObject;
import com.quickjs.QuickJS;

public class MainActivity extends AppCompatActivity {

private QuickJS quickJS;
private JSContext jsContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
quickJS = QuickJS.createRuntime();
jsContext = quickJS.createContext();
jsContext = quickJS.createContext();
test();
}

void test() {
QuickJS quickJS = QuickJS.createRuntime();
JSContext context = quickJS.createContext();
context.executeVoidScript("function test(data){ return data}", "file.js");
JSArray array = new JSArray(context);
array.push(Integer.MAX_VALUE);
int result = context.executeIntegerFunction("test", array);
array.close();
context.close();
quickJS.close();
jsContext.executeVoidScript("function test(data){ return data}", "file.js");
new JSArray(jsContext);
new JSObject(jsContext);
}

void testV8() {
V8 v8 = V8.createV8Runtime();
// v8.executeStringFunction()
// v8.contains();
V8Object jsObject = new V8Object(v8);
jsObject.registerJavaMethod(new com.eclipsesource.v8.JavaVoidCallback() {

@Override
public void invoke(V8Object v8Object, V8Array v8Array) {
// v8Array.getInteger(v8Array.getString())
}
}, "");
// jsObject.executeBooleanFunction();
// v8.executeVoidFunction();
// v8.add()
// v8.add()
// jsObject.add()
// v8.getInteger()
v8.close();

@Override
protected void onDestroy() {
super.onDestroy();
// jsContext.close();
quickJS.close();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package com.quickjs.android;
package com.quickjs;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSObject;
import com.quickjs.JSValue;
import com.quickjs.QuickJS;

import org.junit.After;
import org.junit.Before;
Expand All @@ -22,7 +28,6 @@ public void setUp() throws Exception {

@After
public void tearDown() throws Exception {
array.close();
context.close();
quickJS.close();
}
Expand Down Expand Up @@ -83,8 +88,6 @@ public void getObject() {
Object result = array.getObject(0);
// String result = array.getObject(0).getString("name");
// assertEquals("Wiki", result);
// user.close();
// user.close();
}

@Test
Expand All @@ -93,7 +96,6 @@ public void getArray() {
user.push("Wiki");
array.push(user);
assertEquals("Wiki", array.getArray(0).getString(0));
user.close();
}

@Test
Expand All @@ -116,8 +118,6 @@ public void test() {
// JSObject value = new JSObject(context);
// value.set("name", "Wiki");
// array.push(value);
// value.close();
// array.close();

JSArray array = new JSArray(context);
JSObject value = new JSObject(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package com.quickjs.android;
package com.quickjs;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSFunction;
import com.quickjs.JSObject;
import com.quickjs.QuickJS;

import org.junit.After;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package com.quickjs.android;
package com.quickjs;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSFunction;
import com.quickjs.JSObject;
import com.quickjs.JSValue;
import com.quickjs.JavaCallback;
import com.quickjs.JavaVoidCallback;
import com.quickjs.QuickJS;

import org.junit.After;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.quickjs.android;
package com.quickjs;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSObject;
import com.quickjs.JSValue;
import com.quickjs.JavaCallback;
import com.quickjs.QuickJS;

import org.junit.After;
import org.junit.Before;
Expand All @@ -21,7 +28,6 @@ public void setUp() throws Exception {

@After
public void tearDown() throws Throwable {
object.close();
context.close();
quickJS.close();
}
Expand Down Expand Up @@ -68,7 +74,6 @@ public void getArray() {
array.push(1);
object.set("key1", array);
assertEquals(1, object.getArray("key1").getInteger(0));
array.close();
}

@Test
Expand All @@ -94,7 +99,6 @@ public void executeIntegerFunction() {
JSArray array = new JSArray(context);
array.push(Integer.MAX_VALUE);
int result = context.executeIntegerFunction("test", array);
array.close();
assertEquals(Integer.MAX_VALUE, result);
}

Expand All @@ -104,7 +108,6 @@ public void executeDoubleFunction() {
JSArray array = new JSArray(context);
array.push(3.14);
assertEquals(3.14, context.executeDoubleFunction("test", array), 0);
array.close();
}

@Test
Expand All @@ -113,7 +116,6 @@ public void executeBooleanFunction() {
JSArray array = new JSArray(context);
array.push(true);
boolean result = context.executeBooleanFunction("test", array);
array.close();
assertTrue(result);
}

Expand All @@ -123,7 +125,6 @@ public void executeStringFunction() {
JSArray array = new JSArray(context);
array.push("Hello");
String result = context.executeStringFunction("test", array);
array.close();
assertEquals("Hello", result);
}

Expand Down Expand Up @@ -235,7 +236,5 @@ public void test2() {
value.set("name", "Wiki");
array.set("www", value);
array.set("www", JSValue.NULL());
value.close();
array.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package com.quickjs.android;
package com.quickjs;

import com.quickjs.JSArray;
import com.quickjs.JSContext;
import com.quickjs.JSValue;
import com.quickjs.QuickJS;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -26,12 +31,11 @@ public void tearDown() throws Exception {
public void isUndefined() {
JSArray array = new JSArray(context);
assertTrue(array.getObject(0).isUndefined());
array.close();
}

@Test
public void getJSType() {
assertEquals(JSValue.TYPE.UNDEFINED, JSValue.Undefined(context).getJSType());
public void getType() {
assertEquals(JSValue.TYPE.UNDEFINED, JSValue.Undefined(context).getType());
}

@Test
Expand All @@ -43,13 +47,11 @@ public void undefined() {
public void testEquals() {
JSArray array = new JSArray(context);
assertEquals(JSValue.Undefined(context), array.getObject(0));
array.close();
}

@Test
public void testHashCode() {
JSArray array = new JSArray(context);
assertEquals(JSValue.Undefined(context).hashCode(), array.getObject(0).hashCode());
array.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.quickjs.android;
package com.quickjs;

import org.junit.Test;
import com.quickjs.JSContext;
import com.quickjs.QuickJS;

import static org.junit.Assert.*;
import org.junit.Test;

public class QuickJSTest {

Expand Down
2 changes: 1 addition & 1 deletion quickjs-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quickjs.android">
package="com.quickjs">

</manifest>
Loading

0 comments on commit 075c9ba

Please sign in to comment.