• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --experimental-wasi-unstable-preview1
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const { WASI } = require('wasi');
7const { Worker, parentPort } = require('worker_threads');
8
9// void _start(void) { for (;;); }
10const bytecode = new Uint8Array([
11  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
12  0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01,
13  0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41,
14  0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
15  0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00,
16  0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b,
17  0x00, 0x10, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x09, 0x01, 0x00, 0x06,
18  0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x2f, 0x09, 0x70, 0x72, 0x6f,
19  0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63,
20  0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c,
21  0x61, 0x6e, 0x67, 0x0f, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2d, 0x34,
22  0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x31,
23]);
24
25// Do not use isMainThread so that this test itself can be run inside a Worker.
26if (!process.env.HAS_STARTED_WORKER) {
27  process.env.HAS_STARTED_WORKER = 1;
28  const worker = new Worker(__filename);
29  worker.once('message', (message) => {
30    assert.strictEqual(message, 'start');
31    setTimeout(() => worker.terminate(), common.platformTimeout(50));
32  });
33} else {
34  go();
35}
36
37async function go() {
38  const wasi = new WASI({ returnOnExit: true });
39  const imports = { wasi_snapshot_preview1: wasi.wasiImport };
40  const module = await WebAssembly.compile(bytecode);
41  const instance = await WebAssembly.instantiate(module, imports);
42  parentPort.postMessage('start');
43  wasi.start(instance);
44}
45