2016-02-27 01:52:30 +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");
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-02-27 01:52:30 +00:00
|
|
|
|
|
|
|
(function testExportedMain() {
|
2016-10-25 09:44:15 +00:00
|
|
|
print("TestExportedMain...");
|
2016-03-07 19:32:35 +00:00
|
|
|
var kReturnValue = 88;
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-02-27 01:52:30 +00:00
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", kSig_i_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
|
|
|
kExprI8Const,
|
2016-04-29 09:15:26 +00:00
|
|
|
kReturnValue,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprReturn
|
2016-04-29 09:15:26 +00:00
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc();
|
2016-02-27 01:52:30 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
var module = builder.instantiate();
|
2016-02-27 01:52:30 +00:00
|
|
|
|
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports.main);
|
|
|
|
|
|
|
|
assertEquals(kReturnValue, module.exports.main());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testExportedTwice() {
|
2016-10-25 09:44:15 +00:00
|
|
|
print("TestExportedTwice...");
|
2016-02-27 01:52:30 +00:00
|
|
|
var kReturnValue = 99;
|
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", kSig_i_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
|
|
|
kExprI8Const,
|
2016-04-29 09:15:26 +00:00
|
|
|
kReturnValue,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprReturn
|
2016-04-29 09:15:26 +00:00
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportAs("blah")
|
|
|
|
.exportAs("foo");
|
2016-02-27 01:52:30 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
var module = builder.instantiate();
|
2016-02-27 01:52:30 +00:00
|
|
|
|
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports.blah);
|
|
|
|
assertEquals("function", typeof module.exports.foo);
|
|
|
|
|
|
|
|
assertEquals(kReturnValue, module.exports.foo());
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(kReturnValue, module.exports.blah());
|
2016-10-25 09:44:15 +00:00
|
|
|
assertSame(module.exports.blah, module.exports.foo);
|
2016-02-27 01:52:30 +00:00
|
|
|
})();
|
2016-04-29 09:39:26 +00:00
|
|
|
|
2016-12-16 14:23:35 +00:00
|
|
|
(function testEmptyName() {
|
|
|
|
print("TestEmptyName...");
|
|
|
|
var kReturnValue = 93;
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addFunction("main", kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprI8Const,
|
|
|
|
kReturnValue,
|
|
|
|
kExprReturn
|
|
|
|
])
|
|
|
|
.exportAs("");
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports[""]);
|
|
|
|
|
|
|
|
assertEquals(kReturnValue, module.exports[""]());
|
|
|
|
})();
|
2016-04-29 09:39:26 +00:00
|
|
|
|
|
|
|
(function testNumericName() {
|
2016-10-25 09:44:15 +00:00
|
|
|
print("TestNumericName...");
|
2016-04-29 09:39:26 +00:00
|
|
|
var kReturnValue = 93;
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", kSig_i_v)
|
2016-04-29 09:39:26 +00:00
|
|
|
.addBody([
|
|
|
|
kExprI8Const,
|
|
|
|
kReturnValue,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprReturn
|
2016-04-29 09:39:26 +00:00
|
|
|
])
|
|
|
|
.exportAs("0");
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports["0"]);
|
|
|
|
|
|
|
|
assertEquals(kReturnValue, module.exports["0"]());
|
|
|
|
})();
|
2016-06-16 12:18:15 +00:00
|
|
|
|
|
|
|
(function testExportNameClash() {
|
2016-10-25 09:44:15 +00:00
|
|
|
print("TestExportNameClash...");
|
2016-06-16 12:18:15 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addFunction("one", kSig_v_v).addBody([kExprNop]).exportAs("main");
|
|
|
|
builder.addFunction("two", kSig_v_v).addBody([kExprNop]).exportAs("other");
|
|
|
|
builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main");
|
|
|
|
|
|
|
|
try {
|
|
|
|
builder.instantiate();
|
|
|
|
assertUnreachable("should have thrown an exception");
|
|
|
|
} catch (e) {
|
|
|
|
assertContains("Duplicate export", e.toString());
|
|
|
|
}
|
|
|
|
})();
|
2016-10-25 09:44:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function testExportMultipleIdentity() {
|
|
|
|
print("TestExportMultipleIdentity...");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-12-06 15:33:28 +00:00
|
|
|
var f = builder.addFunction("one", kSig_v_v).addBody([kExprNop])
|
2016-10-25 09:44:15 +00:00
|
|
|
.exportAs("a")
|
|
|
|
.exportAs("b")
|
|
|
|
.exportAs("c");
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let e = instance.exports;
|
|
|
|
assertEquals("function", typeof e.a);
|
|
|
|
assertEquals("function", typeof e.b);
|
|
|
|
assertEquals("function", typeof e.c);
|
|
|
|
assertSame(e.a, e.b);
|
|
|
|
assertSame(e.a, e.c);
|
2016-12-06 15:33:28 +00:00
|
|
|
assertEquals(String(f.index), e.a.name);
|
2016-10-25 09:44:15 +00:00
|
|
|
})();
|
2016-12-20 10:43:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function testReexportJSMultipleIdentity() {
|
|
|
|
print("TestReexportMultipleIdentity...");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
function js() {}
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var a = builder.addImport("m", "a", kSig_v_v);
|
2016-12-20 10:43:54 +00:00
|
|
|
builder.addExport("f", a);
|
|
|
|
builder.addExport("g", a);
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
let instance = builder.instantiate({m: {a: js}});
|
2016-12-20 10:43:54 +00:00
|
|
|
let e = instance.exports;
|
|
|
|
assertEquals("function", typeof e.f);
|
|
|
|
assertEquals("function", typeof e.g);
|
|
|
|
assertFalse(e.f == js);
|
|
|
|
assertFalse(e.g == js);
|
|
|
|
assertTrue(e.f == e.g);
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function testReexportJSMultiple() {
|
|
|
|
print("TestReexportMultiple...");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
function js() {}
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var a = builder.addImport("q", "a", kSig_v_v);
|
|
|
|
var b = builder.addImport("q", "b", kSig_v_v);
|
2016-12-20 10:43:54 +00:00
|
|
|
builder.addExport("f", a);
|
|
|
|
builder.addExport("g", b);
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
let instance = builder.instantiate({q: {a: js, b: js}});
|
2016-12-20 10:43:54 +00:00
|
|
|
let e = instance.exports;
|
|
|
|
assertEquals("function", typeof e.f);
|
|
|
|
assertEquals("function", typeof e.g);
|
|
|
|
assertFalse(e.f == js);
|
|
|
|
assertFalse(e.g == js);
|
|
|
|
assertFalse(e.f == e.g);
|
|
|
|
})();
|