def test_dict(self): f = quickjs.Function( "f", """ function f(obj) { return obj.data; }""") self.assertEqual(f({"data": {"value": 42}}), {"value": 42})
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_es2020_optional_chaining(self): f = quickjs.Function("f", """ function f(x) { return x?.one?.two; } """) self.assertIsNone(f({})) self.assertIsNone(f({"one": 12})) self.assertEqual(f({"one": {"two": 42}}), 42)
def _prepare_function(self, definition: ResourceDefinition): script = definition.script libs = definition.get('libraries', []) for lib_url in libs: _body = self.get_file(lib_url) script = f''' {script} {_body} ''' self._function = quickjs.Function(definition.entrypoint, script)
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_add_callable(self): f = quickjs.Function( "f", """ function f() { return pfunc(); } """) f.add_callable("pfunc", lambda: 42) self.assertEqual(f(), 42)
def test_es2020_null_coalescing(self): f = quickjs.Function("f", """ function f(x) { return x ?? 42; } """) self.assertEqual(f(""), "") self.assertEqual(f(0), 0) self.assertEqual(f(11), 11) self.assertEqual(f(None), 42)
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)
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_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)
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_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)
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_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)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ """ __version__ = '0.0.1' import json import quickjs with open('./test.md') as mdf: md = mdf.read() with open('./remarkpy.js') as f: #with open('../remarkpyjs/index.js') as f: js = f.read() parseMd = quickjs.Function('parseMd', js) print(parseMd('## H1')) #parseHtml = quickjs.Function('parseHtml', js) #print(parseHtml('<h1>hello</h1>')) #print(parseHtml) #help(parseHtml)
def test_empty(self): f = quickjs.Function("f", "function f() { }") self.assertEqual(f(), None)
import json import quickjs msg = {'amesg': {'bmesg': [1, 2, 3, 4]}} with open('./bundle.js') as f: _fn = quickjs.Function('make_schema', f.read()) print(json.dumps(_fn(msg), indent=2))