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 | |
| parent | 67f0fd68f1f60f0629d997d5181e5e06440258d7 (diff) | |
feat: add more examples, work on new public api
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/README.md | 7 | ||||
| -rw-r--r-- | examples/rust/Cargo.toml | 14 | ||||
| -rw-r--r-- | examples/rust/README.md | 5 | ||||
| -rwxr-xr-x | examples/rust/build.sh | 7 | ||||
| -rw-r--r-- | examples/rust/src/fibonacci.rs | 17 | ||||
| -rw-r--r-- | examples/rust/src/tinywasm.rs | 2 | ||||
| -rw-r--r-- | examples/wasm-rust.rs | 19 | ||||
| -rw-r--r-- | examples/wasm/add.wasm | bin | 0 -> 65 bytes |
8 files changed, 62 insertions, 9 deletions
diff --git a/examples/README.md b/examples/README.md index ce47073..94f974b 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,10 +1,10 @@ # Examples -## WasmRust +## Wasm-Rust These are examples using WebAssembly generated from Rust code. -To run these, you first need to build the Rust code into WebAssembly, since the wasm files are not included in the repository to keep it small. -This requires the `wasm32-unknown-unknown` target and `wasm-opt` to be installed (available via Binaryen). +To run these, you first need to build the Rust code, since the resulting wasm files are not included in the repository to keep it small. +This requires the `wasm32-unknown-unknown` target and `wasm-opt` to be installed (available via [Binaryen](https://github.com/WebAssembly/binaryen)). ```bash $ ./examples/rust/build.sh @@ -20,3 +20,4 @@ Where `<example>` is one of the following: - `hello`: A simple example that prints a number to the console. - `tinywasm`: Runs `hello` using TinyWasm - inside of TinyWasm itself! +- `fibonacci`: Calculates the x-th Fibonacci number. diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml index 9ff80dc..d557392 100644 --- a/examples/rust/Cargo.toml +++ b/examples/rust/Cargo.toml @@ -1,5 +1,8 @@ cargo-features=["per-package-target"] +# treat this as an independent package +[workspace] + [package] publish=false name="rust-wasm-examples" @@ -16,3 +19,14 @@ path="src/hello.rs" [[bin]] name="tinywasm" path="src/tinywasm.rs" + +[[bin]] +name="fibonacci" +path="src/fibonacci.rs" + +[profile.wasm] +opt-level="s" +lto="thin" +codegen-units=1 +panic="abort" +inherits="release" diff --git a/examples/rust/README.md b/examples/rust/README.md index 8ecac52..1b6be2f 100644 --- a/examples/rust/README.md +++ b/examples/rust/README.md @@ -1 +1,4 @@ -# Examples using Rust compiled to WebAssembly +# WebAssembly Rust Examples + +This is a seperate crate that generates WebAssembly from Rust code. +It is used by the `wasm-rust` example. diff --git a/examples/rust/build.sh b/examples/rust/build.sh index 2c8069a..a8c587a 100755 --- a/examples/rust/build.sh +++ b/examples/rust/build.sh @@ -1,11 +1,14 @@ #!/usr/bin/env bash cd "$(dirname "$0")" -bins=("hello" "tinywasm") +bins=("hello" "tinywasm" "fibonacci") exclude_wat=("tinywasm") -out_dir="../../target/wasm32-unknown-unknown/wasm" +out_dir="./target/wasm32-unknown-unknown/wasm" dest_dir="out" +# ensure out dir exists +mkdir -p "$dest_dir" + for bin in "${bins[@]}"; do cargo build --target wasm32-unknown-unknown --package rust-wasm-examples --profile=wasm --bin "$bin" 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(()) } diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 3b8877e..3b26863 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -13,6 +13,7 @@ fn main() -> Result<()> { match args[1].as_str() { "hello" => hello()?, + "fibonacci" => fibonacci()?, "tinywasm" => tinywasm()?, _ => {} } @@ -36,7 +37,7 @@ fn tinywasm() -> Result<()> { )?; let instance = module.instantiate(&mut store, Some(imports))?; - let hello = instance.typed_func::<(), ()>(&mut store, "hello")?; + let hello = instance.exported_func::<(), ()>(&mut store, "hello")?; hello.call(&mut store, ())?; Ok(()) @@ -58,8 +59,22 @@ fn hello() -> 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(()) } + +fn fibonacci() -> Result<()> { + const FIBONACCI_WASM: &[u8] = include_bytes!("./rust/out/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 n = 30; + let result = fibonacci.call(&mut store, n)?; + println!("fibonacci({}) = {}", n, result); + + Ok(()) +} diff --git a/examples/wasm/add.wasm b/examples/wasm/add.wasm Binary files differnew file mode 100644 index 0000000..92e3432 --- /dev/null +++ b/examples/wasm/add.wasm |
