I can't find a Q&A streams so please move this thread to wherever is appropriate!
So lapce uses wasmtime to manage their plugins and this is the function that compiles/starts a wasm app from a wasm file https://github.com/lapce/lapce/pull/2090
however, it had an issue where the wasm blob does not appear to have a persistent state (my plugin, written in rust, had a OnceCell that gets reset with every function call)
so i proposed a fix here https://github.com/lapce/lapce/pull/2090
can someone review the fix and/or the function it self?
I know that the Engine can probably be extracted out into a lazy static or smth like that
I'm not quite sure I understand the problem, but the unit of mutable state for wasmtime is the Store
if you want to persist state, you will need to make an Instance on your Store, then get the handle_rpc func out of that Instance, and then anytime you do handle_rpc.call(&mut store)
it will be the same state underneath as the store had at the end of the last call
Okay I read up a bit on WASI and the difference was basically that there are 2 types of WASM: Reactor and Command. Before, I was using it as a Command but now that I instantiate it, I should have a Reactor instead. But now I come into the problem of the environment variable not being set?
I have something like this in Lapce
let wasi = WasiCtxBuilder::new()
.env("VOLT_OS", std::env::consts::OS)?
.env("VOLT_ARCH", std::env::consts::ARCH)?
.env("VOLT_LIBC", volt_libc)?
.env(
"VOLT_URI",
Url::from_directory_path(volt_path)
.map_err(|_| anyhow!("can't convert folder path to uri"))?
.as_ref(),
)?
.stdin(Box::new(wasi_common::pipe::ReadPipe::from_shared(
stdin.clone(),
)))
.stdout(Box::new(wasi_common::pipe::WritePipe::from_shared(
stdout.clone(),
)))
.stderr(Box::new(wasi_common::pipe::WritePipe::from_shared(
stderr.clone(),
)))
.preopened_dir(
wasmtime_wasi::Dir::open_ambient_dir(
volt_path,
wasmtime_wasi::ambient_authority(),
)?,
"/",
)?
.build();
let mut store = wasmtime::Store::new(&engine, wasi);
and then in the wasm plugin I have
format!("envs:{:?}", std::env::vars().collect::<Vec<_>>())
but it is empty??
maybe this is relevant? https://github.com/bytecodealliance/wasmtime-py/issues/74#issuecomment-965536330
so it seems that I should call an exported function called _initialize
but its not there? this crashes the thread
let initialize = instance
.get_typed_func::<(), (), _>(&mut store, "_initialize")
.unwrap();
_initialize should already be called by wasmtime when instantiating.
It is possible that somewhere in wasi-libc or rust's libstd some initialization is missed for the reactor case.
but i dont have a reactor, i have a command. How do i compile my plugin to make sure that it gets compiled as a reactor? https://github.com/lapce/lapce-rust.git
If the plugin is written in rust you can use the unstable -Zwasi-exec-model=reactor
. If it is written in C I believe the flag to use is -fexec-model=reactor
but I might be wrong about that one.
it is written in Rust. See https://github.com/lapce/lapce-rust/blob/master/src/main.rs#L31
which expands like so https://github.com/lapce/lapce-plugin-rust/blob/master/src/lib.rs#L99-L125
Last updated: Nov 22 2024 at 16:03 UTC