wasi: make version non-optional

- remove default for version
- updates tests to specify version
- add test for when version is not specified

Signed-off-by: Michael Dawson <mdawson@devrus.com>

PR-URL: https://github.com/nodejs/node/pull/47391
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Michael Dawson 2023-04-03 17:50:18 -04:00
parent 509b1eb3aa
commit 7efae9341f
11 changed files with 54 additions and 83 deletions

View File

@ -118,6 +118,9 @@ added:
- v13.3.0
- v12.16.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47391
description: The version option is now required and has no default value.
- version: v19.8.0
pr-url: https://github.com/nodejs/node/pull/46469
description: version field added to options.
@ -144,7 +147,8 @@ changes:
* `stderr` {integer} The file descriptor used as standard error in the
WebAssembly application. **Default:** `2`.
* `version` {string} The version of WASI requested. Currently the only
supported versions are `unstable` and `preview1`. **Default:** `preview1`.
supported versions are `unstable` and `preview1`. This option is
mandatory.
### `wasi.getImportObject()`

View File

@ -48,14 +48,12 @@ class WASI {
validateObject(options, 'options');
let _WASI;
if (options.version !== undefined) {
validateString(options.version, 'options.version');
switch (options.version) {
case 'unstable':
({ WASI: _WASI } = internalBinding('wasi'));
this[kBindingName] = 'wasi_unstable';
break;
// When adding support for additional wasi versions add case here
case 'preview1':
({ WASI: _WASI } = internalBinding('wasi'));
this[kBindingName] = 'wasi_snapshot_preview1';
@ -66,11 +64,6 @@ class WASI {
options.version,
'unsupported WASI version');
}
} else {
// TODO(mdawson): Remove this in a SemVer major PR before Node.js 20
({ WASI: _WASI } = internalBinding('wasi'));
this[kBindingName] = 'wasi_snapshot_preview1';
}
if (options.args !== undefined)
validateArray(options.args, 'options.args');

View File

@ -9,7 +9,7 @@ const modulePath = path.join(wasmDir, 'exitcode.wasm');
const buffer = fs.readFileSync(modulePath);
(async () => {
const wasi = new WASI({ returnOnExit: true });
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const { instance } = await WebAssembly.instantiate(buffer, importObject);
@ -19,7 +19,7 @@ const buffer = fs.readFileSync(modulePath);
(async () => {
// Verify that if a WASI application throws an exception, Node rethrows it
// properly.
const wasi = new WASI({ returnOnExit: true });
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
const patchedExit = () => { throw new Error('test error'); };
wasi.wasiImport.proc_exit = patchedExit.bind(wasi.wasiImport);
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

View File

@ -11,7 +11,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
(async () => {
{
// Verify that a WebAssembly.Instance is passed in.
const wasi = new WASI();
const wasi = new WASI({ version: 'preview1' });
assert.throws(
() => { wasi.initialize(); },
@ -24,7 +24,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that the passed instance has an exports objects.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -40,7 +40,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a _initialize() export was passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -63,7 +63,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a _start export was not passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -88,7 +88,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a memory export was passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -106,7 +106,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a WebAssembly.Instance from another VM context is accepted.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const instance = await vm.runInNewContext(`
(async () => {
const wasm = await WebAssembly.compile(bufferSource);
@ -130,7 +130,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that initialize() can only be called once.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);

View File

@ -8,6 +8,7 @@ if (process.argv[2] === 'wasi-child') {
const { WASI } = require('wasi');
const wasi = new WASI({
version: 'preview1',
args: ['foo', '-bar', '--baz=value'],
});
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };

View File

@ -5,34 +5,34 @@ const assert = require('assert');
const { WASI } = require('wasi');
// If args is undefined, it should default to [] and should not throw.
new WASI({});
new WASI({ version: 'preview1' });
// If args is not an Array and not undefined, it should throw.
assert.throws(() => { new WASI({ args: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', args: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bargs\b/ });
// If env is not an Object and not undefined, it should throw.
assert.throws(() => { new WASI({ env: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', env: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\benv\b/ });
// If preopens is not an Object and not undefined, it should throw.
assert.throws(() => { new WASI({ preopens: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', preopens: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bpreopens\b/ });
// If returnOnExit is not a boolean and not undefined, it should throw.
assert.throws(() => { new WASI({ returnOnExit: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', returnOnExit: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\breturnOnExit\b/ });
// If stdin is not an int32 and not undefined, it should throw.
assert.throws(() => { new WASI({ stdin: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', stdin: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bstdin\b/ });
// If stdout is not an int32 and not undefined, it should throw.
assert.throws(() => { new WASI({ stdout: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', stdout: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bstdout\b/ });
// If stderr is not an int32 and not undefined, it should throw.
assert.throws(() => { new WASI({ stderr: 'fhqwhgads' }); },
assert.throws(() => { new WASI({ version: 'preview1', stderr: 'fhqwhgads' }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bstderr\b/ });
// If options is provided, but not an object, the constructor should throw.
@ -43,13 +43,16 @@ assert.throws(() => { new WASI({ stderr: 'fhqwhgads' }); },
// Verify that exceptions thrown from the binding layer are handled.
assert.throws(() => {
new WASI({ preopens: { '/sandbox': '__/not/real/path' } });
new WASI({ version: 'preview1', preopens: { '/sandbox': '__/not/real/path' } });
}, { code: 'UVWASI_ENOENT', message: /uvwasi_init/ });
// If version is not a string, it should throw
assert.throws(() => { new WASI({ version: { x: 'y' } }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bversion\b/ });
// If version is not specified, it should throw.
assert.throws(() => { new WASI(); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bversion\b/ });
// If version is an unsupported version, it should throw
assert.throws(() => { new WASI({ version: 'not_a_version' }); },

View File

@ -11,7 +11,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
(async () => {
{
// Verify that a WebAssembly.Instance is passed in.
const wasi = new WASI();
const wasi = new WASI({ version: 'preview1' });
assert.throws(
() => { wasi.start(); },
@ -24,7 +24,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that the passed instance has an exports objects.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -40,7 +40,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a _start() export was passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -60,7 +60,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that an _initialize export was not passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -85,7 +85,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a memory export was passed.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);
@ -103,7 +103,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that a WebAssembly.Instance from another VM context is accepted.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const instance = await vm.runInNewContext(`
(async () => {
const wasm = await WebAssembly.compile(bufferSource);
@ -127,7 +127,7 @@ const bufferSource = fixtures.readSync('simple.wasm');
{
// Verify that start() can only be called once.
const wasi = new WASI({});
const wasi = new WASI({ version: 'preview1' });
const wasm = await WebAssembly.compile(bufferSource);
const instance = await WebAssembly.instantiate(wasm);

View File

@ -18,7 +18,7 @@ writeFileSync(stdinFile, 'x'.repeat(33));
const stdin = openSync(stdinFile, 'r');
const stdout = openSync(stdoutFile, 'a');
const stderr = openSync(stderrFile, 'a');
const wasi = new WASI({ stdin, stdout, stderr, returnOnExit: true });
const wasi = new WASI({ version: 'preview1', stdin, stdout, stderr, returnOnExit: true });
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
(async () => {

View File

@ -10,6 +10,7 @@ if (process.argv[2] === 'wasi-child') {
const { WASI } = require('wasi');
const wasmDir = path.join(__dirname, 'wasm');
const wasi = new WASI({
version: 'preview1',
args: [],
env: process.env,
preopens: {

View File

@ -34,7 +34,7 @@ if (!process.env.HAS_STARTED_WORKER) {
}
async function go() {
const wasi = new WASI({ returnOnExit: true });
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
const imports = { wasi_snapshot_preview1: wasi.wasiImport };
const module = await WebAssembly.compile(bytecode);
const instance = await WebAssembly.instantiate(module, imports);

View File

@ -1,37 +1,7 @@
'use strict';
const common = require('../common');
if (process.argv[2] === 'wasi-child-default') {
// test default case
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const path = require('path');
common.expectWarning('ExperimentalWarning',
'WASI is an experimental feature and might change at any time');
const { WASI } = require('wasi');
tmpdir.refresh();
const wasmDir = path.join(__dirname, 'wasm');
const wasi = new WASI({
args: ['foo', '-bar', '--baz=value'],
env: process.env,
preopens: {
'/sandbox': fixtures.path('wasi'),
'/tmp': tmpdir.path,
},
});
const importObject = { wasi_snapshot_preview1: 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);
})().then(common.mustCall());
} else if (process.argv[2] === 'wasi-child-preview1') {
if (process.argv[2] === 'wasi-child-preview1') {
// Test version set to preview1
const assert = require('assert');
const fixtures = require('../common/fixtures');
@ -73,7 +43,7 @@ if (process.argv[2] === 'wasi-child-default') {
const cp = require('child_process');
const { checkoutEOL } = common;
function innerRunWASI(options, args, flavor = 'default') {
function innerRunWASI(options, args, flavor = 'preview1') {
console.log('executing', options.test);
const opts = {
env: {
@ -101,7 +71,6 @@ if (process.argv[2] === 'wasi-child-default') {
function runWASI(options) {
innerRunWASI(options, ['--no-turbo-fast-api-calls']);
innerRunWASI(options, ['--turbo-fast-api-calls']);
innerRunWASI(options, ['--turbo-fast-api-calls'], 'preview1');
}
runWASI({ test: 'cant_dotdot' });