Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add experimental wasi module support on top of wasm modules #27850

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ added: v12.3.0

Enable experimental WebAssembly module support.

### `--experimental-wasi-modules`
<!-- YAML
added: REPLACEME
-->

Enable experimental [WASI][] module support.

### `--force-fips`
<!-- YAML
added: v6.0.0
Expand Down Expand Up @@ -1208,3 +1215,4 @@ greater than `4` (its current default value). For more information, see the
[experimental ECMAScript Module]: esm.html#esm_resolve_hook
[libuv threadpool documentation]: http://docs.libuv.org/en/latest/threadpool.html
[remote code execution]: https://www.owasp.org/index.php/Code_Injection
[WASI]: https://wasi.dev
6 changes: 6 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ node --experimental-modules --experimental-wasm-modules index.mjs

would provide the exports interface for the instantiation of `module.wasm`.

### WASI

Additionally, if `--experimental-wasi-modules` is passed, Wasm modules
conforming to [WASI][] specification will be run.

## Experimental Loader hooks

**Note: This API is currently being redesigned and will still change.**
Expand Down Expand Up @@ -749,3 +754,4 @@ success!
[ES Module Integration Proposal for Web Assembly]: https://github.com/webassembly/esm-integration
[dynamic instantiate hook]: #esm_dynamic_instantiate_hook
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
[WASI]: https://wasi.dev
3 changes: 1 addition & 2 deletions lib/internal/modules/esm/create_dynamic_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import.meta.done();
onReady: (cb) => { readyfns.add(cb); },
};

if (imports.length)
reflect.imports = Object.create(null);
reflect.imports = Object.create(null);

callbackMap.set(m, {
initializeImportMeta: (meta, wrap) => {
Expand Down
34 changes: 31 additions & 3 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
JSON,
Object,
SafeMap,
StringPrototype
StringPrototype,
} = primordials;

const { NativeModule } = require('internal/bootstrap/loaders');
Expand All @@ -22,9 +22,11 @@ const { fileURLToPath, URL } = require('url');
const { debuglog } = require('internal/util/debuglog');
const { promisify } = require('internal/util');
const esmLoader = require('internal/process/esm_loader');
const { getOptionValue } = require('internal/options');
const {
ERR_UNKNOWN_BUILTIN_MODULE
} = require('internal/errors').codes;
const WASI = require('internal/wasi');
const readFileAsync = promisify(fs.readFile);
const JsonParse = JSON.parse;

Expand Down Expand Up @@ -154,6 +156,8 @@ translators.set('json', async function jsonStrategy(url) {
});
});

const wasiEnabled = getOptionValue('--experimental-wasi-modules');

// Strategy for loading a wasm module
translators.set('wasm', async function(url) {
const pathname = fileURLToPath(url);
Expand All @@ -167,13 +171,37 @@ translators.set('wasm', async function(url) {
throw err;
}

const imports =
WebAssembly.Module.imports(compiled).map(({ module }) => module);
let usesWasi = false;
const imports = WebAssembly.Module.imports(compiled)
.reduce((acc, { module }) => {
if (wasiEnabled && module === 'wasi_unstable') {
usesWasi = true;
} else {
acc.push(module);
}
return acc;
}, []);
const exports = WebAssembly.Module.exports(compiled).map(({ name }) => name);

return createDynamicModule(imports, exports, url, (reflect) => {
let wasi;
if (usesWasi) {
wasi = new WASI(WASI.defaultConfig);
reflect.imports.wasi_unstable = wasi.exports;
}

const { exports } = new WebAssembly.Instance(compiled, reflect.imports);
for (const expt of Object.keys(exports))
reflect.exports[expt].set(exports[expt]);

if (usesWasi && exports.memory) {
wasi.setMemory(exports.memory);
}

if (exports._start) {
exports._start();
} else if (exports.__wasi_unstable_reactor_start) {
exports.__wasi_unstable_reactor_start();
}
});
});