Skip to content

Commit 9f4b3df

Browse files
author
Wiki
committed
JSContextTest
1 parent 6934703 commit 9f4b3df

File tree

6 files changed

+100
-63
lines changed

6 files changed

+100
-63
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.quickjs.android;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class JSContextTest {
10+
private JSContext context;
11+
private QuickJS quickJS;
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
quickJS = QuickJS.createRuntime();
16+
context = quickJS.createContext();
17+
}
18+
19+
@After
20+
public void tearDown() throws Exception {
21+
context.close();
22+
quickJS.close();
23+
}
24+
25+
26+
@Test
27+
public void executeScript() {
28+
Object result1 = context.executeScript("function test(data){return data};test(123)", "file.js");
29+
assertEquals(123, result1);
30+
Object result2 = context.executeScript("function test(data){return data};test(true)", "file.js");
31+
assertEquals(true, result2);
32+
Object result3 = context.executeScript("function test(data){return data};test(3.14)", "file.js");
33+
assertEquals(3.14, result3);
34+
Object result4 = context.executeScript("function test(data){return data};test('hello')", "file.js");
35+
assertEquals("hello", result4);
36+
Object result5 = context.executeScript("function test(data){return data};test(this)", "file.js");
37+
assertEquals(JSObject.class, result5.getClass());
38+
Object result6 = context.executeScript("function test(data){return data};test([''])", "file.js");
39+
assertEquals(JSArray.class, result6.getClass());
40+
Object result7 = context.executeScript("function test(data){return data};test", "file.js");
41+
assertEquals(JSFunction.class, result7.getClass());
42+
}
43+
44+
@Test
45+
public void executeIntegerScript() {
46+
int result1 = context.executeIntegerScript("function test(data){return data};test(123)", "file.js");
47+
assertEquals(123, result1);
48+
}
49+
50+
@Test
51+
public void executeDoubleScript() {
52+
double result3 = context.executeDoubleScript("function test(data){return data};test(3.14)", "file.js");
53+
assertEquals(3.14, result3, 0);
54+
}
55+
56+
@Test
57+
public void executeBooleanScript() {
58+
boolean result2 = context.executeBooleanScript("function test(data){return data};test(true)", "file.js");
59+
assertTrue(result2);
60+
61+
}
62+
63+
@Test
64+
public void executeStringScript() {
65+
String result4 = context.executeStringScript("function test(data){return data};test('hello')", "file.js");
66+
assertEquals("hello", result4);
67+
}
68+
69+
@Test
70+
public void executeVoidScript() {
71+
context.executeVoidScript("function test(data){return data};test('hello')", "file.js");
72+
}
73+
74+
@Test
75+
public void executeArrayScript() {
76+
JSArray result6 = context.executeArrayScript("function test(data){return data};test([''])", "file.js");
77+
assertEquals(JSArray.class, result6.getClass());
78+
}
79+
80+
@Test
81+
public void executeObjectScript() {
82+
JSObject result5 = context.executeObjectScript("function test(data){return data};test(this)", "file.js");
83+
assertEquals(JSObject.class, result5.getClass());
84+
Object result7 = context.executeScript("function test(data){return data};test", "file.js");
85+
assertEquals(JSFunction.class, result7.getClass());
86+
}
87+
}

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public void setUp() throws Exception {
2121

2222
@After
2323
public void tearDown() throws Exception {
24-
// object.close();
25-
// context.close();
26-
// quickJS.close();
24+
object.close();
25+
context.close();
26+
quickJS.close();
2727
}
2828

2929
@Test
@@ -81,30 +81,6 @@ public void getObject() {
8181
}
8282

8383

84-
@Test
85-
public void call2() {
86-
context.registerJavaMethod((receiver, array) -> {
87-
assertEquals("Hello", array.getString(0));
88-
}, "test");
89-
JSArray args = new JSArray(context);
90-
args.push("Hello");
91-
args.push(3.14);
92-
context.executeVoidFunction("test", args);
93-
args.close();
94-
}
95-
96-
@Test
97-
public void call3() {
98-
context.registerJavaMethod((receiver, args) -> {
99-
return args.getString(0);
100-
}, "test");
101-
JSArray args = new JSArray(context);
102-
args.push("Hello");
103-
args.push(3.14);
104-
assertEquals("Hello", context.executeStringFunction("test", args));
105-
args.close();
106-
}
107-
10884
@Test
10985
public void executeFunction() {
11086

@@ -173,18 +149,6 @@ public void executeObjectFunction() {
173149
assertEquals("Wiki", result.getString("name"));
174150
}
175151

176-
@Test
177-
public void executeVoidFunction() {
178-
}
179-
180-
@Test
181-
public void executeJSFunction() {
182-
183-
}
184-
185-
@Test
186-
public void testExecuteJSFunction() {
187-
}
188152

189153
@Test
190154
public void contains() {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ Java_com_quickjs_android_QuickJS__1executeScript(JNIEnv *env, jclass clazz, jlon
264264
const char *file_name_ = env->GetStringUTFChars(file_name, nullptr);
265265
JSValue val = JS_Eval(ctx, source_, (size_t) source_length, file_name_, JS_EVAL_TYPE_GLOBAL);
266266
jobject result = To_JObject(env, context_ptr, expected_type, val);
267-
// TODO
268-
JS_FreeValue(ctx, val);
269267
return result;
270268
}
271269
extern "C"

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ public void close() {
4343
QuickJS.sContextMap.remove(getContextPtr());
4444
}
4545

46-
public Object executeScript(int expectedType, String source, String fileName) {
47-
return QuickJS.executeScript(this, expectedType, source, fileName);
46+
private Object executeScript(int expectedType, String source, String fileName) {
47+
return QuickJS._executeScript(this.getContextPtr(), expectedType, source, fileName);
4848
}
4949

50+
/**
51+
* @return Integer/Double/Boolean/String/JSObject/JSArray/JSFunction
52+
*/
5053
public Object executeScript(String source, String fileName) {
5154
return executeScript(JSValue.TYPE_UNKNOWN, source, fileName);
5255
}
@@ -79,16 +82,6 @@ public JSObject executeObjectScript(String source, String fileName) {
7982
return (JSObject) executeScript(JSValue.TYPE_JS_OBJECT, source, fileName);
8083
}
8184

82-
void registerCallback(JavaCallback callback, JSValue objectHandle, String jsFunctionName) {
83-
JSFunction functionHandle = QuickJS._registerJavaMethod(this.getContextPtr(), objectHandle, jsFunctionName, false);
84-
registerCallback(callback, functionHandle);
85-
}
86-
87-
void registerCallback(JavaVoidCallback callback, JSValue objectHandle, String jsFunctionName) {
88-
JSFunction functionHandle = QuickJS._registerJavaMethod(this.getContextPtr(), objectHandle, jsFunctionName, true);
89-
registerCallback(callback, functionHandle);
90-
}
91-
9285
void registerCallback(JavaCallback callback, JSFunction functionHandle) {
9386
QuickJS.MethodDescriptor methodDescriptor = new QuickJS.MethodDescriptor();
9487
methodDescriptor.callback = callback;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ public int getType(String key) {
103103

104104

105105
public JSObject registerJavaMethod(JavaCallback callback, String jsFunctionName) {
106-
context.registerCallback(callback, this, jsFunctionName);
106+
JSFunction functionHandle = QuickJS._registerJavaMethod(this.getContextPtr(), this, jsFunctionName, false);
107+
context.registerCallback(callback, functionHandle);
107108
return this;
108109
}
109110

110111
public JSObject registerJavaMethod(JavaVoidCallback callback, String jsFunctionName) {
111-
context.registerCallback(callback, this, jsFunctionName);
112+
JSFunction functionHandle = QuickJS._registerJavaMethod(this.getContextPtr(), this, jsFunctionName, true);
113+
context.registerCallback(callback, functionHandle);
112114
return this;
113115
}
114116

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ public static QuickJS createRuntime() {
1919
return new QuickJS(_createRuntime());
2020
}
2121

22-
23-
static Object executeScript(JSContext context, int expectedType, String source, String fileName) {
24-
return _executeScript(context.getContextPtr(), expectedType, source, fileName);
25-
}
26-
27-
2822
public JSContext createContext() {
2923
JSContext context = new JSContext(_createContext(runtimePtr));
3024
sContextMap.put(context.getContextPtr(), context);
@@ -40,7 +34,6 @@ static class MethodDescriptor {
4034
public JavaCallback callback;
4135
}
4236

43-
4437
@Keep
4538
static void callJavaVoidCallback(JSValue objectHandle, JSValue functionHandle, JSArray argsHandle) {
4639
JSContext context = sContextMap.get(functionHandle.context.getContextPtr());
@@ -118,7 +111,7 @@ static Object executeJSFunction(JSContext context, JSValue objectHandle, String
118111

119112
static native void _releaseContext(long contextPtr);
120113

121-
private static native Object _executeScript(long contextPtr, int expectedType, String source, String fileName);
114+
static native Object _executeScript(long contextPtr, int expectedType, String source, String fileName);
122115

123116
static native JSObject _getGlobalObject(long contextPtr);
124117

0 commit comments

Comments
 (0)