2017-10-13 19:12:48 +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
|
|
|
|
|
|
|
|
(function TestPostMessageUnsharedMemory() {
|
2018-09-19 23:30:04 +00:00
|
|
|
let worker = new Worker('', {type: 'string'});
|
2017-10-13 19:12:48 +00:00
|
|
|
let memory = new WebAssembly.Memory({initial: 1, maximum: 2});
|
|
|
|
|
|
|
|
assertThrows(() => worker.postMessage(memory), Error);
|
|
|
|
})();
|
|
|
|
|
2021-01-28 06:19:44 +00:00
|
|
|
(function TestPostMessageUnsharedMemoryBuffer() {
|
|
|
|
let worker = new Worker('', {type: 'string'});
|
|
|
|
let memory = new WebAssembly.Memory({initial: 1, maximum: 2});
|
|
|
|
|
|
|
|
assertThrows(() => worker.postMessage(memory.buffer), Error);
|
|
|
|
})();
|
|
|
|
|
2017-10-18 23:58:37 +00:00
|
|
|
// Can't use assert in a worker.
|
2021-01-25 12:19:05 +00:00
|
|
|
function workerHelpersHelper() {
|
|
|
|
assertTrue = function(value, msg) {
|
2017-10-18 23:58:37 +00:00
|
|
|
if (!value) {
|
|
|
|
postMessage("Error: " + msg);
|
|
|
|
throw new Error("Exit"); // To stop testing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
assertIsWasmMemory = function(memory, expectedSize) {
|
|
|
|
assertTrue(memory instanceof WebAssembly.Memory,
|
|
|
|
"object is not a WebAssembly.Memory");
|
2017-10-18 23:58:37 +00:00
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
assertTrue(memory.buffer instanceof SharedArrayBuffer,
|
|
|
|
"object.buffer is not a SharedArrayBuffer");
|
2017-10-18 23:58:37 +00:00
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
assertTrue(memory.buffer.byteLength == expectedSize,
|
|
|
|
"object.buffer.byteLength is not " + expectedSize + " bytes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let workerHelpers = "(" + workerHelpersHelper.toString() + ")()";
|
2017-10-18 23:58:37 +00:00
|
|
|
|
2017-10-13 19:12:48 +00:00
|
|
|
(function TestPostMessageSharedMemory() {
|
2021-01-25 12:19:05 +00:00
|
|
|
function workerCode(workerHelpers) {
|
|
|
|
eval(workerHelpers);
|
|
|
|
onmessage = function(memory) {
|
|
|
|
assertIsWasmMemory(memory, 65536);
|
|
|
|
postMessage("OK");
|
|
|
|
};
|
|
|
|
}
|
2017-10-18 23:58:37 +00:00
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
let worker = new Worker(workerCode,
|
|
|
|
{type: 'function', arguments: [workerHelpers]});
|
2017-10-13 19:12:48 +00:00
|
|
|
let memory = new WebAssembly.Memory({initial: 1, maximum: 2, shared: true});
|
|
|
|
worker.postMessage(memory);
|
|
|
|
assertEquals("OK", worker.getMessage());
|
|
|
|
worker.terminate();
|
|
|
|
})();
|
2017-10-18 23:58:37 +00:00
|
|
|
|
|
|
|
(function TestPostMessageComplexObjectWithSharedMemory() {
|
2021-01-25 12:19:05 +00:00
|
|
|
function workerCode(workerHelpers) {
|
|
|
|
eval(workerHelpers);
|
|
|
|
onmessage = function(obj) {
|
2017-10-18 23:58:37 +00:00
|
|
|
assertIsWasmMemory(obj.memories[0], 65536);
|
|
|
|
assertIsWasmMemory(obj.memories[1], 65536);
|
|
|
|
assertTrue(obj.buffer instanceof SharedArrayBuffer,
|
|
|
|
"buffer is not a SharedArrayBuffer");
|
|
|
|
assertTrue(obj.memories[0] === obj.memories[1], "memories aren't equal");
|
|
|
|
assertTrue(obj.memories[0].buffer === obj.buffer,
|
|
|
|
"buffers aren't equal");
|
|
|
|
assertTrue(obj.foo === 1, "foo is not 1");
|
|
|
|
postMessage("OK");
|
2021-01-25 12:19:05 +00:00
|
|
|
};
|
|
|
|
}
|
2017-10-18 23:58:37 +00:00
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
let worker = new Worker(workerCode,
|
|
|
|
{type: 'function', arguments: [workerHelpers]});
|
2017-10-18 23:58:37 +00:00
|
|
|
let memory = new WebAssembly.Memory({initial: 1, maximum: 2, shared: true});
|
|
|
|
let obj = {memories: [memory, memory], buffer: memory.buffer, foo: 1};
|
|
|
|
worker.postMessage(obj);
|
|
|
|
assertEquals("OK", worker.getMessage());
|
|
|
|
worker.terminate();
|
|
|
|
})();
|
2018-06-28 00:39:19 +00:00
|
|
|
|
|
|
|
(function TestTwoWorkers() {
|
2021-01-25 12:19:05 +00:00
|
|
|
function workerCode(workerHelpers) {
|
|
|
|
eval(workerHelpers);
|
|
|
|
onmessage = function(memory) {
|
2018-06-28 00:39:19 +00:00
|
|
|
assertIsWasmMemory(memory, 65536);
|
|
|
|
postMessage("OK");
|
2021-01-25 12:19:05 +00:00
|
|
|
};
|
|
|
|
}
|
2018-06-28 00:39:19 +00:00
|
|
|
|
2021-01-25 12:19:05 +00:00
|
|
|
let workers = [new Worker(workerCode,
|
|
|
|
{type: 'function', arguments: [workerHelpers]}),
|
|
|
|
new Worker(workerCode,
|
|
|
|
{type: 'function', arguments: [workerHelpers]})];
|
2018-06-28 00:39:19 +00:00
|
|
|
let memory = new WebAssembly.Memory({initial: 1, maximum: 2, shared: true});
|
|
|
|
for (let worker of workers) {
|
|
|
|
worker.postMessage(memory);
|
|
|
|
assertEquals("OK", worker.getMessage());
|
|
|
|
worker.terminate();
|
|
|
|
}
|
|
|
|
})();
|