worker-module.js 901 Bytes
Newer Older
1 2 3 4
// Copyright 2018 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.

5
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
6

7
(function TestPostModule() {
8 9
  let builder = new WasmModuleBuilder();
  builder.addFunction("add", kSig_i_ii)
10
    .addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprI32Add])
11 12 13 14
    .exportFunc();

  let module = builder.toModule();

15
  function workerCode() {
16 17 18 19 20 21 22 23 24
    onmessage = function(module) {
      try {
        let instance = new WebAssembly.Instance(module);
        let result = instance.exports.add(40, 2);
        postMessage(result);
      } catch(e) {
        postMessage('ERROR: ' + e);
      }
    }
25
  }
26

27
  let worker = new Worker(workerCode, {type: 'function'});
28 29 30
  worker.postMessage(module);
  assertEquals(42, worker.getMessage());
  worker.terminate();
31
})();