https://github.com/returnstring/wasm-init
Let's pretend that life-before-main exists for Rust targeting WebAssembly
https://github.com/returnstring/wasm-init
Last synced: 5 months ago
JSON representation
Let's pretend that life-before-main exists for Rust targeting WebAssembly
- Host: GitHub
- URL: https://github.com/returnstring/wasm-init
- Owner: returnString
- License: mit
- Created: 2023-08-25T17:10:42.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-08T14:07:53.000Z (over 2 years ago)
- Last Synced: 2025-08-26T06:55:06.447Z (5 months ago)
- Language: Rust
- Homepage:
- Size: 13.7 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
[
](https://crates.io/crates/wasm-init)
[
](https://docs.rs/wasm-init)
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.
```toml
[dependencies]
wasm-init = "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.
```rust
wasm_init::wasm_init! {
// literally any code you want to run at startup goes here
}
```
# 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:
```toml
[dependencies]
wasm-init = { version = "0.2", features = [ "auto-init" ] }
```
But please bear in mind that this will prevent you from using a [wasm bindgen start function](https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-rust-exports/start.html) 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.
```html
import init, { wasm_init } from "./pkg/my_wasm_crate.js";
init().then(() => {
wasm_init();
// now do things as normal!
});
```
# Why?
[inventory](https://github.com/dtolnay/inventory) et al are cool, but can't yet be used when targeting `wasm32-unknown-unknown` or similar.