-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implemented compatibility mode with wasm32-wasi #3324
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Conversation
I am thinking about implementing the WASI systemcalls as optional intrinsics, so that a wasi environment is not needed when running the generated module. |
I just added the environment variable flag "WASM_BINDGEN_EMULATE_WASI" that introduces many WASI systemcalls as intrinsics. While not all syscalls are implemented yet, the tests run fine on wasm32-wasi without any external wasi shims. |
What is the exact use-case of So currently I'm not sure what the purpose of I'm wholly ignorant about WASI btw, so I'm probably missing the big-picture here. |
The main point for doing this was to support compiling C code (e.g. with the |
Apologies for any cluelessness here: would it be possible to compile Rust with the usual |
It should be possible, but currently isn't, mainly because Rust's The difference between them is that Rust flattens structs out into one parameter per field, whereas clang passes them via. a pointer. The There's also then the issue of somehow providing the WASI imports needed by the C library, which would probably involve having to add a way to pass wasm imports to |
Maybe that's the part we could provide here in |
As I understand it, this will allow compiling Rust code to That combined with #3233, means that wasm32-wasi is explicitly incompatible with wasm-bindgen but there is an option to use an ABI, that is the same as clang, thus allowing linking with C code Question: assuming my understanding is correct (please correct me if it isn't), why not make this the default? |
What do you mean with "default"? |
The wasi-abi flag this PR introduces. Why not make it the default? |
Yeah, that sounds reasonable to me too. |
The problem I see with this is that this ABI is currently completely incompatible with any wasm target except wasm32-wasi. I don't think there is a good way to make this the default without breaking most stuff. The main usecase I had for this PR is allowing to run a rust program linked to C code in a browser. For this, the PR also introduces basic wasi shims that do enough to make programs work that expect correct wasi system calls. |
1+ for making |
The whole point here is to build on what Hamza said:
Though I guess making |
If I remember correctly, this is exactly what this PR does. I am honestly not quite sure anymore since so much time has passed. |
Thanks for the correction :) I hope this gets reviewed by the authors, as this PR basically fixes some limitations that arise from |
As mentioned above, I agree mostly with Hamza that I would prefer a solution that adds WASI compatibility by adding a shim to make linked WASI libraries work. This means Rust should be compiled with This isn't a solution that can be applied today because of #3454, so I would argue that fixing #3454 is the way forward right now. Supporting |
To add to what I said previously, I would rather have wasm32-unknown-unknown be compatible with C ABI than wasm32-wasi be supported. wasm32-wasi has functions like file system access which obviously can't be provided in a browser environment so I'm a little hesitant to add wasm32-wasi compatibility mode. We would effectively be shipping a shim like |
That's the idea. |
IMHO, maybe ![]() Either way, this change will allow |
I haven't really been paying attention on the wasm progress in rust. Is there still a realistic usecase for this PR? |
Now that rust-lang/rust#117919 was merged, we could start thinking about the proposal I outlined in #3324 (comment). |
If I understand correctly, one would simply compile a crate with wasm32-unknown-unknown with wasm-bindgen and import c functions with |
The PR didn't introduce a new ABI, just adjusted the
|
This PR adds the new --wasi-abi flag to wasm-bindgen. With this flag, wasm-bindgen generates bindings that are compatible with the ABI used by the rust target wasm32-wasi.
The changes should not affect the behavior of wasm-bindgen when the flag is not specified.
For implementing the behavior of wasm32-wasi, I introduced a few new Instructions to the "Standard".
They are
PackSlice
,UnpackSlice
,PackOption
andUnpackOption
. These instructions translate between the passed-by-pointer structs used in wasm32-wasi and the format expected by the rest of wasm-bindgen. Since I think dereferencing pointers is not quite possible in WIT, I just assumed that the wasi ABI is incompatible with most WIT stuff. If I am wrong about this I am happy to change this.I tried to make the existing tests work with the wasi target, but for this I needed to add a few "mock" system calls in from of empty javascript functions, since there doesn't seem to be a way to disable the std lib with this target.
I am very open to suggestions on this.