Crates.io | wasm-init |
lib.rs | wasm-init |
version | 0.2.2 |
source | src |
created_at | 2023-08-25 17:38:57.138055 |
updated_at | 2023-08-28 16:03:21.642349 |
description | Let's pretend that life-before-main exists for Rust targeting WebAssembly |
homepage | |
repository | https://github.com/returnString/wasm-init |
max_upload_size | |
id | 954929 |
size | 8,345 |
Let's pretend that life-before-main exists for Rust targeting WebAssembly.
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.
[dependencies]
wasm-init = "0.2"
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::wasm_init! {
// literally any code you want to run at startup goes here
}
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.
If you'd like to skip manually initialising wasm-init
, you can enable the auto-init
feature as part of the dependency:
[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 elsewhere in your project.
Call the wasm_init::wasm_init
function inside your Rust entrypoint.
Call the wasm_init
export from your built module.
<script type="module">
import init, { wasm_init } from "./pkg/my_wasm_crate.js";
init().then(() => {
wasm_init();
// now do things as normal!
});
</script>
inventory et al are cool, but can't yet be used when targeting wasm32-unknown-unknown
or similar.