@@ -14,132 +14,135 @@ public class QuickJSCompileTest {
14
14
@ Rule
15
15
public ExpectedException thrown = ExpectedException .none ();
16
16
17
- private QuickJSContext context ;
18
-
19
17
@ Before
20
18
public void setup () {
21
19
QuickJSLoader .init ();
22
20
}
23
21
24
22
@ Test
25
23
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
+ }
34
42
}
35
43
36
44
@ Test
37
45
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
+ }
46
51
}
47
52
48
53
@ Test (expected = QuickJSException .class )
49
54
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
+ }
56
59
}
57
60
58
61
@ Test
59
62
public void testFreeValueReturnedOfExecute () {
60
63
QuickJSLoader .startRedirectingStdoutStderr ("quickjs_android" );
61
- QuickJSContext context = QuickJSContext .create ();
62
- QuickJSLoader .initConsoleLog (context );
64
+ try ( QuickJSContext context = QuickJSContext .create ()) {
65
+ QuickJSLoader .initConsoleLog (context );
63
66
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
+ }
68
71
}
69
72
70
73
@ Test
71
74
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
+ }
83
86
}
84
87
85
88
@ Test
86
89
public void testCompileModuleWithoutModuleLoader () {
87
90
thrown .expect (QuickJSException .class );
88
91
thrown .expectMessage ("Failed to load module, the ModuleLoader can not be null!" );
89
92
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
+ }
93
96
}
94
97
95
98
@ Test
96
99
public void testCompileModuleWithMockModuleLoader () {
97
100
thrown .expect (QuickJSException .class );
98
101
thrown .expectMessage ("Could not find export 'a' in module 'a.js'" );
99
102
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
+ }
111
114
}
112
115
113
116
@ Test
114
117
public void testStringCodeModuleLoaderReturnNull () {
115
118
thrown .expect (QuickJSException .class );
116
119
thrown .expectMessage ("Failed to load module, cause string code was null!" );
117
120
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
+ }
127
130
}
128
131
129
132
@ Test
130
133
public void testBytecodeModuleLoaderReturnNull () {
131
134
thrown .expect (QuickJSException .class );
132
135
thrown .expectMessage ("Failed to load module, cause bytecode was null!" );
133
136
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
+ }
143
146
}
144
147
145
148
}
0 commit comments