| 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, kExprCallImport, kArity2, 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, 0, |
| 27 kExprGetLocal, 1, | 28 kExprGetLocal, 1, |
| 28 kExprGetLocal, 2, | 29 kExprGetLocal, 2, |
| 29 kExprGetLocal, 0, | 30 kExprCallIndirect, kArity2, sig_index |
| 30 kExprCallIndirect, sig_index | |
| 31 ]) | 31 ]) |
| 32 .exportFunc() | 32 .exportFunc() |
| 33 builder.appendToTable([1, 2, 3]); | 33 builder.appendToTable([0, 1, 2]); |
| 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); |
| 41 assertFalse(module === 0); | 41 assertFalse(module === 0); |
| 42 assertEquals("object", typeof module.exports); | 42 assertEquals("object", typeof module.exports); |
| 43 assertEquals("function", typeof module.exports.main); | 43 assertEquals("function", typeof module.exports.main); |
| 44 | 44 |
| 45 assertEquals(5, module.exports.main(1, 12, 7)); | 45 assertEquals(5, module.exports.main(1, 12, 7)); |
| 46 assertEquals(19, module.exports.main(0, 12, 7)); | 46 assertEquals(19, module.exports.main(0, 12, 7)); |
| 47 | 47 |
| 48 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | 48 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
| 49 assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); | 49 assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); |
| 50 | |
| 51 | |
| 52 module = (function () { | |
| 53 var builder = new WasmModuleBuilder(); | |
| 54 | |
| 55 var sig_i_ii = builder.addType(kSig_i_ii); | |
| 56 var sig_i_i = builder.addType(kSig_i_i); | |
| 57 builder.addImport("mul", sig_i_ii); | |
| 58 builder.addFunction("add", sig_i_ii) | |
| 59 .addBody([ | |
| 60 kExprGetLocal, 0, // -- | |
| 61 kExprGetLocal, 1, // -- | |
| 62 kExprI32Add // -- | |
| 63 ]); | |
| 64 builder.addFunction("popcnt", sig_i_i) | |
| 65 .addBody([ | |
| 66 kExprGetLocal, 0, // -- | |
| 67 kExprI32Popcnt // -- | |
| 68 ]); | |
| 69 builder.addFunction("main", kSig_i_iii) | |
| 70 .addBody([ | |
| 71 kExprGetLocal, 1, | |
| 72 kExprGetLocal, 2, | |
| 73 kExprGetLocal, 0, | |
| 74 kExprCallIndirect, sig_i_ii | |
| 75 ]) | |
| 76 .exportFunc() | |
| 77 builder.appendToTable([0, 1, 2, 3]); | |
| 78 | |
| 79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); | |
| 80 })(); | |
| 81 | |
| 82 assertEquals(-6, module.exports.main(0, -2, 3)); | |
| 83 assertEquals(99, module.exports.main(1, 22, 77)); | |
| 84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | |
| 85 assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)"); | |
| 86 assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)"); | |
| OLD | NEW |