OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 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 function AddFunctions(builder) { | 10 function AddFunctions(builder) { |
(...skipping 19 matching lines...) Expand all Loading... |
30 return {mul: mul, add: add, sub: sub}; | 30 return {mul: mul, add: add, sub: sub}; |
31 } | 31 } |
32 | 32 |
33 function js_div(a, b) { return (a / b) | 0; } | 33 function js_div(a, b) { return (a / b) | 0; } |
34 | 34 |
35 (function ExportedTableTest() { | 35 (function ExportedTableTest() { |
36 print("ExportedTableTest..."); | 36 print("ExportedTableTest..."); |
37 | 37 |
38 let builder = new WasmModuleBuilder(); | 38 let builder = new WasmModuleBuilder(); |
39 | 39 |
40 let d = builder.addImport("js_div", kSig_i_ii); | 40 let d = builder.addImport("q", "js_div", kSig_i_ii); |
41 let f = AddFunctions(builder); | 41 let f = AddFunctions(builder); |
42 builder.addFunction("main", kSig_i_ii) | 42 builder.addFunction("main", kSig_i_ii) |
43 .addBody([ | 43 .addBody([ |
44 kExprI32Const, 33, // -- | 44 kExprI32Const, 33, // -- |
45 kExprGetLocal, 0, // -- | 45 kExprGetLocal, 0, // -- |
46 kExprGetLocal, 1, // -- | 46 kExprGetLocal, 1, // -- |
47 kExprCallIndirect, 0, kTableZero]) // -- | 47 kExprCallIndirect, 0, kTableZero]) // -- |
48 .exportAs("main"); | 48 .exportAs("main"); |
49 | 49 |
50 f.add.exportAs("blarg"); | 50 f.add.exportAs("blarg"); |
51 | 51 |
52 builder.setFunctionTableLength(10); | 52 builder.setFunctionTableLength(10); |
53 let g = builder.addImportedGlobal("base", undefined, kAstI32); | 53 let g = builder.addImportedGlobal("q", "base", kAstI32); |
54 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, | 54 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, |
55 f.sub.index, | 55 f.sub.index, |
56 d]); | 56 d]); |
57 builder.addExportOfKind("table", kExternalTable, 0); | 57 builder.addExportOfKind("table", kExternalTable, 0); |
58 | 58 |
59 let module = new WebAssembly.Module(builder.toBuffer()); | 59 let module = new WebAssembly.Module(builder.toBuffer()); |
60 | 60 |
61 for (let i = 0; i < 5; i++) { | 61 for (let i = 0; i < 5; i++) { |
62 print(" base = " + i); | 62 print(" base = " + i); |
63 let instance = new WebAssembly.Instance(module, {base: i, js_div: js_div}); | 63 let instance = new WebAssembly.Instance(module, {q: {base: i, js_div: js_div
}}); |
64 main = instance.exports.main; | 64 main = instance.exports.main; |
65 let table = instance.exports.table; | 65 let table = instance.exports.table; |
66 assertTrue(table instanceof WebAssembly.Table); | 66 assertTrue(table instanceof WebAssembly.Table); |
67 assertEquals(10, table.length); | 67 assertEquals(10, table.length); |
68 for (let j = 0; j < i; j++) { | 68 for (let j = 0; j < i; j++) { |
69 assertSame(null, table.get(j)); | 69 assertSame(null, table.get(j)); |
70 } | 70 } |
71 let mul = table.get(i+0); | 71 let mul = table.get(i+0); |
72 let add = table.get(i+1); | 72 let add = table.get(i+1); |
73 let sub = table.get(i+2); | 73 let sub = table.get(i+2); |
(...skipping 26 matching lines...) Expand all Loading... |
100 assertEquals(-44, exp_div(-88.1, 2)); | 100 assertEquals(-44, exp_div(-88.1, 2)); |
101 } | 101 } |
102 })(); | 102 })(); |
103 | 103 |
104 | 104 |
105 (function ImportedTableTest() { | 105 (function ImportedTableTest() { |
106 let kTableSize = 10; | 106 let kTableSize = 10; |
107 print("ImportedTableTest..."); | 107 print("ImportedTableTest..."); |
108 var builder = new WasmModuleBuilder(); | 108 var builder = new WasmModuleBuilder(); |
109 | 109 |
110 let d = builder.addImport("js_div", kSig_i_ii); | 110 let d = builder.addImport("q", "js_div", kSig_i_ii); |
111 let f = AddFunctions(builder); | 111 let f = AddFunctions(builder); |
112 builder.setFunctionTableLength(kTableSize); | 112 builder.setFunctionTableLength(kTableSize); |
113 let g = builder.addImportedGlobal("base", undefined, kAstI32); | 113 let g = builder.addImportedGlobal("q", "base", kAstI32); |
114 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, | 114 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, |
115 f.sub.index, | 115 f.sub.index, |
116 d]); | 116 d]); |
117 builder.addExportOfKind("table", kExternalTable, 0); | 117 builder.addExportOfKind("table", kExternalTable, 0); |
118 | 118 |
119 let m1 = new WebAssembly.Module(builder.toBuffer()); | 119 let m1 = new WebAssembly.Module(builder.toBuffer()); |
120 | 120 |
121 var builder = new WasmModuleBuilder(); | 121 var builder = new WasmModuleBuilder(); |
122 | 122 |
123 builder.addImportedTable("table", undefined, kTableSize, kTableSize); | 123 builder.addImportedTable("r", "table", kTableSize, kTableSize); |
124 builder.addFunction("main", kSig_i_ii) | 124 builder.addFunction("main", kSig_i_ii) |
125 .addBody([ | 125 .addBody([ |
126 kExprI32Const, 33, // -- | 126 kExprI32Const, 33, // -- |
127 kExprGetLocal, 0, // -- | 127 kExprGetLocal, 0, // -- |
128 kExprGetLocal, 1, // -- | 128 kExprGetLocal, 1, // -- |
129 kExprCallIndirect, 0, kTableZero]) // -- | 129 kExprCallIndirect, 0, kTableZero]) // -- |
130 .exportAs("main"); | 130 .exportAs("main"); |
131 | 131 |
132 let m2 = new WebAssembly.Module(builder.toBuffer()); | 132 let m2 = new WebAssembly.Module(builder.toBuffer()); |
133 | 133 |
134 // Run 5 trials at different table bases. | 134 // Run 5 trials at different table bases. |
135 for (let i = 0; i < 5; i++) { | 135 for (let i = 0; i < 5; i++) { |
136 print(" base = " + i); | 136 print(" base = " + i); |
137 let i1 = new WebAssembly.Instance(m1, {base: i, js_div: js_div}); | 137 let i1 = new WebAssembly.Instance(m1, {q: {base: i, js_div: js_div}}); |
138 let table = i1.exports.table; | 138 let table = i1.exports.table; |
139 assertEquals(10, table.length); | 139 assertEquals(10, table.length); |
140 let i2 = new WebAssembly.Instance(m2, {table: table}); | 140 let i2 = new WebAssembly.Instance(m2, {r: {table: table}}); |
141 let main = i2.exports.main; | 141 let main = i2.exports.main; |
142 | 142 |
143 for (var j = 0; j < i; j++) { | 143 for (var j = 0; j < i; j++) { |
144 assertThrows(() => main(0, j)); | 144 assertThrows(() => main(0, j)); |
145 assertSame(null, table.get(j)); | 145 assertSame(null, table.get(j)); |
146 } | 146 } |
147 | 147 |
148 // mul | 148 // mul |
149 assertEquals("function", typeof table.get(i+0)); | 149 assertEquals("function", typeof table.get(i+0)); |
150 assertEquals(0, main(0, i+0)); | 150 assertEquals(0, main(0, i+0)); |
(...skipping 20 matching lines...) Expand all Loading... |
171 } | 171 } |
172 } | 172 } |
173 })(); | 173 })(); |
174 | 174 |
175 (function ImportedTableTest() { | 175 (function ImportedTableTest() { |
176 let kTableSize = 10; | 176 let kTableSize = 10; |
177 print("ManualTableTest..."); | 177 print("ManualTableTest..."); |
178 | 178 |
179 var builder = new WasmModuleBuilder(); | 179 var builder = new WasmModuleBuilder(); |
180 | 180 |
181 let d = builder.addImport("js_div", kSig_i_ii); | 181 let d = builder.addImport("q", "js_div", kSig_i_ii); |
182 builder.addImportedTable("table", undefined, kTableSize, kTableSize); | 182 builder.addImportedTable("q", "table", kTableSize, kTableSize); |
183 let g = builder.addImportedGlobal("base", undefined, kAstI32); | 183 let g = builder.addImportedGlobal("q", "base", kAstI32); |
184 let f = AddFunctions(builder); | 184 let f = AddFunctions(builder); |
185 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, | 185 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, |
186 f.sub.index, | 186 f.sub.index, |
187 d]); | 187 d]); |
188 builder.addFunction("main", kSig_i_ii) | 188 builder.addFunction("main", kSig_i_ii) |
189 .addBody([ | 189 .addBody([ |
190 kExprI32Const, 55, // -- | 190 kExprI32Const, 55, // -- |
191 kExprGetLocal, 0, // -- | 191 kExprGetLocal, 0, // -- |
192 kExprGetLocal, 1, // -- | 192 kExprGetLocal, 1, // -- |
193 kExprCallIndirect, 0, kTableZero]) // -- | 193 kExprCallIndirect, 0, kTableZero]) // -- |
194 .exportAs("main"); | 194 .exportAs("main"); |
195 | 195 |
196 let m2 = new WebAssembly.Module(builder.toBuffer()); | 196 let m2 = new WebAssembly.Module(builder.toBuffer()); |
197 | 197 |
198 // Run 5 trials at different table bases. | 198 // Run 5 trials at different table bases. |
199 for (let i = 0; i < 5; i++) { | 199 for (let i = 0; i < 5; i++) { |
200 print(" base = " + i); | 200 print(" base = " + i); |
201 let table = new WebAssembly.Table({element: "anyfunc", | 201 let table = new WebAssembly.Table({element: "anyfunc", |
202 initial: kTableSize}); | 202 initial: kTableSize}); |
203 assertEquals(10, table.length); | 203 assertEquals(10, table.length); |
204 let i2 = new WebAssembly.Instance(m2, {base: i, table: table, | 204 let i2 = new WebAssembly.Instance(m2, {q: {base: i, table: table, |
205 js_div: js_div}); | 205 js_div: js_div}}); |
206 let main = i2.exports.main; | 206 let main = i2.exports.main; |
207 | 207 |
208 for (var j = 0; j < i; j++) { | 208 for (var j = 0; j < i; j++) { |
209 assertThrows(() => main(0, j)); | 209 assertThrows(() => main(0, j)); |
210 assertSame(null, table.get(j)); | 210 assertSame(null, table.get(j)); |
211 } | 211 } |
212 | 212 |
213 // mul | 213 // mul |
214 assertEquals("function", typeof table.get(i+0)); | 214 assertEquals("function", typeof table.get(i+0)); |
215 assertEquals(0, main(0, i+0)); | 215 assertEquals(0, main(0, i+0)); |
(...skipping 23 matching lines...) Expand all Loading... |
239 | 239 |
240 | 240 |
241 (function CumulativeTest() { | 241 (function CumulativeTest() { |
242 print("CumulativeTest..."); | 242 print("CumulativeTest..."); |
243 | 243 |
244 let kTableSize = 10; | 244 let kTableSize = 10; |
245 let table = new WebAssembly.Table({element: "anyfunc", initial: 10}); | 245 let table = new WebAssembly.Table({element: "anyfunc", initial: 10}); |
246 | 246 |
247 var builder = new WasmModuleBuilder(); | 247 var builder = new WasmModuleBuilder(); |
248 | 248 |
249 builder.addImportedTable("table", undefined, kTableSize, kTableSize); | 249 builder.addImportedTable("x", "table", kTableSize, kTableSize); |
250 let g = builder.addImportedGlobal("base", undefined, kAstI32); | 250 let g = builder.addImportedGlobal("x", "base", kAstI32); |
251 let sig_index = builder.addType(kSig_i_v); | 251 let sig_index = builder.addType(kSig_i_v); |
252 builder.addFunction("g", sig_index) | 252 builder.addFunction("g", sig_index) |
253 .addBody([ | 253 .addBody([ |
254 kExprGetGlobal, g | 254 kExprGetGlobal, g |
255 ]); | 255 ]); |
256 builder.addFunction("main", kSig_i_ii) | 256 builder.addFunction("main", kSig_i_ii) |
257 .addBody([ | 257 .addBody([ |
258 kExprGetLocal, 0, | 258 kExprGetLocal, 0, |
259 kExprCallIndirect, sig_index, kTableZero]) // -- | 259 kExprCallIndirect, sig_index, kTableZero]) // -- |
260 .exportAs("main"); | 260 .exportAs("main"); |
261 builder.addFunctionTableInit(g, true, [g]); | 261 builder.addFunctionTableInit(g, true, [g]); |
262 | 262 |
263 let module = new WebAssembly.Module(builder.toBuffer()); | 263 let module = new WebAssembly.Module(builder.toBuffer()); |
264 | 264 |
265 for (var i = 0; i < kTableSize; i++) { | 265 for (var i = 0; i < kTableSize; i++) { |
266 print(" base = " + i); | 266 print(" base = " + i); |
267 let instance = new WebAssembly.Instance(module, {base: i, table: table}); | 267 let instance = new WebAssembly.Instance(module, {x: {base: i, table: table}}
); |
268 | 268 |
269 for (var j = 0; j < kTableSize; j++) { | 269 for (var j = 0; j < kTableSize; j++) { |
270 let func = table.get(j); | 270 let func = table.get(j); |
271 if (j > i) { | 271 if (j > i) { |
272 assertSame(null, func); | 272 assertSame(null, func); |
273 assertTraps(kTrapFuncSigMismatch, () => instance.exports.main(j)); | 273 assertTraps(kTrapFuncSigMismatch, () => instance.exports.main(j)); |
274 } else { | 274 } else { |
275 assertEquals("function", typeof func); | 275 assertEquals("function", typeof func); |
276 assertEquals(j, func()); | 276 assertEquals(j, func()); |
277 assertEquals(j, instance.exports.main(j)); | 277 assertEquals(j, instance.exports.main(j)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 .addBody([kExprI32Const, 22]); | 312 .addBody([kExprI32Const, 22]); |
313 | 313 |
314 builder.addFunction("main", kSig_i_ii) | 314 builder.addFunction("main", kSig_i_ii) |
315 .addBody([ | 315 .addBody([ |
316 kExprGetLocal, 0, // -- | 316 kExprGetLocal, 0, // -- |
317 kExprCallIndirect, sig_index2, kTableZero]) // -- | 317 kExprCallIndirect, sig_index2, kTableZero]) // -- |
318 .exportAs("main"); | 318 .exportAs("main"); |
319 | 319 |
320 builder.setFunctionTableLength(kTableSize); | 320 builder.setFunctionTableLength(kTableSize); |
321 builder.addFunctionTableInit(1, false, [f2.index]); | 321 builder.addFunctionTableInit(1, false, [f2.index]); |
322 builder.addImportedTable("table", undefined, kTableSize, kTableSize); | 322 builder.addImportedTable("z", "table", kTableSize, kTableSize); |
323 | 323 |
324 var m2 = new WebAssembly.Module(builder.toBuffer()); | 324 var m2 = new WebAssembly.Module(builder.toBuffer()); |
325 | 325 |
326 assertFalse(sig_index1 == sig_index2); | 326 assertFalse(sig_index1 == sig_index2); |
327 | 327 |
328 var i1 = new WebAssembly.Instance(m1); | 328 var i1 = new WebAssembly.Instance(m1); |
329 var i2 = new WebAssembly.Instance(m2, {table: i1.exports.table}); | 329 var i2 = new WebAssembly.Instance(m2, {z: {table: i1.exports.table}}); |
330 | 330 |
331 assertEquals(11, i1.exports.main(0)); | 331 assertEquals(11, i1.exports.main(0)); |
332 assertEquals(11, i2.exports.main(0)); | 332 assertEquals(11, i2.exports.main(0)); |
333 | 333 |
334 assertEquals(22, i1.exports.main(1)); | 334 assertEquals(22, i1.exports.main(1)); |
335 assertEquals(22, i2.exports.main(1)); | 335 assertEquals(22, i2.exports.main(1)); |
336 | 336 |
337 assertThrows(() => i1.exports.main(2)); | 337 assertThrows(() => i1.exports.main(2)); |
338 assertThrows(() => i2.exports.main(2)); | 338 assertThrows(() => i2.exports.main(2)); |
339 assertThrows(() => i1.exports.main(3)); | 339 assertThrows(() => i1.exports.main(3)); |
340 assertThrows(() => i2.exports.main(3)); | 340 assertThrows(() => i2.exports.main(3)); |
341 | 341 |
342 })(); | 342 })(); |
343 | 343 |
344 (function MismatchedTableSize() { | 344 (function MismatchedTableSize() { |
345 print("MismatchedTableSize..."); | 345 print("MismatchedTableSize..."); |
346 let kTableSize = 5; | 346 let kTableSize = 5; |
347 | 347 |
348 for (var expsize = 1; expsize < 4; expsize++) { | 348 for (var expsize = 1; expsize < 4; expsize++) { |
349 for (var impsize = 1; impsize < 4; impsize++) { | 349 for (var impsize = 1; impsize < 4; impsize++) { |
350 print(" expsize = " + expsize + ", impsize = " + impsize); | 350 print(" expsize = " + expsize + ", impsize = " + impsize); |
351 var builder = new WasmModuleBuilder(); | 351 var builder = new WasmModuleBuilder(); |
352 builder.setFunctionTableLength(expsize); | 352 builder.setFunctionTableLength(expsize); |
353 builder.addExportOfKind("expfoo", kExternalTable, 0); | 353 builder.addExportOfKind("expfoo", kExternalTable, 0); |
354 | 354 |
355 let m1 = new WebAssembly.Module(builder.toBuffer()); | 355 let m1 = new WebAssembly.Module(builder.toBuffer()); |
356 | 356 |
357 var builder = new WasmModuleBuilder(); | 357 var builder = new WasmModuleBuilder(); |
358 builder.addImportedTable("impfoo", undefined, impsize, impsize); | 358 builder.addImportedTable("y", "impfoo", impsize, impsize); |
359 | 359 |
360 let m2 = new WebAssembly.Module(builder.toBuffer()); | 360 let m2 = new WebAssembly.Module(builder.toBuffer()); |
361 | 361 |
362 var i1 = new WebAssembly.Instance(m1); | 362 var i1 = new WebAssembly.Instance(m1); |
363 | 363 |
364 // TODO(titzer): v8 currently requires import table size to match | 364 // TODO(titzer): v8 currently requires import table size to match |
365 // export table size. | 365 // export table size. |
366 var ffi = {impfoo: i1.exports.expfoo}; | 366 var ffi = {y: {impfoo: i1.exports.expfoo}}; |
367 if (expsize == impsize) { | 367 if (expsize == impsize) { |
368 var i2 = new WebAssembly.Instance(m2, ffi); | 368 var i2 = new WebAssembly.Instance(m2, ffi); |
369 } else { | 369 } else { |
370 assertThrows(() => new WebAssembly.Instance(m2, ffi)); | 370 assertThrows(() => new WebAssembly.Instance(m2, ffi)); |
371 } | 371 } |
372 } | 372 } |
373 } | 373 } |
374 | 374 |
375 | 375 |
376 | 376 |
377 })(); | 377 })(); |
OLD | NEW |