Skip to content

Commit 69d7b20

Browse files
author
Wiki
committed
fix
1 parent 6ea30c9 commit 69d7b20

File tree

12 files changed

+61
-109
lines changed

12 files changed

+61
-109
lines changed

quickjs-android/src/androidTest/java/com/quickjs/android/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

quickjs-android/src/androidTest/java/com/quickjs/android/JSArrayTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public void setUp() throws Exception {
2222

2323
@After
2424
public void tearDown() throws Exception {
25-
// array.close();
26-
// context.close();
27-
// quickJS.close();
25+
array.close();
26+
context.close();
27+
quickJS.close();
2828
}
2929

3030
@Test
@@ -80,7 +80,8 @@ public void getObject() {
8080
JSObject user = new JSObject(context);
8181
user.set("name", "Wiki");
8282
array.push(user);
83-
assertEquals("Wiki", array.getObject(0).getString("name"));
83+
String result = array.getObject(0).getString("name");
84+
assertEquals("Wiki", result);
8485
user.close();
8586
}
8687

quickjs-android/src/androidTest/java/com/quickjs/android/JSFunctionTest.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.quickjs.android;
22

3+
import org.junit.After;
34
import org.junit.Before;
45
import org.junit.Test;
56

@@ -17,12 +18,18 @@ public void setUp() throws Exception {
1718
context = quickJS.createContext();
1819
}
1920

21+
@After
22+
public void tearDown() throws Exception {
23+
// context.close();
24+
// quickJS.close();
25+
}
26+
2027
@Test
2128
public void testJavaCallback() {
22-
context.set("intFunction", new JSFunction(context, jsArray -> Integer.MAX_VALUE));
23-
context.set("doubleFunction", new JSFunction(context, jsArray -> Double.MAX_VALUE));
24-
context.set("boolFunction", new JSFunction(context, jsArray -> true));
25-
context.set("stringFunction", new JSFunction(context, jsArray -> "Hello"));
29+
context.set("intFunction", new JSFunction(context, (receiver, args) -> Integer.MAX_VALUE));
30+
context.set("doubleFunction", new JSFunction(context, (receiver, args) -> Double.MAX_VALUE));
31+
context.set("boolFunction", new JSFunction(context, (receiver, args) -> true));
32+
context.set("stringFunction", new JSFunction(context, (receiver, args) -> "Hello"));
2633
assertEquals(Integer.MAX_VALUE, context.executeIntegerFunction("intFunction", null));
2734
assertEquals(Double.MAX_VALUE, context.executeDoubleFunction("doubleFunction", null), 1);
2835
assertTrue(context.executeBooleanFunction("boolFunction", null));
@@ -32,11 +39,11 @@ public void testJavaCallback() {
3239

3340
@Test
3441
public void testJavaVoidCallback() {
35-
context.set("test", new JSFunction(context, array -> {
36-
assertEquals(1, array.getInteger(0));
37-
assertEquals(3.14, array.getDouble(1), 0);
38-
assertTrue(array.getBoolean(2));
39-
assertEquals("Hello", array.getString(3));
42+
context.set("test", new JSFunction(context, (receiver, args) -> {
43+
assertEquals(1, args.getInteger(0));
44+
assertEquals(3.14, args.getDouble(1), 0);
45+
assertTrue(args.getBoolean(2));
46+
assertEquals("Hello", args.getString(3));
4047
}));
4148
context.executeVoidScript("test(1, 3.14, true, 'Hello')", "file.js");
4249
}
@@ -53,7 +60,7 @@ public void call() {
5360
public void call2() {
5461
context.registerJavaMethod(new JavaVoidCallback() {
5562
@Override
56-
public void invoke(JSArray array) {
63+
public void invoke(JSObject receiver, JSArray array) {
5764
assertEquals("Hello", array.getString(0));
5865
}
5966
}, "test");
@@ -68,7 +75,7 @@ public void invoke(JSArray array) {
6875
public void call3() {
6976
context.registerJavaMethod(new JavaCallback() {
7077
@Override
71-
public Object invoke(JSArray array) {
78+
public Object invoke(JSObject receiver, JSArray array) {
7279
return array.getDouble(1);
7380
}
7481
}, "test");

quickjs-android/src/androidTest/java/com/quickjs/android/JSObjectTest.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,26 @@ public void getObject() {
8383

8484
@Test
8585
public void call2() {
86-
context.registerJavaMethod(new JavaVoidCallback() {
87-
@Override
88-
public void invoke(JSArray array) {
89-
assertEquals("Hello", array.getString(0));
90-
}
86+
context.registerJavaMethod((receiver, array) -> {
87+
assertEquals("Hello", array.getString(0));
9188
}, "test");
9289
JSArray args = new JSArray(context);
9390
args.push("Hello");
9491
args.push(3.14);
9592
context.executeVoidFunction("test", args);
93+
args.close();
9694
}
9795

9896
@Test
9997
public void call3() {
100-
context.registerJavaMethod(new JavaCallback() {
101-
@Override
102-
public Object invoke(JSArray array) {
103-
return array.getString(0);
104-
}
98+
context.registerJavaMethod((receiver, args) -> {
99+
return args.getString(0);
105100
}, "test");
106101
JSArray args = new JSArray(context);
107102
args.push("Hello");
108103
args.push(3.14);
109-
String result = context.executeStringFunction("test", args);
110-
assertEquals("Hello", result);
104+
assertEquals("Hello", context.executeStringFunction("test", args));
105+
args.close();
111106
}
112107

113108
@Test
@@ -130,9 +125,8 @@ public void executeDoubleFunction() {
130125
context.executeVoidScript("function test(data){ return data}", "file.js");
131126
JSArray array = new JSArray(context);
132127
array.push(3.14);
133-
double result = context.executeDoubleFunction("test", array);
128+
assertEquals(3.14, context.executeDoubleFunction("test", array), 0);
134129
array.close();
135-
assertEquals(3.14, result, 0);
136130
}
137131

138132
@Test
@@ -224,8 +218,8 @@ public void executeFunction2() {
224218
@Test
225219
public void console() {
226220
JSObject console = new JSObject(context);
227-
console.registerJavaMethod(array -> {
228-
assertEquals("Hello", array.getString(0));
221+
console.registerJavaMethod((receiver, args) -> {
222+
assertEquals("Hello", args.getString(0));
229223
}, "log");
230224
context.set("console", console);
231225
context.executeVoidScript("console.log('Hello')", "file.js");

quickjs-android/src/main/cpp/quickjs-jni.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,13 @@ JNIEXPORT jobject JNICALL
398398
Java_com_quickjs_android_QuickJS__1executeFunction(JNIEnv *env, jclass clazz, jlong context_ptr,
399399
jint expected_type, jobject object_handle,
400400
jstring name, jobject parameters_handle) {
401+
auto *ctx = reinterpret_cast<JSContext *>(context_ptr);
401402
JSValue result = executeJSFunction(env, context_ptr, object_handle, name, parameters_handle);
402-
return To_JObject(env, context_ptr, expected_type, result);
403+
jobject jResult = To_JObject(env, context_ptr, expected_type, result);
404+
if (!env->IsInstanceOf(jResult, jsValueCls)) {
405+
JS_FreeValue(ctx, result);
406+
}
407+
return jResult;
403408
}
404409

405410
JSValue

quickjs-android/src/main/java/com/quickjs/android/JSContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ void registerCallback(JavaVoidCallback callback, JSValue objectHandle, String js
7171
}
7272

7373
void registerCallback(JavaCallback callback, JSFunction functionHandle) {
74-
MethodDescriptor methodDescriptor = new MethodDescriptor();
74+
QuickJS.MethodDescriptor methodDescriptor = new QuickJS.MethodDescriptor();
7575
methodDescriptor.callback = callback;
7676
QuickJS.functionRegistry.put(functionHandle.tag, methodDescriptor);
7777
}
7878

7979
void registerCallback(JavaVoidCallback callback, JSFunction functionHandle) {
80-
MethodDescriptor methodDescriptor = new MethodDescriptor();
80+
QuickJS.MethodDescriptor methodDescriptor = new QuickJS.MethodDescriptor();
8181
methodDescriptor.voidCallback = callback;
8282
QuickJS.functionRegistry.put(functionHandle.tag, methodDescriptor);
8383
}

quickjs-android/src/main/java/com/quickjs/android/JSObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,6 @@ public String[] getKeys() {
188188

189189

190190
Object executeFunction(int expectedType, String name, JSArray parameters) {
191-
return QuickJS.executeFunction(context, expectedType, this, name, parameters);
191+
return QuickJS._executeFunction(context.getContextPtr(), expectedType, this, name, parameters);
192192
}
193193
}

quickjs-android/src/main/java/com/quickjs/android/JSValue.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class JSValue {
2525
public static final int UNDEFINED = 99;
2626

2727
protected JSContext context;
28-
// protected long objectHandle;
2928
// protected boolean released;
3029

3130
long tag;
@@ -49,36 +48,10 @@ public class JSValue {
4948
this.u_ptr = value.u_ptr;
5049
}
5150

52-
// typedef union JSValueUnion {
53-
// int32_t int32;
54-
// double float64;
55-
// void *ptr;
56-
// } JSValueUnion;
57-
//
58-
// typedef struct JSValue {
59-
// JSValueUnion u;
60-
// int64_t tag;
61-
// } JSValue;
62-
63-
// protected void initialize(long contextPtr, Object data) {
64-
// long objectHandle = this.context.initNewJSObject(contextPtr);
65-
// this.released = false;
66-
// this.addObjectReference(objectHandle);
67-
// }
68-
69-
// protected void addObjectReference(long objectHandle) {
70-
// this.objectHandle = objectHandle;
71-
// // TODO 考虑自动管理
72-
// }
73-
7451
long getContextPtr() {
7552
return context.getContextPtr();
7653
}
7754

78-
// long getHandle() {
79-
// return this.objectHandle;
80-
// }
81-
8255
public void close() {
8356
QuickJS._release(getContextPtr(), this);
8457
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.quickjs.android;
22

33
public interface JavaCallback {
4-
Object invoke(JSArray array);
4+
Object invoke(JSObject receiver,JSArray array);
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.quickjs.android;
22

33
public interface JavaVoidCallback {
4-
void invoke(JSArray array);
4+
void invoke(JSObject receiver, JSArray args);
55
}

0 commit comments

Comments
 (0)