OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 const kTableSize = 3; |
| 11 |
| 12 var m_i_v; |
| 13 var m_i_i; |
| 14 var m_i_ii; |
| 15 |
| 16 (function BuildExportedMethods() { |
| 17 print("BuildingExportedMethods..."); |
| 18 let builder = new WasmModuleBuilder(); |
| 19 |
| 20 let sig_v_v = builder.addType(kSig_v_v); |
| 21 let sig_i_ii = builder.addType(kSig_i_ii); |
| 22 let sig_i_i = builder.addType(kSig_i_i); |
| 23 let sig_i_v = builder.addType(kSig_i_v); |
| 24 |
| 25 print("exported[i_v] = " + sig_i_v); |
| 26 print("exported[i_i] = " + sig_i_i); |
| 27 print("exported[i_ii] = " + sig_i_ii); |
| 28 |
| 29 builder.addFunction("", sig_i_v) |
| 30 .addBody([ |
| 31 kExprI32Const, 41]) // -- |
| 32 .exportAs("m_i_v"); |
| 33 |
| 34 builder.addFunction("m_i_i", sig_i_i) |
| 35 .addBody([ |
| 36 kExprI32Const, 42]) // -- |
| 37 .exportAs("m_i_i"); |
| 38 |
| 39 builder.addFunction("m_i_ii", sig_i_ii) |
| 40 .addBody([ |
| 41 kExprI32Const, 43]) // -- |
| 42 .exportAs("m_i_ii"); |
| 43 |
| 44 let module = new WebAssembly.Module(builder.toBuffer()); |
| 45 let instance = new WebAssembly.Instance(module); |
| 46 m_i_v = instance.exports.m_i_v; |
| 47 m_i_i = instance.exports.m_i_i; |
| 48 m_i_ii = instance.exports.m_i_ii; |
| 49 |
| 50 m_i_v.sig = kSig_i_v; |
| 51 m_i_i.sig = kSig_i_i; |
| 52 m_i_ii.sig = kSig_i_ii; |
| 53 })(); |
| 54 |
| 55 function caller_module() { |
| 56 let builder = new WasmModuleBuilder(); |
| 57 |
| 58 let sig_i_v = builder.addType(kSig_i_v); |
| 59 let sig_i_ii = builder.addType(kSig_i_ii); |
| 60 let sig_i_i = builder.addType(kSig_i_i); |
| 61 |
| 62 print("imported[i_v] = " + sig_i_v); |
| 63 print("imported[i_i] = " + sig_i_i); |
| 64 print("imported[i_ii] = " + sig_i_ii); |
| 65 |
| 66 |
| 67 builder.addFunction("call1", sig_i_i) |
| 68 .addBody([ |
| 69 kExprGetLocal, 0, // -- |
| 70 kExprCallIndirect, sig_i_v, kTableZero]) // -- |
| 71 .exportAs("call1"); |
| 72 |
| 73 builder.addFunction("call2", sig_i_i) |
| 74 .addBody([ |
| 75 kExprI32Const, 11, // -- |
| 76 kExprGetLocal, 0, |
| 77 kExprCallIndirect, sig_i_i, kTableZero]) // -- |
| 78 .exportAs("call2"); |
| 79 |
| 80 builder.addFunction("call3", sig_i_i) |
| 81 .addBody([ |
| 82 kExprI32Const, 21, |
| 83 kExprI32Const, 22, |
| 84 kExprGetLocal, 0, |
| 85 kExprCallIndirect, sig_i_ii, kTableZero]) // -- |
| 86 .exportAs("call3"); |
| 87 |
| 88 builder.addImportedTable("imp", "table", kTableSize, kTableSize); |
| 89 |
| 90 return builder.toModule(); |
| 91 } |
| 92 |
| 93 function call(func, ...args) { |
| 94 try { |
| 95 return "" + func.apply(undefined, args); |
| 96 } catch (e) { |
| 97 return "!" + e; |
| 98 } |
| 99 } |
| 100 |
| 101 function DoCalls(table, calls) { |
| 102 for (func of calls) { |
| 103 print("func = " + func); |
| 104 for (var i = 0; i < 4; i++) { |
| 105 print(" i = " + i); |
| 106 var expectThrow = true; |
| 107 var exp = null; |
| 108 if (i < table.length) { |
| 109 exported = table.get(i); |
| 110 expectThrow = (exported.sig != func.sig); |
| 111 print(" exp=" + exported); |
| 112 print(" throw=" + expectThrow); |
| 113 } else { |
| 114 print(" exp=<oob>"); |
| 115 } |
| 116 print(" result=" + call(func, i)); |
| 117 if (expectThrow) { |
| 118 assertThrows(() => func(i), WebAssembly.RuntimeError); |
| 119 } else { |
| 120 assertEquals(exported(0), func(i)); |
| 121 } |
| 122 } |
| 123 } |
| 124 } |
| 125 |
| 126 (function TestExecute() { |
| 127 print("TestExecute"); |
| 128 let module = caller_module(); |
| 129 let table = new WebAssembly.Table({element: "anyfunc", |
| 130 initial: kTableSize, maximum: kTableSize}); |
| 131 let instance = new WebAssembly.Instance(module, {imp: {table: table}}); |
| 132 instance.exports.call1.sig = kSig_i_v; |
| 133 instance.exports.call2.sig = kSig_i_i; |
| 134 instance.exports.call3.sig = kSig_i_ii; |
| 135 |
| 136 let exports = [m_i_v, m_i_i, m_i_ii]; |
| 137 let calls = [instance.exports.call1, |
| 138 instance.exports.call2, |
| 139 instance.exports.call3]; |
| 140 |
| 141 for (f0 of exports) { |
| 142 for (f1 of exports) { |
| 143 for (f2 of exports) { |
| 144 table.set(0, f0); |
| 145 table.set(1, f1); |
| 146 table.set(2, f2); |
| 147 |
| 148 DoCalls(table, calls); |
| 149 } |
| 150 } |
| 151 } |
| 152 })(); |
OLD | NEW |