11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciif (process.argv[2] === 'wasi-child') {
51cb0ef41Sopenharmony_ci  const assert = require('assert');
61cb0ef41Sopenharmony_ci  const fs = require('fs');
71cb0ef41Sopenharmony_ci  const path = require('path');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  const { WASI } = require('wasi');
101cb0ef41Sopenharmony_ci  const wasi = new WASI({
111cb0ef41Sopenharmony_ci    args: ['foo', '-bar', '--baz=value'],
121cb0ef41Sopenharmony_ci  });
131cb0ef41Sopenharmony_ci  const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const modulePath = path.join(__dirname, 'wasm', 'main_args.wasm');
161cb0ef41Sopenharmony_ci  const buffer = fs.readFileSync(modulePath);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  assert.rejects(async () => {
191cb0ef41Sopenharmony_ci    const { instance } = await WebAssembly.instantiate(buffer, importObject);
201cb0ef41Sopenharmony_ci    instance.exports._start();
211cb0ef41Sopenharmony_ci  }, {
221cb0ef41Sopenharmony_ci    name: 'Error',
231cb0ef41Sopenharmony_ci    code: 'ERR_WASI_NOT_STARTED',
241cb0ef41Sopenharmony_ci    message: 'wasi.start() has not been called',
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci} else {
271cb0ef41Sopenharmony_ci  const assert = require('assert');
281cb0ef41Sopenharmony_ci  const cp = require('child_process');
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const child = cp.spawnSync(process.execPath, [
311cb0ef41Sopenharmony_ci    __filename,
321cb0ef41Sopenharmony_ci    'wasi-child',
331cb0ef41Sopenharmony_ci  ], {
341cb0ef41Sopenharmony_ci    env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' },
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
371cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
381cb0ef41Sopenharmony_ci}
39