Skip to content

Commit fc07107

Browse files
authored
Merge pull request #90 from HarlonWang/feature/2.5.0
Feature/2.5.0
2 parents ab5e384 + bdfa638 commit fc07107

File tree

10 files changed

+878
-856
lines changed

10 files changed

+878
-856
lines changed

native/cpp/quickjs_wrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ jsModuleLoaderFunc(JSContext *ctx, const char *module_name, void *opaque) {
238238
int scriptLen = env->GetStringUTFLength((jstring) result);
239239
JSValue func_val = JS_Eval(ctx, script, scriptLen, module_name,
240240
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
241+
env->ReleaseStringUTFChars((jstring)(result), script);
241242
if (JS_IsException(func_val)) {
242243
JS_FreeValue(ctx, func_val);
243244
throwJSException(env, ctx);

wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSCompileTest.java

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,132 +14,135 @@ public class QuickJSCompileTest {
1414
@Rule
1515
public ExpectedException thrown = ExpectedException.none();
1616

17-
private QuickJSContext context;
18-
1917
@Before
2018
public void setup() {
2119
QuickJSLoader.init();
2220
}
2321

2422
@Test
2523
public void helloWorld() {
26-
context = QuickJSContext.create();
27-
byte[] code = context.compile("'hello, world!'.toUpperCase();");
28-
context.destroy();
29-
30-
context = QuickJSContext.create();
31-
Object hello = context.execute(code);
32-
assertEquals(hello, "HELLO, WORLD!");
33-
context.destroy();
24+
try (QuickJSContext context = QuickJSContext.create()) {
25+
byte[] code = context.compile("'hello, world!'.toUpperCase();");
26+
Object hello = context.execute(code);
27+
assertEquals(hello, "HELLO, WORLD!");
28+
}
29+
}
30+
31+
@Test
32+
public void testDifferentContexts() {
33+
byte[] code;
34+
try (QuickJSContext context = QuickJSContext.create()) {
35+
code = context.compile("'hello, world!'.toUpperCase();");
36+
}
37+
38+
try (QuickJSContext context = QuickJSContext.create()) {
39+
Object hello = context.execute(code);
40+
assertEquals(hello, "HELLO, WORLD!");
41+
}
3442
}
3543

3644
@Test
3745
public void testPromise() {
38-
context = QuickJSContext.create();
39-
byte[] bytes = context.compile("var ret; new Promise((resolve, reject) => { ret = 'resolved'; }); ret;");
40-
context.destroy();
41-
42-
context = QuickJSContext.create();
43-
Object ret = context.execute(bytes);
44-
assertEquals(ret, "resolved");
45-
context.destroy();
46+
try (QuickJSContext context = QuickJSContext.create()) {
47+
byte[] bytes = context.compile("var ret; new Promise((resolve, reject) => { ret = 'resolved'; }); ret;");
48+
Object ret = context.execute(bytes);
49+
assertEquals(ret, "resolved");
50+
}
4651
}
4752

4853
@Test(expected = QuickJSException.class)
4954
public void testThrowErrorWithFileName() {
50-
context = QuickJSContext.create();
51-
byte[] bytes = context.compile("test;", "test.js");
52-
context.destroy();
53-
context = QuickJSContext.create();
54-
context.execute(bytes);
55-
context.destroy();
55+
try (QuickJSContext context = QuickJSContext.create()) {
56+
byte[] bytes = context.compile("test;", "test.js");
57+
context.execute(bytes);
58+
}
5659
}
5760

5861
@Test
5962
public void testFreeValueReturnedOfExecute() {
6063
QuickJSLoader.startRedirectingStdoutStderr("quickjs_android");
61-
QuickJSContext context = QuickJSContext.create();
62-
QuickJSLoader.initConsoleLog(context);
64+
try (QuickJSContext context = QuickJSContext.create()) {
65+
QuickJSLoader.initConsoleLog(context);
6366

64-
byte[] bytes = context.compile("test = () => { console.log('test'); }");
65-
JSObject ret = (JSObject) context.execute(bytes);
66-
ret.release();
67-
context.destroy();
67+
byte[] bytes = context.compile("test = () => { console.log('test'); }");
68+
JSObject ret = (JSObject) context.execute(bytes);
69+
ret.release();
70+
}
6871
}
6972

7073
@Test
7174
public void testCompileModule() {
72-
QuickJSContext context = QuickJSContext.create();
73-
QuickJSLoader.initConsoleLog(context);
74-
context.setModuleLoader(new QuickJSContext.BytecodeModuleLoader() {
75-
@Override
76-
public byte[] getModuleBytecode(String moduleName) {
77-
return context.compileModule("export const a = {name: 'test'};", moduleName);
78-
}
79-
});
80-
byte[] bytes = context.compileModule("import {a} from 'a.js'; if(a.name !== 'test') { throw new Error('failed') }", "aaa.js");
81-
context.execute(bytes);
82-
context.destroy();
75+
try (QuickJSContext context = QuickJSContext.create()) {
76+
QuickJSLoader.initConsoleLog(context);
77+
context.setModuleLoader(new QuickJSContext.BytecodeModuleLoader() {
78+
@Override
79+
public byte[] getModuleBytecode(String moduleName) {
80+
return context.compileModule("export const a = {name: 'test'};", moduleName);
81+
}
82+
});
83+
byte[] bytes = context.compileModule("import {a} from 'a.js'; if(a.name !== 'test') { throw new Error('failed') }", "aaa.js");
84+
context.execute(bytes);
85+
}
8386
}
8487

8588
@Test
8689
public void testCompileModuleWithoutModuleLoader() {
8790
thrown.expect(QuickJSException.class);
8891
thrown.expectMessage("Failed to load module, the ModuleLoader can not be null!");
8992

90-
QuickJSContext context = QuickJSContext.create();
91-
context.compileModule("import { a } from 'a.js';");
92-
context.destroy();
93+
try (QuickJSContext context = QuickJSContext.create()) {
94+
context.compileModule("import { a } from 'a.js';");
95+
}
9396
}
9497

9598
@Test
9699
public void testCompileModuleWithMockModuleLoader() {
97100
thrown.expect(QuickJSException.class);
98101
thrown.expectMessage("Could not find export 'a' in module 'a.js'");
99102

100-
QuickJSContext context = QuickJSContext.create();
101-
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
102-
@Override
103-
public String getModuleStringCode(String moduleName) {
104-
return "";
105-
}
106-
});
107-
// 在 ModuleLoader 中返回空字符串,可以实现仅编译当前模块字节码,而不用编译它所依赖的模块
108-
byte[] bytes = context.compileModule("import { a } from 'a.js';");
109-
context.execute(bytes);
110-
context.destroy();
103+
try (QuickJSContext context = QuickJSContext.create()) {
104+
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
105+
@Override
106+
public String getModuleStringCode(String moduleName) {
107+
return "";
108+
}
109+
});
110+
// 在 ModuleLoader 中返回空字符串,可以实现仅编译当前模块字节码,而不用编译它所依赖的模块
111+
byte[] bytes = context.compileModule("import { a } from 'a.js';");
112+
context.execute(bytes);
113+
}
111114
}
112115

113116
@Test
114117
public void testStringCodeModuleLoaderReturnNull() {
115118
thrown.expect(QuickJSException.class);
116119
thrown.expectMessage("Failed to load module, cause string code was null!");
117120

118-
QuickJSContext context = QuickJSContext.create();
119-
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
120-
@Override
121-
public String getModuleStringCode(String moduleName) {
122-
return null;
123-
}
124-
});
125-
context.compileModule("import { a } from 'a.js';");
126-
context.destroy();
121+
try (QuickJSContext context = QuickJSContext.create()) {
122+
context.setModuleLoader(new QuickJSContext.DefaultModuleLoader() {
123+
@Override
124+
public String getModuleStringCode(String moduleName) {
125+
return null;
126+
}
127+
});
128+
context.compileModule("import { a } from 'a.js';");
129+
}
127130
}
128131

129132
@Test
130133
public void testBytecodeModuleLoaderReturnNull() {
131134
thrown.expect(QuickJSException.class);
132135
thrown.expectMessage("Failed to load module, cause bytecode was null!");
133136

134-
QuickJSContext context = QuickJSContext.create();
135-
context.setModuleLoader(new QuickJSContext.BytecodeModuleLoader() {
136-
@Override
137-
public byte[] getModuleBytecode(String moduleName) {
138-
return null;
139-
}
140-
});
141-
context.compileModule("import { a } from 'a.js';");
142-
context.destroy();
137+
try (QuickJSContext context = QuickJSContext.create()) {
138+
context.setModuleLoader(new QuickJSContext.BytecodeModuleLoader() {
139+
@Override
140+
public byte[] getModuleBytecode(String moduleName) {
141+
return null;
142+
}
143+
});
144+
context.compileModule("import { a } from 'a.js';");
145+
}
143146
}
144147

145148
}

wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSModuleTest.java

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

0 commit comments

Comments
 (0)