diff options
| author | Henry Gressmann <mail@henrygressmann.de> | 2024-03-19 15:52:01 +0100 |
|---|---|---|
| committer | Henry Gressmann <mail@henrygressmann.de> | 2024-03-19 15:52:01 +0100 |
| commit | 8477bff3278ba76c12e39e5f911701d25cd31f1f (patch) | |
| tree | 2acd4cf6055cf74f583ab7fcd4a19992621bdd3c /examples | |
| parent | 03020080366241f1c5b610546d3e17687232eb30 (diff) | |
chore: dynamically load wasm examples
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/wasm-rust.rs | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index 9b057f1..921c4a3 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::Result; +use color_eyre::eyre::{eyre, Result}; use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store}; /// Examples of using WebAssembly compiled from Rust with tinywasm. @@ -20,6 +20,10 @@ use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store}; fn main() -> Result<()> { pretty_env_logger::init(); + if !std::path::Path::new("./examples/rust/out/").exists() { + return Err(eyre!("No WebAssembly files found. See examples/wasm-rust.rs for instructions.")); + } + let args = std::env::args().collect::<Vec<_>>(); if args.len() < 2 { println!("Usage: cargo run --example wasm-rust <rust_example>"); @@ -54,8 +58,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_file("./examples/rust/out/tinywasm.wasm")?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -76,8 +79,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_file("./examples/rust/out/hello.wasm")?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -106,8 +108,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_file("./examples/rust/out/print.wasm")?; let mut store = Store::default(); let mut imports = Imports::new(); @@ -128,8 +129,7 @@ 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_file("./examples/rust/out/fibonacci.wasm")?; let mut store = Store::default(); let instance = module.instantiate(&mut store, None)?; |
