-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Producing wasm-binary if there's main.rs #74
Comments
This is sort of related to https://github.com/rust-lang-nursery/rust-wasm/issues/18 as the wasm feature here we'd leverage is the |
But until the |
Oh certainly! That was actually a bug in wasm-gc which I pushed/published yesterday. Wasm-bindgen internally uses wasm-gc, so updating dependencies should hopefully fix that bug! |
Nice. |
Hm perhaps! I think I might prefer though to use the |
Hmmm, I can understand. Using the |
In the meantime while we're waiting for rustc to catch up though I'd be totally down for doing something like |
Sorry for the late reply, I got busy with exams and stuff. But I think the |
I implemented this today but I don't think it's necessarily workable. The cyclic dependency between the JS and wasm emitted means that the JS is basically unusable so you can't call imports, but that's pretty non-obvious when writing the wasm. Until that's fixed I'd personally be a bit hesitant to land this. |
I'd love to have this available, such that the js file automatically calls the tagged main/start function as soon as it finishes loading the wasm. |
Same, writing applications entirely in Rust should preferably not need extra bootstrapping code hand-written. |
@LaylConway I definitely agree. |
is this even possible in wasm? you can't have a "normal" main function which would normally host the main loop, but in wasm you setup things in the "main" function and return from it, returning the control to the browser/runtime-environment. |
This is definitely possible for us to do, and it largely just hasn't had anyone backing the effort needed to implement it yet! I believe a rough implementation strategy for this would look like:
We may wish to optionally update the |
How would you do that? By using something to break out of the main fn without calling the destructors like emscripten (code)? |
@chpio that's a good point yeah, and while Rust has a different set of issues than C/C++ it's definitely true that the libstd initialization function, when it exits, performs global cleanup it assumes won't ever be necessary again. So actually now that I think about this, I think it's not as clear that we can implement this in the standard library. In the standard library we'd have to bake in the concept of a "wasm main exiting isn't the end of the world, there's no definite end of the world!", but that's not necessarily an easy decision to make. Instead in the meantime it may be best to simply stick with |
This commit adds a new attribute to `#[wasm_bindgen]`: `start`. The `start` attribute can be used to indicate that a function should be executed when the module is loaded, configuring the `start` function of the wasm executable. While this doesn't necessarily literally configure the `start` section, it does its best! Only one crate in a crate graph may indicate `#[wasm_bindgen(start)]`, so it's not recommended to be used in libraries but only end-user applications. Currently this still must be used with the `crate-type = ["cdylib"]` annotation in `Cargo.toml`. The implementation here is somewhat tricky because of the circular dependency between our generated JS and the wasm file that we emit. This circular dependency makes running initialization routines (like the `start` shim) particularly fraught with complications because one may need to run before the other but bundlers may not necessarily respect it. Workarounds have been implemented for various emission strategies, for example calling the start function directly after exports are wired up with `--no-modules` and otherwise working around what appears to be a Webpack bug with initializers running in a different order than we'd like. In any case, this in theory doesn't show up to the end user! Closes rustwasm#74
This commit adds a new attribute to `#[wasm_bindgen]`: `start`. The `start` attribute can be used to indicate that a function should be executed when the module is loaded, configuring the `start` function of the wasm executable. While this doesn't necessarily literally configure the `start` section, it does its best! Only one crate in a crate graph may indicate `#[wasm_bindgen(start)]`, so it's not recommended to be used in libraries but only end-user applications. Currently this still must be used with the `crate-type = ["cdylib"]` annotation in `Cargo.toml`. The implementation here is somewhat tricky because of the circular dependency between our generated JS and the wasm file that we emit. This circular dependency makes running initialization routines (like the `start` shim) particularly fraught with complications because one may need to run before the other but bundlers may not necessarily respect it. Workarounds have been implemented for various emission strategies, for example calling the start function directly after exports are wired up with `--no-modules` and otherwise working around what appears to be a Webpack bug with initializers running in a different order than we'd like. In any case, this in theory doesn't show up to the end user! Closes rustwasm#74
This commit adds a new attribute to `#[wasm_bindgen]`: `start`. The `start` attribute can be used to indicate that a function should be executed when the module is loaded, configuring the `start` function of the wasm executable. While this doesn't necessarily literally configure the `start` section, it does its best! Only one crate in a crate graph may indicate `#[wasm_bindgen(start)]`, so it's not recommended to be used in libraries but only end-user applications. Currently this still must be used with the `crate-type = ["cdylib"]` annotation in `Cargo.toml`. The implementation here is somewhat tricky because of the circular dependency between our generated JS and the wasm file that we emit. This circular dependency makes running initialization routines (like the `start` shim) particularly fraught with complications because one may need to run before the other but bundlers may not necessarily respect it. Workarounds have been implemented for various emission strategies, for example calling the start function directly after exports are wired up with `--no-modules` and otherwise working around what appears to be a Webpack bug with initializers running in a different order than we'd like. In any case, this in theory doesn't show up to the end user! Closes rustwasm#74
Currently, the only way to execute rust code is via exposing a function and then calling it on js side. It would be nice to be able to do something like this:
main.rs
Then using the cli to generate the required js and including the js directly in the html to execute it, instead of going through an intermediate state of needing to manually write another js file.
The text was updated successfully, but these errors were encountered: