call-js.js (4233B)
1 wasmFullPass(` 2 (module 3 (import "env" "f" (func $f (result i32 i32 i32))) 4 (func (export "run") (result i32) 5 (call $f) 6 i32.sub 7 i32.sub))`, 8 42, 9 { env: { f: () => [52, 10, 0] } }); 10 11 wasmFullPass(` 12 (module 13 (import "env" "f" (func $f (result i64 i64 i64))) 14 (func (export "run") (result i64) 15 (call $f) 16 i64.sub 17 i64.sub))`, 18 42n, 19 { env: { f: () => [52n, 10n, 0n] } }); 20 21 wasmFullPass(` 22 (module 23 (import "env" "f" (func $f (result f32 f32 f32))) 24 (func (export "run") (result i32) 25 (call $f) 26 f32.sub 27 f32.sub 28 i32.trunc_f32_s))`, 29 42, 30 { env: { f: () => [52.25, 10.5, 0.25] } }); 31 32 wasmFullPass(` 33 (module 34 (import "env" "f" (func $f (result f64 f64 f64))) 35 (func (export "run") (result i32) 36 (call $f) 37 f64.sub 38 f64.sub 39 i32.trunc_f64_s))`, 40 42, 41 { env: { f: () => [52.25, 10.5, 0.25] } }); 42 43 // Multiple values are returned as an iterable; it doesn't have to be an array. 44 function expectMultiValuePass(f) { 45 wasmFullPass(` 46 (module 47 (import "env" "f" (func $f (result i32 i32 i32))) 48 (func (export "run") (result i32) 49 (call $f) 50 i32.sub 51 i32.sub))`, 52 42, 53 { env: { f } }); 54 } 55 function expectMultiValueError(f, type, pattern) { 56 let module = new WebAssembly.Module(wasmTextToBinary(` 57 (module 58 (import "env" "f" (func $f (result i32 i32 i32))) 59 (func (export "run") (result i32) 60 (call $f) 61 i32.sub 62 i32.sub))`)); 63 64 let instance = new WebAssembly.Instance(module, { env: { f } } ); 65 assertErrorMessage(() => instance.exports.run(), type, pattern); 66 } 67 68 expectMultiValuePass(() => [52, 10, 0]); 69 expectMultiValuePass(() => [32, -10, 0]); 70 expectMultiValuePass(() => [52.75, 10, 0.5]); // Values converted to i32 via ToInt32. 71 expectMultiValuePass(() => [42, undefined, undefined]); 72 expectMultiValuePass(() => (function*() { yield 52; yield 10; yield 0; })()); 73 expectMultiValuePass(() => (function*() { yield '52'; yield '10'; yield 0; })()); 74 75 // Multi-value result must be iterable. 76 expectMultiValueError(() => 1, TypeError, /iterable/); 77 expectMultiValueError(() => 1n, TypeError, /iterable/); 78 79 // Check that the iterator's values are collected first, and that the 80 // length of the result is correct. 81 expectMultiValueError(() => [1], TypeError, /expected 3, got 1/); 82 expectMultiValueError(() => [1n], TypeError, /expected 3, got 1/); 83 expectMultiValueError(() => [52, 10, 0, 0], TypeError, /expected 3, got 4/); 84 expectMultiValueError(() => (function*() { yield 52; yield 10; yield 0; yield 0; })(), 85 TypeError, /expected 3, got 4/); 86 87 // Check that side effects from conversions are done in order. 88 { 89 let calls = []; 90 function log(x) { calls.push(x); return x; } 91 function logged(x) { return { valueOf: () => log(x) } } 92 expectMultiValuePass(() => [logged(52), logged(10), logged(0)]); 93 assertEq(calls.join(','), '52,10,0'); 94 } 95 96 function expectMultiValueResult(text, expected) { 97 let instance = wasmEvalText(text); 98 assertDeepEq(instance.exports.run(), expected); 99 } 100 101 expectMultiValueResult(` 102 (module 103 (func (export "run") (result i32 i32 i32) 104 (i32.const 0) 105 (i32.const 52) 106 (i32.const 10)))`, [0, 52, 10]); 107 expectMultiValueResult(` 108 (module 109 (func (export "run") (result f32 f32 f32) 110 (f32.const 0.5) 111 (f32.const 52.5) 112 (f32.const 10.5)))`, [0.5, 52.5, 10.5]); 113 expectMultiValueResult(` 114 (module 115 (func (export "run") (result f64 f64 f64) 116 (f64.const 0.5) 117 (f64.const 52.5) 118 (f64.const 10.5)))`, [0.5, 52.5, 10.5]); 119 120 expectMultiValueResult(` 121 (module 122 (func (export "run") (result i32 i64 i32) 123 (i32.const 0) 124 (i64.const 52) 125 (i32.const 10)))`, [0, 52n, 10]); 126 expectMultiValueResult(` 127 (module 128 (func (export "run") (result i64 i32 i64) 129 (i64.const 0) 130 (i32.const 52) 131 (i64.const 10)))`, [0n, 52, 10n]); 132 expectMultiValueResult(` 133 (module 134 (func (export "run") (result i64 i64 i64) 135 (i64.const 0) 136 (i64.const 52) 137 (i64.const 10)))`, [0n, 52n, 10n]);