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.addSignature([kAstI32, kAstI32, kAstI32]); | 13 var sig_index = builder.addSignature([kAstI32, kAstI32, kAstI32]); |
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 kExprCallImport, 0, kExprGetLocal, 0, kExprGetLocal, 1 | 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 kExprI32Sub, kExprGetLocal, 0, kExprGetLocal, 1 | 21 kExprGetLocal, 0, // -- |
| 22 kExprGetLocal, 1, // -- |
| 23 kExprI32Sub, // -- |
22 ]); | 24 ]); |
23 builder.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) | 25 builder.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) |
24 .addBody([ | 26 .addBody([ |
25 kExprCallIndirect, sig_index, | |
26 kExprGetLocal, 0, | 27 kExprGetLocal, 0, |
27 kExprGetLocal, 1, | 28 kExprGetLocal, 1, |
28 kExprGetLocal, 2]) | 29 kExprGetLocal, 2, |
| 30 kExprCallIndirect, kArity2, sig_index |
| 31 ]) |
29 .exportFunc() | 32 .exportFunc() |
30 builder.appendToFunctionTable([0, 1, 2]); | 33 builder.appendToFunctionTable([0, 1, 2]); |
31 | 34 |
32 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); | 35 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); |
33 })(); | 36 })(); |
34 | 37 |
35 // Check the module exists. | 38 // Check the module exists. |
36 assertFalse(module === undefined); | 39 assertFalse(module === undefined); |
37 assertFalse(module === null); | 40 assertFalse(module === null); |
38 assertFalse(module === 0); | 41 assertFalse(module === 0); |
39 assertEquals("object", typeof module.exports); | 42 assertEquals("object", typeof module.exports); |
40 assertEquals("function", typeof module.exports.main); | 43 assertEquals("function", typeof module.exports.main); |
41 | 44 |
42 assertEquals(5, module.exports.main(1, 12, 7)); | 45 assertEquals(5, module.exports.main(1, 12, 7)); |
43 assertEquals(19, module.exports.main(0, 12, 7)); | 46 assertEquals(19, module.exports.main(0, 12, 7)); |
44 | 47 |
45 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | 48 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
46 assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); | 49 assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); |
OLD | NEW |