OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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"); | |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | |
9 | |
10 function AddFunctions(builder) { | |
11 var mul = builder.addFunction("mul", kSig_i_ii) | |
12 .addBody([ | |
13 kExprGetLocal, 0, // -- | |
14 kExprGetLocal, 1, // -- | |
15 kExprI32Mul // -- | |
16 ]); | |
17 var add = builder.addFunction("add", kSig_i_ii) | |
18 .addBody([ | |
19 kExprGetLocal, 0, // -- | |
20 kExprGetLocal, 1, // -- | |
21 kExprI32Add // -- | |
22 ]); | |
23 var sub = builder.addFunction("sub", kSig_i_ii) | |
24 .addBody([ | |
25 kExprGetLocal, 0, // -- | |
26 kExprGetLocal, 1, // -- | |
27 kExprI32Sub // -- | |
28 ]); | |
29 return {mul: mul, add: add, sub: sub}; | |
30 } | |
31 | |
32 (function ExportedTableTest() { | |
33 print("ExportedTableTest..."); | |
34 | |
35 let builder = new WasmModuleBuilder(); | |
36 | |
37 let f = AddFunctions(builder); | |
38 builder.addFunction("main", kSig_i_ii) | |
39 .addBody([ | |
40 kExprI32Const, 33, // -- | |
41 kExprGetLocal, 0, // -- | |
42 kExprGetLocal, 1, // -- | |
43 kExprCallIndirect, 0]) // -- | |
44 .exportAs("main"); | |
45 | |
46 f.add.exportAs("add"); | |
47 | |
48 builder.setFunctionTableLength(10); | |
49 let g = builder.addImportedGlobal("base", undefined, kAstI32); | |
50 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index]) ; | |
bradnelson
2016/10/25 07:33:39
>80
titzer
2016/10/25 08:27:37
Done.
| |
51 builder.addExportOfKind("table", kExternalTable, 0); | |
52 | |
53 let module = new WebAssembly.Module(builder.toBuffer()); | |
bradnelson
2016/10/25 07:33:39
What about a case for imports into a different mod
titzer
2016/10/25 08:27:37
Add a case for adding an imported function to the
| |
54 | |
55 for (let i = 0; i < 5; i++) { | |
56 print(" base = " + i); | |
57 let instance = new WebAssembly.Instance(module, {base: i}); | |
58 main = instance.exports.main; | |
59 let table = instance.exports.table; | |
60 assertTrue(table instanceof WebAssembly.Table); | |
61 assertEquals(10, table.length); | |
62 for (let j = 0; j < i; j++) { | |
63 assertSame(null, table.get(j)); | |
64 } | |
65 let mul = table.get(i+0); | |
66 let add = table.get(i+1); | |
67 let sub = table.get(i+2); | |
68 | |
69 print(" mul=" + mul); | |
70 print(" add=" + add); | |
71 print(" sub=" + sub); | |
72 assertEquals("function", typeof mul); | |
73 assertEquals("function", typeof add); | |
74 assertEquals("function", typeof sub); | |
75 assertEquals(2, mul.length); | |
76 assertEquals(2, add.length); | |
77 assertEquals(2, sub.length); | |
78 | |
79 assertEquals("add", add.name); | |
bradnelson
2016/10/25 07:33:39
Do we maybe want to have export names that don't m
titzer
2016/10/25 08:27:37
Done.
| |
80 | |
81 for (let j = i + 3; j < 10; j++) { | |
82 assertSame(null, table.get(j)); | |
83 } | |
84 | |
85 assertEquals(-33, mul(-11, 3)); | |
86 assertEquals(4444444, add(3333333, 1111111)); | |
87 assertEquals(-9999, sub(1, 10000)); | |
88 } | |
89 })(); | |
OLD | NEW |