Js-polyfill using wasi-common crate#720
Js-polyfill using wasi-common crate#720kubkon wants to merge 4 commits intobytecodealliance:mainfrom
Conversation
|
@sunfishcode @alexcrichton @peterhuene I'm not sure whether review requests work in Draft PR mode so I'm cc'ing all of you. Apologies for this additional spam! |
|
I don't have a ton of thoughts about this myself per se, I'm pretty unfamiliar with the Emscripten target in Rust. In terms of build config I'd have a slight preference for a script checked in vs |
OK cool, I can definitely do that! I actually can’t stand the way ones has to specify |
|
@alexcrichton I've now migrated from |
|
OK, I'm gonna go ahead and mark this PR as ready for review even though there is quite a few rough edges I'd like to smooth out. Still, I reckon this is a reasonable step towards porting the One thing to note here, in order to successfully build the Furthermore, also note that the polyfill requires WASI binaries targetting Finally, I haven't done extensive tests for the polyfill, but a simple |
This commit drafts out js-polyfill using `wasi-common` crate.
|
Hm so I'm not sure if I'm the best to review this, I'm not really familiar with how this is expected to be used or how we're looking to maintain this over time. Right now it looks like a huge amount of JS which is handwritten but doesn't have any tests? Some questions I might have are:
I think we probably really do want to auto-generate any JS glue here (if necessary) from |
Yeah, the tests would be great. I didn't add or figure that out for this PR yet, as I wanted this PR to lay some ground for future work in getting the
I haven't looked into that in detail yet, but if it was possible, that would be great and I'm all for that.
AFAIK, although not an expert here, this is meant to replace the polyfill used by https://wasi.dev/polyfill. The idea seems quite old, and reported in #520 (and much earlier in the original, archived wasi-common). I wanted this PR to be the first step in having the polyfill use
I think the main application is similar to https://wasi.dev/polyfill but perhaps @sunfishcode has more ideas about this one?
Yep, agreed! I basically tried to recreate the original |
|
Personally I think that anything new WASI-related being added to this repository should be driven from the As to how this would be used, I'm not thinking so much what the purpose of this is but rather at a technical level how it's expected to be hooked up into other applications. |
Agreed! Before I make those changes I'd really like @sunfishcode to have a look since I'm not even sure the code presented here is going in the right direction. Also, if this PR should include auto-generation from Also, while we're here, perhaps @kripken could have a look/join in the discussion and offer some guidance as well, if it's not too much to ask ofc :-)
Oh OK. That's an excellent question which I actually don't have an answer to :-D |
| return ret; | ||
| }, | ||
|
|
||
| clock_time_get: function(clock_id, precision, time) { |
There was a problem hiding this comment.
precision is a 64-bit integer, how are you handling that in this project?
There was a problem hiding this comment.
So far, we're just not handling it. The C version of this polyfill didn't handle it either. We should fix that, but that can be separate.
|
Aside from one emscripten comment I don't see anything that looks wrong to me. But I just skimmed as I don't understand the setup here. cc @tlively for the rust-emscripten questions above. |
|
The Rust-emscripten regression mentioned in the opening post is fixed in rust-lang/rust#67976, which has been approved but might take a while to get through the commit queue. Please cc me on any other issues you run into with Rust's Emscripten backends. There are certainly some rough corners left since we switched away from using Fastcomp with Rust. |
|
Thanks for putting this together @kubkon! I think it makes sense to merge this, and iterate from here. |
|
On second thought, I agree with @alexcrichton here. There's a lot of boilerplate here, and as WASI evolves, this is going to be more boilerplate that has to be manually maintained. I think we should look for ways to generate more of this code through witx first. |
sunfishcode
left a comment
There was a problem hiding this comment.
Marking "request changes" for PR bookkeeping.
I agree with both @alexcrichton and you to have most of the boilerplate auto-generated from |
Subscribe to Label ActionThis issue or pull request has been labeled: "w", "a", "s", "i" To subscribe or unsubscribe from this label, edit the |
1 similar comment
Subscribe to Label ActionThis issue or pull request has been labeled: "w", "a", "s", "i" To subscribe or unsubscribe from this label, edit the |
Subscribe to Label ActionThis issue or pull request has been labeled: "wasi" Users Subscribed to "wasi"To subscribe or unsubscribe from this label, edit the |
|
I was looking at some older PRs on Wasmtime and I came across this. This is pretty old at this point and isn't entirely actionable as-is. Nowadays jco is probably the best go-to for WASI-on-the-web, however, if someone comes across this now or again in the future. |
This commit drafts out js-polyfill using
wasi-commoncrate.There is a couple of issues with this PR, so I'm gonna mark it as a draft PR until all (or at least a vast majority is satisfactorily resolved) before moving forward with it.
This PR addresses #520.
Rustc regression wrt Emscripten in beta/nightly channels
It seems there might be a compiler regression wrt
wasm32-unknown-emscriptentarget on beta and nightly channels. I'm still to add a CI job for building the introducedjs-polyfillcrate, but if you try and build with the latest Emscripten upstream LLVM backend, you will be presented with cryptic compilation failure of thenum-integercrate:If you decide to use the fastcomp and Rust stable channel instead, you will be greated by linker errors to do with our usage of
u128, etc. Either way, this needs further investigation and potential bug filing in Rust itself.Clean up of .cargo/config (or an alternative?)
I'm currently using
.cargo/configto specifywasm32-unknown-emscriptenas the default target, plus pass all the necessarylink-argstoemcccompiler. This is super messy, so if anyone comes up with a better alternative, please shout out! Another caveat of this approach here, is that the JS + Wasm artifacts land in theassets/folder.Dealing with Wasm memory on the WASI syscall/polyfill boundary
The way we have
wasi-commondesigned currently, is that (most of) syscalls accept a combination of&mut WasiCtxand&mut [u8]where the latter is a mutable view at Wasm memory which we use to decode/encode the passed in pointers to and from. The generated C bindings usingwasi-common-cbindgencrate the require the syscalls to accept*mut WasiCtxand*mut u8+usize(memory's address and its length). In order to avoid a lot of changes in the original wasi.js glue-code, I'm simply passing in the entire Emscripten memory (HEAP8) to everywasi-commonsyscall, which certainly carries some overhead. This should be cleaned up one way or another. If you've got any ideas for this, please do shout out! Even if we don't necessarily fix it in this PR, it'll be good to have some discussion about the best approach for later.Anyhow, my main idea for this is to rewrite translation routines on the JS side to always alloc a contiguous and aligned memory chunks that are big enough to fit the contents located in the WASI Guest heap and required by the syscall to operate on. The tricky bit here is ensuring contiguity and alignment at the same time. I noticed a bit of speed up when I’ve repacked ciovec’s manually this way into a contiguous (compressed if you will) chunk of memory allocated in Emscripten’s heap with only a single
_malloccall.Dealing with Wasi context on the WASI syscall/polyfill boundary
Currently, in this PR, we generate the
WasiCtxstruct as a thread-local struct insrc/main.rsmuch like it was done in the original polyfill.c. In order to pass it as an arg in every WASI syscall in JS, I've exposed an unsafe "getter" fnget_wasi_ctx() -> *mut WasiCtxbut I'm wondering if there could be a better way to handle this. One thing I've had in mind was to feature-gate the generated syscalls inwasi-commonso that if compiled withjs-polyfillfeature on (for instance),WasiCtxwould be accessed statically. All thoughts on this are much appreciated!