wasi: add support for version when creating WASI

Refs: https://github.com/nodejs/node/issues/46254

- add version to options when creating WASI object
- add convenience function to return importObject

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

PR-URL: https://github.com/nodejs/node/pull/46469
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Michael Dawson 2023-01-31 18:46:55 -05:00
parent fda0de4c78
commit 2d35f669ae
5 changed files with 120 additions and 21 deletions

View file

@ -10,6 +10,7 @@ const {
} = primordials;
const {
ERR_INVALID_ARG_VALUE,
ERR_WASI_ALREADY_STARTED
} = require('internal/errors').codes;
const {
@ -22,13 +23,14 @@ const {
validateFunction,
validateInt32,
validateObject,
validateString,
validateUndefined,
} = require('internal/validators');
const { WASI: _WASI } = internalBinding('wasi');
const kExitCode = Symbol('kExitCode');
const kSetMemory = Symbol('kSetMemory');
const kStarted = Symbol('kStarted');
const kInstance = Symbol('kInstance');
const kBindingName = Symbol('kBindingName');
emitExperimentalWarning('WASI');
@ -45,6 +47,31 @@ class WASI {
constructor(options = kEmptyObject) {
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';
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');
}
} 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');
const args = ArrayPrototypeMap(options.args || [], String);
@ -138,8 +165,11 @@ class WASI {
_initialize();
}
}
}
getImportObject() {
return { [this[kBindingName]]: this.wasiImport };
}
}
module.exports = { WASI };