Just to ask a question about what should be done for wasm configuration: My use case is that I have a wasm module that I load with wasmtime, and then call various functions of it as hooks. IIUC, it means that my module is semantically a "reactor".
I'm compiling this module from Rust code. So I came upon https://github.com/rust-lang/rust/pull/79997 .
However, I couldn't make it work in practice, be it from my program or using wasmtime --invoke. It looks to me, like rustc doesn't actually create the _initialize function. (I posted more details on the PR thread itself)
Is what I'm looking for actually wasm reactors? And if yes, am I doing something wrong there, or is it just that rustc is currently unable to compile a WASI-able reactor? (The only idea for a workaround I'd have would be to make my thing not a reactor but a command, and have it just call a "dothething" function in the host that'd then do all the callbacks, thus ensuring that datastructures are initialized by virtue of being in the wasm's main scope... But that sounds awful)
At first glance, it does sound like what you're doing is a reactor. I think the problem is that Rust needs to switch to crt1-reactor.o (instead of crt1.o), but I haven't dug into it yet.
Doing so may also require an update to a newer WASI libc
Thank you for your feedback! According to a recent message on the PR, it looks like I did something wrong in the compilation process — I've documented the procedure I took to make things work on there :)
Ah, yes, using --entry _initialize means some things may work without crt1-initialize.o
er, crt1-reactor.o
Thanks for helping test this and figure out something that works! Hopefully in the future we'll fix the pub fn main
requirement and generally smooth out the process :smile:
:heart:
Ah, also while I have this topic paged in here, _initialize
is called automatically if you use wasmtime's Linker::module
function
And not otherwise. When we added support for that, there were differing opinions about whether we should always run _initialize on any instantiate
call, and we ended up deciding not to. But this may evolve as our understanding of what a reactor is evolves.
Ooh right I missed it. That said there's something I don't get about Linker::module: usually the Command/Reactor would be the last thing one would want to add to a linker... yet it looks to me like after Linker::module one has to run Linker::instantiate again with... another module?
After Linker::module
, the reactor should be instantiated and its exports available for get
etc.
IOW, it would sound more logical to me if Linker::module did not do reactor initialization, but Linker::instantiate did (or allowed to do)... but I may be missing something?
Oooh you're running .get() directly on the Linker, and not on an Instance?
Hmm I guess no it doesn't have the necessary things for that
when you're using a Linker
, it generally owns the instances, and you can do get
to get their exports
Hmm the thing I'm wondering about is, to get an Instance from a Linker, I see only Linker::instantiate; which requires giving another Module?
or, actually, that's not quite right. It doesn't own the instances. But it does collect all the exports and makes them available
the linker doesn't generally hand out instances; it hands out exports.
Oooh I misread the signature of Linker::get ._.
Ok so if I understand correctly, Linker::instantiate should very rarely be used?
I think it comes down to whether you want a lower-level interface where you have more control, or a higher-level interface that does more things for you
instantiate
directly corresponds to an operation in the wasm spec, so if you know that that's exactly what you want, it's there
Hmm ok thank you! I've just checked and was indeed able to get things working this way :) So I've repurposed https://github.com/bytecodealliance/wasmtime/issues/2580 for the documentation issue. Thank you! :D
Last updated: Dec 23 2024 at 12:05 UTC