Guides
WebAssembly
Build a WebAssembly module and load it in your page.
Kurogane serves .wasm files like any other asset of your frontend. The page loads them with the standard WebAssembly API. No runtime tooling is involved.
Build a module
Add the WebAssembly target once:
rustup target add wasm32-unknown-unknown
Write the module in plain Rust. This one exports a single function:
// add.rs
#![no_std]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
a + b
}
Compile it:
rustc --target wasm32-unknown-unknown -O --crate-type=cdylib add.rs -o add.wasm
Put it with your frontend
The module must end up in the built frontend next to index.html. With Vite put it in frontend/public/. Vite serves it during development and copies it into frontend/dist/ when it builds:
frontend/
├── index.html
├── main.ts
└── public/
└── add.wasm
kurogane bundle then carries it into the bundle’s content with the rest of the frontend.
Load it in the page
Fetch it by a relative URL. The same code then works from the dev server and from app://app in a release build:
const { instance } = await WebAssembly.instantiateStreaming(fetch("add.wasm"));
console.log(instance.exports.add(2, 3)); // 5
app:// serves .wasm files as application/wasm. instantiateStreaming needs that type and compiles the module while it downloads.
Call JavaScript from the module
A module imports what the page hands it. Declare the imports in Rust:
// counter.rs
#![no_std]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
#[link(wasm_import_module = "env")]
extern "C" {
fn report(value: i32);
}
static mut COUNT: i32 = 0;
#[no_mangle]
pub extern "C" fn tick() {
unsafe {
COUNT += 1;
report(COUNT);
}
}
Hand the page’s functions over as the second argument:
const imports = { env: { report: (value) => console.log("count", value) } };
const { instance } = await WebAssembly.instantiateStreaming(fetch("counter.wasm"), imports);
instance.exports.tick();
Higher-level tooling
wasm-bindgen, wasm-pack and Vite’s WebAssembly plugins work as they do in a browser. Kurogane serves whatever files their build output holds.
WebAssembly or a command
WebAssembly runs inside the renderer next to your page. It suits work the page does on its own data, such as parsing, image processing or simulation. That work needs no round trip to Rust.
A command runs in the browser process with your app’s full rights. Use one for files, the network and anything else outside the page.