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 var debug = (((typeof arguments) != undefined) && arguments[0] == "debug"); | |
11 | |
12 (function BasicTest() { | |
13 var module = new WasmModuleBuilder(); | |
14 module.addMemory(1, 2, false); | |
15 module.addFunction("foo", [kAstI32]) | |
16 .addBody([kExprI8Const, 11]) | |
17 .exportAs("blarg"); | |
18 | |
19 var buffer = module.toBuffer(debug); | |
20 var instance = _WASMEXP_.instantiateModule(buffer); | |
21 assertEquals(11, instance.exports.blarg()); | |
22 })(); | |
23 | |
24 (function ImportTest() { | |
25 var module = new WasmModuleBuilder(); | |
26 var index = module.addImport("print", [kAstStmt, kAstI32]); | |
27 module.addFunction("foo", [kAstStmt]) | |
28 .addBody([kExprCallImport, index, kExprI8Const, 13]) | |
29 .exportAs("main"); | |
30 | |
31 var buffer = module.toBuffer(debug); | |
32 var instance = _WASMEXP_.instantiateModule(buffer, {print: print}); | |
33 print("should print 13! "); | |
34 instance.exports.main(); | |
35 })(); | |
36 | |
37 (function LocalsTest() { | |
38 var module = new WasmModuleBuilder(); | |
39 module.addFunction(undefined, [kAstI32, kAstI32]) | |
40 .addLocals({i32_count: 1}) | |
41 .addBody([kExprSetLocal, 1, kExprGetLocal, 0]) | |
42 .exportAs("main"); | |
43 | |
44 var buffer = module.toBuffer(debug); | |
45 var instance = _WASMEXP_.instantiateModule(buffer); | |
46 assertEquals(19, instance.exports.main(19)); | |
47 assertEquals(27777, instance.exports.main(27777)); | |
48 })(); | |
49 | |
50 (function CallTest() { | |
51 var module = new WasmModuleBuilder(); | |
52 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) | |
53 .addBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]); | |
54 module.addFunction("main", [kAstI32, kAstI32, kAstI32]) | |
55 .addBody([kExprCallFunction, 0, kExprGetLocal, 0, kExprGetLocal, 1]) | |
56 .exportAs("main"); | |
57 | |
58 var instance = module.instantiate(); | |
59 assertEquals(44, instance.exports.main(11, 33)); | |
60 assertEquals(7777, instance.exports.main(2222, 5555)); | |
61 })(); | |
62 | |
63 (function IndirectCallTest() { | |
64 var module = new WasmModuleBuilder(); | |
65 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) | |
66 .addBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]); | |
67 module.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) | |
68 .addBody([kExprCallIndirect, 0, kExprGetLocal, | |
69 0, kExprGetLocal, 1, kExprGetLocal, 2]) | |
70 .exportAs("main"); | |
71 module.appendToFunctionTable([0]); | |
72 | |
73 var instance = module.instantiate(); | |
74 assertEquals(44, instance.exports.main(0, 11, 33)); | |
75 assertEquals(7777, instance.exports.main(0, 2222, 5555)); | |
76 assertThrows(function() { instance.exports.main(1, 1, 1); }); | |
77 })(); | |
78 | |
79 (function DataSegmentTest() { | |
80 var module = new WasmModuleBuilder(); | |
81 module.addMemory(1, 1, false); | |
82 module.addFunction("load", [kAstI32, kAstI32]) | |
83 .addBody([kExprI32LoadMem, 0, kExprGetLocal, 0]) | |
84 .exportAs("load"); | |
85 module.addDataSegment(0, [9, 9, 9, 9], true); | |
86 | |
87 var buffer = module.toBuffer(debug); | |
88 var instance = _WASMEXP_.instantiateModule(buffer); | |
89 assertEquals(151587081, instance.exports.load(0)); | |
90 })(); | |
OLD | NEW |