slowli / externref

Low-cost reference type shims for WASM modules
Apache License 2.0
19 stars 2 forks source link

Getting error "When linking module: "externref" , function name: "insert"" #109

Open IgorKhomenko opened 8 months ago

IgorKhomenko commented 8 months ago

I have a rust module defined like this

pub mod funcs {
  pub struct Arena(());

  // use wasmedge_bindgen_macro::*;
  use externref::{externref, Resource};

  // #[wasmedge_bindgen]
  #[no_mangle]
  #[externref]
  pub extern "C" fn call_add(arena: &Resource<Arena>, val1: i32, val2: f32) {

  }
}

and compile it like this cargo build --target wasm32-wasi --releas

Then, I try to use this module in a host app written in C, but when I compile and run it via gcc main.c -lwasmedge && ./a.out, it produces an error:

% gcc main.c -lwasmedge && ./a.out
[2024-02-23 15:10:08.146] [error] instantiation failed: unknown import, Code: 0x62
[2024-02-23 15:10:08.147] [error]     When linking module: "externref" , function name: "insert"
[2024-02-23 15:10:08.147] [error]     At AST node: import description
[2024-02-23 15:10:08.147] [error]     At AST node: import section
[2024-02-23 15:10:08.147] [error]     At AST node: module

Is there any additional things I need to do to make it works?

slowli commented 8 months ago

As specified in the externref crate readme, you must post-process compiled WASM either programmatically or using the CLI tool. This will replace surrogate imports like externref::insert you're having problems with, with local functions; see here for a more detailed description of what the processor does.