summaryrefslogtreecommitdiff
path: root/test/wasi/test-wasi.js
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2019-09-04 17:56:51 -0400
committerAnna Henningsen <anna@addaleax.net>2019-11-30 18:06:39 +0100
commit09b1228c3a2723c6ecb768b40a507688015a478f (patch)
tree88acbfc979bc6f73572c86e8ee804bf0fa4ad326 /test/wasi/test-wasi.js
parent73c837b1ae91cb8852e75a921fa24c714471d690 (diff)
downloadandroid-node-v8-09b1228c3a2723c6ecb768b40a507688015a478f.tar.gz
android-node-v8-09b1228c3a2723c6ecb768b40a507688015a478f.tar.bz2
android-node-v8-09b1228c3a2723c6ecb768b40a507688015a478f.zip
wasi: introduce initial WASI support
Co-authored-by: Gus Caplan <me@gus.host> Co-authored-by: Daniel Bevenius <daniel.bevenius@gmail.com> Co-authored-by: Jiawen Geng <technicalcute@gmail.com> Co-authored-by: Tobias Nießen <tniessen@tnie.de> Co-authored-by: Chengzhong Wu <legendecas@gmail.com> PR-URL: https://github.com/nodejs/node/pull/30258 Refs: https://github.com/nodejs/node/pull/27850 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/wasi/test-wasi.js')
-rw-r--r--test/wasi/test-wasi.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/wasi/test-wasi.js b/test/wasi/test-wasi.js
new file mode 100644
index 0000000000..fa2e0894c9
--- /dev/null
+++ b/test/wasi/test-wasi.js
@@ -0,0 +1,81 @@
+'use strict';
+const common = require('../common');
+
+if (process.argv[2] === 'wasi-child') {
+ const fixtures = require('../common/fixtures');
+ const tmpdir = require('../../test/common/tmpdir');
+ const fs = require('fs');
+ const path = require('path');
+
+ common.expectWarning('ExperimentalWarning',
+ 'WASI is an experimental feature. This feature could ' +
+ 'change at any time');
+
+ const { WASI } = require('wasi');
+ tmpdir.refresh();
+ const wasmDir = path.join(__dirname, 'wasm');
+ const wasi = new WASI({
+ args: [],
+ env: process.env,
+ preopens: {
+ '/sandbox': fixtures.path('wasi'),
+ '/tmp': tmpdir.path
+ }
+ });
+ const importObject = { wasi_unstable: wasi.wasiImport };
+ const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`);
+ const buffer = fs.readFileSync(modulePath);
+
+ (async () => {
+ const { instance } = await WebAssembly.instantiate(buffer, importObject);
+
+ wasi.start(instance);
+ })();
+} else {
+ const assert = require('assert');
+ const cp = require('child_process');
+ const { EOL } = require('os');
+
+ function runWASI(options) {
+ console.log('executing', options.test);
+ const opts = { env: { ...process.env, NODE_DEBUG_NATIVE: 'wasi' } };
+
+ if (options.stdin !== undefined)
+ opts.input = options.stdin;
+
+ const child = cp.spawnSync(process.execPath, [
+ '--experimental-wasi-unstable-preview0',
+ '--experimental-wasm-bigint',
+ __filename,
+ 'wasi-child',
+ options.test
+ ], opts);
+ console.log(child.stderr.toString());
+ assert.strictEqual(child.status, options.exitCode || 0);
+ assert.strictEqual(child.signal, null);
+ assert.strictEqual(child.stdout.toString(), options.stdout || '');
+ }
+
+ runWASI({ test: 'cant_dotdot' });
+ runWASI({ test: 'clock_getres' });
+ runWASI({ test: 'exitcode', exitCode: 120 });
+ runWASI({ test: 'fd_prestat_get_refresh' });
+ runWASI({ test: 'getentropy' });
+ runWASI({ test: 'getrusage' });
+ runWASI({ test: 'gettimeofday' });
+ runWASI({ test: 'notdir' });
+ // runWASI({ test: 'poll' });
+ runWASI({ test: 'preopen_populates' });
+ runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` });
+ runWASI({
+ test: 'read_file_twice',
+ stdout: `hello from input.txt${EOL}hello from input.txt${EOL}`
+ });
+ runWASI({ test: 'stat' });
+ runWASI({ test: 'write_file' });
+
+ // Tests that are currently unsupported on Windows.
+ if (!common.isWindows) {
+ runWASI({ test: 'stdin', stdin: 'hello world', stdout: 'hello world' });
+ }
+}