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