I want to pass binary data between rust and c/webassembly.
use std::ffi::c_void;
use std::ptr;
#[repr(C)]
pub struct Buffer {
ptr: *const c_void,
len: usize,
}
#[link(wasm_import_module = "demo")]
extern "system" {
#[link_name = "test"]
fn test() -> Buffer;
#[link_name = "test2"]
fn test2(_: Buffer);
#[link_name = "test3"]
fn test3(_: i32) -> i32;
}
#[repr(C)]
pub struct Bridge {
pub test: unsafe extern "system" fn() -> Buffer,
pub test2: unsafe extern "system" fn(_: Buffer),
pub test3: unsafe extern "system" fn(_: i32) -> i32,
}
#[cfg(target_arch = "wasm32")]
pub fn init_bridge(_: *const c_void) -> Bridge {
Bridge {
test,
test2,
test3,
}
}
#[cfg(not(target_arch = "wasm32"))]
fn init_bridge(ptr: *const c_void) -> Box<Bridge> {
let size = std::mem::size_of::<Bridge>();
let mut data = vec![0u8; size];
let target = data.as_mut_ptr();
std::mem::forget(data);
unsafe {
std::ptr::copy(ptr as _, target, size);
Box::from_raw(target as *mut Bridge)
}
}
#[no_mangle]
extern "system" fn run() {
init_bridge(ptr::null());
}
This code works fine in native, but in wasm the imported function type is not right.
import - ImportType { name: ImportName { module: "demo", field: "test" }, ty: Func(FuncType { params: [I32], results: [] }) }
import - ImportType { name: ImportName { module: "demo", field: "test2" }, ty: Func(FuncType { params: [I32], results: [] }) }
import - ImportType { name: ImportName { module: "demo", field: "test3" }, ty: Func(FuncType { params: [I32], results: [I32] }) }
My program needs some injection functions to work. And the program can be run in native or webassembly with the same code and just difference target to compile.
I wonder how to pass repr(C) struct with webassembly? Thanks