-
Notifications
You must be signed in to change notification settings - Fork 9
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just taken a quick look and compiled and run the tests successfully, and able to try it out a little.
Can I ask a question about how this would be used?
Would users compile their code to WASI binary format (.wasm), and then be able to run it in node as a module, like they would today with a WebAssembly module. A WASI instance, from this PR, would be created and any arguments/environment variables/files to pre-open specified be specified. This instance would then be passed when instantiating the a wasm module as one of the importsObject. Is that how it is intended to be used?
@danbev I'll try to explain the way I see things working in my head: First off, a lot of this work uses wasmtime and @devsnek's nodejs/node#27850 as a reference, and both of those are great work IMO. WASI in general is still very experimental. The goal of this work is to allow people to start using WASI in Node, without overcommitting to APIs.
I believe there is ongoing work in the WebAssembly org to come up with a C level binding between something like V8 and something like uvwasi. In the interim, Node bindings can expose the system calls to JavaScript land - although I'm not sure if we want to expose them to users or not. Node will need an API to bind a WebAssembly module to the syscall interface though (I believe I left a TODO comment somewhere in here). nodejs/node#27850 did this somewhat automatically. I think until WebAssembly settles on an approach, we should make this a manual step, and not something that automatically happens. Once a WebAssembly module is bound to WASI, we can execute it. Since all of the system calls are synchronous, we probably want to support running these bound modules off of the main thread. @guybedford has some thoughts around this in https://gist.github.com/guybedford/890339e83e8cb1abc636a84b003f7a78 and https://gist.github.com/guybedford/d5f771cf549311af5156c4e6412ef1dd. EDIT: Here is small code snippet that you can currently run in Node with this PR just to see some things logged to the console: 'use strict';
const assert = require('assert');
const { WASI } = require('wasi');
const wasi = new WASI({
args: ['--bar=baz', '-foo', 99],
env: process.env
});
const $argc = 0;
const $argvBufSize = 4;
const $environCount = 8;
const $environBufSize = 12;
const $environ = 16;
let r;
r = wasi._wasi.args_sizes_get($argc, $argvBufSize);
assert.strictEqual(r, 0);
const argc = wasi._view.getUint32($argc, true);
const argvBufSize = wasi._view.getUint32($argvBufSize, true);
console.log('argc', argc);
console.log('argvBufSize', argvBufSize);
r = wasi._wasi.environ_sizes_get($environCount, $environBufSize);
assert.strictEqual(r, 0);
const environCount = wasi._view.getUint32($environCount, true);
const environBufSize = wasi._view.getUint32($environBufSize, true);
console.log('environCount', environCount);
console.log('environBufSize', environBufSize);
const $environBuf = $environ + (environCount * 4);
r = wasi._wasi.environ_get($environ, $environBuf);
assert.strictEqual(r, 0);
for (let i = 0; i < environCount; i++) {
const offset = $environ + (i * 4);
let ptr = wasi._view.getUint32(offset, true);
let str = '';
let c;
while ((c = wasi._memory[ptr]) !== 0) {
str += String.fromCharCode(c);
ptr++;
}
console.log('environ[%d] = "%s"', i, str);
}
const $argv = $environBuf + 4;
const $argvBuf = $argv + (argc * 4);
r = wasi._wasi.args_get($argv, $argvBuf);
assert.strictEqual(r, 0);
for (let i = 0; i < argc; i++) {
const offset = $argv + (i * 4);
let ptr = wasi._view.getUint32(offset, true);
let str = '';
let c;
while ((c = wasi._memory[ptr]) !== 0) {
str += String.fromCharCode(c);
ptr++;
}
console.log('argv[%d] = "%s"', i, str);
} |
Amazing to see progress here! I've started a thread in #3 to continue any API discussions as necessary there. Also feel free to entirely ignore and do what seems best to you! |
@cjihrig Thanks for the explanation and the example, I appreciate it! |
This is not even remotely ready for use yet.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes