| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
| 6 | 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 | 9 |
| 10 var module = (function () { | 10 var module = (function () { |
| 11 var builder = new WasmModuleBuilder(); | 11 var builder = new WasmModuleBuilder(); |
| 12 | 12 |
| 13 var sig_index = builder.addType(kSig_i_ii); | 13 var sig_index = builder.addType(kSig_i_ii); |
| 14 builder.addImport("add", sig_index); | 14 builder.addImport("add", sig_index); |
| 15 builder.addFunction("add", sig_index) | 15 builder.addFunction("add", sig_index) |
| 16 .addBody([ | 16 .addBody([ |
| 17 kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0 | 17 kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0 |
| 18 ]); | 18 ]); |
| 19 builder.addFunction("sub", sig_index) | 19 builder.addFunction("sub", sig_index) |
| 20 .addBody([ | 20 .addBody([ |
| 21 kExprGetLocal, 0, // -- | 21 kExprGetLocal, 0, // -- |
| 22 kExprGetLocal, 1, // -- | 22 kExprGetLocal, 1, // -- |
| 23 kExprI32Sub, // -- | 23 kExprI32Sub, // -- |
| 24 ]); | 24 ]); |
| 25 builder.addFunction("main", kSig_i_iii) | 25 builder.addFunction("main", kSig_i_iii) |
| 26 .addBody([ | 26 .addBody([ |
| 27 kExprGetLocal, 1, | 27 kExprGetLocal, 1, |
| 28 kExprGetLocal, 2, | 28 kExprGetLocal, 2, |
| 29 kExprGetLocal, 0, | 29 kExprGetLocal, 0, |
| 30 kExprCallIndirect, sig_index | 30 kExprCallIndirect, kTableZero, sig_index |
| 31 ]) | 31 ]) |
| 32 .exportFunc() | 32 .exportFunc() |
| 33 builder.appendToTable([1, 2, 3]); | 33 builder.appendToTable([1, 2, 3]); |
| 34 | 34 |
| 35 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); | 35 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); |
| 36 })(); | 36 })(); |
| 37 | 37 |
| 38 // Check the module exists. | 38 // Check the module exists. |
| 39 assertFalse(module === undefined); | 39 assertFalse(module === undefined); |
| 40 assertFalse(module === null); | 40 assertFalse(module === null); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 64 var popcnt = builder.addFunction("popcnt", sig_i_i) | 64 var popcnt = builder.addFunction("popcnt", sig_i_i) |
| 65 .addBody([ | 65 .addBody([ |
| 66 kExprGetLocal, 0, // -- | 66 kExprGetLocal, 0, // -- |
| 67 kExprI32Popcnt // -- | 67 kExprI32Popcnt // -- |
| 68 ]); | 68 ]); |
| 69 var main = builder.addFunction("main", kSig_i_iii) | 69 var main = builder.addFunction("main", kSig_i_iii) |
| 70 .addBody([ | 70 .addBody([ |
| 71 kExprGetLocal, 1, | 71 kExprGetLocal, 1, |
| 72 kExprGetLocal, 2, | 72 kExprGetLocal, 2, |
| 73 kExprGetLocal, 0, | 73 kExprGetLocal, 0, |
| 74 kExprCallIndirect, sig_i_ii | 74 kExprCallIndirect, kTableZero, sig_i_ii |
| 75 ]) | 75 ]) |
| 76 .exportFunc() | 76 .exportFunc() |
| 77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); | 77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); |
| 78 | 78 |
| 79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); | 79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); |
| 80 })(); | 80 })(); |
| 81 | 81 |
| 82 assertEquals(-6, module.exports.main(0, -2, 3)); | 82 assertEquals(-6, module.exports.main(0, -2, 3)); |
| 83 assertEquals(99, module.exports.main(1, 22, 77)); | 83 assertEquals(99, module.exports.main(1, 22, 77)); |
| 84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | 84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 105 .addBody([ | 105 .addBody([ |
| 106 kExprGetLocal, 0, // -- | 106 kExprGetLocal, 0, // -- |
| 107 kExprGetLocal, 1, // -- | 107 kExprGetLocal, 1, // -- |
| 108 kExprI32Sub // -- | 108 kExprI32Sub // -- |
| 109 ]); | 109 ]); |
| 110 builder.addFunction("main", kSig_i_ii) | 110 builder.addFunction("main", kSig_i_ii) |
| 111 .addBody([ | 111 .addBody([ |
| 112 kExprI32Const, 33, // -- | 112 kExprI32Const, 33, // -- |
| 113 kExprGetLocal, 0, // -- | 113 kExprGetLocal, 0, // -- |
| 114 kExprGetLocal, 1, // -- | 114 kExprGetLocal, 1, // -- |
| 115 kExprCallIndirect, 0]) // -- | 115 kExprCallIndirect, kTableZero, 0]) // -- |
| 116 .exportAs("main"); | 116 .exportAs("main"); |
| 117 | 117 |
| 118 builder.appendToTable([mul.index, add.index, sub.index]); | 118 builder.appendToTable([mul.index, add.index, sub.index]); |
| 119 | 119 |
| 120 return builder.instantiate(); | 120 return builder.instantiate(); |
| 121 })(); | 121 })(); |
| 122 | 122 |
| 123 assertEquals(33, module.exports.main(1, 0)); | 123 assertEquals(33, module.exports.main(1, 0)); |
| 124 assertEquals(66, module.exports.main(2, 0)); | 124 assertEquals(66, module.exports.main(2, 0)); |
| 125 assertEquals(34, module.exports.main(1, 1)); | 125 assertEquals(34, module.exports.main(1, 1)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 149 .addBody([ | 149 .addBody([ |
| 150 kExprGetLocal, 0, // -- | 150 kExprGetLocal, 0, // -- |
| 151 kExprGetLocal, 1, // -- | 151 kExprGetLocal, 1, // -- |
| 152 kExprI32Sub // -- | 152 kExprI32Sub // -- |
| 153 ]); | 153 ]); |
| 154 builder.addFunction("main", kSig_i_ii) | 154 builder.addFunction("main", kSig_i_ii) |
| 155 .addBody([ | 155 .addBody([ |
| 156 kExprI32Const, 33, // -- | 156 kExprI32Const, 33, // -- |
| 157 kExprGetLocal, 0, // -- | 157 kExprGetLocal, 0, // -- |
| 158 kExprGetLocal, 1, // -- | 158 kExprGetLocal, 1, // -- |
| 159 kExprCallIndirect, 0]) // -- | 159 kExprCallIndirect, kTableZero, 0]) // -- |
| 160 .exportAs("main"); | 160 .exportAs("main"); |
| 161 | 161 |
| 162 builder.setFunctionTableLength(length); | 162 builder.setFunctionTableLength(length); |
| 163 builder.addFunctionTableInit(base, false, [add.index, sub.index, mul.index])
; | 163 builder.addFunctionTableInit(base, false, [add.index, sub.index, mul.index])
; |
| 164 | 164 |
| 165 return builder.instantiate(); | 165 return builder.instantiate(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 for (var i = 0; i < 5; i++) { | 168 for (var i = 0; i < 5; i++) { |
| 169 print(" base = " + i); | 169 print(" base = " + i); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 .addBody([ | 203 .addBody([ |
| 204 kExprGetLocal, 0, // -- | 204 kExprGetLocal, 0, // -- |
| 205 kExprGetLocal, 1, // -- | 205 kExprGetLocal, 1, // -- |
| 206 kExprI32Sub // -- | 206 kExprI32Sub // -- |
| 207 ]); | 207 ]); |
| 208 builder.addFunction("main", kSig_i_ii) | 208 builder.addFunction("main", kSig_i_ii) |
| 209 .addBody([ | 209 .addBody([ |
| 210 kExprI32Const, 33, // -- | 210 kExprI32Const, 33, // -- |
| 211 kExprGetLocal, 0, // -- | 211 kExprGetLocal, 0, // -- |
| 212 kExprGetLocal, 1, // -- | 212 kExprGetLocal, 1, // -- |
| 213 kExprCallIndirect, 0]) // -- | 213 kExprCallIndirect, kTableZero, 0]) // -- |
| 214 .exportAs("main"); | 214 .exportAs("main"); |
| 215 | 215 |
| 216 builder.setFunctionTableLength(10); | 216 builder.setFunctionTableLength(10); |
| 217 var g = builder.addImportedGlobal("base", undefined, kAstI32); | 217 var g = builder.addImportedGlobal("base", undefined, kAstI32); |
| 218 builder.addFunctionTableInit(g, true, [mul.index, add.index, sub.index]); | 218 builder.addFunctionTableInit(g, true, [mul.index, add.index, sub.index]); |
| 219 | 219 |
| 220 var module = new WebAssembly.Module(builder.toBuffer()); | 220 var module = new WebAssembly.Module(builder.toBuffer()); |
| 221 | 221 |
| 222 for (var i = 0; i < 5; i++) { | 222 for (var i = 0; i < 5; i++) { |
| 223 print(" base = " + i); | 223 print(" base = " + i); |
| 224 var instance = new WebAssembly.Instance(module, {base: i}); | 224 var instance = new WebAssembly.Instance(module, {base: i}); |
| 225 main = instance.exports.main; | 225 main = instance.exports.main; |
| 226 for (var j = 0; j < i; j++) { | 226 for (var j = 0; j < i; j++) { |
| 227 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); | 227 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
| 228 } | 228 } |
| 229 assertEquals(33, main(1, i + 0)); | 229 assertEquals(33, main(1, i + 0)); |
| 230 assertEquals(66, main(2, i + 0)); | 230 assertEquals(66, main(2, i + 0)); |
| 231 assertEquals(34, main(1, i + 1)); | 231 assertEquals(34, main(1, i + 1)); |
| 232 assertEquals(35, main(2, i + 1)); | 232 assertEquals(35, main(2, i + 1)); |
| 233 assertEquals(32, main(1, i + 2)); | 233 assertEquals(32, main(1, i + 2)); |
| 234 assertEquals(31, main(2, i + 2)); | 234 assertEquals(31, main(2, i + 2)); |
| 235 assertTraps(kTrapFuncInvalid, "main(12, 10)"); | 235 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
| 236 } | 236 } |
| 237 })(); | 237 })(); |
| OLD | NEW |