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 function TestImported(type, val, expected) { | 10 function TestImported(type, val, expected) { |
11 print("TestImported " + type + "(" + val +")" + " = " + expected); | 11 print("TestImported " + type + "(" + val +")" + " = " + expected); |
12 var builder = new WasmModuleBuilder(); | 12 var builder = new WasmModuleBuilder(); |
13 var sig = makeSig([], [type]); | 13 var sig = makeSig([], [type]); |
14 var g = builder.addImportedGlobal("uuu", "foo", type); | 14 var g = builder.addImportedGlobal("uuu", "foo", type); |
15 builder.addFunction("main", sig) | 15 builder.addFunction("main", sig) |
16 .addBody([kExprGetGlobal, g.index]) | 16 .addBody([kExprGetGlobal, g.index]) |
17 .exportAs("main"); | 17 .exportAs("main"); |
18 builder.addGlobal(kAstI32); // pad | 18 builder.addGlobal(kWasmI32); // pad |
19 | 19 |
20 var instance = builder.instantiate({uuu: {foo: val}}); | 20 var instance = builder.instantiate({uuu: {foo: val}}); |
21 assertEquals(expected, instance.exports.main()); | 21 assertEquals(expected, instance.exports.main()); |
22 } | 22 } |
23 | 23 |
24 TestImported(kAstI32, 300.1, 300); | 24 TestImported(kWasmI32, 300.1, 300); |
25 TestImported(kAstF32, 87234.87238, Math.fround(87234.87238)); | 25 TestImported(kWasmF32, 87234.87238, Math.fround(87234.87238)); |
26 TestImported(kAstF64, 77777.88888, 77777.88888); | 26 TestImported(kWasmF64, 77777.88888, 77777.88888); |
27 | 27 |
28 | 28 |
29 function TestExported(type, val, expected) { | 29 function TestExported(type, val, expected) { |
30 print("TestExported " + type + "(" + val +")" + " = " + expected); | 30 print("TestExported " + type + "(" + val +")" + " = " + expected); |
31 var builder = new WasmModuleBuilder(); | 31 var builder = new WasmModuleBuilder(); |
32 var sig = makeSig([type], []); | 32 var sig = makeSig([type], []); |
33 builder.addGlobal(kAstI32); // pad | 33 builder.addGlobal(kWasmI32); // pad |
34 var g = builder.addGlobal(type, false) | 34 var g = builder.addGlobal(type, false) |
35 .exportAs("foo"); | 35 .exportAs("foo"); |
36 g.init = val; | 36 g.init = val; |
37 builder.addGlobal(kAstI32); // pad | 37 builder.addGlobal(kWasmI32); // pad |
38 | 38 |
39 var instance = builder.instantiate(); | 39 var instance = builder.instantiate(); |
40 assertEquals(expected, instance.exports.foo); | 40 assertEquals(expected, instance.exports.foo); |
41 } | 41 } |
42 | 42 |
43 TestExported(kAstI32, 455.5, 455); | 43 TestExported(kWasmI32, 455.5, 455); |
44 TestExported(kAstF32, -999.34343, Math.fround(-999.34343)); | 44 TestExported(kWasmF32, -999.34343, Math.fround(-999.34343)); |
45 TestExported(kAstF64, 87347.66666, 87347.66666); | 45 TestExported(kWasmF64, 87347.66666, 87347.66666); |
46 | 46 |
47 | 47 |
48 function TestImportedExported(type, val, expected) { | 48 function TestImportedExported(type, val, expected) { |
49 print("TestImportedExported " + type + "(" + val +")" + " = " + expected); | 49 print("TestImportedExported " + type + "(" + val +")" + " = " + expected); |
50 var builder = new WasmModuleBuilder(); | 50 var builder = new WasmModuleBuilder(); |
51 var sig = makeSig([type], []); | 51 var sig = makeSig([type], []); |
52 var i = builder.addImportedGlobal("ttt", "foo", type); | 52 var i = builder.addImportedGlobal("ttt", "foo", type); |
53 builder.addGlobal(kAstI32); // pad | 53 builder.addGlobal(kWasmI32); // pad |
54 var o = builder.addGlobal(type, false) | 54 var o = builder.addGlobal(type, false) |
55 .exportAs("bar"); | 55 .exportAs("bar"); |
56 o.init_index = i; | 56 o.init_index = i; |
57 builder.addGlobal(kAstI32); // pad | 57 builder.addGlobal(kWasmI32); // pad |
58 | 58 |
59 var instance = builder.instantiate({ttt: {foo: val}}); | 59 var instance = builder.instantiate({ttt: {foo: val}}); |
60 assertEquals(expected, instance.exports.bar); | 60 assertEquals(expected, instance.exports.bar); |
61 } | 61 } |
62 | 62 |
63 TestImportedExported(kAstI32, 415.5, 415); | 63 TestImportedExported(kWasmI32, 415.5, 415); |
64 TestImportedExported(kAstF32, -979.34343, Math.fround(-979.34343)); | 64 TestImportedExported(kWasmF32, -979.34343, Math.fround(-979.34343)); |
65 TestImportedExported(kAstF64, 81347.66666, 81347.66666); | 65 TestImportedExported(kWasmF64, 81347.66666, 81347.66666); |
66 | 66 |
67 function TestGlobalIndexSpace(type, val) { | 67 function TestGlobalIndexSpace(type, val) { |
68 print("TestGlobalIndexSpace(" + val + ") = " + val); | 68 print("TestGlobalIndexSpace(" + val + ") = " + val); |
69 var builder = new WasmModuleBuilder(); | 69 var builder = new WasmModuleBuilder(); |
70 var im = builder.addImportedGlobal("nnn", "foo", type); | 70 var im = builder.addImportedGlobal("nnn", "foo", type); |
71 assertEquals(0, im); | 71 assertEquals(0, im); |
72 var def = builder.addGlobal(type, false); | 72 var def = builder.addGlobal(type, false); |
73 assertEquals(1, def.index); | 73 assertEquals(1, def.index); |
74 def.init_index = im; | 74 def.init_index = im; |
75 | 75 |
76 var sig = makeSig([], [type]); | 76 var sig = makeSig([], [type]); |
77 builder.addFunction("main", sig) | 77 builder.addFunction("main", sig) |
78 .addBody([kExprGetGlobal, def.index]) | 78 .addBody([kExprGetGlobal, def.index]) |
79 .exportAs("main"); | 79 .exportAs("main"); |
80 | 80 |
81 var instance = builder.instantiate({nnn: {foo: val}}); | 81 var instance = builder.instantiate({nnn: {foo: val}}); |
82 assertEquals(val, instance.exports.main()); | 82 assertEquals(val, instance.exports.main()); |
83 } | 83 } |
84 | 84 |
85 TestGlobalIndexSpace(kAstI32, 123); | 85 TestGlobalIndexSpace(kWasmI32, 123); |
86 TestGlobalIndexSpace(kAstF32, 54321.125); | 86 TestGlobalIndexSpace(kWasmF32, 54321.125); |
87 TestGlobalIndexSpace(kAstF64, 12345.678); | 87 TestGlobalIndexSpace(kWasmF64, 12345.678); |
OLD | NEW |