Debian Codegraph @

js/src/jit-test/tests/xdr/stencil.js

raw
1
2 load(libdir + "asserts.js");
3
4 /*
5 * This exercises stencil XDR encoding and decoding using a broad
6 * smoke testing.
7 *
8 * A set of scripts exercising various codepaths are XDR-encoded,
9 * then decoded, and then executed. Their output is compared to
10 * the execution of the scripts through a normal path and the
11 * outputs checked.
12 */
13
14 /*
15 * Exercises global scope access and object literals, as well as some
16 * simple object destructuring.
17 */
18 const testGlobal0 = 13;
19 let testGlobal1 = undefined;
20 var testGlobal2 = undefined;
21
22 const SCRIPT_0 = `
23 testGlobal1 = 123456789012345678901234567890n;
24 testGlobal2 = {'foo':3, 'bar': [1, 2, 3],
25 '4': 'zing'};
26 var testGlobal3 = /NewlyDefinedGlobal/;
27 function testGlobal4(a, {b}) {
28 return a + b.foo + b['bar'].reduce((a,b) => (a+b), 0);
29 + b[4].length +
30 + testGlobal3.toString().length;
31 };
32 testGlobal4(Number(testGlobal1), {b:testGlobal2})
33 `;
34
35 /*
36 * Exercises function scopes, lexical scopes, var and let
37 * within them, and some longer identifiers, and array destructuring in
38 * arguments. Also contains some tiny atoms and globls access.
39 */
40 const SCRIPT_1 = `
41 function foo(a, b, c) {
42 var q = a * (b + c);
43 let bix = function (d, e) {
44 let x = a + d;
45 var y = e * b;
46 const a0 = q + x + y;
47 for (let i = 0; i < 3; i++) {
48 y = a0 + Math.PI + y;
49 }
50 return y;
51 };
52 function bang(d, [e, f]) {
53 let reallyLongIdentifierName = a + d;
54 var y = e * b;
55 const z = reallyLongIdentifierName + f;
56 return z;
57 }
58 return bix(1, 2) + bang(3, [4, 5, 6]);
59 }
60 foo(1, 2, 3)
61 `;
62
63 /*
64 * Exercises eval and with scopes, object destructuring, function rest
65 * arguments.
66 */
67 const SCRIPT_2 = `
68 function foo(with_obj, ...xs) {
69 const [x0, x1, ...xrest] = xs;
70 eval('var x2 = x0 + x1');
71 var sum = [];
72 with (with_obj) {
73 sum.push(x2 + xrest.length);
74 }
75 sum.push(x2 + xrest.length);
76 return sum;
77 }
78 foo({x2: 99}, 1, 2, 3, 4, 5, 6)
79 `;
80
81 function test_script(script_str) {
82 const eval_f = eval;
83 const options = {
84 fileName: "compileToStencilXDR-DATA.js",
85 lineNumber: 1,
86 forceFullParse: true,
87 };
88 const bytes = compileToStencilXDR(script_str, options);
89 const result = evalStencilXDR(bytes, options);
90 assertDeepEq(result, eval_f(script_str));
91 }
92
93 function tests() {
94 test_script(SCRIPT_0);
95 test_script(SCRIPT_1);
96 test_script(SCRIPT_2);
97 }
98
99 tests()