Stream: wamr

Topic: Access external resources in host function implementation


view this post on Zulip mainrs (Mar 25 2025 at 22:47):

My WASM VM is part of a C++ class. The class loads the binary blob, instantiates the module and keeps a reference to the module instantiation around.

I couldn't figure out a good way to access the class's resources: let's say I have a host function that needs access to some internal state of the class to properly calculate a value. How would such a pattern look like?

Host functions only take the exec_env and the function's parameter as input values, which makes sense. There just doesn't seem to be a way to somehow attach or use custom data/classes.

thank you!

view this post on Zulip Jun Xu (Mar 26 2025 at 02:21):

IIUC, you want to access some custom data through exec_env, in WAMR this is API to attach user data to it:

wasm_runtime_set_user_data

you can use this API to attach the pointer to your class instance after creating the exec_env, then in your native functions use wasm_runtime_get_user_data to get it back.

Refer to: https://github.com/bytecodealliance/wasm-micro-runtime/blob/8acaaa2892758eb309759a4ae760f2b9e25b9785/core/iwasm/include/wasm_export.h#L1792

WebAssembly Micro Runtime (WAMR). Contribute to bytecodealliance/wasm-micro-runtime development by creating an account on GitHub.

view this post on Zulip mainrs (Mar 26 2025 at 07:41):

Jun Xu said:

IIUC, you want to access some custom data through exec_env, in WAMR this is API to attach user data to it:

wasm_runtime_set_user_data

you can use this API to attach the pointer to your class instance after creating the exec_env, then in your native functions use wasm_runtime_get_user_data to get it back.

Refer to: https://github.com/bytecodealliance/wasm-micro-runtime/blob/8acaaa2892758eb309759a4ae760f2b9e25b9785/core/iwasm/include/wasm_export.h#L1792

Thank you!


Last updated: Apr 10 2025 at 15:03 UTC