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