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 var debug = false; | 10 var debug = false; |
11 | 11 |
12 (function BasicTest() { | 12 (function BasicTest() { |
13 var module = new WasmModuleBuilder(); | 13 var module = new WasmModuleBuilder(); |
14 module.addMemory(1, 2, false); | 14 module.addMemory(1, 2, false); |
15 module.addFunction("foo", [kAstI32]) | 15 module.addFunction("foo", [kAstI32]) |
16 .addBody([kExprI8Const, 11]) | 16 .addBody([kExprI8Const, 11]) |
17 .exportAs("blarg"); | 17 .exportAs("blarg"); |
18 | 18 |
19 var buffer = module.toBuffer(debug); | 19 var buffer = module.toBuffer(debug); |
20 var instance = Wasm.instantiateModule(buffer); | 20 var instance = Wasm.instantiateModule(buffer); |
21 assertEquals(11, instance.exports.blarg()); | 21 assertEquals(11, instance.exports.blarg()); |
22 })(); | 22 })(); |
23 | 23 |
24 (function ImportTest() { | 24 (function ImportTest() { |
25 var module = new WasmModuleBuilder(); | 25 var module = new WasmModuleBuilder(); |
26 var index = module.addImport("print", [kAstStmt, kAstI32]); | 26 var index = module.addImport("print", [kAstStmt, kAstI32]); |
27 module.addFunction("foo", [kAstStmt]) | 27 module.addFunction("foo", [kAstStmt]) |
28 .addBody([kExprCallImport, index, kExprI8Const, 13]) | 28 .addBody([kExprI8Const, 13, kExprCallImport, kArity1, index]) |
29 .exportAs("main"); | 29 .exportAs("main"); |
30 | 30 |
31 var buffer = module.toBuffer(debug); | 31 var buffer = module.toBuffer(debug); |
32 var instance = Wasm.instantiateModule(buffer, {print: print}); | 32 var instance = Wasm.instantiateModule(buffer, {print: print}); |
33 print("should print 13! "); | 33 print("should print 13! "); |
34 instance.exports.main(); | 34 instance.exports.main(); |
35 })(); | 35 })(); |
36 | 36 |
37 (function LocalsTest() { | 37 (function LocalsTest() { |
38 var module = new WasmModuleBuilder(); | 38 var module = new WasmModuleBuilder(); |
39 module.addFunction(undefined, [kAstI32, kAstI32]) | 39 module.addFunction(undefined, [kAstI32, kAstI32]) |
40 .addLocals({i32_count: 1}) | 40 .addLocals({i32_count: 1}) |
41 .addBody([kExprSetLocal, 1, kExprGetLocal, 0]) | 41 .addBody([kExprGetLocal, 0, kExprSetLocal, 1]) |
42 .exportAs("main"); | 42 .exportAs("main"); |
43 | 43 |
44 var buffer = module.toBuffer(debug); | 44 var buffer = module.toBuffer(debug); |
45 var instance = Wasm.instantiateModule(buffer); | 45 var instance = Wasm.instantiateModule(buffer); |
46 assertEquals(19, instance.exports.main(19)); | 46 assertEquals(19, instance.exports.main(19)); |
47 assertEquals(27777, instance.exports.main(27777)); | 47 assertEquals(27777, instance.exports.main(27777)); |
48 })(); | 48 })(); |
49 | 49 |
50 (function LocalsTest2() { | 50 (function LocalsTest2() { |
51 // TODO(titzer): i64 only works on 64-bit platforms. | 51 // TODO(titzer): i64 only works on 64-bit platforms. |
52 var types = [ | 52 var types = [ |
53 {locals: {i32_count: 1}, type: kAstI32}, | 53 {locals: {i32_count: 1}, type: kAstI32}, |
54 // {locals: {i64_count: 1}, type: kAstI64}, | 54 // {locals: {i64_count: 1}, type: kAstI64}, |
55 {locals: {f32_count: 1}, type: kAstF32}, | 55 {locals: {f32_count: 1}, type: kAstF32}, |
56 {locals: {f64_count: 1}, type: kAstF64}, | 56 {locals: {f64_count: 1}, type: kAstF64}, |
57 ]; | 57 ]; |
58 | 58 |
59 for (p of types) { | 59 for (p of types) { |
60 var module = new WasmModuleBuilder(); | 60 var module = new WasmModuleBuilder(); |
61 module.addFunction(undefined, [p.type, p.type]) | 61 module.addFunction(undefined, [p.type, p.type]) |
62 .addLocals(p.locals) | 62 .addLocals(p.locals) |
63 .addBody([kExprSetLocal, 1, kExprGetLocal, 0]) | 63 .addBody([kExprGetLocal, 0, kExprSetLocal, 1]) |
64 .exportAs("main"); | 64 .exportAs("main"); |
65 | 65 |
66 var buffer = module.toBuffer(debug); | 66 var buffer = module.toBuffer(debug); |
67 var instance = Wasm.instantiateModule(buffer); | 67 var instance = Wasm.instantiateModule(buffer); |
68 assertEquals(19, instance.exports.main(19)); | 68 assertEquals(19, instance.exports.main(19)); |
69 assertEquals(27777, instance.exports.main(27777)); | 69 assertEquals(27777, instance.exports.main(27777)); |
70 } | 70 } |
71 })(); | 71 })(); |
72 | 72 |
73 (function CallTest() { | 73 (function CallTest() { |
74 var module = new WasmModuleBuilder(); | 74 var module = new WasmModuleBuilder(); |
75 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) | 75 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) |
76 .addBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]); | 76 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); |
77 module.addFunction("main", [kAstI32, kAstI32, kAstI32]) | 77 module.addFunction("main", [kAstI32, kAstI32, kAstI32]) |
78 .addBody([kExprCallFunction, 0, kExprGetLocal, 0, kExprGetLocal, 1]) | 78 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, kArity2
, 0]) |
79 .exportAs("main"); | 79 .exportAs("main"); |
80 | 80 |
81 var instance = module.instantiate(); | 81 var instance = module.instantiate(); |
82 assertEquals(44, instance.exports.main(11, 33)); | 82 assertEquals(44, instance.exports.main(11, 33)); |
83 assertEquals(7777, instance.exports.main(2222, 5555)); | 83 assertEquals(7777, instance.exports.main(2222, 5555)); |
84 })(); | 84 })(); |
85 | 85 |
86 (function IndirectCallTest() { | 86 (function IndirectCallTest() { |
87 var module = new WasmModuleBuilder(); | 87 var module = new WasmModuleBuilder(); |
88 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) | 88 module.addFunction("add", [kAstI32, kAstI32, kAstI32]) |
89 .addBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]); | 89 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); |
90 module.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) | 90 module.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) |
91 .addBody([kExprCallIndirect, 0, kExprGetLocal, | 91 .addBody([kExprGetLocal, |
92 0, kExprGetLocal, 1, kExprGetLocal, 2]) | 92 0, kExprGetLocal, 1, kExprGetLocal, 2, kExprCallIndirect, kAri
ty2, 0]) |
93 .exportAs("main"); | 93 .exportAs("main"); |
94 module.appendToFunctionTable([0]); | 94 module.appendToFunctionTable([0]); |
95 | 95 |
96 var instance = module.instantiate(); | 96 var instance = module.instantiate(); |
97 assertEquals(44, instance.exports.main(0, 11, 33)); | 97 assertEquals(44, instance.exports.main(0, 11, 33)); |
98 assertEquals(7777, instance.exports.main(0, 2222, 5555)); | 98 assertEquals(7777, instance.exports.main(0, 2222, 5555)); |
99 assertThrows(function() { instance.exports.main(1, 1, 1); }); | 99 assertThrows(function() { instance.exports.main(1, 1, 1); }); |
100 })(); | 100 })(); |
101 | 101 |
102 (function DataSegmentTest() { | 102 (function DataSegmentTest() { |
103 var module = new WasmModuleBuilder(); | 103 var module = new WasmModuleBuilder(); |
104 module.addMemory(1, 1, false); | 104 module.addMemory(1, 1, false); |
105 module.addFunction("load", [kAstI32, kAstI32]) | 105 module.addFunction("load", [kAstI32, kAstI32]) |
106 .addBody([kExprI32LoadMem, 0, 0, kExprGetLocal, 0]) | 106 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
107 .exportAs("load"); | 107 .exportAs("load"); |
108 module.addDataSegment(0, [9, 9, 9, 9], true); | 108 module.addDataSegment(0, [9, 9, 9, 9], true); |
109 | 109 |
110 var buffer = module.toBuffer(debug); | 110 var buffer = module.toBuffer(debug); |
111 var instance = Wasm.instantiateModule(buffer); | 111 var instance = Wasm.instantiateModule(buffer); |
112 assertEquals(151587081, instance.exports.load(0)); | 112 assertEquals(151587081, instance.exports.load(0)); |
113 })(); | 113 })(); |
114 | 114 |
115 | 115 |
116 (function BasicTestWithUint8Array() { | 116 (function BasicTestWithUint8Array() { |
(...skipping 19 matching lines...) Expand all Loading... |
136 array2[i] = array[i]; | 136 array2[i] = array[i]; |
137 } | 137 } |
138 var instance = Wasm.instantiateModule(array2); | 138 var instance = Wasm.instantiateModule(array2); |
139 assertEquals(17, instance.exports.blarg()); | 139 assertEquals(17, instance.exports.blarg()); |
140 })(); | 140 })(); |
141 | 141 |
142 (function ImportTestTwoLevel() { | 142 (function ImportTestTwoLevel() { |
143 var module = new WasmModuleBuilder(); | 143 var module = new WasmModuleBuilder(); |
144 var index = module.addImportWithModule("mod", "print", [kAstStmt, kAstI32]); | 144 var index = module.addImportWithModule("mod", "print", [kAstStmt, kAstI32]); |
145 module.addFunction("foo", [kAstStmt]) | 145 module.addFunction("foo", [kAstStmt]) |
146 .addBody([kExprCallImport, index, kExprI8Const, 19]) | 146 .addBody([kExprI8Const, 19, kExprCallImport, kArity1, index]) |
147 .exportAs("main"); | 147 .exportAs("main"); |
148 | 148 |
149 var buffer = module.toBuffer(debug); | 149 var buffer = module.toBuffer(debug); |
150 var instance = Wasm.instantiateModule(buffer, {mod: {print: print}}); | 150 var instance = Wasm.instantiateModule(buffer, {mod: {print: print}}); |
151 print("should print 19! "); | 151 print("should print 19! "); |
152 instance.exports.main(); | 152 instance.exports.main(); |
153 })(); | 153 })(); |
OLD | NEW |