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