David Alsh
David Alsh

Reputation: 7609

Build a wasm module from Rust to be used as a plugin for a Rust process reading it with Wasmer

I have a Rust project that wants to consume plugins distributed as wasm modules. The guides I see online are largely for running building/running wasm modules on the web and not exactly my use case.

I have two projects. The first is the library:

// lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(left: i32, right: i32) -> i32 {
    left + right
}
[package]
name = "plugin"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.89"

built with:

rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

And the other is the runner:

// main.rs
use wasmer::{Store, Module, Instance, Value, imports};

fn main() {
    let bin = std::fs::read("/path/to/target/wasm32-unknown-unknown/debug/plugin.wasm").unwrap();
    let mut store = Store::default();
    
    let module = Module::from_binary(&store, &bin).expect("Should compile");

    let import_object = imports! {};
    let instance = Instance::new(&mut store, &module, &import_object).expect("Should create instance");

    let add = instance.exports.get_function("add").expect("Should get function");
    let result = add.call(&mut store, &[Value::I32(5), Value::I32(5)]).expect("Should call add");
    let value = result[0].i32();

    println!("Result: {:?}", value);
}
[package]
name = "core"

[dependencies]
wasmer = "4.2.5"

Run with

cargo run

However the runner panics on Instance::new with:

Link(Import("__wbindgen_placeholder__", "__wbindgen_describe", UnknownImport(Function(FunctionType { params: [I32], results: [] }))))

Any pointers on what I am doing wrong?

Upvotes: 1

Views: 297

Answers (0)

Related Questions