Skip to content

Commit 8477bff

Browse files
chore: dynamically load wasm examples
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 0302008 commit 8477bff

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

Cargo.lock

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/wasm-rust.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use color_eyre::eyre::Result;
1+
use color_eyre::eyre::{eyre, Result};
22
use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store};
33

44
/// Examples of using WebAssembly compiled from Rust with tinywasm.
@@ -20,6 +20,10 @@ use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store};
2020
fn main() -> Result<()> {
2121
pretty_env_logger::init();
2222

23+
if !std::path::Path::new("./examples/rust/out/").exists() {
24+
return Err(eyre!("No WebAssembly files found. See examples/wasm-rust.rs for instructions."));
25+
}
26+
2327
let args = std::env::args().collect::<Vec<_>>();
2428
if args.len() < 2 {
2529
println!("Usage: cargo run --example wasm-rust <rust_example>");
@@ -54,8 +58,7 @@ fn main() -> Result<()> {
5458
}
5559

5660
fn tinywasm() -> Result<()> {
57-
const TINYWASM: &[u8] = include_bytes!("./rust/out/tinywasm.wasm");
58-
let module = Module::parse_bytes(TINYWASM)?;
61+
let module = Module::parse_file("./examples/rust/out/tinywasm.wasm")?;
5962
let mut store = Store::default();
6063

6164
let mut imports = Imports::new();
@@ -76,8 +79,7 @@ fn tinywasm() -> Result<()> {
7679
}
7780

7881
fn hello() -> Result<()> {
79-
const HELLO_WASM: &[u8] = include_bytes!("./rust/out/hello.wasm");
80-
let module = Module::parse_bytes(HELLO_WASM)?;
82+
let module = Module::parse_file("./examples/rust/out/hello.wasm")?;
8183
let mut store = Store::default();
8284

8385
let mut imports = Imports::new();
@@ -106,8 +108,7 @@ fn hello() -> Result<()> {
106108
}
107109

108110
fn printi32() -> Result<()> {
109-
const HELLO_WASM: &[u8] = include_bytes!("./rust/out/print.wasm");
110-
let module = Module::parse_bytes(HELLO_WASM)?;
111+
let module = Module::parse_file("./examples/rust/out/print.wasm")?;
111112
let mut store = Store::default();
112113

113114
let mut imports = Imports::new();
@@ -128,8 +129,7 @@ fn printi32() -> Result<()> {
128129
}
129130

130131
fn fibonacci() -> Result<()> {
131-
const FIBONACCI_WASM: &[u8] = include_bytes!("./rust/out/fibonacci.wasm");
132-
let module = Module::parse_bytes(FIBONACCI_WASM)?;
132+
let module = Module::parse_file("./examples/rust/out/fibonacci.wasm")?;
133133
let mut store = Store::default();
134134

135135
let instance = module.instantiate(&mut store, None)?;

0 commit comments

Comments
 (0)