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 = true; | 10 var debug = true; |
11 | 11 |
| 12 function instantiate(buffer, ffi) { |
| 13 return new WebAssembly.Instance(WebAssembly.Module(buffer), ffi); |
| 14 } |
| 15 |
12 (function BasicTest() { | 16 (function BasicTest() { |
13 var module = new WasmModuleBuilder(); | 17 let builder = new WasmModuleBuilder(); |
14 module.addMemory(1, 2, false); | 18 builder.addMemory(1, 2, false); |
15 module.addFunction("foo", kSig_i_v) | 19 builder.addFunction("foo", kSig_i_v) |
16 .addBody([kExprI8Const, 11]) | 20 .addBody([kExprI8Const, 11]) |
17 .exportAs("blarg"); | 21 .exportAs("blarg"); |
18 | 22 |
19 var buffer = module.toBuffer(debug); | 23 var buffer = builder.toBuffer(debug); |
20 var instance = Wasm.instantiateModule(buffer); | 24 var instance = instantiate(buffer); |
21 assertEquals(11, instance.exports.blarg()); | 25 assertEquals(11, instance.exports.blarg()); |
22 })(); | 26 })(); |
23 | 27 |
24 (function ImportTest() { | 28 (function ImportTest() { |
25 var module = new WasmModuleBuilder(); | 29 let builder = new WasmModuleBuilder(); |
26 var index = module.addImport("print", makeSig_v_x(kAstI32)); | 30 var index = builder.addImport("print", makeSig_v_x(kAstI32)); |
27 module.addFunction("foo", kSig_v_v) | 31 builder.addFunction("foo", kSig_v_v) |
28 .addBody([kExprI8Const, 13, kExprCallFunction, index]) | 32 .addBody([kExprI8Const, 13, kExprCallFunction, index]) |
29 .exportAs("main"); | 33 .exportAs("main"); |
30 | 34 |
31 var buffer = module.toBuffer(debug); | 35 var buffer = builder.toBuffer(debug); |
32 var instance = Wasm.instantiateModule(buffer, {print: print}); | 36 var instance = instantiate(buffer, {print: print}); |
33 print("should print 13! "); | 37 print("should print 13! "); |
34 instance.exports.main(); | 38 instance.exports.main(); |
35 })(); | 39 })(); |
36 | 40 |
37 (function LocalsTest() { | 41 (function LocalsTest() { |
38 var module = new WasmModuleBuilder(); | 42 let builder = new WasmModuleBuilder(); |
39 module.addFunction(undefined, kSig_i_i) | 43 builder.addFunction(undefined, kSig_i_i) |
40 .addLocals({i32_count: 1}) | 44 .addLocals({i32_count: 1}) |
41 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) | 45 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) |
42 .exportAs("main"); | 46 .exportAs("main"); |
43 | 47 |
44 var buffer = module.toBuffer(debug); | 48 var buffer = builder.toBuffer(debug); |
45 var instance = Wasm.instantiateModule(buffer); | 49 var instance = instantiate(buffer); |
46 assertEquals(19, instance.exports.main(19)); | 50 assertEquals(19, instance.exports.main(19)); |
47 assertEquals(27777, instance.exports.main(27777)); | 51 assertEquals(27777, instance.exports.main(27777)); |
48 })(); | 52 })(); |
49 | 53 |
50 (function LocalsTest2() { | 54 (function LocalsTest2() { |
51 // TODO(titzer): i64 only works on 64-bit platforms. | 55 // TODO(titzer): i64 only works on 64-bit platforms. |
52 var types = [ | 56 var types = [ |
53 {locals: {i32_count: 1}, type: kAstI32}, | 57 {locals: {i32_count: 1}, type: kAstI32}, |
54 // {locals: {i64_count: 1}, type: kAstI64}, | 58 // {locals: {i64_count: 1}, type: kAstI64}, |
55 {locals: {f32_count: 1}, type: kAstF32}, | 59 {locals: {f32_count: 1}, type: kAstF32}, |
56 {locals: {f64_count: 1}, type: kAstF64}, | 60 {locals: {f64_count: 1}, type: kAstF64}, |
57 ]; | 61 ]; |
58 | 62 |
59 for (p of types) { | 63 for (p of types) { |
60 var module = new WasmModuleBuilder(); | 64 let builder = new WasmModuleBuilder(); |
61 module.addFunction(undefined, makeSig_r_x(p.type, p.type)) | 65 builder.addFunction(undefined, makeSig_r_x(p.type, p.type)) |
62 .addLocals(p.locals) | 66 .addLocals(p.locals) |
63 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) | 67 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) |
64 .exportAs("main"); | 68 .exportAs("main"); |
65 | 69 |
66 var buffer = module.toBuffer(debug); | 70 var buffer = builder.toBuffer(debug); |
67 var instance = Wasm.instantiateModule(buffer); | 71 var instance = instantiate(buffer); |
68 assertEquals(19, instance.exports.main(19)); | 72 assertEquals(19, instance.exports.main(19)); |
69 assertEquals(27777, instance.exports.main(27777)); | 73 assertEquals(27777, instance.exports.main(27777)); |
70 } | 74 } |
71 })(); | 75 })(); |
72 | 76 |
73 (function CallTest() { | 77 (function CallTest() { |
74 var module = new WasmModuleBuilder(); | 78 let builder = new WasmModuleBuilder(); |
75 module.addFunction("add", kSig_i_ii) | 79 builder.addFunction("add", kSig_i_ii) |
76 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); | 80 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); |
77 module.addFunction("main", kSig_i_ii) | 81 builder.addFunction("main", kSig_i_ii) |
78 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0]) | 82 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0]) |
79 .exportAs("main"); | 83 .exportAs("main"); |
80 | 84 |
81 var instance = module.instantiate(); | 85 var instance = builder.instantiate(); |
82 assertEquals(44, instance.exports.main(11, 33)); | 86 assertEquals(44, instance.exports.main(11, 33)); |
83 assertEquals(7777, instance.exports.main(2222, 5555)); | 87 assertEquals(7777, instance.exports.main(2222, 5555)); |
84 })(); | 88 })(); |
85 | 89 |
86 (function IndirectCallTest() { | 90 (function IndirectCallTest() { |
87 var module = new WasmModuleBuilder(); | 91 let builder = new WasmModuleBuilder(); |
88 module.addFunction("add", kSig_i_ii) | 92 builder.addFunction("add", kSig_i_ii) |
89 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); | 93 .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); |
90 module.addFunction("main", kSig_i_iii) | 94 builder.addFunction("main", kSig_i_iii) |
91 .addBody([kExprGetLocal, | 95 .addBody([kExprGetLocal, |
92 1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0, k
TableZero]) | 96 1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0, k
TableZero]) |
93 .exportAs("main"); | 97 .exportAs("main"); |
94 module.appendToTable([0]); | 98 builder.appendToTable([0]); |
95 | 99 |
96 var instance = module.instantiate(); | 100 var instance = builder.instantiate(); |
97 assertEquals(44, instance.exports.main(0, 11, 33)); | 101 assertEquals(44, instance.exports.main(0, 11, 33)); |
98 assertEquals(7777, instance.exports.main(0, 2222, 5555)); | 102 assertEquals(7777, instance.exports.main(0, 2222, 5555)); |
99 assertThrows(function() { instance.exports.main(1, 1, 1); }); | 103 assertThrows(function() { instance.exports.main(1, 1, 1); }); |
100 })(); | 104 })(); |
101 | 105 |
102 (function DataSegmentTest() { | 106 (function DataSegmentTest() { |
103 var module = new WasmModuleBuilder(); | 107 let builder = new WasmModuleBuilder(); |
104 module.addMemory(1, 1, false); | 108 builder.addMemory(1, 1, false); |
105 module.addFunction("load", kSig_i_i) | 109 builder.addFunction("load", kSig_i_i) |
106 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) | 110 .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) |
107 .exportAs("load"); | 111 .exportAs("load"); |
108 module.addDataSegment(0, [9, 9, 9, 9]); | 112 builder.addDataSegment(0, [9, 9, 9, 9]); |
109 | 113 |
110 var buffer = module.toBuffer(debug); | 114 var buffer = builder.toBuffer(debug); |
111 var instance = Wasm.instantiateModule(buffer); | 115 var instance = instantiate(buffer); |
112 assertEquals(151587081, instance.exports.load(0)); | 116 assertEquals(151587081, instance.exports.load(0)); |
113 })(); | 117 })(); |
114 | 118 |
115 | 119 |
116 (function BasicTestWithUint8Array() { | 120 (function BasicTestWithUint8Array() { |
117 var module = new WasmModuleBuilder(); | 121 let builder = new WasmModuleBuilder(); |
118 module.addMemory(1, 2, false); | 122 builder.addMemory(1, 2, false); |
119 module.addFunction("foo", kSig_i_v) | 123 builder.addFunction("foo", kSig_i_v) |
120 .addBody([kExprI8Const, 17]) | 124 .addBody([kExprI8Const, 17]) |
121 .exportAs("blarg"); | 125 .exportAs("blarg"); |
122 | 126 |
123 var buffer = module.toBuffer(debug); | 127 var buffer = builder.toBuffer(debug); |
124 var array = new Uint8Array(buffer); | 128 var array = new Uint8Array(buffer); |
125 var instance = Wasm.instantiateModule(array); | 129 var instance = instantiate(array); |
126 assertEquals(17, instance.exports.blarg()); | 130 assertEquals(17, instance.exports.blarg()); |
127 | 131 |
128 var kPad = 5; | 132 var kPad = 5; |
129 var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad); | 133 var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad); |
130 var whole = new Uint8Array(buffer2); | 134 var whole = new Uint8Array(buffer2); |
131 for (var i = 0; i < whole.byteLength; i++) { | 135 for (var i = 0; i < whole.byteLength; i++) { |
132 whole[i] = 0xff; | 136 whole[i] = 0xff; |
133 } | 137 } |
134 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); | 138 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); |
135 for (var i = 0; i < array2.byteLength; i++) { | 139 for (var i = 0; i < array2.byteLength; i++) { |
136 array2[i] = array[i]; | 140 array2[i] = array[i]; |
137 } | 141 } |
138 var instance = Wasm.instantiateModule(array2); | 142 var instance = instantiate(array2); |
139 assertEquals(17, instance.exports.blarg()); | 143 assertEquals(17, instance.exports.blarg()); |
140 })(); | 144 })(); |
141 | 145 |
142 (function ImportTestTwoLevel() { | 146 (function ImportTestTwoLevel() { |
143 var module = new WasmModuleBuilder(); | 147 let builder = new WasmModuleBuilder(); |
144 var index = module.addImportWithModule("mod", "print", makeSig_v_x(kAstI32))
; | 148 var index = builder.addImportWithModule("mod", "print", makeSig_v_x(kAstI32)
); |
145 module.addFunction("foo", kSig_v_v) | 149 builder.addFunction("foo", kSig_v_v) |
146 .addBody([kExprI8Const, 19, kExprCallFunction, index]) | 150 .addBody([kExprI8Const, 19, kExprCallFunction, index]) |
147 .exportAs("main"); | 151 .exportAs("main"); |
148 | 152 |
149 var buffer = module.toBuffer(debug); | 153 var buffer = builder.toBuffer(debug); |
150 var instance = Wasm.instantiateModule(buffer, {mod: {print: print}}); | 154 var instance = instantiate(buffer, {mod: {print: print}}); |
151 print("should print 19! "); | 155 print("should print 19! "); |
152 instance.exports.main(); | 156 instance.exports.main(); |
153 })(); | 157 })(); |
OLD | NEW |