ion2wasm.js (4271B)
1 var ITERATIONS = 10; 2 var INNER_ITERATIONS = 100; 3 4 let instance = wasmEvalText(`(module 5 (func (export "add") (param i32) (param i32) (result i32) 6 local.get 0 7 local.get 1 8 i32.add 9 ) 10 11 (func (export "no_arg") (result i32) 12 i32.const 42 13 i32.const 58 14 i32.add 15 ) 16 17 (global $g (mut i32) (i32.const 0)) 18 19 (func (export "set_global_one") (param i32) 20 local.get 0 21 global.set $g 22 ) 23 24 (func (export "set_global_two") (param i32) (param i32) 25 local.get 0 26 local.get 1 27 i32.add 28 global.set $g 29 ) 30 31 (func (export "glob") (result i32) 32 global.get $g 33 ) 34 )`).exports; 35 36 function run(name, func) { 37 for (let i = ITERATIONS; i --> 0;) { 38 func(); 39 } 40 } 41 42 function testCallKnown() { 43 for (let i = 0; i < INNER_ITERATIONS; i++) { 44 assertEq(instance.add(i, i + 1), 2*i + 1); 45 } 46 } 47 48 function testCallKnownRectifying() { 49 for (let i = 0; i < INNER_ITERATIONS; i++) { 50 assertEq(instance.add(i + 1), i+1); 51 } 52 } 53 54 function jsAdd(x, y) { 55 return (x|0) + (y|0) | 0; 56 } 57 58 function testCallGeneric() { 59 var arr = [instance.add, jsAdd]; 60 for (let i = 0; i < INNER_ITERATIONS; i++) { 61 assertEq(arr[i%2](i, i+1), 2*i + 1); 62 } 63 } 64 65 function testCallGenericRectifying() { 66 var arr = [instance.add, jsAdd]; 67 for (let i = 0; i < INNER_ITERATIONS; i++) { 68 assertEq(arr[i%2](i+1), i + 1); 69 } 70 } 71 72 function testCallScriptedGetter() { 73 var obj = {}; 74 Object.defineProperty(obj, 'x', { 75 get: instance.no_arg 76 }); 77 for (let i = 0; i < INNER_ITERATIONS; i++) { 78 assertEq(obj.x, 100); 79 } 80 } 81 82 function testCallScriptedGetterRectifying() { 83 var obj = {}; 84 Object.defineProperty(obj, 'x', { 85 // Missing two arguments. 86 get: instance.add 87 }); 88 for (let i = 0; i < INNER_ITERATIONS; i++) { 89 assertEq(obj.x, 0); 90 } 91 } 92 93 function testCallScriptedSetter() { 94 var obj = {}; 95 Object.defineProperty(obj, 'x', { 96 set: instance.set_global_one 97 }); 98 for (let i = 0; i < INNER_ITERATIONS; i++) { 99 obj.x = i; 100 } 101 assertEq(instance.glob(), INNER_ITERATIONS-1); 102 } 103 104 function testCallScriptedSetterRectifying() { 105 var obj = {}; 106 Object.defineProperty(obj, 'x', { 107 set: instance.set_global_two 108 }); 109 for (let i = 0; i < INNER_ITERATIONS; i++) { 110 obj.x = i; 111 } 112 assertEq(instance.glob(), INNER_ITERATIONS-1); 113 } 114 115 function testFunctionApplyArray() { 116 for (let i = 0; i < INNER_ITERATIONS; i++) { 117 assertEq(instance.add.apply(null, [i, i + 1]), 2*i+1); 118 } 119 } 120 121 function testFunctionApplyArrayRectifying() { 122 for (let i = 0; i < INNER_ITERATIONS; i++) { 123 assertEq(instance.add.apply(null, [i + 1]), i+1); 124 } 125 } 126 127 function testFunctionApplyArgs() { 128 function wrapper() { 129 assertEq(instance.add.apply(null, arguments), 2*arguments[0]+1); 130 } 131 for (let i = 0; i < INNER_ITERATIONS; i++) { 132 wrapper(i, i + 1); 133 } 134 } 135 136 function testFunctionApplyArgsRectifying() { 137 function wrapper() { 138 assertEq(instance.add.apply(null, arguments), arguments[0]); 139 } 140 for (let i = 0; i < INNER_ITERATIONS; i++) { 141 wrapper(i + 1); 142 } 143 } 144 145 function testFunctionCall() { 146 for (let i = 0; i < INNER_ITERATIONS; i++) { 147 assertEq(instance.add.call(null, i, i + 1), 2*i+1); 148 } 149 } 150 151 function testFunctionCallRectifying() { 152 for (let i = 0; i < INNER_ITERATIONS; i++) { 153 assertEq(instance.add.call(null, i + 1), i+1); 154 } 155 } 156 157 run('call known', testCallKnown); 158 run('call known rectifying', testCallKnownRectifying); 159 160 run('call generic', testCallGeneric); 161 run('call generic rectifying', testCallGenericRectifying); 162 163 run('scripted getter', testCallScriptedGetter); 164 run('scripted getter rectifiying', testCallScriptedGetterRectifying); 165 run('scripted setter', testCallScriptedSetter); 166 run('scripted setter rectifiying', testCallScriptedSetterRectifying); 167 168 run('function.apply array', testFunctionApplyArray); 169 run('function.apply array rectifying', testFunctionApplyArrayRectifying); 170 run('function.apply args', testFunctionApplyArgs); 171 run('function.apply args rectifying', testFunctionApplyArgsRectifying); 172 173 run('function.call', testFunctionCall); 174 run('function.call rectifying', testFunctionCallRectifying);