An unnamed factory ship game: getrandom in WASM based scripts #1537
alec-deason
started this conversation in
Devlogs
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been experimenting with using Rust based WASM modules as scripts in my game. One of the frustrations with that is the lack of
getrandom
and thusrand
in the WASM guest environment. In WASM on the web or as a WASI executable there are existing tools to solve that problem but hosting directly in rust is a bit more raw. Turns out it's not that hard to do.I'm just talking about the
getrandom
issue here. If my WASM scripting continues to work out well I'll likely write more about how I'm setting the over all system up.I'm using wasmer as my runtime. On the host side you'll need to write a shim that calls the host's version of
getrandom
and then pass that into the WASM module when it's initialized. Then on the guest side you have to configuregetrandom
to call the external shim.Host setup
First you need to setup some context objects that will be used by the shim function:
The interesting bit here is
Env.memory
which will be initialized by the runtime with a reference to the guest's main memory. We need that because we're going to need to resolve pointers into that memory inside the shim:Once we have that shim we setup the import object that will inject it into the guest and then initialize the guest module itself:
And that's it on the host side. Now the guest has access to the host's getrandom.
Guest setup
Switching to the crate that will be our WASM guest. First off it needs to have it's getrandom dependency declared with the "custom" feature which allows us to delegate to the shim function injected by the host. Then we just wire up the shim:
There's not much going on there except that we convert the slice which getrandom gives us into a ptr and a length which we can pass across the guest->host boundary.
And hopefully that saves someone an afternoon of fussing with this stuff.
Beta Was this translation helpful? Give feedback.
All reactions