Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --expose-wasm |
| 6 | |
| 7 | load("test/mjsunit/wasm/wasm-constants.js"); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 8 | load("test/mjsunit/wasm/wasm-module-builder.js"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | |
| 10 | var module = (function () { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 11 | var builder = new WasmModuleBuilder(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 13 | var sig_index = builder.addSignature([kAstI32, kAstI32, kAstI32]); |
| 14 | builder.addImport("add", sig_index); |
| 15 | builder.addFunction("add", sig_index) |
| 16 | .addBody([ |
| 17 | kExprCallImport, 0, kExprGetLocal, 0, kExprGetLocal, 1 |
| 18 | ]); |
| 19 | builder.addFunction("sub", sig_index) |
| 20 | .addBody([ |
| 21 | kExprI32Sub, kExprGetLocal, 0, kExprGetLocal, 1 |
| 22 | ]); |
| 23 | builder.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) |
| 24 | .addBody([ |
| 25 | kExprCallIndirect, sig_index, |
| 26 | kExprGetLocal, 0, |
| 27 | kExprGetLocal, 1, |
| 28 | kExprGetLocal, 2]) |
| 29 | .exportFunc() |
| 30 | builder.appendToFunctionTable([0, 1, 2]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 32 | return builder.instantiate({add: function(a, b) { return a + b | 0; }}); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | })(); |
| 34 | |
| 35 | // Check the module exists. |
| 36 | assertFalse(module === undefined); |
| 37 | assertFalse(module === null); |
| 38 | assertFalse(module === 0); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 39 | assertEquals("object", typeof module.exports); |
| 40 | assertEquals("function", typeof module.exports.main); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 42 | assertEquals(5, module.exports.main(1, 12, 7)); |
| 43 | assertEquals(19, module.exports.main(0, 12, 7)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 45 | assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
| 46 | assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); |