From 8477bff3278ba76c12e39e5f911701d25cd31f1f Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 19 Mar 2024 15:52:01 +0100 Subject: chore: dynamically load wasm examples Signed-off-by: Henry Gressmann --- examples/wasm-rust.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'examples/wasm-rust.rs') 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::>(); if args.len() < 2 { println!("Usage: cargo run --example wasm-rust "); @@ -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)?; -- cgit v1.3.1