Stream: wasmtime

Topic: fixing wasm that doesn't preserve states


view this post on Zulip Hanif Ariffin (Jan 31 2023 at 15:33):

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

Signed-off-by: Hanif Ariffin hanif.ariffin.4326@gmail.com Added an entry to CHANGELOG.md if this change could be valuable to users

view this post on Zulip Pat Hickey (Feb 01 2023 at 00:15):

I'm not quite sure I understand the problem, but the unit of mutable state for wasmtime is the Store

view this post on Zulip Pat Hickey (Feb 01 2023 at 00:18):

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

view this post on Zulip Hanif Ariffin (Feb 02 2023 at 01:08):

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??

view this post on Zulip Hanif Ariffin (Feb 02 2023 at 01:26):

maybe this is relevant? https://github.com/bytecodealliance/wasmtime-py/issues/74#issuecomment-965536330

I'm not having any success getting environment variables in the WASM environment. My code is below. There is a function written in C compiled to WASM that does a getenv("FOO") which i...

view this post on Zulip Hanif Ariffin (Feb 02 2023 at 01:40):

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();

view this post on Zulip bjorn3 (Feb 02 2023 at 09:03):

_initialize should already be called by wasmtime when instantiating.

view this post on Zulip bjorn3 (Feb 02 2023 at 09:04):

It is possible that somewhere in wasi-libc or rust's libstd some initialization is missed for the reactor case.

view this post on Zulip Hanif Ariffin (Feb 02 2023 at 11:59):

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

Contribute to lapce/lapce-rust development by creating an account on GitHub.

view this post on Zulip bjorn3 (Feb 02 2023 at 12:10):

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.

view this post on Zulip Hanif Ariffin (Feb 02 2023 at 12:52):

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

Contribute to lapce/lapce-rust development by creating an account on GitHub.
Contribute to lapce/lapce-plugin-rust development by creating an account on GitHub.

Last updated: Dec 23 2024 at 13:07 UTC