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