| 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 | 9 |
| 9 var module = (function () { | 10 var module = (function () { |
| 10 var kFuncWithBody = 9; | 11 var builder = new WasmModuleBuilder(); |
| 11 var kFuncImported = 7; | |
| 12 var kBodySize1 = 5; | |
| 13 var kBodySize2 = 8; | |
| 14 var kFuncTableSize = 8; | |
| 15 var kSubOffset = kHeaderSize + 13 + kFuncWithBody + kBodySize1 + kFuncImported
+ kFuncWithBody + kBodySize2 + kFuncTableSize + 1; | |
| 16 var kAddOffset = kSubOffset + 4; | |
| 17 var kMainOffset = kAddOffset + 4; | |
| 18 | 12 |
| 19 var ffi = new Object(); | 13 var sig_index = builder.addSignature([kAstI32, kAstI32, kAstI32]); |
| 20 ffi.add = (function(a, b) { return a + b | 0; }); | 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]); |
| 21 | 31 |
| 22 return _WASMEXP_.instantiateModule(bytesWithHeader( | 32 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); |
| 23 // -- signatures | |
| 24 kDeclSignatures, 2, | |
| 25 2, kAstI32, kAstI32, kAstI32, // int, int -> int | |
| 26 3, kAstI32, kAstI32, kAstI32, kAstI32, // int, int, int -> int | |
| 27 // -- function #0 (sub) | |
| 28 kDeclFunctions, 3, | |
| 29 kDeclFunctionName, | |
| 30 0, 0, // signature offset | |
| 31 kSubOffset, 0, 0, 0, // name offset | |
| 32 kBodySize1, 0, // body size | |
| 33 kExprI32Sub, // -- | |
| 34 kExprGetLocal, 0, // -- | |
| 35 kExprGetLocal, 1, // -- | |
| 36 // -- function #1 (add) | |
| 37 kDeclFunctionName | kDeclFunctionImport, | |
| 38 0, 0, // signature offset | |
| 39 kAddOffset, 0, 0, 0, // name offset | |
| 40 // -- function #2 (main) | |
| 41 kDeclFunctionName | kDeclFunctionExport, | |
| 42 1, 0, // signature offset | |
| 43 kMainOffset, 0, 0, 0, // name offset | |
| 44 kBodySize2, 0, // body size | |
| 45 kExprCallIndirect, 0, | |
| 46 kExprGetLocal, 0, | |
| 47 kExprGetLocal, 1, | |
| 48 kExprGetLocal, 2, | |
| 49 // -- function table | |
| 50 kDeclFunctionTable, | |
| 51 3, | |
| 52 0, 0, | |
| 53 1, 0, | |
| 54 2, 0, | |
| 55 kDeclEnd, | |
| 56 's', 'u', 'b', 0, // name | |
| 57 'a', 'd', 'd', 0, // name | |
| 58 'm', 'a', 'i', 'n', 0 // name | |
| 59 ), ffi); | |
| 60 })(); | 33 })(); |
| 61 | 34 |
| 62 // Check the module exists. | 35 // Check the module exists. |
| 63 assertFalse(module === undefined); | 36 assertFalse(module === undefined); |
| 64 assertFalse(module === null); | 37 assertFalse(module === null); |
| 65 assertFalse(module === 0); | 38 assertFalse(module === 0); |
| 66 assertEquals("object", typeof module); | 39 assertEquals("object", typeof module.exports); |
| 67 assertEquals("function", typeof module.main); | 40 assertEquals("function", typeof module.exports.main); |
| 68 | 41 |
| 69 assertEquals(5, module.main(0, 12, 7)); | 42 assertEquals(5, module.exports.main(1, 12, 7)); |
| 70 assertEquals(19, module.main(1, 12, 7)); | 43 assertEquals(19, module.exports.main(0, 12, 7)); |
| 71 | 44 |
| 72 assertTraps(kTrapFuncSigMismatch, "module.main(2, 12, 33)"); | 45 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
| 73 assertTraps(kTrapFuncInvalid, "module.main(3, 12, 33)"); | 46 assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)"); |
| OLD | NEW |