Stream: general

Topic: How to expose a global from Rust?


view this post on Zulip JMS (Jul 14 2022 at 04:17):

I want to expose a [u32; 3] constant from a wasm guest (Rust) to the wasmtime host (also Rust). I believe from the host's side, I can access a global extern. How can I expose this in the Rust guest code though?

view this post on Zulip Sergei Pepyakin (Jul 18 2022 at 21:01):

Hey,

In your Rust guest you can use a static with #[no_mangle], such as:

#[no_mangle]
pub static DN: [u32; 3] = [1, 2, 3];

that should create an exported global variable of i32 wasm type named "DN". This is a pointer in linear memory where the array is stored. If you use wasmtime and wasm32-unknown-unknown, you should be able to instantiate the module, get the memory, get the global and then read the 3*4 bytes starting at the value pointing by the global DN.


Last updated: Nov 22 2024 at 16:03 UTC