Skip to content
Prev Previous commit
Next Next commit
squash: address comments
Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Feb 6, 2023
commit 9f07256ae38d2ba54ce23c2cd3e5c06a9500987d
19 changes: 15 additions & 4 deletions doc/api/wasi.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ 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 version is `wasi_snapshot_preview1`.
supported versions are `unstable` and `preview1`. **Default:** `preview1`.

### `wasi.getImportObject()`

Expand All @@ -156,9 +156,20 @@ added: REPLACEME
-->

Return an import object that can be passed to `WebAssembly.instantiate()` if
no other WASM imports are needed beyond those provided by WASI. It
will reflect the version of WASI requested when the WASI constructor was
called.
no other WASM imports are needed beyond those provided by WASI.

If version `unstable` was passed into the constructor it will return:

```json
{ wasi_unstable: wasi.wasiImport }
```

If version `preview1` was passed into the constructor or no version was
specified it will return:

```json
{ wasi_snapshot_preview1: wasi.wasiImport }
```

### `wasi.start(instance)`

Expand Down
15 changes: 10 additions & 5 deletions lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const kExitCode = Symbol('kExitCode');
const kSetMemory = Symbol('kSetMemory');
const kStarted = Symbol('kStarted');
const kInstance = Symbol('kInstance');
const kVersion = Symbol('kVersion');
const kBindingName = Symbol('kBindingName');

emitExperimentalWarning('WASI');

Expand All @@ -51,20 +51,25 @@ class WASI {
if (options.version !== undefined) {
validateString(options.version, 'options.version');
switch (options.version) {
case 'wasi_snapshot_preview1':
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';
break;
// When adding support for additional wasi versions add case here
default:
throw new ERR_INVALID_ARG_VALUE('options.version',
options.version,
'unsupported WASI version');
}
this[kVersion] = options.version;
} else {
// TODO(mdawson): Remove this in a SemVer major PR before Node.js 20
({ WASI: _WASI } = internalBinding('wasi'));
Comment thread
mhdawson marked this conversation as resolved.
this[kVersion] = 'wasi_snapshot_preview1';
this[kBindingName] = 'wasi_snapshot_preview1';
}

if (options.args !== undefined)
Expand Down Expand Up @@ -162,7 +167,7 @@ class WASI {
}

getImportObject() {
return { [this[kVersion]]: this.wasiImport };
return { [this[kBindingName]]: this.wasiImport };
}
}

Expand Down
39 changes: 38 additions & 1 deletion test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if (process.argv[2] === 'wasi-child-default') {
tmpdir.refresh();
const wasmDir = path.join(__dirname, 'wasm');
const wasiPreview1 = new WASI({
version: 'wasi_snapshot_preview1',
version: 'preview1',
args: ['foo', '-bar', '--baz=value'],
env: process.env,
preopens: {
Expand All @@ -68,6 +68,43 @@ if (process.argv[2] === 'wasi-child-default') {

wasiPreview1.start(instancePreview1);
})().then(common.mustCall());
} else if (process.argv[2] === 'wasi-child-unstable') {
// Test version set to wasi_snapshot_preview1
const assert = require('assert');
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 wasiUnstable = new WASI({
version: 'unstable',
args: ['foo', '-bar', '--baz=value'],
env: process.env,
preopens: {
'/sandbox': fixtures.path('wasi'),
'/tmp': tmpdir.path,
},
});

// Validate the getImportObject helper
assert.strictEqual(wasiUnstable.wasiImport,
wasiUnstable.getImportObject().wasi_unstable);
const modulePathUnstable = path.join(wasmDir, `${process.argv[3]}.wasm`);
const bufferUnstable = fs.readFileSync(modulePathUnstable);

(async () => {
const { instance: instanceUnstable } =
await WebAssembly.instantiate(bufferUnstable,
wasiUnstable.getImportObject());

wasiUnstable.start(instanceUnstable);
})().then(common.mustCall());
} else {
const assert = require('assert');
const cp = require('child_process');
Expand Down