-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Memory export API of an instance #94
Comments
Currently, there isn't an API like that in the Rust SDK. We are planning to introduce some new APIs. Could you please tell me about your use case? |
Thank you for your response! Here's an example of our use case and the implementation we’ve worked on so far: Wasm Module ExampleWe are working with Wasm modules that export functions like (module
(import "env" "addToCrc" (func $addToCrc (param i32)))
(memory $mem 1)
(export "_memory" (memory $mem))
(func $main
(export "_main")
(result i32)
i32.const 42)) you can see the working wasmtime example: pub fn run_wasmtime(args: Cli) -> anyhow::Result<u32> {
let wasm_bytes = fs::read(args.program)?;
let engine =
if args.should_optimize {
Engine::new(Config::new().cranelift_opt_level(OptLevel::SpeedAndSize))?
} else {
Engine::new(Config::new().cranelift_opt_level(OptLevel::None))?
};
let module = Module::new(&engine, wasm_bytes)?;
let shared_digest: Arc<Mutex<Option<crc::Digest<u32>>>> = Arc::new(Mutex::new(Some(CRC_ALGORITHM.digest())));
let mut store = Store::new(
&engine,
Env { digest: shared_digest.clone() }
);
let add_to_crc_func = Func::wrap(&mut store, |caller: Caller<'_, Env>, val: i32| {
add_to_crc(caller.data(), val);
});
let import_object = [add_to_crc_func.into()];
let instance = Instance::new(&mut store, &module, &import_object)?;
let main_func = instance.get_typed_func::<(), i32>(&mut store, "_main")?;
let crc_globals_func = instance.get_typed_func::<(), ()>(&mut store, "_crc_globals")?;
let memory = instance.get_memory(&mut store, "_memory").unwrap();
let main_result: i32 = main_func.call(&mut store, ())?;
add_to_crc(&Env { digest: shared_digest.clone() }, main_result);
crc_globals_func.call(&mut store, ())?;
let mem_ptr: *mut i32 = memory.data_ptr(&store) as *mut i32;
let mem_size = (memory.data_size(&store) / 4) as isize;
for address in 0..mem_size {
let mem_value: i32;
unsafe {
mem_value = *mem_ptr.offset(address) as i32;
}
add_to_crc(&Env { digest: shared_digest.clone() }, mem_value);
}
Ok(get_crc(&Env { digest: shared_digest.clone() }))
} |
I guess WAMR has APIs ( let main_func = instance.get_typed_func::<(), i32>(&mut store, "_main")?;
let crc_globals_func = instance.get_typed_func::<(), ()>(&mut store, "_crc_globals")?;
let memory = instance.get_memory(&mut store, "_memory").unwrap(); |
Hi,
is there any API to export memory of an instance using wamr-rust-sdk?
Like wasmtime does: let memory = instance.get_memory(&mut store, "_memory").unwrap();
or WasmEdge: module_instance.get_memory_ref("_memory") I would expect something like :find_export_mem like find_export_func.
thanks a lot
The text was updated successfully, but these errors were encountered: