2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-12-17 10:54:08 +00:00
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
var module = (function () {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
var sig_index = builder.addType(kSig_i_ii);
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addImport("q", "add", sig_index);
|
2016-03-07 19:32:35 +00:00
|
|
|
builder.addFunction("add", sig_index)
|
|
|
|
.addBody([
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0
|
2016-03-07 19:32:35 +00:00
|
|
|
]);
|
|
|
|
builder.addFunction("sub", sig_index)
|
|
|
|
.addBody([
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Sub, // --
|
2016-03-07 19:32:35 +00:00
|
|
|
]);
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("main", kSig_i_iii)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 1,
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 2,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprGetLocal, 0,
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, sig_index, kTableZero
|
2016-04-29 09:15:26 +00:00
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc()
|
2016-09-27 20:46:10 +00:00
|
|
|
builder.appendToTable([1, 2, 3]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
return builder.instantiate({q: {add: function(a, b) { return a + b | 0; }}});
|
2015-12-11 12:26:16 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Check the module exists.
|
|
|
|
assertFalse(module === undefined);
|
|
|
|
assertFalse(module === null);
|
|
|
|
assertFalse(module === 0);
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports.main);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(5, module.exports.main(1, 12, 7));
|
|
|
|
assertEquals(19, module.exports.main(0, 12, 7));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
|
|
|
|
assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)");
|
2016-09-27 20:46:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
module = (function () {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
var sig_i_ii = builder.addType(kSig_i_ii);
|
|
|
|
var sig_i_i = builder.addType(kSig_i_i);
|
2016-12-20 15:32:56 +00:00
|
|
|
var mul = builder.addImport("q", "mul", sig_i_ii);
|
2016-10-11 12:40:24 +00:00
|
|
|
var add = builder.addFunction("add", sig_i_ii)
|
2016-09-27 20:46:10 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Add // --
|
|
|
|
]);
|
2016-10-11 12:40:24 +00:00
|
|
|
var popcnt = builder.addFunction("popcnt", sig_i_i)
|
2016-09-27 20:46:10 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprI32Popcnt // --
|
|
|
|
]);
|
2016-10-11 12:40:24 +00:00
|
|
|
var main = builder.addFunction("main", kSig_i_iii)
|
2016-09-27 20:46:10 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 1,
|
|
|
|
kExprGetLocal, 2,
|
|
|
|
kExprGetLocal, 0,
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, sig_i_ii, kTableZero
|
2016-09-27 20:46:10 +00:00
|
|
|
])
|
2016-10-25 09:44:15 +00:00
|
|
|
.exportFunc();
|
2017-03-22 13:32:59 +00:00
|
|
|
builder.appendToTable([mul, add.index, popcnt.index, main.index]);
|
2016-09-27 20:46:10 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
return builder.instantiate({q: {mul: function(a, b) { return a * b | 0; }}});
|
2016-09-27 20:46:10 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
assertEquals(-6, module.exports.main(0, -2, 3));
|
|
|
|
assertEquals(99, module.exports.main(1, 22, 77));
|
|
|
|
assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
|
|
|
|
assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)");
|
|
|
|
assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)");
|
2016-10-11 12:40:24 +00:00
|
|
|
|
2016-10-25 09:44:15 +00:00
|
|
|
function AddFunctions(builder) {
|
2016-10-11 12:40:24 +00:00
|
|
|
var mul = builder.addFunction("mul", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Mul // --
|
|
|
|
]);
|
|
|
|
var add = builder.addFunction("add", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Add // --
|
|
|
|
]);
|
|
|
|
var sub = builder.addFunction("sub", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Sub // --
|
|
|
|
]);
|
2016-10-25 09:44:15 +00:00
|
|
|
return {mul: mul, add: add, sub: sub};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module = (function () {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
var f = AddFunctions(builder);
|
2016-10-11 12:40:24 +00:00
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 33, // --
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, 0, kTableZero]) // --
|
2016-10-11 12:40:24 +00:00
|
|
|
.exportAs("main");
|
|
|
|
|
2016-10-25 09:44:15 +00:00
|
|
|
builder.appendToTable([f.mul.index, f.add.index, f.sub.index]);
|
2016-10-11 12:40:24 +00:00
|
|
|
|
|
|
|
return builder.instantiate();
|
|
|
|
})();
|
|
|
|
|
|
|
|
assertEquals(33, module.exports.main(1, 0));
|
|
|
|
assertEquals(66, module.exports.main(2, 0));
|
|
|
|
assertEquals(34, module.exports.main(1, 1));
|
|
|
|
assertEquals(35, module.exports.main(2, 1));
|
|
|
|
assertEquals(32, module.exports.main(1, 2));
|
|
|
|
assertEquals(31, module.exports.main(2, 2));
|
|
|
|
assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)");
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
|
|
|
|
(function ConstBaseTest() {
|
|
|
|
print("ConstBaseTest...");
|
|
|
|
function instanceWithTable(base, length) {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-10-25 09:44:15 +00:00
|
|
|
var f = AddFunctions(builder);
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 33, // --
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, 0, kTableZero]) // --
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
.exportAs("main");
|
|
|
|
|
2017-09-05 17:48:46 +00:00
|
|
|
builder.setFunctionTableBounds(length, length);
|
2016-10-25 09:44:15 +00:00
|
|
|
builder.addFunctionTableInit(base, false, [f.add.index, f.sub.index, f.mul.index]);
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
|
|
|
|
return builder.instantiate();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
print(" base = " + i);
|
|
|
|
var module = instanceWithTable(i, 10);
|
|
|
|
main = module.exports.main;
|
|
|
|
for (var j = 0; j < i; j++) {
|
|
|
|
assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")");
|
|
|
|
}
|
|
|
|
assertEquals(34, main(1, i + 0));
|
|
|
|
assertEquals(35, main(2, i + 0));
|
|
|
|
assertEquals(32, main(1, i + 1));
|
|
|
|
assertEquals(31, main(2, i + 1));
|
|
|
|
assertEquals(33, main(1, i + 2));
|
|
|
|
assertEquals(66, main(2, i + 2));
|
|
|
|
assertTraps(kTrapFuncInvalid, "main(12, 10)");
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function GlobalBaseTest() {
|
|
|
|
print("GlobalBaseTest...");
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-10-25 09:44:15 +00:00
|
|
|
var f = AddFunctions(builder);
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 33, // --
|
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, 0, kTableZero]) // --
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
.exportAs("main");
|
|
|
|
|
2017-09-05 17:48:46 +00:00
|
|
|
builder.setFunctionTableBounds(10, 10);
|
2016-12-21 13:43:00 +00:00
|
|
|
var g = builder.addImportedGlobal("fff", "base", kWasmI32);
|
2016-10-25 09:44:15 +00:00
|
|
|
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index]);
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
|
|
|
|
var module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
print(" base = " + i);
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = new WebAssembly.Instance(module, {fff: {base: i}});
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
main = instance.exports.main;
|
|
|
|
for (var j = 0; j < i; j++) {
|
|
|
|
assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")");
|
|
|
|
}
|
|
|
|
assertEquals(33, main(1, i + 0));
|
|
|
|
assertEquals(66, main(2, i + 0));
|
|
|
|
assertEquals(34, main(1, i + 1));
|
|
|
|
assertEquals(35, main(2, i + 1));
|
|
|
|
assertEquals(32, main(1, i + 2));
|
|
|
|
assertEquals(31, main(2, i + 2));
|
|
|
|
assertTraps(kTrapFuncInvalid, "main(12, 10)");
|
|
|
|
}
|
|
|
|
})();
|