2017-07-10 21:56:49 +00:00
|
|
|
// Copyright 2017 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: --experimental-wasm-threads
|
|
|
|
|
2017-09-14 06:14:48 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
2017-12-20 01:09:56 +00:00
|
|
|
function assertMemoryIsValid(memory, shared) {
|
2017-07-10 21:56:49 +00:00
|
|
|
assertSame(WebAssembly.Memory.prototype, memory.__proto__);
|
|
|
|
assertSame(WebAssembly.Memory, memory.constructor);
|
|
|
|
assertTrue(memory instanceof Object);
|
|
|
|
assertTrue(memory instanceof WebAssembly.Memory);
|
2017-12-20 01:09:56 +00:00
|
|
|
if (shared) {
|
|
|
|
assertTrue(memory.buffer instanceof SharedArrayBuffer);
|
|
|
|
// Assert that the buffer is frozen when memory is shared.
|
|
|
|
assertTrue(Object.isFrozen(memory.buffer));
|
|
|
|
}
|
2017-07-10 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(function TestConstructorWithShared() {
|
|
|
|
print("TestConstructorWithShared");
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: true});
|
2017-12-20 01:09:56 +00:00
|
|
|
assertMemoryIsValid(memory, true);
|
2017-07-10 21:56:49 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestConstructorWithUndefinedShared() {
|
|
|
|
print("TestConstructorWithUndefinedShared");
|
|
|
|
// Maximum = undefined, shared = undefined.
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: undefined, shared: undefined});
|
|
|
|
assertMemoryIsValid(memory);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestConstructorWithNumericShared() {
|
|
|
|
print("TestConstructorWithNumericShared");
|
|
|
|
// For numeric values, shared = true.
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: 2098665});
|
2017-12-20 01:09:56 +00:00
|
|
|
assertMemoryIsValid(memory, true);
|
2017-07-10 21:56:49 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestConstructorWithEmptyStringShared() {
|
|
|
|
print("TestConstructorWithEmptyStringShared");
|
|
|
|
// Maximum = undefined, shared = false.
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: undefined, shared: ""});
|
|
|
|
assertMemoryIsValid(memory);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestConstructorWithUndefinedMaxShared() {
|
|
|
|
print("TestConstructorWithUndefinedMaxShared");
|
|
|
|
// New memory with Maximum = undefined, shared = true => TypeError.
|
|
|
|
assertThrows(() => new WebAssembly.Memory({initial: 0, shared: true}),
|
|
|
|
TypeError);
|
|
|
|
})();
|
2017-09-14 06:14:48 +00:00
|
|
|
|
|
|
|
(function TestCompileWithUndefinedShared() {
|
|
|
|
print("TestCompileWithUndefinedShared");
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: true});
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addImportedMemory("m", "imported_mem", 0, undefined, "shared");
|
|
|
|
assertThrows(() => new WebAssembly.Module(builder.toBuffer()),
|
|
|
|
WebAssembly.CompileError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestCompileAtomicOpUndefinedShared() {
|
|
|
|
print("TestCompileAtomicOpUndefinedShared");
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: true});
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2017-09-14 06:14:48 +00:00
|
|
|
kAtomicPrefix,
|
|
|
|
kExprI32AtomicAdd]);
|
|
|
|
builder.addImportedMemory("m", "imported_mem");
|
|
|
|
assertThrows(() => new WebAssembly.Module(builder.toBuffer()),
|
|
|
|
WebAssembly.CompileError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestInstantiateWithUndefinedShared() {
|
|
|
|
print("TestInstantiateWithUndefinedShared");
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: true});
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addImportedMemory("m", "imported_mem");
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
assertThrows(() => new WebAssembly.Instance(module,
|
|
|
|
{m: {imported_mem: memory}}), WebAssembly.LinkError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestInstantiateWithImportNotSharedDefined() {
|
|
|
|
print("TestInstantiateWithImportNotSharedDefined");
|
|
|
|
let memory = new WebAssembly.Memory({
|
|
|
|
initial: 0, maximum: 10, shared: false});
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addImportedMemory("m", "imported_mem", 0, 10, "shared");
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
assertThrows(() => new WebAssembly.Instance(module,
|
|
|
|
{m: {imported_mem: memory}}), WebAssembly.LinkError);
|
|
|
|
})();
|
2017-12-20 01:09:56 +00:00
|
|
|
|
|
|
|
(function TestInstantiateWithSharedDefined() {
|
|
|
|
print("TestInstantiateWithSharedDefined");
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(2, 10, true, "shared");
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
let instance = new WebAssembly.Instance(module);
|
|
|
|
assertMemoryIsValid(instance.exports.memory, true);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestAtomicOpWithSharedMemoryDefined() {
|
|
|
|
print("TestAtomicOpWithSharedMemoryDefined");
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(2, 10, false, "shared");
|
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2017-12-20 01:09:56 +00:00
|
|
|
kAtomicPrefix,
|
|
|
|
kExprI32AtomicAdd, 2, 0])
|
|
|
|
.exportFunc();
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
let instance = new WebAssembly.Instance(module);
|
|
|
|
assertEquals(0, instance.exports.main(0, 0x11111111));
|
|
|
|
assertEquals(0x11111111, instance.exports.main(0, 0x11111111));
|
|
|
|
})();
|
2019-02-04 13:50:11 +00:00
|
|
|
|
2019-02-21 09:21:36 +00:00
|
|
|
(function TestMemoryConstructorShouldNotCallHasProperty() {
|
|
|
|
print(arguments.callee.name);
|
2019-02-04 13:50:11 +00:00
|
|
|
// from test/wasm-js/data/test/js-api/memory/constructor.any.js
|
|
|
|
const proxy = new Proxy({}, {
|
|
|
|
has(o, x) {
|
|
|
|
throw new Error(`Should not call [[HasProperty]] with ${x}`);
|
|
|
|
},
|
|
|
|
get(o, x) {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
new WebAssembly.Memory(proxy);
|
|
|
|
})();
|