I compiled the C code through emscripten into .wasm file and tried calling it through Rust code.
Here is my C code:
so_runtime.h:
typedef struct tagSOCommInfo {
...
} SO_COMM_INFO_S;
unsigned int SORUN_GetSoInstId(void *pThis);
so_runtime.c:
SO_COMM_INFO_S* get_ctx_mgr_head(void *pthis) {
if (pthis == NULL) {
return NULL; // 或者处理错误
}
return (SO_COMM_INFO_S *)((unsigned char *)pthis - sizeof(SO_COMM_INFO_S));
}
unsigned int SORUN_GetSoInstId(void *pThis)
{
if (pThis == 0) {
return 0xFFFFFFFF;
}
SO_COMM_INFO_S *pstCommInfo = get_ctx_mgr_head(pThis);
return (unsigned int)(pstCommInfo->ucInstSeq);
}
Here is my Rust code:
use wasi_common::sync::WasiCtxBuilder;
use wasmtime::*;
use std::ffi::c_void;
fn main() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
wasi_common::sync::add_to_linker(&mut linker, |s| s)?;
let linking1 = Module::from_file(&engine, "so_runtime.wasm")?;
let wasi = WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()?
.build();
let mut store = Store::new(&engine, wasi);
//let memory_ty = MemoryType::new(1, None);
//let memory = Memory::new(&mut store, memory_ty);
let linking2 = Module::new(&engine, "(module (memory (import \"\" \"\") 1))")?;
//let instance = Instance::new(&mut store, &linking2, &[memory.unwrap().into()])?;
let linking2 = linker.instantiate(&mut store, &linking2)?;
linker.instance(&mut store, "env", linking2)?;
let linking1 = linker.instantiate(&mut store, &linking1)?;
let this = std::ptr::null_mut::<c_void>();
let this_ptr = this as i32;
let foo = linking1.get_typed_func::<i32, u32>(&mut store, "SORUN_GetSoInstId")?;
println!("result = {}", foo.call(&mut store, this_ptr)?);
Ok(())
}
wasm can be compiled successfully, but when I use Rust code to run it will report an error, for this error I have tried many ways, but can not solve it, hope someone give me a hint, thank you very much!
Error: unknown import: ::
has not been defined