| 101 | } |
| 102 | |
| 103 | fn hello() -> Result<()> { |
| 104 | let module = tinywasm::parse_file("./examples/rust/out/hello.opt.wasm")?; |
| 105 | let mut store = Store::default(); |
| 106 | |
| 107 | let print_utf8 = HostFunction::from(&mut store, |ctx: FuncContext<'_>, (ptr, len): (i64, i32)| { |
| 108 | let mem = ctx.memory("memory")?; |
| 109 | let string = mem.read_string(ctx.store(), ptr as usize, len as usize)?; |
| 110 | println!("{string}"); |
| 111 | Ok(()) |
| 112 | }); |
| 113 | |
| 114 | let mut imports = Imports::new(); |
| 115 | imports.define("env", "print_utf8", print_utf8); |
| 116 | |
| 117 | let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?; |
| 118 | let arg_ptr = instance.func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; |
| 119 | let arg = b"world"; |
| 120 | |
| 121 | instance.memory("memory")?.copy_from_slice(&mut store, arg_ptr as usize, arg)?; |
| 122 | let hello = instance.func::<i32, ()>(&store, "hello")?; |
| 123 | hello.call(&mut store, arg.len() as i32)?; |
| 124 | |
| 125 | Ok(()) |
| 126 | } |
| 127 | |
| 128 | fn host_fn() -> Result<()> { |
| 129 | let module = tinywasm::parse_file("./examples/rust/out/host_fn.opt.wasm")?; |