-
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
Add a #[wasm_bindgen(start)]
attribute
#1057
Conversation
For posterity, the major downside of The downsides of using
The thinking is that we can start off conservatively with a specific attribute like this, and then in the future if we see a way forward to |
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.
LGTM!
pub fn foo2(x: u32) {} | ||
|
||
#[wasm_bindgen(start)] | ||
pub fn foo3<T>() {} |
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.
Would be good to have a test for two otherwise valid start functions, to ensure that we properly error and don't accidentally just take the first one or something.
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.
Ah unfortunately we don't have a great place to put such tests today, but I've filed #1058 to track this!
@@ -7,7 +7,7 @@ use wasm_bindgen::prelude::*; | |||
use wasm_bindgen::JsCast; | |||
use web_sys::{Document, Element, HtmlElement, Window}; | |||
|
|||
#[wasm_bindgen] | |||
#[wasm_bindgen(start)] |
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.
Nice
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
. Thestart
attribute can be used to indicate that a function should beexecuted when the module is loaded, configuring the
start
function ofthe 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 inCargo.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 mayneed 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 bea 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 #74