1
- use color_eyre:: eyre:: Result ;
1
+ use color_eyre:: eyre:: { eyre , Result } ;
2
2
use tinywasm:: { Extern , FuncContext , Imports , MemoryStringExt , Module , Store } ;
3
3
4
4
/// Examples of using WebAssembly compiled from Rust with tinywasm.
@@ -20,6 +20,10 @@ use tinywasm::{Extern, FuncContext, Imports, MemoryStringExt, Module, Store};
20
20
fn main ( ) -> Result < ( ) > {
21
21
pretty_env_logger:: init ( ) ;
22
22
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
+
23
27
let args = std:: env:: args ( ) . collect :: < Vec < _ > > ( ) ;
24
28
if args. len ( ) < 2 {
25
29
println ! ( "Usage: cargo run --example wasm-rust <rust_example>" ) ;
@@ -54,8 +58,7 @@ fn main() -> Result<()> {
54
58
}
55
59
56
60
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" ) ?;
59
62
let mut store = Store :: default ( ) ;
60
63
61
64
let mut imports = Imports :: new ( ) ;
@@ -76,8 +79,7 @@ fn tinywasm() -> Result<()> {
76
79
}
77
80
78
81
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" ) ?;
81
83
let mut store = Store :: default ( ) ;
82
84
83
85
let mut imports = Imports :: new ( ) ;
@@ -106,8 +108,7 @@ fn hello() -> Result<()> {
106
108
}
107
109
108
110
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" ) ?;
111
112
let mut store = Store :: default ( ) ;
112
113
113
114
let mut imports = Imports :: new ( ) ;
@@ -128,8 +129,7 @@ fn printi32() -> Result<()> {
128
129
}
129
130
130
131
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" ) ?;
133
133
let mut store = Store :: default ( ) ;
134
134
135
135
let instance = module. instantiate ( & mut store, None ) ?;
0 commit comments