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 let sig_index = builder.addType(kSig_i_ii); |
| 12 let mul = builder.addFunction("mul", sig_index) |
| 13 .addBody([ |
| 14 kExprGetLocal, 0, // -- |
| 15 kExprGetLocal, 1, // -- |
| 16 kExprI32Mul // -- |
| 17 ]); |
| 18 let add = builder.addFunction("add", sig_index) |
| 19 .addBody([ |
| 20 kExprGetLocal, 0, // -- |
| 21 kExprGetLocal, 1, // -- |
| 22 kExprI32Add // -- |
| 23 ]); |
| 24 let sub = builder.addFunction("sub", sig_index) |
| 25 .addBody([ |
| 26 kExprGetLocal, 0, // -- |
| 27 kExprGetLocal, 1, // -- |
| 28 kExprI32Sub // -- |
| 29 ]); |
| 30 return {mul: mul, add: add, sub: sub}; |
| 31 } |
| 32 |
| 33 (function ExportedTableTest() { |
| 34 print("ExportedTableTest..."); |
| 35 |
| 36 let builder = new WasmModuleBuilder(); |
| 37 |
| 38 let d = builder.addImport("js_div", kSig_i_ii); |
| 39 let f = AddFunctions(builder); |
| 40 builder.addFunction("main", kSig_i_ii) |
| 41 .addBody([ |
| 42 kExprI32Const, 33, // -- |
| 43 kExprGetLocal, 0, // -- |
| 44 kExprGetLocal, 1, // -- |
| 45 kExprCallIndirect, 0]) // -- |
| 46 .exportAs("main"); |
| 47 |
| 48 f.add.exportAs("blarg"); |
| 49 |
| 50 builder.setFunctionTableLength(10); |
| 51 let g = builder.addImportedGlobal("base", undefined, kAstI32); |
| 52 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, |
| 53 f.sub.index, |
| 54 d]); |
| 55 builder.addExportOfKind("table", kExternalTable, 0); |
| 56 |
| 57 let module = new WebAssembly.Module(builder.toBuffer()); |
| 58 |
| 59 function js_div(a, b) { return (a / b) | 0; } |
| 60 |
| 61 for (let i = 0; i < 5; i++) { |
| 62 print(" base = " + i); |
| 63 let instance = new WebAssembly.Instance(module, {base: i, js_div: js_div}); |
| 64 main = instance.exports.main; |
| 65 let table = instance.exports.table; |
| 66 assertTrue(table instanceof WebAssembly.Table); |
| 67 assertEquals(10, table.length); |
| 68 for (let j = 0; j < i; j++) { |
| 69 assertSame(null, table.get(j)); |
| 70 } |
| 71 let mul = table.get(i+0); |
| 72 let add = table.get(i+1); |
| 73 let sub = table.get(i+2); |
| 74 |
| 75 print(" mul=" + mul); |
| 76 print(" add=" + add); |
| 77 print(" sub=" + sub); |
| 78 assertEquals("function", typeof mul); |
| 79 assertEquals("function", typeof add); |
| 80 assertEquals("function", typeof sub); |
| 81 assertEquals(2, mul.length); |
| 82 assertEquals(2, add.length); |
| 83 assertEquals(2, sub.length); |
| 84 assertEquals("blarg", add.name); |
| 85 |
| 86 let exp_div = table.get(i+3); |
| 87 assertEquals("function", typeof exp_div); |
| 88 print(" js_div=" + exp_div); |
| 89 // Should have a new, wrapped version of the import. |
| 90 assertFalse(js_div == exp_div); |
| 91 |
| 92 |
| 93 for (let j = i + 4; j < 10; j++) { |
| 94 assertSame(null, table.get(j)); |
| 95 } |
| 96 |
| 97 assertEquals(-33, mul(-11, 3)); |
| 98 assertEquals(4444444, add(3333333, 1111111)); |
| 99 assertEquals(-9999, sub(1, 10000)); |
| 100 assertEquals(-44, exp_div(-88.1, 2)); |
| 101 } |
| 102 })(); |
OLD | NEW |