1'use strict'; 2const common = require('../common'); 3 4if (process.argv[2] === 'wasi-child') { 5 const fixtures = require('../common/fixtures'); 6 const tmpdir = require('../common/tmpdir'); 7 const fs = require('fs'); 8 const path = require('path'); 9 10 common.expectWarning('ExperimentalWarning', 11 'WASI is an experimental feature. This feature could ' + 12 'change at any time'); 13 14 const { WASI } = require('wasi'); 15 tmpdir.refresh(); 16 const wasmDir = path.join(__dirname, 'wasm'); 17 const wasi = new WASI({ 18 args: ['foo', '-bar', '--baz=value'], 19 env: process.env, 20 preopens: { 21 '/sandbox': fixtures.path('wasi'), 22 '/tmp': tmpdir.path 23 } 24 }); 25 const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; 26 const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`); 27 const buffer = fs.readFileSync(modulePath); 28 29 (async () => { 30 const { instance } = await WebAssembly.instantiate(buffer, importObject); 31 32 wasi.start(instance); 33 })().then(common.mustCall()); 34} else { 35 const assert = require('assert'); 36 const cp = require('child_process'); 37 const { EOL } = require('os'); 38 39 function runWASI(options) { 40 console.log('executing', options.test); 41 const opts = { 42 env: { 43 ...process.env, 44 NODE_DEBUG_NATIVE: 'wasi', 45 NODE_PLATFORM: process.platform 46 } 47 }; 48 49 if (options.stdin !== undefined) 50 opts.input = options.stdin; 51 52 const child = cp.spawnSync(process.execPath, [ 53 '--experimental-wasi-unstable-preview1', 54 '--experimental-wasm-bigint', 55 __filename, 56 'wasi-child', 57 options.test, 58 ], opts); 59 console.log(child.stderr.toString()); 60 assert.strictEqual(child.status, options.exitCode || 0); 61 assert.strictEqual(child.signal, null); 62 assert.strictEqual(child.stdout.toString(), options.stdout || ''); 63 } 64 65 runWASI({ test: 'cant_dotdot' }); 66 67 // Tests that are currently unsupported on IBM i PASE. 68 if (!common.isIBMi) { 69 runWASI({ test: 'clock_getres' }); 70 } 71 runWASI({ test: 'exitcode', exitCode: 120 }); 72 runWASI({ test: 'fd_prestat_get_refresh' }); 73 runWASI({ test: 'freopen', stdout: `hello from input2.txt${EOL}` }); 74 runWASI({ test: 'ftruncate' }); 75 runWASI({ test: 'getentropy' }); 76 77 // Tests that are currently unsupported on IBM i PASE. 78 if (!common.isIBMi) { 79 runWASI({ test: 'getrusage' }); 80 } 81 runWASI({ test: 'gettimeofday' }); 82 runWASI({ test: 'main_args' }); 83 runWASI({ test: 'notdir' }); 84 runWASI({ test: 'poll' }); 85 runWASI({ test: 'preopen_populates' }); 86 87 if (!common.isWindows && process.platform !== 'android') { 88 runWASI({ test: 'readdir' }); 89 } 90 91 runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` }); 92 runWASI({ 93 test: 'read_file_twice', 94 stdout: `hello from input.txt${EOL}hello from input.txt${EOL}` 95 }); 96 runWASI({ test: 'stat' }); 97 runWASI({ test: 'write_file' }); 98 99 // Tests that are currently unsupported on Windows. 100 if (!common.isWindows) { 101 runWASI({ test: 'stdin', stdin: 'hello world', stdout: 'hello world' }); 102 } 103} 104