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 = _WASMEXP_.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([kExprCallImport, index, kExprI8Const, 13]) |
29 .exportAs("main"); | 29 .exportAs("main"); |
30 | 30 |
31 var buffer = module.toBuffer(debug); | 31 var buffer = module.toBuffer(debug); |
32 var instance = _WASMEXP_.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([kExprSetLocal, 1, kExprGetLocal, 0]) |
42 .exportAs("main"); | 42 .exportAs("main"); |
43 | 43 |
44 var buffer = module.toBuffer(debug); | 44 var buffer = module.toBuffer(debug); |
45 var instance = _WASMEXP_.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([kExprSetLocal, 1, kExprGetLocal, 0]) |
64 .exportAs("main"); | 64 .exportAs("main"); |
65 | 65 |
66 var buffer = module.toBuffer(debug); | 66 var buffer = module.toBuffer(debug); |
67 var instance = _WASMEXP_.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([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]); |
77 module.addFunction("main", [kAstI32, kAstI32, kAstI32]) | 77 module.addFunction("main", [kAstI32, kAstI32, kAstI32]) |
(...skipping 23 matching lines...) Expand all Loading... |
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, kExprGetLocal, 0]) | 106 .addBody([kExprI32LoadMem, 0, kExprGetLocal, 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 = _WASMEXP_.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() { |
117 var module = new WasmModuleBuilder(); | 117 var module = new WasmModuleBuilder(); |
118 module.addMemory(1, 2, false); | 118 module.addMemory(1, 2, false); |
119 module.addFunction("foo", [kAstI32]) | 119 module.addFunction("foo", [kAstI32]) |
120 .addBody([kExprI8Const, 17]) | 120 .addBody([kExprI8Const, 17]) |
121 .exportAs("blarg"); | 121 .exportAs("blarg"); |
122 | 122 |
123 var buffer = module.toBuffer(debug); | 123 var buffer = module.toBuffer(debug); |
124 var array = new Uint8Array(buffer); | 124 var array = new Uint8Array(buffer); |
125 var instance = _WASMEXP_.instantiateModule(array); | 125 var instance = Wasm.instantiateModule(array); |
126 assertEquals(17, instance.exports.blarg()); | 126 assertEquals(17, instance.exports.blarg()); |
127 | 127 |
128 var kPad = 5; | 128 var kPad = 5; |
129 var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad); | 129 var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad); |
130 var whole = new Uint8Array(buffer2); | 130 var whole = new Uint8Array(buffer2); |
131 for (var i = 0; i < whole.byteLength; i++) { | 131 for (var i = 0; i < whole.byteLength; i++) { |
132 whole[i] = 0xff; | 132 whole[i] = 0xff; |
133 } | 133 } |
134 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); | 134 var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength); |
135 for (var i = 0; i < array2.byteLength; i++) { | 135 for (var i = 0; i < array2.byteLength; i++) { |
136 array2[i] = array[i]; | 136 array2[i] = array[i]; |
137 } | 137 } |
138 var instance = _WASMEXP_.instantiateModule(array2); | 138 var instance = Wasm.instantiateModule(array2); |
139 assertEquals(17, instance.exports.blarg()); | 139 assertEquals(17, instance.exports.blarg()); |
140 })(); | 140 })(); |
OLD | NEW |