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) { | 12 function instantiate(buffer, ffi) { |
13 return new WebAssembly.Instance(WebAssembly.Module(buffer), ffi); | 13 return new WebAssembly.Instance(WebAssembly.Module(buffer), ffi); |
14 } | 14 } |
15 | 15 |
16 (function BasicTest() { | 16 (function BasicTest() { |
17 let builder = new WasmModuleBuilder(); | 17 let builder = new WasmModuleBuilder(); |
18 builder.addMemory(1, 2, false); | 18 builder.addMemory(1, 2, false); |
19 builder.addFunction("foo", kSig_i_v) | 19 builder.addFunction("foo", kSig_i_v) |
20 .addBody([kExprI8Const, 11]) | 20 .addBody([kExprI8Const, 11]) |
21 .exportAs("blarg"); | 21 .exportAs("blarg"); |
22 | 22 |
23 var buffer = builder.toBuffer(debug); | 23 var buffer = builder.toBuffer(debug); |
24 var instance = instantiate(buffer); | 24 var instance = instantiate(buffer); |
25 assertEquals(11, instance.exports.blarg()); | 25 assertEquals(11, instance.exports.blarg()); |
26 })(); | 26 })(); |
27 | 27 |
28 (function ImportTest() { | 28 (function ImportTest() { |
29 let builder = new WasmModuleBuilder(); | 29 let builder = new WasmModuleBuilder(); |
30 var index = builder.addImport("", "print", makeSig_v_x(kAstI32)); | 30 var index = builder.addImport("", "print", makeSig_v_x(kWasmI32)); |
31 builder.addFunction("foo", kSig_v_v) | 31 builder.addFunction("foo", kSig_v_v) |
32 .addBody([kExprI8Const, 13, kExprCallFunction, index]) | 32 .addBody([kExprI8Const, 13, kExprCallFunction, index]) |
33 .exportAs("main"); | 33 .exportAs("main"); |
34 | 34 |
35 var buffer = builder.toBuffer(debug); | 35 var buffer = builder.toBuffer(debug); |
36 var instance = instantiate(buffer, {"": {print: print}}); | 36 var instance = instantiate(buffer, {"": {print: print}}); |
37 print("should print 13! "); | 37 print("should print 13! "); |
38 instance.exports.main(); | 38 instance.exports.main(); |
39 })(); | 39 })(); |
40 | 40 |
41 (function LocalsTest() { | 41 (function LocalsTest() { |
42 let builder = new WasmModuleBuilder(); | 42 let builder = new WasmModuleBuilder(); |
43 builder.addFunction(undefined, kSig_i_i) | 43 builder.addFunction(undefined, kSig_i_i) |
44 .addLocals({i32_count: 1}) | 44 .addLocals({i32_count: 1}) |
45 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) | 45 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) |
46 .exportAs("main"); | 46 .exportAs("main"); |
47 | 47 |
48 var buffer = builder.toBuffer(debug); | 48 var buffer = builder.toBuffer(debug); |
49 var instance = instantiate(buffer); | 49 var instance = instantiate(buffer); |
50 assertEquals(19, instance.exports.main(19)); | 50 assertEquals(19, instance.exports.main(19)); |
51 assertEquals(27777, instance.exports.main(27777)); | 51 assertEquals(27777, instance.exports.main(27777)); |
52 })(); | 52 })(); |
53 | 53 |
54 (function LocalsTest2() { | 54 (function LocalsTest2() { |
55 // TODO(titzer): i64 only works on 64-bit platforms. | 55 // TODO(titzer): i64 only works on 64-bit platforms. |
56 var types = [ | 56 var types = [ |
57 {locals: {i32_count: 1}, type: kAstI32}, | 57 {locals: {i32_count: 1}, type: kWasmI32}, |
58 // {locals: {i64_count: 1}, type: kAstI64}, | 58 // {locals: {i64_count: 1}, type: kWasmI64}, |
59 {locals: {f32_count: 1}, type: kAstF32}, | 59 {locals: {f32_count: 1}, type: kWasmF32}, |
60 {locals: {f64_count: 1}, type: kAstF64}, | 60 {locals: {f64_count: 1}, type: kWasmF64}, |
61 ]; | 61 ]; |
62 | 62 |
63 for (p of types) { | 63 for (p of types) { |
64 let builder = new WasmModuleBuilder(); | 64 let builder = new WasmModuleBuilder(); |
65 builder.addFunction(undefined, makeSig_r_x(p.type, p.type)) | 65 builder.addFunction(undefined, makeSig_r_x(p.type, p.type)) |
66 .addLocals(p.locals) | 66 .addLocals(p.locals) |
67 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) | 67 .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) |
68 .exportAs("main"); | 68 .exportAs("main"); |
69 | 69 |
70 var buffer = builder.toBuffer(debug); | 70 var buffer = builder.toBuffer(debug); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); | 138 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); |
139 for (var i = 0; i < array2.byteLength; i++) { | 139 for (var i = 0; i < array2.byteLength; i++) { |
140 array2[i] = array[i]; | 140 array2[i] = array[i]; |
141 } | 141 } |
142 var instance = instantiate(array2); | 142 var instance = instantiate(array2); |
143 assertEquals(17, instance.exports.blarg()); | 143 assertEquals(17, instance.exports.blarg()); |
144 })(); | 144 })(); |
145 | 145 |
146 (function ImportTestTwoLevel() { | 146 (function ImportTestTwoLevel() { |
147 let builder = new WasmModuleBuilder(); | 147 let builder = new WasmModuleBuilder(); |
148 var index = builder.addImport("mod", "print", makeSig_v_x(kAstI32)); | 148 var index = builder.addImport("mod", "print", makeSig_v_x(kWasmI32)); |
149 builder.addFunction("foo", kSig_v_v) | 149 builder.addFunction("foo", kSig_v_v) |
150 .addBody([kExprI8Const, 19, kExprCallFunction, index]) | 150 .addBody([kExprI8Const, 19, kExprCallFunction, index]) |
151 .exportAs("main"); | 151 .exportAs("main"); |
152 | 152 |
153 var buffer = builder.toBuffer(debug); | 153 var buffer = builder.toBuffer(debug); |
154 var instance = instantiate(buffer, {mod: {print: print}}); | 154 var instance = instantiate(buffer, {mod: {print: print}}); |
155 print("should print 19! "); | 155 print("should print 19! "); |
156 instance.exports.main(); | 156 instance.exports.main(); |
157 })(); | 157 })(); |
OLD | NEW |