2016-10-06 15:43:10 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
|
|
|
function TestImported(type, val, expected) {
|
|
|
|
print("TestImported " + type + "(" + val +")" + " = " + expected);
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = makeSig([], [type]);
|
2016-12-20 15:32:56 +00:00
|
|
|
var g = builder.addImportedGlobal("uuu", "foo", type);
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", sig)
|
2017-02-16 14:12:01 +00:00
|
|
|
.addBody([kExprGetGlobal, g])
|
2016-10-06 15:43:10 +00:00
|
|
|
.exportAs("main");
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({uuu: {foo: val}});
|
2016-10-06 15:43:10 +00:00
|
|
|
assertEquals(expected, instance.exports.main());
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestImported(kWasmI32, 300.1, 300);
|
|
|
|
TestImported(kWasmF32, 87234.87238, Math.fround(87234.87238));
|
|
|
|
TestImported(kWasmF64, 77777.88888, 77777.88888);
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
function TestExported(type, val, expected) {
|
|
|
|
print("TestExported " + type + "(" + val +")" + " = " + expected);
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = makeSig([type], []);
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
var g = builder.addGlobal(type, false)
|
|
|
|
.exportAs("foo");
|
|
|
|
g.init = val;
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
var instance = builder.instantiate();
|
|
|
|
assertEquals(expected, instance.exports.foo);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestExported(kWasmI32, 455.5, 455);
|
|
|
|
TestExported(kWasmF32, -999.34343, Math.fround(-999.34343));
|
|
|
|
TestExported(kWasmF64, 87347.66666, 87347.66666);
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2017-01-13 14:38:37 +00:00
|
|
|
(function TestI64Exported() {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = makeSig([kWasmI64], []);
|
|
|
|
builder.addGlobal(kWasmI32); // pad
|
|
|
|
var g = builder.addGlobal(kWasmI64, false)
|
|
|
|
.exportAs("foo");
|
|
|
|
g.init = 1234;
|
|
|
|
builder.addGlobal(kWasmI32); // pad
|
|
|
|
|
|
|
|
assertThrows(()=> {builder.instantiate()}, WebAssembly.LinkError);
|
|
|
|
})();
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
function TestImportedExported(type, val, expected) {
|
|
|
|
print("TestImportedExported " + type + "(" + val +")" + " = " + expected);
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = makeSig([type], []);
|
2016-12-20 15:32:56 +00:00
|
|
|
var i = builder.addImportedGlobal("ttt", "foo", type);
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
var o = builder.addGlobal(type, false)
|
|
|
|
.exportAs("bar");
|
|
|
|
o.init_index = i;
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({ttt: {foo: val}});
|
2016-10-06 15:43:10 +00:00
|
|
|
assertEquals(expected, instance.exports.bar);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestImportedExported(kWasmI32, 415.5, 415);
|
|
|
|
TestImportedExported(kWasmF32, -979.34343, Math.fround(-979.34343));
|
|
|
|
TestImportedExported(kWasmF64, 81347.66666, 81347.66666);
|
2016-10-12 15:19:22 +00:00
|
|
|
|
|
|
|
function TestGlobalIndexSpace(type, val) {
|
|
|
|
print("TestGlobalIndexSpace(" + val + ") = " + val);
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-12-20 15:32:56 +00:00
|
|
|
var im = builder.addImportedGlobal("nnn", "foo", type);
|
2016-10-12 15:19:22 +00:00
|
|
|
assertEquals(0, im);
|
|
|
|
var def = builder.addGlobal(type, false);
|
|
|
|
assertEquals(1, def.index);
|
|
|
|
def.init_index = im;
|
|
|
|
|
|
|
|
var sig = makeSig([], [type]);
|
|
|
|
builder.addFunction("main", sig)
|
|
|
|
.addBody([kExprGetGlobal, def.index])
|
|
|
|
.exportAs("main");
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({nnn: {foo: val}});
|
2016-10-12 15:19:22 +00:00
|
|
|
assertEquals(val, instance.exports.main());
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestGlobalIndexSpace(kWasmI32, 123);
|
|
|
|
TestGlobalIndexSpace(kWasmF32, 54321.125);
|
|
|
|
TestGlobalIndexSpace(kWasmF64, 12345.678);
|