diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-01-29 20:39:31 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-01-29 20:39:31 +0100 |
| commit | f8651619e576ac97209b72660144568b54eb965c (patch) | |
| tree | 6bbf232f2b7444d02ef6cb2192759f628ce2ab8f /examples | |
| parent | 4c9385df1bcc0098c7e1db7a2f7c7e3dbeb00c8b (diff) | |
feat: pre-processed wasm
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rust/src/tinywasm_no_std.rs | 33 | ||||
| -rw-r--r-- | examples/wasm-rust.rs | 18 |
2 files changed, 42 insertions, 9 deletions
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(()) +} diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 96e3419..112222c 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -26,7 +26,7 @@ fn main() -> Result<()> { fn tinywasm() -> Result<()> { const TINYWASM: &[u8] = include_bytes!("./rust/out/tinywasm.wasm"); - let module = Module::parse_bytes(&TINYWASM)?; + let module = Module::parse_bytes(TINYWASM)?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -40,7 +40,7 @@ fn tinywasm() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let hello = instance.exported_func::<(), ()>(&mut store, "hello")?; + let hello = instance.exported_func::<(), ()>(&store, "hello")?; hello.call(&mut store, ())?; Ok(()) @@ -48,7 +48,7 @@ fn tinywasm() -> Result<()> { fn hello() -> Result<()> { const HELLO_WASM: &[u8] = include_bytes!("./rust/out/hello.wasm"); - let module = Module::parse_bytes(&HELLO_WASM)?; + let module = Module::parse_bytes(HELLO_WASM)?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -66,11 +66,11 @@ fn hello() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let arg_ptr = instance.exported_func::<(), i32>(&mut store, "arg_ptr")?.call(&mut store, ())?; + let arg_ptr = instance.exported_func::<(), i32>(&store, "arg_ptr")?.call(&mut store, ())?; let arg = b"world"; instance.exported_memory_mut(&mut store, "memory")?.store(arg_ptr as usize, arg.len(), arg)?; - let hello = instance.exported_func::<i32, ()>(&mut store, "hello")?; + let hello = instance.exported_func::<i32, ()>(&store, "hello")?; hello.call(&mut store, arg.len() as i32)?; Ok(()) @@ -78,7 +78,7 @@ fn hello() -> Result<()> { fn printi32() -> Result<()> { const HELLO_WASM: &[u8] = include_bytes!("./rust/out/print.wasm"); - let module = Module::parse_bytes(&HELLO_WASM)?; + let module = Module::parse_bytes(HELLO_WASM)?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -92,7 +92,7 @@ fn printi32() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + let add_and_print = instance.exported_func::<(i32, i32), ()>(&store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) @@ -100,11 +100,11 @@ fn printi32() -> Result<()> { fn fibonacci() -> Result<()> { const FIBONACCI_WASM: &[u8] = include_bytes!("./rust/out/fibonacci.wasm"); - let module = Module::parse_bytes(&FIBONACCI_WASM)?; + let module = Module::parse_bytes(FIBONACCI_WASM)?; let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; - let fibonacci = instance.exported_func::<i32, i32>(&mut store, "fibonacci")?; + let fibonacci = instance.exported_func::<i32, i32>(&store, "fibonacci")?; let n = 30; let result = fibonacci.call(&mut store, n)?; println!("fibonacci({}) = {}", n, result); |
