1// META: global=window,dedicatedworker,jsshell 2// META: script=/wasm/jsapi/wasm-module-builder.js 3// META: script=/wasm/jsapi/assertions.js 4 5const type_if_fi = makeSig([kWasmF64, kWasmI32], [kWasmI32, kWasmF64]); 6 7promise_test(async () => { 8 const builder = new WasmModuleBuilder(); 9 10 builder 11 .addFunction("swap", type_if_fi) 12 .addBody([ 13 kExprLocalGet, 1, 14 kExprLocalGet, 0, 15 kExprReturn, 16 ]) 17 .exportFunc(); 18 19 const buffer = builder.toBuffer(); 20 21 const result = await WebAssembly.instantiate(buffer); 22 const swapped = result.instance.exports.swap(4.2, 7); 23 assert_true(Array.isArray(swapped)); 24 assert_equals(Object.getPrototypeOf(swapped), Array.prototype); 25 assert_array_equals(swapped, [7, 4.2]); 26}, "multiple return values from wasm to js"); 27 28promise_test(async () => { 29 const builder = new WasmModuleBuilder(); 30 31 const swap = builder 32 .addFunction("swap", type_if_fi) 33 .addBody([ 34 kExprLocalGet, 1, 35 kExprLocalGet, 0, 36 kExprReturn, 37 ]); 38 builder 39 .addFunction("callswap", kSig_i_v) 40 .addBody([ 41 ...wasmF64Const(4.2), 42 ...wasmI32Const(7), 43 kExprCallFunction, swap.index, 44 kExprDrop, 45 kExprReturn, 46 ]) 47 .exportFunc(); 48 49 const buffer = builder.toBuffer(); 50 51 const result = await WebAssembly.instantiate(buffer); 52 const swapped = result.instance.exports.callswap(); 53 assert_equals(swapped, 7); 54}, "multiple return values inside wasm"); 55 56promise_test(async () => { 57 const builder = new WasmModuleBuilder(); 58 59 const fnIndex = builder.addImport("module", "fn", type_if_fi); 60 builder 61 .addFunction("callfn", kSig_i_v) 62 .addBody([ 63 ...wasmF64Const(4.2), 64 ...wasmI32Const(7), 65 kExprCallFunction, fnIndex, 66 kExprDrop, 67 kExprReturn, 68 ]) 69 .exportFunc(); 70 71 const buffer = builder.toBuffer(); 72 73 const actual = []; 74 const imports = { 75 "module": { 76 fn(f32, i32) { 77 assert_equals(f32, 4.2); 78 assert_equals(i32, 7); 79 const result = [2, 7.3]; 80 let i = 0; 81 return { 82 get [Symbol.iterator]() { 83 actual.push("@@iterator getter"); 84 return function iterator() { 85 actual.push("@@iterator call"); 86 return { 87 get next() { 88 actual.push("next getter"); 89 return function next(...args) { 90 assert_array_equals(args, []); 91 let j = ++i; 92 actual.push(`next call ${j}`); 93 if (j > result.length) { 94 return { 95 get done() { 96 actual.push(`done call ${j}`); 97 return true; 98 } 99 }; 100 } 101 return { 102 get done() { 103 actual.push(`done call ${j}`); 104 return false; 105 }, 106 get value() { 107 actual.push(`value call ${j}`); 108 return { 109 get valueOf() { 110 actual.push(`valueOf get ${j}`); 111 return function() { 112 actual.push(`valueOf call ${j}`); 113 return result[j - 1]; 114 }; 115 } 116 }; 117 } 118 }; 119 }; 120 } 121 }; 122 } 123 }, 124 }; 125 }, 126 } 127 }; 128 129 const { instance } = await WebAssembly.instantiate(buffer, imports); 130 const result = instance.exports.callfn(); 131 assert_equals(result, 2); 132 assert_array_equals(actual, [ 133 "@@iterator getter", 134 "@@iterator call", 135 "next getter", 136 "next call 1", 137 "done call 1", 138 "value call 1", 139 "next call 2", 140 "done call 2", 141 "value call 2", 142 "next call 3", 143 "done call 3", 144 "valueOf get 1", 145 "valueOf call 1", 146 "valueOf get 2", 147 "valueOf call 2", 148 ]); 149}, "multiple return values from js to wasm"); 150