Stream: wasmtime

Topic: ✔ call wasm function by index in function section


view this post on Zulip Wilson Wang (Jan 09 2023 at 02:52):

I have a general question regarding wasmtime. Can I call a funtion by its index in the function section in the wasm module file?

view this post on Zulip bjorn3 (Jan 09 2023 at 14:22):

AFAIK there is no such option. You have to either look up by name or iterate the exports list (which contains all kinds of exports, not just functions).

view this post on Zulip Wilson Wang (Jan 09 2023 at 17:48):

the reason I am asking this is because I want wasmtime host side to call a wasm function which it got from a host call. So in this case, this function is an index into the function section. It can be a function not exported.

view this post on Zulip Alex Crichton (Jan 09 2023 at 17:53):

in a situation like that you're probably getting a table index for the function, not the actual wasm-binary-encoding function index for the function, so the module will need to export its function table and you can acquire a Func through that

view this post on Zulip Wilson Wang (Jan 09 2023 at 17:55):

thanks alex, do you have some online document about this?

view this post on Zulip Alex Crichton (Jan 09 2023 at 17:55):

I do not, sorry

view this post on Zulip Wilson Wang (Jan 09 2023 at 17:55):

np thanks

view this post on Zulip Notification Bot (Jan 10 2023 at 19:46):

Wilson Wang has marked this topic as resolved.

view this post on Zulip Julien Cretin (ia0) (Jan 10 2023 at 20:14):

If I can add something although the topic is closed. I don't think it makes logical sense to call a function by index because that function may not be exported. This is probably not specified by the standard since all bets are off during host calls, but it may break some implicit logical contracts between the module and its embedder. What you probably want to do instead is export an "apply" function (actually a family of such function, one per different function type) that takes a function index (in the table) of the correct type and the function arguments and apply them to the function. This would essentially just be some call_indirect wrapper. Because you can derive the exported function name from the function type you want to call, you don't have any problem using the usual exported function API. You would simply call this function with the index you got during a previous host call (which is the index into the table, not the index of the function) followed by the arguments the function expects. This is cleaner and actually simpler to implement than trying to call functions by index.

view this post on Zulip Wilson Wang (Jan 11 2023 at 02:20):

thanks for the reply. I am actually thinking if it is possible to update the wasm spec to support de/serializing the function and call it from a remote runtime. Just like python using pickle to de/serialize function/class.


Last updated: Oct 23 2024 at 20:03 UTC