forked from PetterS/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_quickjs.py
393 lines (336 loc) · 11.7 KB
/
test_quickjs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import concurrent.futures
import json
import unittest
import quickjs
class LoadModule(unittest.TestCase):
def test_42(self):
self.assertEqual(quickjs.test(), 42)
class Context(unittest.TestCase):
def setUp(self):
self.context = quickjs.Context()
def test_eval_int(self):
self.assertEqual(self.context.eval("40 + 2"), 42)
def test_eval_float(self):
self.assertEqual(self.context.eval("40.0 + 2.0"), 42.0)
def test_eval_str(self):
self.assertEqual(self.context.eval("'4' + '2'"), "42")
def test_eval_bool(self):
self.assertEqual(self.context.eval("true || false"), True)
self.assertEqual(self.context.eval("true && false"), False)
def test_eval_null(self):
self.assertIsNone(self.context.eval("null"))
def test_eval_undefined(self):
self.assertIsNone(self.context.eval("undefined"))
def test_wrong_type(self):
with self.assertRaises(TypeError):
self.assertEqual(self.context.eval(1), 42)
def test_context_between_calls(self):
self.context.eval("x = 40; y = 2;")
self.assertEqual(self.context.eval("x + y"), 42)
def test_function(self):
self.context.eval("""
function special(x) {
return 40 + x;
}
""")
self.assertEqual(self.context.eval("special(2)"), 42)
def test_get(self):
self.context.eval("x = 42; y = 'foo';")
self.assertEqual(self.context.get("x"), 42)
self.assertEqual(self.context.get("y"), "foo")
self.assertEqual(self.context.get("z"), None)
def test_module(self):
self.context.module("""
export function test() {
return 42;
}
""")
def test_error(self):
with self.assertRaisesRegex(quickjs.JSException, "ReferenceError: missing is not defined"):
self.context.eval("missing + missing")
def test_lifetime(self):
def get_f():
context = quickjs.Context()
f = context.eval("""
a = function(x) {
return 40 + x;
}
""")
return f
f = get_f()
self.assertTrue(f)
# The context has left the scope after f. f needs to keep the context alive for the
# its lifetime. Otherwise, we will get problems.
def test_memory_limit(self):
code = """
(function() {
let arr = [];
for (let i = 0; i < 1000; ++i) {
arr.push(i);
}
})();
"""
self.context.eval(code)
self.context.set_memory_limit(1000)
with self.assertRaisesRegex(quickjs.JSException, "null"):
self.context.eval(code)
self.context.set_memory_limit(1000000)
self.context.eval(code)
def test_time_limit(self):
code = """
(function() {
let arr = [];
for (let i = 0; i < 100000; ++i) {
arr.push(i);
}
return arr;
})();
"""
self.context.eval(code)
self.context.set_time_limit(0)
with self.assertRaisesRegex(quickjs.JSException, "InternalError: interrupted"):
self.context.eval(code)
self.context.set_time_limit(-1)
self.context.eval(code)
def test_memory_usage(self):
self.assertIn("memory_used_size", self.context.memory().keys())
class Object(unittest.TestCase):
def setUp(self):
self.context = quickjs.Context()
def test_function_is_object(self):
f = self.context.eval("""
a = function(x) {
return 40 + x;
}
""")
self.assertIsInstance(f, quickjs.Object)
def test_function_call_int(self):
f = self.context.eval("""
f = function(x) {
return 40 + x;
}
""")
self.assertEqual(f(2), 42)
def test_function_call_int_two_args(self):
f = self.context.eval("""
f = function(x, y) {
return 40 + x + y;
}
""")
self.assertEqual(f(3, -1), 42)
def test_function_call_many_times(self):
n = 1000
f = self.context.eval("""
f = function(x, y) {
return x + y;
}
""")
s = 0
for i in range(n):
s += f(1, 1)
self.assertEqual(s, 2 * n)
def test_function_call_str(self):
f = self.context.eval("""
f = function(a) {
return a + " hej";
}
""")
self.assertEqual(f("1"), "1 hej")
def test_function_call_str_three_args(self):
f = self.context.eval("""
f = function(a, b, c) {
return a + " hej " + b + " ho " + c;
}
""")
self.assertEqual(f("1", "2", "3"), "1 hej 2 ho 3")
def test_function_call_object(self):
d = self.context.eval("d = {data: 42};")
f = self.context.eval("""
f = function(d) {
return d.data;
}
""")
self.assertEqual(f(d), 42)
# Try again to make sure refcounting works.
self.assertEqual(f(d), 42)
self.assertEqual(f(d), 42)
def test_function_call_unsupported_arg(self):
f = self.context.eval("""
f = function(x) {
return 40 + x;
}
""")
with self.assertRaisesRegex(TypeError, "Unsupported type"):
self.assertEqual(f({}), 42)
def test_json(self):
d = self.context.eval("d = {data: 42};")
self.assertEqual(json.loads(d.json()), {"data": 42})
def test_call_nonfunction(self):
d = self.context.eval("({data: 42})")
with self.assertRaisesRegex(quickjs.JSException, "TypeError: not a function"):
d(1)
def test_wrong_context(self):
context1 = quickjs.Context()
context2 = quickjs.Context()
f = context1.eval("(function(x) { return x.a; })")
d = context2.eval("({a: 1})")
with self.assertRaisesRegex(ValueError, "Can not mix JS objects from different contexts."):
f(d)
class FunctionTest(unittest.TestCase):
def test_adder(self):
f = quickjs.Function(
"adder", """
function adder(x, y) {
return x + y;
}
""")
self.assertEqual(f(1, 1), 2)
self.assertEqual(f(100, 200), 300)
self.assertEqual(f("a", "b"), "ab")
def test_identity(self):
identity = quickjs.Function(
"identity", """
function identity(x) {
return x;
}
""")
for x in [True, [1], {"a": 2}, 1, 1.5, "hej", None]:
self.assertEqual(identity(x), x)
def test_bool(self):
f = quickjs.Function(
"f", """
function f(x) {
return [typeof x ,!x];
}
""")
self.assertEqual(f(False), ["boolean", True])
self.assertEqual(f(True), ["boolean", False])
def test_empty(self):
f = quickjs.Function("f", "function f() { }")
self.assertEqual(f(), None)
def test_lists(self):
f = quickjs.Function(
"f", """
function f(arr) {
const result = [];
arr.forEach(function(elem) {
result.push(elem + 42);
});
return result;
}""")
self.assertEqual(f([0, 1, 2]), [42, 43, 44])
def test_dict(self):
f = quickjs.Function(
"f", """
function f(obj) {
return obj.data;
}""")
self.assertEqual(f({"data": {"value": 42}}), {"value": 42})
def test_time_limit(self):
f = quickjs.Function(
"f", """
function f() {
let arr = [];
for (let i = 0; i < 100000; ++i) {
arr.push(i);
}
return arr;
}
""")
f()
f.set_time_limit(0)
with self.assertRaisesRegex(quickjs.JSException, "InternalError: interrupted"):
f()
f.set_time_limit(-1)
f()
def test_garbage_collection(self):
f = quickjs.Function(
"f", """
function f() {
let a = {};
let b = {};
a.b = b;
b.a = a;
a.i = 42;
return a.i;
}
""")
initial_count = f.memory()["obj_count"]
for i in range(10):
prev_count = f.memory()["obj_count"]
self.assertEqual(f(run_gc=False), 42)
current_count = f.memory()["obj_count"]
self.assertGreater(current_count, prev_count)
f.gc()
self.assertLessEqual(f.memory()["obj_count"], initial_count)
def test_deep_recursion(self):
f = quickjs.Function(
"f", """
function f(v) {
if (v <= 0) {
return 0;
} else {
return 1 + f(v - 1);
}
}
""")
self.assertEqual(f(100), 100)
limit = 500
with self.assertRaises(quickjs.StackOverflow):
f(limit)
f.set_max_stack_size(2000 * limit)
self.assertEqual(f(limit), limit)
class Strings(unittest.TestCase):
def test_unicode(self):
identity = quickjs.Function(
"identity", """
function identity(x) {
return x;
}
""")
context = quickjs.Context()
for x in ["äpple", "≤≥", "☺"]:
self.assertEqual(identity(x), x)
self.assertEqual(context.eval('(function(){ return "' + x + '";})()'), x)
class Threads(unittest.TestCase):
def setUp(self):
self.context = quickjs.Context()
self.executor = concurrent.futures.ThreadPoolExecutor()
def tearDown(self):
self.executor.shutdown()
def test_concurrent(self):
"""Demonstrates that the execution will crash unless the function executes on the same
thread every time.
If the executor in Function is not present, this test will fail.
"""
data = list(range(1000))
jssum = quickjs.Function(
"sum", """
function sum(data) {
return data.reduce((a, b) => a + b, 0)
}
""")
futures = [self.executor.submit(jssum, data) for _ in range(10)]
expected = sum(data)
for future in concurrent.futures.as_completed(futures):
self.assertEqual(future.result(), expected)
def test_concurrent_own_executor(self):
data = list(range(1000))
jssum1 = quickjs.Function("sum",
"""
function sum(data) {
return data.reduce((a, b) => a + b, 0)
}
""",
own_executor=True)
jssum2 = quickjs.Function("sum",
"""
function sum(data) {
return data.reduce((a, b) => a + b, 0)
}
""",
own_executor=True)
futures = [self.executor.submit(f, data) for _ in range(10) for f in (jssum1, jssum2)]
expected = sum(data)
for future in concurrent.futures.as_completed(futures):
self.assertEqual(future.result(), expected)