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("q", "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, kExprCallFunction, 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, 1, | 27 kExprGetLocal, 1, |
28 kExprGetLocal, 2, | 28 kExprGetLocal, 2, |
29 kExprGetLocal, 0, | 29 kExprGetLocal, 0, |
30 kExprCallIndirect, sig_index, kTableZero | 30 kExprCallIndirect, sig_index, kTableZero |
31 ]) | 31 ]) |
32 .exportFunc() | 32 .exportFunc() |
33 builder.appendToTable([1, 2, 3]); | 33 builder.appendToTable([1, 2, 3]); |
34 | 34 |
35 return builder.instantiate({add: function(a, b) { return a + b | 0; }}); | 35 return builder.instantiate({q: {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 | 50 |
51 | 51 |
52 module = (function () { | 52 module = (function () { |
53 var builder = new WasmModuleBuilder(); | 53 var builder = new WasmModuleBuilder(); |
54 | 54 |
55 var sig_i_ii = builder.addType(kSig_i_ii); | 55 var sig_i_ii = builder.addType(kSig_i_ii); |
56 var sig_i_i = builder.addType(kSig_i_i); | 56 var sig_i_i = builder.addType(kSig_i_i); |
57 var mul = builder.addImport("mul", sig_i_ii); | 57 var mul = builder.addImport("q", "mul", sig_i_ii); |
58 var add = builder.addFunction("add", sig_i_ii) | 58 var add = builder.addFunction("add", sig_i_ii) |
59 .addBody([ | 59 .addBody([ |
60 kExprGetLocal, 0, // -- | 60 kExprGetLocal, 0, // -- |
61 kExprGetLocal, 1, // -- | 61 kExprGetLocal, 1, // -- |
62 kExprI32Add // -- | 62 kExprI32Add // -- |
63 ]); | 63 ]); |
64 var popcnt = builder.addFunction("popcnt", sig_i_i) | 64 var popcnt = builder.addFunction("popcnt", sig_i_i) |
65 .addBody([ | 65 .addBody([ |
66 kExprGetLocal, 0, // -- | 66 kExprGetLocal, 0, // -- |
67 kExprI32Popcnt // -- | 67 kExprI32Popcnt // -- |
68 ]); | 68 ]); |
69 var main = builder.addFunction("main", kSig_i_iii) | 69 var main = builder.addFunction("main", kSig_i_iii) |
70 .addBody([ | 70 .addBody([ |
71 kExprGetLocal, 1, | 71 kExprGetLocal, 1, |
72 kExprGetLocal, 2, | 72 kExprGetLocal, 2, |
73 kExprGetLocal, 0, | 73 kExprGetLocal, 0, |
74 kExprCallIndirect, sig_i_ii, kTableZero | 74 kExprCallIndirect, sig_i_ii, kTableZero |
75 ]) | 75 ]) |
76 .exportFunc(); | 76 .exportFunc(); |
77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); | 77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); |
78 | 78 |
79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); | 79 return builder.instantiate({q: {mul: function(a, b) { return a * b | 0; }}}); |
80 })(); | 80 })(); |
81 | 81 |
82 assertEquals(-6, module.exports.main(0, -2, 3)); | 82 assertEquals(-6, module.exports.main(0, -2, 3)); |
83 assertEquals(99, module.exports.main(1, 22, 77)); | 83 assertEquals(99, module.exports.main(1, 22, 77)); |
84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | 84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
85 assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)"); | 85 assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)"); |
86 assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)"); | 86 assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)"); |
87 | 87 |
88 function AddFunctions(builder) { | 88 function AddFunctions(builder) { |
89 var mul = builder.addFunction("mul", kSig_i_ii) | 89 var mul = builder.addFunction("mul", kSig_i_ii) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 var f = AddFunctions(builder); | 178 var f = AddFunctions(builder); |
179 builder.addFunction("main", kSig_i_ii) | 179 builder.addFunction("main", kSig_i_ii) |
180 .addBody([ | 180 .addBody([ |
181 kExprI32Const, 33, // -- | 181 kExprI32Const, 33, // -- |
182 kExprGetLocal, 0, // -- | 182 kExprGetLocal, 0, // -- |
183 kExprGetLocal, 1, // -- | 183 kExprGetLocal, 1, // -- |
184 kExprCallIndirect, 0, kTableZero]) // -- | 184 kExprCallIndirect, 0, kTableZero]) // -- |
185 .exportAs("main"); | 185 .exportAs("main"); |
186 | 186 |
187 builder.setFunctionTableLength(10); | 187 builder.setFunctionTableLength(10); |
188 var g = builder.addImportedGlobal("base", undefined, kAstI32); | 188 var g = builder.addImportedGlobal("fff", "base", kAstI32); |
189 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index])
; | 189 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index])
; |
190 | 190 |
191 var module = new WebAssembly.Module(builder.toBuffer()); | 191 var module = new WebAssembly.Module(builder.toBuffer()); |
192 | 192 |
193 for (var i = 0; i < 5; i++) { | 193 for (var i = 0; i < 5; i++) { |
194 print(" base = " + i); | 194 print(" base = " + i); |
195 var instance = new WebAssembly.Instance(module, {base: i}); | 195 var instance = new WebAssembly.Instance(module, {fff: {base: i}}); |
196 main = instance.exports.main; | 196 main = instance.exports.main; |
197 for (var j = 0; j < i; j++) { | 197 for (var j = 0; j < i; j++) { |
198 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); | 198 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
199 } | 199 } |
200 assertEquals(33, main(1, i + 0)); | 200 assertEquals(33, main(1, i + 0)); |
201 assertEquals(66, main(2, i + 0)); | 201 assertEquals(66, main(2, i + 0)); |
202 assertEquals(34, main(1, i + 1)); | 202 assertEquals(34, main(1, i + 1)); |
203 assertEquals(35, main(2, i + 1)); | 203 assertEquals(35, main(2, i + 1)); |
204 assertEquals(32, main(1, i + 2)); | 204 assertEquals(32, main(1, i + 2)); |
205 assertEquals(31, main(2, i + 2)); | 205 assertEquals(31, main(2, i + 2)); |
206 assertTraps(kTrapFuncInvalid, "main(12, 10)"); | 206 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
207 } | 207 } |
208 })(); | 208 })(); |
OLD | NEW |