Let's pretend that life-before-main exists for Rust targeting WebAssembly.
Installation
Add a dependency on wasm-init. This crate intentionally provides a no-op implementation for non-wasm platforms, so cfg handling isn't necessary and we don't pull in unused extra deps.
[]
= "0.2"
User Code
Add as many calls to wasm_init! as required on the Rust side, e.g. for decentralised plugin registration or collecting data from all types that use a particular derive macro.
wasm_init!
Initialisation
To ensure your wasm_init! calls are executed on startup, you have three options. Note that all of these options are idempotent - multiple invocations are perfectly safe (if unnecessary), even in a threaded context.
Auto-init feature
If you'd like to skip manually initialising wasm-init, you can enable the auto-init feature as part of the dependency:
[]
= { = "0.2", = [ "auto-init" ] }
But please bear in mind that this will prevent you from using a wasm bindgen start function elsewhere in your project.
From Rust
Call the wasm_init::wasm_init function inside your Rust entrypoint.
From JavaScript/TypeScript
Call the wasm_init export from your built module.
Why?
inventory et al are cool, but can't yet be used when targeting wasm32-unknown-unknown or similar.