diff options
| author | Henry <mail@henrygressmann.de> | 2026-08-10 16:21:47 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-08-10 16:21:47 +0200 |
| commit | 70c5bb2e4816f7ee458a6180845af3bd6b1d2d1d (patch) | |
| tree | efaff316f2fb7a84d641faf70c20d49ead3c0cbc /examples | |
| parent | 666c7f59301acc9fcef10ac2da5beb464e57d665 (diff) | |
chore: cleanup tests / devdeps, fix doom example
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/archive.rs | 2 | ||||
| -rw-r--r-- | examples/doom/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/doom/src/main.rs | 10 | ||||
| -rw-r--r-- | examples/doom/src/runtime.rs | 35 | ||||
| -rw-r--r-- | examples/funcref_callbacks.rs | 2 | ||||
| -rw-r--r-- | examples/linking.rs | 2 | ||||
| -rw-r--r-- | examples/reentrance.rs | 2 | ||||
| -rw-r--r-- | examples/resumable.rs | 2 | ||||
| -rw-r--r-- | examples/simple.rs | 2 | ||||
| -rw-r--r-- | examples/simple2.rs | 2 | ||||
| -rw-r--r-- | examples/wasm-rust.rs | 4 |
11 files changed, 32 insertions, 33 deletions
diff --git a/examples/archive.rs b/examples/archive.rs index 7a2ebab..3f64704 100644 --- a/examples/archive.rs +++ b/examples/archive.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{Module, ModuleInstance, Store, parser::Parser}; const WASM: &str = r#" diff --git a/examples/doom/Cargo.toml b/examples/doom/Cargo.toml index 38e16c4..abec73d 100644 --- a/examples/doom/Cargo.toml +++ b/examples/doom/Cargo.toml @@ -7,7 +7,7 @@ publish = false [workspace] [dependencies] -eyre = "0.6" +anyhow = "1.0" log = "0.4" pretty_env_logger = "0.5" softbuffer = "0.4" diff --git a/examples/doom/src/main.rs b/examples/doom/src/main.rs index 095183d..c5e49b5 100644 --- a/examples/doom/src/main.rs +++ b/examples/doom/src/main.rs @@ -4,7 +4,7 @@ use std::num::NonZeroU32; use std::path::{Path, PathBuf}; use std::rc::Rc; -use eyre::{ContextCompat, Result, bail, eyre}; +use anyhow::{Context, Result, anyhow, bail}; use runtime::{Runtime, SCREEN_HEIGHT, SCREEN_WIDTH}; use softbuffer::{Context as SoftbufferContext, Surface}; use winit::application::ApplicationHandler; @@ -60,10 +60,10 @@ impl DoomApp { surface .resize(NonZeroU32::new(SCREEN_WIDTH as u32).unwrap(), NonZeroU32::new(SCREEN_HEIGHT as u32).unwrap()) - .map_err(|err| eyre!(err.to_string()))?; - let mut buffer = surface.buffer_mut().map_err(|err| eyre!(err.to_string()))?; + .map_err(|err| anyhow!(err.to_string()))?; + let mut buffer = surface.buffer_mut().map_err(|err| anyhow!(err.to_string()))?; self.runtime.write_framebuffer(&mut buffer)?; - buffer.present().map_err(|err| eyre!(err.to_string()))?; + buffer.present().map_err(|err| anyhow!(err.to_string()))?; Ok(()) } } @@ -129,7 +129,7 @@ impl ApplicationHandler for DoomApp { window.request_redraw(); } - if self.runtime.host_state.borrow().exit_code.is_some() { + if self.runtime.host_state.lock().unwrap().exit_code.is_some() { event_loop.exit(); } } diff --git a/examples/doom/src/runtime.rs b/examples/doom/src/runtime.rs index bee961c..2ad02ae 100644 --- a/examples/doom/src/runtime.rs +++ b/examples/doom/src/runtime.rs @@ -1,12 +1,11 @@ -use std::cell::RefCell; use std::collections::BTreeMap; use std::fs::{File, OpenOptions, create_dir_all}; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; -use std::rc::Rc; -use std::time::Instant; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; -use eyre::Result; +use anyhow::Result; use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store}; const IMPORT_MODULE: &str = "env"; @@ -21,15 +20,15 @@ pub struct Runtime { key_up: tinywasm::FunctionTyped<i32, ()>, memory: tinywasm::Memory, framebuffer_bytes: Vec<u8>, - pub host_state: Rc<RefCell<HostState>>, + pub host_state: Arc<Mutex<HostState>>, } impl Runtime { pub fn new(wad_path: PathBuf, guest_path: PathBuf) -> Result<Self> { let module = tinywasm::parse_file(&guest_path)?; let mut store = Store::default(); - let host_state = Rc::new(RefCell::new(HostState::new(wad_path))); - let imports = build_imports(&mut store, host_state.clone()); + let host_state = Arc::new(Mutex::new(HostState::new(wad_path))); + let imports = build_imports(host_state.clone()); let instance = ModuleInstance::instantiate(&mut store, &module, Some(&imports))?; let wad_path_buf = instance.func::<(), i32>(&store, "tinywasm_doom_wad_path_buf")?; @@ -41,7 +40,7 @@ impl Runtime { let memory = instance.memory("memory")?; let buf_ptr = wad_path_buf.call(&mut store, ())? as usize; - let wad_path_string = host_state.borrow().wad_path.to_string_lossy().into_owned(); + let wad_path_string = host_state.lock().unwrap().wad_path.to_string_lossy().into_owned(); memory.write_cstring_bytes(&mut store, buf_ptr, &wad_path_string)?; init.call(&mut store, ())?; @@ -161,7 +160,7 @@ impl HostState { } } -fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { +fn build_imports(state: Arc<Mutex<HostState>>) -> Imports { let mut imports = Imports::new(); { @@ -175,7 +174,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { let mode = memory.read_cstring_until_null(ctx.store(), mode_ptr as usize, 16)?; let filename = filename.to_string_lossy(); let mode = mode.to_string_lossy(); - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let path = if filename == state.wad_path.to_string_lossy() || state.should_redirect_to_wad(&filename) { state.wad_path.clone() } else { @@ -209,7 +208,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_close", HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { - state.borrow_mut().files.remove(&handle); + state.lock().unwrap().files.remove(&handle); Ok(()) }), ); @@ -221,7 +220,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_read", HostFunction::from(move |mut ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let Some(file) = state.files.get_mut(&handle) else { return Ok(0); }; @@ -240,7 +239,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { "host_write", HostFunction::from(move |ctx: FuncContext<'_>, (handle, buf_ptr, count): (i32, i32, i32)| { let data = ctx.memory("memory")?.read_vec(ctx.store(), buf_ptr as usize, count.max(0) as usize)?; - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let Some(file) = state.files.get_mut(&handle) else { return Ok(-1); }; @@ -262,7 +261,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { 2 => SeekFrom::End(offset as i64), _ => return Err(tinywasm::Error::Other(format!("invalid seek origin: {origin}"))), }; - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let Some(file) = state.files.get_mut(&handle) else { return Ok(-1); }; @@ -278,7 +277,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_tell", HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let Some(file) = state.files.get_mut(&handle) else { return Ok(-1); }; @@ -294,7 +293,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_eof", HostFunction::from(move |_ctx: FuncContext<'_>, handle: i32| { - let mut state = state.borrow_mut(); + let mut state = state.lock().unwrap(); let Some(file) = state.files.get_mut(&handle) else { return Ok(1); }; @@ -311,7 +310,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_gettime", HostFunction::from(move |mut ctx: FuncContext<'_>, (sec_ptr, usec_ptr): (i32, i32)| { - let elapsed = state.borrow().start.elapsed(); + let elapsed = state.lock().unwrap().start.elapsed() + Duration::from_secs(1); let sec = elapsed.as_secs().min(i32::MAX as u64) as i32; let usec = elapsed.subsec_micros() as i32; let memory = ctx.memory("memory")?; @@ -328,7 +327,7 @@ fn build_imports(store: &mut Store, state: Rc<RefCell<HostState>>) -> Imports { IMPORT_MODULE, "host_exit", HostFunction::from(move |_ctx: FuncContext<'_>, code: i32| { - state.borrow_mut().exit_code = Some(code); + state.lock().unwrap().exit_code = Some(code); Ok(()) }), ); diff --git a/examples/funcref_callbacks.rs b/examples/funcref_callbacks.rs index 5e21a6b..959d798 100644 --- a/examples/funcref_callbacks.rs +++ b/examples/funcref_callbacks.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store, types::FuncRef}; const LHS: i32 = 5; diff --git a/examples/linking.rs b/examples/linking.rs index 02cb88d..2df7133 100644 --- a/examples/linking.rs +++ b/examples/linking.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{ModuleInstance, Store}; // WebAssembly module defining and exporting an `add` function. diff --git a/examples/reentrance.rs b/examples/reentrance.rs index 8a1988e..0084a61 100644 --- a/examples/reentrance.rs +++ b/examples/reentrance.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store}; const WASM: &str = r#" diff --git a/examples/resumable.rs b/examples/resumable.rs index 0163414..26b5f47 100644 --- a/examples/resumable.rs +++ b/examples/resumable.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{ExecProgress, ModuleInstance, Store}; const WASM: &str = r#" diff --git a/examples/simple.rs b/examples/simple.rs index a902b4c..0d57265 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{ModuleInstance, Store}; const WASM: &str = r#" diff --git a/examples/simple2.rs b/examples/simple2.rs index da7def7..f673dfe 100644 --- a/examples/simple2.rs +++ b/examples/simple2.rs @@ -1,4 +1,4 @@ -use eyre::Result; +use anyhow::Result; use tinywasm::{ModuleInstance, Store}; const WASM: &str = r#" diff --git a/examples/wasm-rust.rs b/examples/wasm-rust.rs index e4af3cd..672b290 100644 --- a/examples/wasm-rust.rs +++ b/examples/wasm-rust.rs @@ -1,6 +1,6 @@ use std::hint::black_box; -use eyre::{Result, eyre}; +use anyhow::{Result, anyhow}; use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store}; /// Examples of using WebAssembly compiled from Rust with tinywasm. @@ -23,7 +23,7 @@ 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.")); + return Err(anyhow!("No WebAssembly files found. See examples/wasm-rust.rs for instructions.")); } let args = std::env::args().collect::<Vec<_>>(); |
