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.
|
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
// Flags: --expose-wasm --allow-natives-syntax
|
2015-12-17 10:54:08 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2017-01-09 13:57:26 +00:00
|
|
|
let kReturnValue = 17;
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2016-06-21 12:52:57 +00:00
|
|
|
let buffer = (() => {
|
|
|
|
let builder = new WasmModuleBuilder();
|
2016-03-07 19:32:35 +00:00
|
|
|
builder.addMemory(1, 1, true);
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addFunction('main', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, kReturnValue])
|
|
|
|
.exportFunc();
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2016-06-21 12:52:57 +00:00
|
|
|
return builder.toBuffer();
|
2017-04-06 09:49:38 +00:00
|
|
|
})();
|
2016-06-21 12:52:57 +00:00
|
|
|
|
|
|
|
function CheckInstance(instance) {
|
|
|
|
assertFalse(instance === undefined);
|
|
|
|
assertFalse(instance === null);
|
|
|
|
assertFalse(instance === 0);
|
2017-04-06 09:49:38 +00:00
|
|
|
assertEquals('object', typeof instance);
|
2016-06-21 12:52:57 +00:00
|
|
|
|
2016-12-19 18:45:07 +00:00
|
|
|
// Check the exports object is frozen.
|
|
|
|
assertFalse(Object.isExtensible(instance.exports));
|
|
|
|
assertTrue(Object.isFrozen(instance.exports));
|
|
|
|
|
2017-01-12 16:48:09 +00:00
|
|
|
// Check the memory is WebAssembly.Memory.
|
2016-06-21 12:52:57 +00:00
|
|
|
var mem = instance.exports.memory;
|
|
|
|
assertFalse(mem === undefined);
|
|
|
|
assertFalse(mem === null);
|
|
|
|
assertFalse(mem === 0);
|
2017-04-06 09:49:38 +00:00
|
|
|
assertEquals('object', typeof mem);
|
2016-09-27 20:46:10 +00:00
|
|
|
assertTrue(mem instanceof WebAssembly.Memory);
|
|
|
|
var buf = mem.buffer;
|
|
|
|
assertTrue(buf instanceof ArrayBuffer);
|
|
|
|
assertEquals(65536, buf.byteLength);
|
|
|
|
for (var i = 0; i < 4; i++) {
|
2016-06-21 12:52:57 +00:00
|
|
|
instance.exports.memory = 0; // should be ignored
|
2017-04-06 09:49:38 +00:00
|
|
|
mem.buffer = 0; // should be ignored
|
2016-06-21 12:52:57 +00:00
|
|
|
assertSame(mem, instance.exports.memory);
|
2016-09-27 20:46:10 +00:00
|
|
|
assertSame(buf, mem.buffer);
|
2016-06-21 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the properties of the main function.
|
|
|
|
let main = instance.exports.main;
|
|
|
|
assertFalse(main === undefined);
|
|
|
|
assertFalse(main === null);
|
|
|
|
assertFalse(main === 0);
|
2017-04-06 09:49:38 +00:00
|
|
|
assertEquals('function', typeof main);
|
2016-06-21 12:52:57 +00:00
|
|
|
|
|
|
|
assertEquals(kReturnValue, main());
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 12:52:57 +00:00
|
|
|
// Official API
|
2017-01-11 01:14:19 +00:00
|
|
|
(function BasicJSAPITest() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('sync module compile...');
|
2017-01-11 01:14:19 +00:00
|
|
|
let module = new WebAssembly.Module(buffer);
|
2017-04-06 09:49:38 +00:00
|
|
|
print('sync module instantiate...');
|
2017-01-11 01:14:19 +00:00
|
|
|
CheckInstance(new WebAssembly.Instance(module));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
print('async module compile...');
|
2017-01-11 01:14:19 +00:00
|
|
|
let promise = WebAssembly.compile(buffer);
|
2017-10-25 14:12:49 +00:00
|
|
|
assertPromiseResult(
|
|
|
|
promise, module => CheckInstance(new WebAssembly.Instance(module)));
|
2017-01-11 01:14:19 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
print('async instantiate...');
|
2017-01-11 01:14:19 +00:00
|
|
|
let instance_promise = WebAssembly.instantiate(buffer);
|
2017-10-25 14:12:49 +00:00
|
|
|
assertPromiseResult(instance_promise, pair => CheckInstance(pair.instance));
|
2017-01-11 01:14:19 +00:00
|
|
|
})();
|
2016-07-14 16:27:51 +00:00
|
|
|
|
2016-09-29 18:02:36 +00:00
|
|
|
// Check that validate works correctly for a module.
|
|
|
|
assertTrue(WebAssembly.validate(buffer));
|
|
|
|
assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
|
|
|
|
|
2016-07-14 16:27:51 +00:00
|
|
|
// Negative tests.
|
|
|
|
(function InvalidModules() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('InvalidModules...');
|
|
|
|
let invalid_cases = [undefined, 1, '', 'a', {some: 1, obj: 'b'}];
|
2016-07-14 16:27:51 +00:00
|
|
|
let len = invalid_cases.length;
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
try {
|
2016-09-29 18:02:36 +00:00
|
|
|
let instance = new WebAssembly.Instance(invalid_cases[i]);
|
2017-04-06 09:49:38 +00:00
|
|
|
assertUnreachable('should not be able to instantiate invalid modules.');
|
2016-07-14 16:27:51 +00:00
|
|
|
} catch (e) {
|
2017-04-06 09:49:38 +00:00
|
|
|
assertContains('Argument 0', e.toString());
|
2016-07-14 16:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Compile async an invalid blob.
|
|
|
|
(function InvalidBinaryAsyncCompilation() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('InvalidBinaryAsyncCompilation...');
|
2016-07-14 16:27:51 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addFunction('f', kSig_i_i).addBody([kExprCallFunction, 0]);
|
2019-02-06 13:09:43 +00:00
|
|
|
assertThrowsAsync(
|
|
|
|
WebAssembly.compile(builder.toBuffer()), WebAssembly.CompileError);
|
2016-07-14 16:27:51 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Multiple instances tests.
|
|
|
|
(function ManyInstances() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('ManyInstances...');
|
2016-07-14 16:27:51 +00:00
|
|
|
let compiled_module = new WebAssembly.Module(buffer);
|
|
|
|
let instance_1 = new WebAssembly.Instance(compiled_module);
|
|
|
|
let instance_2 = new WebAssembly.Instance(compiled_module);
|
|
|
|
assertTrue(instance_1 != instance_2);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function ManyInstancesAsync() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('ManyInstancesAsync...');
|
2016-07-14 16:27:51 +00:00
|
|
|
let promise = WebAssembly.compile(buffer);
|
2017-10-25 14:12:49 +00:00
|
|
|
assertPromiseResult(promise, compiled_module => {
|
|
|
|
let instance_1 = new WebAssembly.Instance(compiled_module);
|
|
|
|
let instance_2 = new WebAssembly.Instance(compiled_module);
|
|
|
|
assertTrue(instance_1 != instance_2);
|
|
|
|
});
|
2016-07-14 16:27:51 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function InstancesAreIsolatedFromEachother() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('InstancesAreIsolatedFromEachother...');
|
2016-07-14 16:27:51 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addImportedMemory('', 'memory', 1);
|
2016-12-21 13:43:00 +00:00
|
|
|
var kSig_v_i = makeSig([kWasmI32], []);
|
2016-07-14 16:27:51 +00:00
|
|
|
var signature = builder.addType(kSig_v_i);
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addImport('m', 'some_value', kSig_i_v);
|
|
|
|
builder.addImport('m', 'writer', signature);
|
|
|
|
|
|
|
|
builder.addFunction('main', kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0, kExprI32LoadMem, 0, 0, kExprI32Const, 1,
|
|
|
|
kExprCallIndirect, signature, kTableZero, kExprGetLocal, 0,
|
|
|
|
kExprI32LoadMem, 0, 0, kExprCallFunction, 0, kExprI32Add
|
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-07-14 16:27:51 +00:00
|
|
|
|
|
|
|
// writer(mem[i]);
|
|
|
|
// return mem[i] + some_value();
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addFunction('_wrap_writer', signature).addBody([
|
|
|
|
kExprGetLocal, 0, kExprCallFunction, 1
|
|
|
|
]);
|
2016-09-27 20:46:10 +00:00
|
|
|
builder.appendToTable([2, 3]);
|
2016-07-14 16:27:51 +00:00
|
|
|
|
|
|
|
var module = new WebAssembly.Module(builder.toBuffer());
|
2016-10-28 18:13:01 +00:00
|
|
|
var mem_1 = new WebAssembly.Memory({initial: 1});
|
|
|
|
var mem_2 = new WebAssembly.Memory({initial: 1});
|
|
|
|
var view_1 = new Int32Array(mem_1.buffer);
|
|
|
|
var view_2 = new Int32Array(mem_2.buffer);
|
2016-07-14 16:27:51 +00:00
|
|
|
|
|
|
|
view_1[0] = 42;
|
|
|
|
view_2[0] = 1000;
|
|
|
|
|
|
|
|
var outval_1;
|
|
|
|
var outval_2;
|
2017-04-06 09:49:38 +00:00
|
|
|
var i1 = new WebAssembly.Instance(module, {
|
|
|
|
m: {some_value: () => 1, writer: (x) => outval_1 = x},
|
|
|
|
'': {memory: mem_1}
|
|
|
|
});
|
2017-01-12 16:48:09 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
var i2 = new WebAssembly.Instance(module, {
|
|
|
|
m: {some_value: () => 2, writer: (x) => outval_2 = x},
|
|
|
|
'': {memory: mem_2}
|
|
|
|
});
|
2016-07-14 16:27:51 +00:00
|
|
|
|
|
|
|
assertEquals(43, i1.exports.main(0));
|
|
|
|
assertEquals(1002, i2.exports.main(0));
|
|
|
|
|
|
|
|
assertEquals(42, outval_1);
|
|
|
|
assertEquals(1000, outval_2);
|
|
|
|
})();
|
2016-08-31 00:35:40 +00:00
|
|
|
|
|
|
|
(function GlobalsArePrivateToTheInstance() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('GlobalsArePrivateToTheInstance...');
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addGlobal(kWasmI32, true);
|
|
|
|
builder.addFunction('read', kSig_i_v)
|
|
|
|
.addBody([kExprGetGlobal, 0])
|
|
|
|
.exportFunc();
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Committed: https://crrev.com/01f5af515728aebe6c5246f4f7dd6c573e8748af
Review-Url: https://codereview.chromium.org/2305903002
Cr-Original-Commit-Position: refs/heads/master@{#39153}
Cr-Commit-Position: refs/heads/master@{#39361}
2016-09-12 23:12:25 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addFunction('write', kSig_v_i)
|
|
|
|
.addBody([kExprGetLocal, 0, kExprSetGlobal, 0])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
var module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
var i1 = new WebAssembly.Instance(module);
|
|
|
|
var i2 = new WebAssembly.Instance(module);
|
|
|
|
i1.exports.write(1);
|
|
|
|
i2.exports.write(2);
|
|
|
|
assertEquals(1, i1.exports.read());
|
|
|
|
assertEquals(2, i2.exports.read());
|
|
|
|
})();
|
2017-04-03 14:34:19 +00:00
|
|
|
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Committed: https://crrev.com/01f5af515728aebe6c5246f4f7dd6c573e8748af
Review-Url: https://codereview.chromium.org/2305903002
Cr-Original-Commit-Position: refs/heads/master@{#39153}
Cr-Commit-Position: refs/heads/master@{#39361}
2016-09-12 23:12:25 +00:00
|
|
|
(function InstanceMemoryIsIsolated() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('InstanceMemoryIsIsolated...');
|
2016-08-31 00:35:40 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addImportedMemory('', 'memory', 1);
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Review-Url: https://codereview.chromium.org/2305903002
Cr-Commit-Position: refs/heads/master@{#39153}
2016-09-05 10:08:02 +00:00
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addFunction('f', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0])
|
|
|
|
.exportFunc();
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Committed: https://crrev.com/01f5af515728aebe6c5246f4f7dd6c573e8748af
Review-Url: https://codereview.chromium.org/2305903002
Cr-Original-Commit-Position: refs/heads/master@{#39153}
Cr-Commit-Position: refs/heads/master@{#39361}
2016-09-12 23:12:25 +00:00
|
|
|
|
2016-10-28 18:13:01 +00:00
|
|
|
var mem_1 = new WebAssembly.Memory({initial: 1});
|
|
|
|
var mem_2 = new WebAssembly.Memory({initial: 1});
|
|
|
|
var view_1 = new Int32Array(mem_1.buffer);
|
|
|
|
var view_2 = new Int32Array(mem_2.buffer);
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Committed: https://crrev.com/01f5af515728aebe6c5246f4f7dd6c573e8748af
Review-Url: https://codereview.chromium.org/2305903002
Cr-Original-Commit-Position: refs/heads/master@{#39153}
Cr-Commit-Position: refs/heads/master@{#39361}
2016-09-12 23:12:25 +00:00
|
|
|
view_1[0] = 1;
|
|
|
|
view_2[0] = 1000;
|
2016-08-31 00:35:40 +00:00
|
|
|
|
|
|
|
var module = new WebAssembly.Module(builder.toBuffer());
|
2017-04-06 09:49:38 +00:00
|
|
|
var i1 = new WebAssembly.Instance(module, {'': {memory: mem_1}});
|
|
|
|
var i2 = new WebAssembly.Instance(module, {'': {memory: mem_2}});
|
[wasm] reuse the first compiled module.
This change avoids needing to keep around an unused compiled
module. Instead, the result of compiling the wasm bytes is
given to the first instance. The module object and that instance object
point to the same compiled module. Instances are, then, cloned from
the compiled module the module object points to. When an instance is
collected, we make sure that the module object still has a clone
available, and, if the last instance is GC-ed, we also reset the compiled
module so that it does not reference its heap, so that it (==heap) may
be collected.
This is achieved by linking the clones in a double-linked list and
registering a finalizer for each. When we create an instance, we tie it
in the front of the list, making the module object point to it (O(1)). When
the finalizer is called, we relink the list over the dying object (O(1)). The
costliest operation is finalizing the last instance, since we need to visit
all wasm functions and reset heap references.
BUG=v8:5316
Committed: https://crrev.com/01f5af515728aebe6c5246f4f7dd6c573e8748af
Review-Url: https://codereview.chromium.org/2305903002
Cr-Original-Commit-Position: refs/heads/master@{#39153}
Cr-Commit-Position: refs/heads/master@{#39361}
2016-09-12 23:12:25 +00:00
|
|
|
|
|
|
|
assertEquals(1, i1.exports.f());
|
|
|
|
assertEquals(1000, i2.exports.f());
|
2016-08-31 00:35:40 +00:00
|
|
|
})();
|
2016-10-28 18:13:01 +00:00
|
|
|
|
|
|
|
(function MustBeMemory() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('MustBeMemory...');
|
2016-10-28 18:13:01 +00:00
|
|
|
var memory = new ArrayBuffer(65536);
|
2017-01-12 16:48:09 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addImportedMemory('', 'memory');
|
2017-01-12 16:48:09 +00:00
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
|
2017-04-06 09:49:38 +00:00
|
|
|
assertThrows(
|
|
|
|
() => new WebAssembly.Instance(module, {'': {memory: memory}}),
|
|
|
|
WebAssembly.LinkError);
|
2016-10-28 18:13:01 +00:00
|
|
|
})();
|
2017-01-16 17:44:47 +00:00
|
|
|
|
|
|
|
(function TestNoMemoryToExport() {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.exportMemoryAs('memory');
|
|
|
|
assertThrows(() => builder.instantiate(), WebAssembly.CompileError);
|
|
|
|
})();
|
2017-01-20 19:35:16 +00:00
|
|
|
|
|
|
|
(function TestIterableExports() {
|
2017-04-06 09:49:38 +00:00
|
|
|
print('TestIterableExports...');
|
2017-01-20 19:35:16 +00:00
|
|
|
let builder = new WasmModuleBuilder;
|
2017-04-06 09:49:38 +00:00
|
|
|
builder.addExport('a', builder.addFunction('', kSig_v_v).addBody([]));
|
|
|
|
builder.addExport('b', builder.addFunction('', kSig_v_v).addBody([]));
|
|
|
|
builder.addExport('c', builder.addFunction('', kSig_v_v).addBody([]));
|
|
|
|
builder.addExport('d', builder.addFunction('', kSig_v_v).addBody([]));
|
|
|
|
builder.addExport('e', builder.addGlobal(kWasmI32, false));
|
2017-01-20 19:35:16 +00:00
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
let instance = new WebAssembly.Instance(module);
|
|
|
|
|
|
|
|
let exports_count = 0;
|
|
|
|
for (var e in instance.exports) ++exports_count;
|
|
|
|
|
|
|
|
assertEquals(5, exports_count);
|
|
|
|
})();
|