Skip to content
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

Introduce #[wasm_bindgen(main)] #3299

Merged
merged 4 commits into from
Apr 12, 2023
Merged

Conversation

daxpedda
Copy link
Collaborator

@daxpedda daxpedda commented Feb 12, 2023

This PR introduces a new attribute for the wasm_bindgen macro: main.
The intention here is to be used on fn main() in a Cargo binary or example.

Currently wasm-bindgen adjusts the main function to be used on startup anyway, but this attribute has a couple of advantages:

  • It supports async fn main() in binaries by removing the async from the function signature but leaving the required fn main() otherwise intact.
  • Currently returning an error from the main function will do nothing, this adjusts it to actually properly throw the error.
  • All return types supported by Rust fn main() are supported, see Termination. This is done with the help of autoref-based specialization, see this case study by dtolnay.

The ui-tests in CI have been adjusted to test on Rust v1.61 to support Termination and it's connected ExitCode, which is why some unrelated ui-test results had to be adjusted.

Replaces #3252.
Replaces #3263.

Fixes #3076.
Fixes #3262.

@daxpedda daxpedda force-pushed the main-attribute branch 2 times, most recently from 7d48138 to adcf286 Compare February 12, 2023 20:25
@daxpedda daxpedda changed the title Implement #[wasm_bindgen(main)] Introduce #[wasm_bindgen(main)] Feb 12, 2023
@daxpedda daxpedda force-pushed the main-attribute branch 4 times, most recently from 4640269 to 56535fc Compare February 13, 2023 22:09
@daxpedda
Copy link
Collaborator Author

Rebased after #3338.

@daxpedda daxpedda force-pushed the main-attribute branch 2 times, most recently from 1c742d7 to 684a570 Compare March 25, 2023 21:59
@daxpedda
Copy link
Collaborator Author

Rebased after #3360.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved

Unlike `#[wasm_bindgen(start)]` this will not export a function to be executed
on startup, it should only be used in Cargo binaries or examples for the `main`
function. `#[wasm_bindgen(start)]` will prevent the `main` function to start and
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what this line is trying to say? I believe that combining them will actually cause the CLI to throw an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm trying to say is that using #[wasm_bindgen(main)] doesn't actually call that function when using the init() function. It only works as expected on fn main in a binary or example.

I will try to improve this, English isn't my first language, please feel free to suggest better wording.

Copy link
Collaborator

@Liamolucko Liamolucko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add an entry for the guide page to guide/src/SUMMARY.md so that it'll show up in the sidebar.

guide/src/reference/attributes/on-rust-exports/main.md Outdated Show resolved Hide resolved
guide/src/reference/attributes/on-rust-exports/main.md Outdated Show resolved Hide resolved
@daxpedda daxpedda requested a review from Liamolucko April 12, 2023 12:45
@Liamolucko
Copy link
Collaborator

Er, you didn't actually remove Termination support, you just removed the UI tests and updated the docs.

@daxpedda
Copy link
Collaborator Author

Er, you didn't actually remove Termination support, you just removed the UI tests and updated the docs.

My bad, I forgot to save the file before committing 😆.

daxpedda and others added 2 commits April 12, 2023 15:44
Copy link
Collaborator

@Liamolucko Liamolucko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Liamolucko Liamolucko merged commit 153a6aa into rustwasm:main Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support more types in Result<(), E> for #[wasm_bindgen(start)] async main() support
2 participants