11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { WASI } = require('wasi');
61cb0ef41Sopenharmony_ciconst { Worker, parentPort } = require('worker_threads');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// void _start(void) { for (;;); }
91cb0ef41Sopenharmony_ciconst bytecode = new Uint8Array([
101cb0ef41Sopenharmony_ci  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
111cb0ef41Sopenharmony_ci  0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01,
121cb0ef41Sopenharmony_ci  0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41,
131cb0ef41Sopenharmony_ci  0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f,
141cb0ef41Sopenharmony_ci  0x72, 0x79, 0x02, 0x00, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00,
151cb0ef41Sopenharmony_ci  0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b,
161cb0ef41Sopenharmony_ci  0x00, 0x10, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x01, 0x09, 0x01, 0x00, 0x06,
171cb0ef41Sopenharmony_ci  0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x2f, 0x09, 0x70, 0x72, 0x6f,
181cb0ef41Sopenharmony_ci  0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63,
191cb0ef41Sopenharmony_ci  0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c,
201cb0ef41Sopenharmony_ci  0x61, 0x6e, 0x67, 0x0f, 0x31, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2d, 0x34,
211cb0ef41Sopenharmony_ci  0x75, 0x62, 0x75, 0x6e, 0x74, 0x75, 0x31,
221cb0ef41Sopenharmony_ci]);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// Do not use isMainThread so that this test itself can be run inside a Worker.
251cb0ef41Sopenharmony_ciif (!process.env.HAS_STARTED_WORKER) {
261cb0ef41Sopenharmony_ci  process.env.HAS_STARTED_WORKER = 1;
271cb0ef41Sopenharmony_ci  const worker = new Worker(__filename);
281cb0ef41Sopenharmony_ci  worker.once('message', (message) => {
291cb0ef41Sopenharmony_ci    assert.strictEqual(message, 'start');
301cb0ef41Sopenharmony_ci    setTimeout(() => worker.terminate(), common.platformTimeout(50));
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci} else {
331cb0ef41Sopenharmony_ci  go();
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciasync function go() {
371cb0ef41Sopenharmony_ci  const wasi = new WASI({ returnOnExit: true });
381cb0ef41Sopenharmony_ci  const imports = { wasi_snapshot_preview1: wasi.wasiImport };
391cb0ef41Sopenharmony_ci  const module = await WebAssembly.compile(bytecode);
401cb0ef41Sopenharmony_ci  const instance = await WebAssembly.instantiate(module, imports);
411cb0ef41Sopenharmony_ci  parentPort.postMessage('start');
421cb0ef41Sopenharmony_ci  wasi.start(instance);
431cb0ef41Sopenharmony_ci}
44