diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-01-26 15:16:05 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-01-26 15:16:05 +0100 |
| commit | d8d439e6401607fab18feb5025f3b91f135e3a50 (patch) | |
| tree | f20f1d5961a5b0810cceca575a206ad45d2a3a9c /examples/rust/src | |
| parent | 67f0fd68f1f60f0629d997d5181e5e06440258d7 (diff) | |
feat: add more examples, work on new public api
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples/rust/src')
| -rw-r--r-- | examples/rust/src/fibonacci.rs | 17 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm.rs | 2 |
2 files changed, 18 insertions, 1 deletions
diff --git a/examples/rust/src/fibonacci.rs b/examples/rust/src/fibonacci.rs new file mode 100644 index 0000000..7493132 --- /dev/null +++ b/examples/rust/src/fibonacci.rs @@ -0,0 +1,17 @@ +#![no_std] +#![no_main] + +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + core::arch::wasm32::unreachable() +} + +#[no_mangle] +// The rust compiler will convert this to an iterative algorithm. +pub extern "C" fn fibonacci(n: i32) -> i32 { + if n <= 1 { + return n; + } + fibonacci(n - 1) + fibonacci(n - 2) +} diff --git a/examples/rust/src/tinywasm.rs b/examples/rust/src/tinywasm.rs index 0f18ab9..0fb9261 100644 --- a/examples/rust/src/tinywasm.rs +++ b/examples/rust/src/tinywasm.rs @@ -26,7 +26,7 @@ fn run() -> tinywasm::Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let add_and_print = instance.typed_func::<(i32, i32), ()>(&mut store, "add_and_print")?; + let add_and_print = instance.exported_func::<(i32, i32), ()>(&mut store, "add_and_print")?; add_and_print.call(&mut store, (1, 2))?; Ok(()) } |
