From f8651619e576ac97209b72660144568b54eb965c Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Mon, 29 Jan 2024 20:39:31 +0100 Subject: feat: pre-processed wasm Signed-off-by: Henry Gressmann --- examples/rust/src/tinywasm_no_std.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 examples/rust/src/tinywasm_no_std.rs (limited to 'examples/rust/src/tinywasm_no_std.rs') diff --git a/examples/rust/src/tinywasm_no_std.rs b/examples/rust/src/tinywasm_no_std.rs new file mode 100644 index 0000000..9f2b28c --- /dev/null +++ b/examples/rust/src/tinywasm_no_std.rs @@ -0,0 +1,33 @@ +#![no_main] +#![no_std] +use tinywasm::{Extern, FuncContext}; + +#[link(wasm_import_module = "env")] +extern "C" { + fn printi32(x: i32); +} + +#[no_mangle] +pub extern "C" fn hello() { + let _ = run(); +} + +fn run() -> tinywasm::Result<()> { + let module = tinywasm::Module::parse_bytes(include_bytes!("../out/print.wasm"))?; + let mut store = tinywasm::Store::default(); + let mut imports = tinywasm::Imports::new(); + + imports.define( + "env", + "printi32", + Extern::typed_func(|_: FuncContext<'_>, v: i32| { + unsafe { printi32(v) } + Ok(()) + }), + )?; + let instance = module.instantiate(&mut store, Some(imports))?; + + let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + add_and_print.call(&mut store, (1, 2))?; + Ok(()) +} -- cgit v1.3.1