-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Wasmtime: Allow importing types that from libraries with wasm-bindgen
dependencies
#5557
Comments
Thanks for the report! I definitely agree that the widespread use of "Support
Neither of these fundamental pillars of supporting Again though I don't disagree that there's pain here in mixing browser and out-of-browser environment but I don't believe unifying the environments is a solution. Instead I think it would be better to be clearer and more consistent about what code compiles where and where it runs. This is also a very difficult question and isn't necessarily all that much easier than the above, but it's more scalable into the future I believe. |
To be clear I agree that it would be a very bad idea to try to implement the browser APIs. The behavior I'd expect is the behavior on any other target: the code compiles and runs but if I actually try to call a wasm-bindgen function it panics. I guess I don't understand why this is possible for arbitrary native targets but not WASI, but again, I'm sure that's my own ignorance. |
Just to expand with a very simple example: fn get_window() -> Option<web_sys::Window> {
web_sys::window()
}
fn main() {
if false {
get_window();
}
println!("Hello, world!");
} If I If I
|
You know, I wonder if this is just that this check in #[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] |
There may be ways to get wasm-bindgen to generate better code here, I don't know anything about that. But I believe the behavior you expected is available with |
@jameysharp Oh fantastic, that does it! I will close this and consider opening something over in wasm-bindgen for the possible improvement there. Thanks so much. |
Feature
#3937 contains a good discussion of the problems encountered when trying to compile a program that has any
wasm-bindgen
dependencies in a WASI environment. However, this has significant effects on the ability to usewasmtime
, or any environments that usewasmtime
, to render HTML for apps created in any of the common Rust frontend frameworks. This is because virtually the whole Rust web ecosystem includingweb-sys
andjs-sys
depend onwasm-bindgen
, and they are the building blocks for all the Rust frontend web frameworks. For example, it's very common fIf you are building a native server binary, these
wasm-bindgen
ecosystem libraries compile fine, but panic if you try to run them, for obvious reasons — you are not running in the browser, so do not have access to web APIs. Frameworks are built to handle this. For example, it's very common to declare an HTML element event listener that depends on one of theweb_sys::Event
types. This can be compiled into a native server binary; it simply won't ever be run on the server, so it does not cause an issue.However, attempting to do the same in
wasmtime
causes the error in the issue linked above. See e.g., leptos-rs/leptos#295Benefit
This would allow people to use
wasmtime
for server-side rendering in frameworks like Yew, Sycamore, Leptos, etc. that depend onweb-sys
andwasm-bindgen
, just as they can use native binaries for that same task.Implementation
I don't know enough about how
wasm-bindgen
works internally to make a real proposal, but given that none of thewasm-bindgen
code actually needs to run, it seems that the "allow it to compile and panic if called" solution used for building native binaries for other platforms should work just as well. I'm sure the fact that this seems straightforward is a testament to my own ignorance, here, so apologies.Alternatives
I've laid out a couple alternative approaches for users to this kind of issue in my reply to the issue in the Leptos repo (leptos-rs/leptos#295)
The text was updated successfully, but these errors were encountered: