OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 function TestImported(type, val, expected) { |
| 11 print("TestImported " + type + "(" + val +")" + " = " + expected); |
| 12 var builder = new WasmModuleBuilder(); |
| 13 var sig = makeSig([], [type]); |
| 14 var g = builder.addImportedGlobal("foo", undefined, type); |
| 15 builder.addFunction("main", sig) |
| 16 .addBody([kExprGetGlobal, g.index]) |
| 17 .exportAs("main"); |
| 18 builder.addGlobal(kAstI32); // pad |
| 19 |
| 20 var instance = builder.instantiate({foo: val}); |
| 21 assertEquals(expected, instance.exports.main()); |
| 22 } |
| 23 |
| 24 TestImported(kAstI32, 300.1, 300); |
| 25 TestImported(kAstF32, 87234.87238, Math.fround(87234.87238)); |
| 26 TestImported(kAstF64, 77777.88888, 77777.88888); |
| 27 TestImported(kAstF64, "89", 89); |
| 28 |
| 29 |
| 30 function TestExported(type, val, expected) { |
| 31 print("TestExported " + type + "(" + val +")" + " = " + expected); |
| 32 var builder = new WasmModuleBuilder(); |
| 33 var sig = makeSig([type], []); |
| 34 builder.addGlobal(kAstI32); // pad |
| 35 var g = builder.addGlobal(type, false) |
| 36 .exportAs("foo"); |
| 37 g.init = val; |
| 38 builder.addGlobal(kAstI32); // pad |
| 39 |
| 40 var instance = builder.instantiate(); |
| 41 assertEquals(expected, instance.exports.foo); |
| 42 } |
| 43 |
| 44 TestExported(kAstI32, 455.5, 455); |
| 45 TestExported(kAstF32, -999.34343, Math.fround(-999.34343)); |
| 46 TestExported(kAstF64, 87347.66666, 87347.66666); |
| 47 |
| 48 |
| 49 function TestImportedExported(type, val, expected) { |
| 50 print("TestImportedExported " + type + "(" + val +")" + " = " + expected); |
| 51 var builder = new WasmModuleBuilder(); |
| 52 var sig = makeSig([type], []); |
| 53 var i = builder.addImportedGlobal("foo", undefined, type); |
| 54 builder.addGlobal(kAstI32); // pad |
| 55 var o = builder.addGlobal(type, false) |
| 56 .exportAs("bar"); |
| 57 o.init_index = i; |
| 58 builder.addGlobal(kAstI32); // pad |
| 59 |
| 60 var instance = builder.instantiate({foo: val}); |
| 61 assertEquals(expected, instance.exports.bar); |
| 62 } |
| 63 |
| 64 TestImportedExported(kAstI32, 415.5, 415); |
| 65 TestImportedExported(kAstF32, -979.34343, Math.fround(-979.34343)); |
| 66 TestImportedExported(kAstF64, 81347.66666, 81347.66666); |
OLD | NEW |