Stream: git-wasmtime

Topic: wasmtime / issue #3356 [help] import func


view this post on Zulip Wasmtime GitHub notifications bot (Sep 16 2021 at 10:40):

silence-coding opened issue #3356:

How to import a function like log_str that can pass complex structures

    /// # fn main() -> anyhow::Result<()> {
    /// # let engine = Engine::default();
    /// let mut linker = Linker::new(&engine);
    /// linker.func_wrap("host", "double", |x: i32| x * 2)?;
    /// linker.func_wrap("host", "log_i32", |x: i32| println!("{}", x))?;
    /// linker.func_wrap("host", "log_str", |caller: Caller<'_, ()>, ptr: i32, len: i32| {
    ///     // ...
    /// })?;

view this post on Zulip Wasmtime GitHub notifications bot (Sep 16 2021 at 10:49):

bjorn3 commented on issue #3356:

Wasm only allows i32, i64, f32 and f64 as parameter types. It doesn't support structs or anything like that. Instead more complex types need to be stored in the linear memory of the wasm module. You can use witx-bindgen to help generate bindings on both the host and guest side that handle this for you.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 16 2021 at 12:21):

silence-coding commented on issue #3356:

@bjorn3 Thank you very much. It's very useful.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 16 2021 at 12:21):

silence-coding closed issue #3356:

How to import a function like log_str that can pass complex structures

    /// # fn main() -> anyhow::Result<()> {
    /// # let engine = Engine::default();
    /// let mut linker = Linker::new(&engine);
    /// linker.func_wrap("host", "double", |x: i32| x * 2)?;
    /// linker.func_wrap("host", "log_i32", |x: i32| println!("{}", x))?;
    /// linker.func_wrap("host", "log_str", |caller: Caller<'_, ()>, ptr: i32, len: i32| {
    ///     // ...
    /// })?;


Last updated: Nov 22 2024 at 16:03 UTC