worker-memory.js 2.89 KB
Newer Older
1 2 3 4 5 6 7
// 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() {
8
  let worker = new Worker('', {type: 'string'});
9 10 11 12 13
  let memory = new WebAssembly.Memory({initial: 1, maximum: 2});

  assertThrows(() => worker.postMessage(memory), Error);
})();

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// Can't use assert in a worker.
let workerHelpers =
  `function assertTrue(value, msg) {
    if (!value) {
      postMessage("Error: " + msg);
      throw new Error("Exit");  // To stop testing.
    }
   }

   function assertIsWasmMemory(memory, expectedSize) {
      assertTrue(memory instanceof WebAssembly.Memory,
                 "object is not a WebAssembly.Memory");

      assertTrue(memory.buffer instanceof SharedArrayBuffer,
                 "object.buffer is not a SharedArrayBuffer");

      assertTrue(memory.buffer.byteLength == expectedSize,
                 "object.buffer.byteLength is not " + expectedSize + " bytes");
   }
`;

35
(function TestPostMessageSharedMemory() {
36
  let workerScript = workerHelpers +
37
    `onmessage = function(memory) {
38
       assertIsWasmMemory(memory, 65536);
39 40
       postMessage("OK");
     };`;
41

42
  let worker = new Worker(workerScript, {type: 'string'});
43 44 45 46 47
  let memory = new WebAssembly.Memory({initial: 1, maximum: 2, shared: true});
  worker.postMessage(memory);
  assertEquals("OK", worker.getMessage());
  worker.terminate();
})();
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

(function TestPostMessageComplexObjectWithSharedMemory() {
  let workerScript = workerHelpers +
    `onmessage = function(obj) {
       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");
     };`;

63
  let worker = new Worker(workerScript, {type: 'string'});
64 65 66 67 68 69
  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();
})();
70 71 72 73 74 75 76 77

(function TestTwoWorkers() {
  let workerScript = workerHelpers +
    `onmessage = function(memory) {
       assertIsWasmMemory(memory, 65536);
       postMessage("OK");
     };`;

78 79
  let workers = [new Worker(workerScript, {type: 'string'}),
                 new Worker(workerScript, {type: 'string'})];
80 81 82 83 84 85 86
  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();
  }
})();