I have a general question regarding wasmtime. Can I call a funtion by its index in the function section in the wasm module file?
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).
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.
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
thanks alex, do you have some online document about this?
I do not, sorry
np thanks
Wilson Wang has marked this topic as resolved.
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.
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: Dec 23 2024 at 13:07 UTC