MCPcopy Index your code
hub / github.com/explodingcamera/tinywasm / hello

Function hello

examples/wasm-rust.rs:103–126  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

101}
102
103fn 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
128fn host_fn() -> Result<()> {
129 let module = tinywasm::parse_file("./examples/rust/out/host_fn.opt.wasm")?;

Callers 2

mainFunction · 0.70
test_helloFunction · 0.70

Calls 8

parse_fileFunction · 0.85
read_stringMethod · 0.80
defineMethod · 0.80
callMethod · 0.80
memoryMethod · 0.45
storeMethod · 0.45
copy_from_sliceMethod · 0.45
lenMethod · 0.45

Tested by 1

test_helloFunction · 0.56