I spent the last few weeks migrating from wasmi to wasmtime, and now seem to have hit a blocker. In AssemblyScript, the start function is used to initialize globals. The globals might call a host function, and that host function might then call an import from the module. But when I try it wasmtime time says the import is not found. What are the rules for what a host function called in start can do?
Do you have the example?
Hi, I don't have a minimal example. Do you need a minimal reproduction case?
CC @Aaron Turner
I now understand my mistake. I tried to use Caller::get_export
to get a function, but that only works for memories at the moment. Would it be straight forward to allow that to work for functions? I can write a patch for that.
Added in https://github.com/bytecodealliance/wasmtime/pull/1290 , only memory was needed for wasi use at that moment
The ability to obtain exports from the caller of a function is a temporary mechanism. We need it for WASI-like situations where it's necessary to grab the "memory" export of the caller in order to dereference pointers it passes, however once we have Interface Types and WASI calls, that won't be needed anymore
The current roadmap is that non-JS engines won't support cyclic dependencies, so if AssemblyScript supports cyclic dependencies, it'll need to introduce indirection, such as using call_indirect, to break the cycle.
I don't totally understand why this is considered a cyclic dependency, is it what happens with start
-> host function -> module function nested calls? I really need this at the moment, is there no hope of that working because the plan is to not support this?
To see if I understand, it's start
(in module A) -> host function -> module funciton (in module A)?
In which case, the module imports from the host, and the host imports from the module.
yes, is that generally wrong? I thought bi-directional communication was generally ok, we certainly use it all the time in our application.
Bidirectional communication per se is fine; it's bidirectional imports that are the issue
Specifically in this case, the memory allocator is implemented in WASM, so the host calls to it when it needs to allocate.
so there is a better way to communicate that's not imports? Or that will be interface types, when it exists?
In principle, AssemblyScript should factor out its runtime into a separate module, as described here: https://github.com/AssemblyScript/assemblyscript/issues/1301#issuecomment-635599053
I'll need to read up on the background. If this is in principle wrong, then my project is deep in wrong patterns, and I don't have a choice in the short term. I hacked in support for functions in Caller::get_exports
, and it worked. Are you interested in a PR or should I keep this in a fork?
I think I'd prefer if you keep it on your side.
I can file a bug on the AssemblyScript repo to help start the conversation there though
I'll file an initial issue, and if you wanted to follow up with your use case as an example, that'd be great!
Alright we'll maintain a fork. If AS is the right place to discuss this, then lets take it there.
Ok, I've now filed https://github.com/AssemblyScript/assemblyscript/issues/1333
If you wanted to describe your example there, that'd help!
Thanks! The AS maintainers are probably aware that this (anti)pattern is common, not sure I have much to add this point, but I'll subscribe to the issue and chime in if necessary.
Thanks for the mention @Till Schneidereit :) I had Friday off, so sorry for the delay, didn't see this come in.
And thank you @Dan Gohman for kicking off the discussion over in the AS repo! I'm not super familiar with the runtime internals, but it seems like dcodeIO is looking into it :)
Last updated: Nov 22 2024 at 16:03 UTC