11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciif (process.argv[2] === 'wasi-child') { 51cb0ef41Sopenharmony_ci const fixtures = require('../common/fixtures'); 61cb0ef41Sopenharmony_ci const tmpdir = require('../common/tmpdir'); 71cb0ef41Sopenharmony_ci const fs = require('fs'); 81cb0ef41Sopenharmony_ci const path = require('path'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci common.expectWarning('ExperimentalWarning', 111cb0ef41Sopenharmony_ci 'WASI is an experimental feature and might change at any time'); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci const { WASI } = require('wasi'); 141cb0ef41Sopenharmony_ci tmpdir.refresh(); 151cb0ef41Sopenharmony_ci const wasmDir = path.join(__dirname, 'wasm'); 161cb0ef41Sopenharmony_ci const wasi = new WASI({ 171cb0ef41Sopenharmony_ci args: ['foo', '-bar', '--baz=value'], 181cb0ef41Sopenharmony_ci env: process.env, 191cb0ef41Sopenharmony_ci preopens: { 201cb0ef41Sopenharmony_ci '/sandbox': fixtures.path('wasi'), 211cb0ef41Sopenharmony_ci '/tmp': tmpdir.path, 221cb0ef41Sopenharmony_ci }, 231cb0ef41Sopenharmony_ci }); 241cb0ef41Sopenharmony_ci const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; 251cb0ef41Sopenharmony_ci const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`); 261cb0ef41Sopenharmony_ci const buffer = fs.readFileSync(modulePath); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci (async () => { 291cb0ef41Sopenharmony_ci const { instance } = await WebAssembly.instantiate(buffer, importObject); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci wasi.start(instance); 321cb0ef41Sopenharmony_ci })().then(common.mustCall()); 331cb0ef41Sopenharmony_ci} else { 341cb0ef41Sopenharmony_ci const assert = require('assert'); 351cb0ef41Sopenharmony_ci const cp = require('child_process'); 361cb0ef41Sopenharmony_ci const { checkoutEOL } = common; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci function runWASI(options) { 391cb0ef41Sopenharmony_ci console.log('executing', options.test); 401cb0ef41Sopenharmony_ci const opts = { 411cb0ef41Sopenharmony_ci env: { 421cb0ef41Sopenharmony_ci ...process.env, 431cb0ef41Sopenharmony_ci NODE_DEBUG_NATIVE: 'wasi', 441cb0ef41Sopenharmony_ci NODE_PLATFORM: process.platform, 451cb0ef41Sopenharmony_ci }, 461cb0ef41Sopenharmony_ci }; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci if (options.stdin !== undefined) 491cb0ef41Sopenharmony_ci opts.input = options.stdin; 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci const child = cp.spawnSync(process.execPath, [ 521cb0ef41Sopenharmony_ci __filename, 531cb0ef41Sopenharmony_ci 'wasi-child', 541cb0ef41Sopenharmony_ci options.test, 551cb0ef41Sopenharmony_ci ], opts); 561cb0ef41Sopenharmony_ci console.log(child.stderr.toString()); 571cb0ef41Sopenharmony_ci assert.strictEqual(child.status, options.exitCode || 0); 581cb0ef41Sopenharmony_ci assert.strictEqual(child.signal, null); 591cb0ef41Sopenharmony_ci assert.strictEqual(child.stdout.toString(), options.stdout || ''); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci runWASI({ test: 'cant_dotdot' }); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci // Tests that are currently unsupported on IBM i PASE. 651cb0ef41Sopenharmony_ci if (!common.isIBMi) { 661cb0ef41Sopenharmony_ci runWASI({ test: 'clock_getres' }); 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci runWASI({ test: 'exitcode', exitCode: 120 }); 691cb0ef41Sopenharmony_ci runWASI({ test: 'fd_prestat_get_refresh' }); 701cb0ef41Sopenharmony_ci runWASI({ test: 'freopen', stdout: `hello from input2.txt${checkoutEOL}` }); 711cb0ef41Sopenharmony_ci runWASI({ test: 'ftruncate' }); 721cb0ef41Sopenharmony_ci runWASI({ test: 'getentropy' }); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci // Tests that are currently unsupported on IBM i PASE. 751cb0ef41Sopenharmony_ci if (!common.isIBMi) { 761cb0ef41Sopenharmony_ci runWASI({ test: 'getrusage' }); 771cb0ef41Sopenharmony_ci } 781cb0ef41Sopenharmony_ci runWASI({ test: 'gettimeofday' }); 791cb0ef41Sopenharmony_ci runWASI({ test: 'main_args' }); 801cb0ef41Sopenharmony_ci runWASI({ test: 'notdir' }); 811cb0ef41Sopenharmony_ci runWASI({ test: 'poll' }); 821cb0ef41Sopenharmony_ci runWASI({ test: 'preopen_populates' }); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci if (!common.isWindows && process.platform !== 'android') { 851cb0ef41Sopenharmony_ci runWASI({ test: 'readdir' }); 861cb0ef41Sopenharmony_ci } 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci runWASI({ test: 'read_file', stdout: `hello from input.txt${checkoutEOL}` }); 891cb0ef41Sopenharmony_ci runWASI({ 901cb0ef41Sopenharmony_ci test: 'read_file_twice', 911cb0ef41Sopenharmony_ci stdout: `hello from input.txt${checkoutEOL}hello from input.txt${checkoutEOL}`, 921cb0ef41Sopenharmony_ci }); 931cb0ef41Sopenharmony_ci runWASI({ test: 'stat' }); 941cb0ef41Sopenharmony_ci runWASI({ test: 'sock' }); 951cb0ef41Sopenharmony_ci runWASI({ test: 'write_file' }); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci // Tests that are currently unsupported on Windows. 981cb0ef41Sopenharmony_ci if (!common.isWindows) { 991cb0ef41Sopenharmony_ci runWASI({ test: 'stdin', stdin: 'hello world', stdout: 'hello world' }); 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci} 102