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
|
|
|
|
|
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...");
|
2017-01-09 13:57:26 +00:00
|
|
|
var kReturnValue = 44;
|
2016-03-07 19:32:35 +00:00
|
|
|
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([
|
2017-01-09 13:57:26 +00:00
|
|
|
kExprI32Const,
|
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...");
|
2017-01-09 13:57:26 +00:00
|
|
|
var kReturnValue = 45;
|
2016-02-27 01:52:30 +00:00
|
|
|
|
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([
|
2017-01-09 13:57:26 +00:00
|
|
|
kExprI32Const,
|
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...");
|
2017-01-09 13:57:26 +00:00
|
|
|
var kReturnValue = 46;
|
2016-12-16 14:23:35 +00:00
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addFunction("main", kSig_i_v)
|
|
|
|
.addBody([
|
2017-01-09 13:57:26 +00:00
|
|
|
kExprI32Const,
|
2016-12-16 14:23:35 +00:00
|
|
|
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...");
|
2017-01-09 13:57:26 +00:00
|
|
|
var kReturnValue = 47;
|
2016-04-29 09:39:26 +00:00
|
|
|
|
|
|
|
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([
|
2017-01-09 13:57:26 +00:00
|
|
|
kExprI32Const,
|
2016-04-29 09:39:26 +00:00
|
|
|
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");
|
|
|
|
|
2017-07-10 09:22:08 +00:00
|
|
|
assertThrows(() => builder.instantiate(), WebAssembly.CompileError,
|
|
|
|
/Duplicate export name 'main' for function 0 and function 2/);
|
2016-06-16 12:18:15 +00:00
|
|
|
})();
|
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);
|
|
|
|
})();
|