Stream: general

Topic: ✔ Importing host functions


view this post on Zulip JMS (Jun 20 2022 at 21:53):

I see that I can provide a list of Externs generated from Funcs, but how do I specify the name of each function? Otherwise, how will the wasm module know what to call? I'm looking for the equivalent of this https://docs.wasmer.io/integrations/examples/host-functions#declaring-functions-and-imports

view this post on Zulip Dan Gohman (Jun 20 2022 at 21:56):

If you're using an API like Instance::new which takes the externs as &[Extern], they're linked to the module's imports in the order that they appear in the module.

view this post on Zulip JMS (Jun 20 2022 at 21:58):

Oh. I don't think I can control that - the module is generated from Rust, which has a bunch of scattered extern "C" functions all over the place.

view this post on Zulip Dan Gohman (Jun 20 2022 at 21:58):

For a higher-level API that links things by name see the Linker API

view this post on Zulip JMS (Jun 20 2022 at 21:59):

Oh nice, ty!

view this post on Zulip Dan Gohman (Jun 20 2022 at 22:01):

https://docs.wasmtime.dev/examples-rust-linking.html has an example using that API, in case it helps.

view this post on Zulip JMS (Jun 20 2022 at 22:04):

Yep, figured it out, ty!

view this post on Zulip JMS (Jun 20 2022 at 22:26):

Do you know how I can wrap this function? I don't think it likes that it borrows Bay.

fn setup_linker(engine: &Engine, bay: &Bay) -> Result<Linker<StoreData>, Box<dyn Error>> {
    let mut linker = Linker::new(engine);

    linker.func_wrap(
        "host",
        "__move_towards",
        |mut caller: Caller<'_, StoreData>| match caller.data().bot_action {
            None => {
                let action_result = bay.can_move_towards()?;
                caller.data_mut().bot_action = Some(BotAction::MoveTowards);
                Ok(action_result)
            }
            _ => Err(ActionError::AlreadyActed),
        },
    )?;

    Ok(linker)
}

view this post on Zulip JMS (Jun 20 2022 at 22:29):

Trying to store &'a Bay in my StoreData didn't work either :/

view this post on Zulip JMS (Jun 20 2022 at 22:53):

Nvm, was an issue with my return type

view this post on Zulip Notification Bot (Jun 21 2022 at 00:51):

JMS has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC