diff options
| author | Mica White <botahamec@outlook.com> | 2026-07-29 21:25:01 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-07-29 21:25:01 -0400 |
| commit | 5ab4339c98179958b11b716b5887ecd3224666e2 (patch) | |
| tree | f9c7f01d142cbdf72471b40a5d36fe92bbfbeb6f /crates | |
| parent | c7c26e8900e86f8b2116648e988b1e34c85f4077 (diff) | |
Support tortuisebota-tortuise
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/tinywasm/src/func.rs | 19 | ||||
| -rw-r--r-- | crates/tinywasm/src/instance.rs | 8 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/executor.rs | 7 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/function.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/memory/lazy.rs | 2 | ||||
| -rw-r--r-- | crates/tinywasm/src/store/memory/mod.rs | 13 | ||||
| -rw-r--r-- | crates/types/src/value.rs | 40 |
7 files changed, 70 insertions, 23 deletions
diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs index 05767d5..a5618ed 100644 --- a/crates/tinywasm/src/func.rs +++ b/crates/tinywasm/src/func.rs @@ -1,7 +1,7 @@ use crate::interpreter::stack::{CallFrame, ValueStack}; use crate::reference::StoreItem; use crate::{Error, FunctionInstance, InterpreterRuntime, Result, Store, Trap}; -use alloc::{borrow::Cow, boxed::Box, format, rc::Rc, sync::Arc, vec, vec::Vec}; +use alloc::{borrow::Cow, boxed::Box, format, sync::Arc, vec, vec::Vec}; use core::hint::cold_path; use tinywasm_types::{ExternRef, FuncRef, FuncType, ModuleInstanceAddr, WasmType, WasmValue}; @@ -176,7 +176,7 @@ impl HostFunction { pub fn from_untyped( store: &mut Store, ty: &FuncType, - func: impl Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>> + 'static, + func: impl Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>> + Sync + Send + 'static, ) -> Function { let ty = Arc::new(ty.clone()); let host_ty = ty.clone(); @@ -195,7 +195,8 @@ impl HostFunction { Ok(result) }; - let addr = store.add_func(FunctionInstance::Host(Rc::new(Self { func: Box::new(inner_func), ty: ty.clone() }))); + let addr = + store.add_func(FunctionInstance::Host(Arc::new(Self { func: Box::new(inner_func), ty: ty.clone() }))); Function { item: crate::StoreItem::new(store.id(), addr), module_addr: 0, addr, ty } } @@ -224,7 +225,10 @@ impl HostFunction { /// # Ok(()) /// # } /// ``` - pub fn from<P, R>(store: &mut Store, func: impl Fn(FuncContext<'_>, P) -> Result<R> + 'static) -> Function + pub fn from<P, R>( + store: &mut Store, + func: impl Fn(FuncContext<'_>, P) -> Result<R> + Sync + Send + 'static, + ) -> Function where P: FromWasmValues + ToWasmTypes, R: IntoWasmValues + ToWasmTypes, @@ -234,12 +238,13 @@ impl HostFunction { }; let ty = Arc::new(tinywasm_types::FuncType::new(&P::wasm_types(), &R::wasm_types())); - let addr = store.add_func(FunctionInstance::Host(Rc::new(Self { func: Box::new(inner_func), ty: ty.clone() }))); + let addr = + store.add_func(FunctionInstance::Host(Arc::new(Self { func: Box::new(inner_func), ty: ty.clone() }))); Function { item: crate::StoreItem::new(store.id(), addr), module_addr: 0, addr, ty } } } -pub(crate) type HostFuncInner = Box<dyn Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>>>; +pub(crate) type HostFuncInner = Box<dyn Fn(FuncContext<'_>, &[WasmValue]) -> Result<Vec<WasmValue>> + Sync + Send>; /// The context of a host-function call #[cfg_attr(feature = "debug", derive(core::fmt::Debug))] @@ -709,6 +714,8 @@ impl_scalar_wasm_traits!( i64 => I64, f32 => F32, f64 => F64, + u32 => I32, + u64 => I64, FuncRef => RefFunc, ExternRef => RefExtern, ); diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs index a727638..ac670fd 100644 --- a/crates/tinywasm/src/instance.rs +++ b/crates/tinywasm/src/instance.rs @@ -1,4 +1,4 @@ -use alloc::{boxed::Box, format, rc::Rc, sync::Arc}; +use alloc::{boxed::Box, format, sync::Arc}; use core::hint::cold_path; use tinywasm_types::*; @@ -36,12 +36,12 @@ pub enum ExternItem { /// # } /// ``` /// -/// Backed by an Rc, so cloning is cheap +/// Backed by an Arc, so cloning is cheap /// /// See <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances> #[derive(Clone)] #[cfg_attr(feature = "debug", derive(Debug))] -pub struct ModuleInstance(Rc<ModuleInstanceInner>); +pub struct ModuleInstance(Arc<ModuleInstanceInner>); #[cfg_attr(feature = "debug", derive(Debug))] struct ModuleInstanceInner { @@ -207,7 +207,7 @@ impl ModuleInstance { exports: module.exports.clone(), }; - let instance = ModuleInstance(Rc::new(instance)); + let instance = ModuleInstance(Arc::new(instance)); store.add_instance(instance.clone()); if let Some(trap) = elem_trapped.or(data_trapped) { diff --git a/crates/tinywasm/src/interpreter/executor.rs b/crates/tinywasm/src/interpreter/executor.rs index 739c136..4d584f5 100644 --- a/crates/tinywasm/src/interpreter/executor.rs +++ b/crates/tinywasm/src/interpreter/executor.rs @@ -5,16 +5,15 @@ use core::hint::cold_path; use super::no_std_floats::NoStdFloatExt; use alloc::boxed::Box; -use alloc::rc::Rc; +use alloc::sync::Arc; use alloc::vec::Vec; -use alloc::sync::Arc; use interpreter::stack::{CallFrame, ValueStack}; use tinywasm_types::*; -use super::ExecState; use super::num_helpers::*; use super::values::*; +use super::ExecState; use crate::engine::FuelPolicy; use crate::func::{FuncContext, HostFunction}; use crate::interpreter::Value128; @@ -960,7 +959,7 @@ impl<'store, const BUDGETED: bool> Executor<'store, BUDGETED> { Ok(()) } - fn exec_call_host(&mut self, host_func: Rc<HostFunction>) -> Result<(), Trap> { + fn exec_call_host(&mut self, host_func: Arc<HostFunction>) -> Result<(), Trap> { let mut params = self.store.value_stack.pop_types(host_func.ty.params().iter().rev()).collect::<Vec<_>>(); params.reverse(); let res = match host_func.call(FuncContext { store: self.store, module_addr: self.module.idx() }, ¶ms) { diff --git a/crates/tinywasm/src/store/function.rs b/crates/tinywasm/src/store/function.rs index 5f5a90e..7cac55c 100644 --- a/crates/tinywasm/src/store/function.rs +++ b/crates/tinywasm/src/store/function.rs @@ -1,4 +1,4 @@ -use alloc::{rc::Rc, sync::Arc}; +use alloc::sync::Arc; use tinywasm_types::*; use crate::func::HostFunction; @@ -10,7 +10,7 @@ use crate::func::HostFunction; #[cfg_attr(feature = "debug", derive(Debug))] pub(crate) enum FunctionInstance { /// A host function - Host(Rc<HostFunction>), + Host(Arc<HostFunction>), /// A pointer to a WebAssembly function Wasm(WasmFunctionInstance), diff --git a/crates/tinywasm/src/store/memory/lazy.rs b/crates/tinywasm/src/store/memory/lazy.rs index ef6dace..91759c4 100644 --- a/crates/tinywasm/src/store/memory/lazy.rs +++ b/crates/tinywasm/src/store/memory/lazy.rs @@ -15,7 +15,7 @@ pub struct LazyLinearMemory { ty: MemoryType, initial_len: usize, backend: MemoryBackend, - inner: Option<Box<dyn LinearMemory>>, + inner: Option<Box<dyn LinearMemory + Send + Sync>>, } impl LazyLinearMemory { diff --git a/crates/tinywasm/src/store/memory/mod.rs b/crates/tinywasm/src/store/memory/mod.rs index a3f0053..6b36f42 100644 --- a/crates/tinywasm/src/store/memory/mod.rs +++ b/crates/tinywasm/src/store/memory/mod.rs @@ -259,7 +259,7 @@ pub trait LinearMemory { } } -type MemoryFactory = dyn Fn(MemoryType) -> Result<Box<dyn LinearMemory>> + Send + Sync; +type MemoryFactory = dyn Fn(MemoryType) -> Result<Box<dyn LinearMemory + Send + Sync>> + Send + Sync; /// Configures how runtime memory instances are created. #[derive(Clone, Default)] @@ -300,21 +300,22 @@ impl MemoryBackend { pub fn custom<F, M>(factory: F) -> Self where F: Fn(MemoryType) -> Result<M> + Send + Sync + 'static, - M: LinearMemory + 'static, + M: LinearMemory + Send + Sync + 'static, { Self(MemoryBackendInner::Custom(Arc::new(move |ty| { let memory = factory(ty)?; - Ok(Box::new(memory) as Box<dyn LinearMemory>) + Ok(Box::new(memory) as Box<dyn LinearMemory + Send + Sync>) }))) } pub(crate) fn create(&self, ty: MemoryType, initial_len: usize) -> Result<MemoryStorage> { let storage = match &self.0 { MemoryBackendInner::Vec => { - Box::new(VecMemory::try_new(initial_len).map_err(Error::Trap)?) as Box<dyn LinearMemory> + Box::new(VecMemory::try_new(initial_len).map_err(Error::Trap)?) as Box<dyn LinearMemory + Send + Sync> } MemoryBackendInner::Paged { chunk_size } => { - Box::new(PagedMemory::try_new(initial_len, *chunk_size).map_err(Error::Trap)?) as Box<dyn LinearMemory> + Box::new(PagedMemory::try_new(initial_len, *chunk_size).map_err(Error::Trap)?) + as Box<dyn LinearMemory + Send + Sync> } MemoryBackendInner::Custom(factory) => factory(ty)?, }; @@ -347,7 +348,7 @@ impl core::fmt::Debug for MemoryBackend { } } -pub(crate) type MemoryStorage = Box<dyn LinearMemory>; +pub(crate) type MemoryStorage = Box<dyn LinearMemory + Send + Sync>; /// A trait for types that can be converted to and from static byte arrays pub(crate) trait MemValue<const N: usize>: Copy + Default { diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 782cdb8..129b7c1 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -232,3 +232,43 @@ impl_conversion_for_wasmvalue! { ExternRef => RefExtern, as_ref_extern, "Return the [`ExternRef`] from a `WasmValue`, if it is one"; FuncRef => RefFunc, as_ref_func, "Return the [`FuncRef`] from a `WasmValue`, if it is one"; } + +impl From<u32> for WasmValue { + #[inline] + fn from(i: u32) -> Self { + Self::I32(i as i32) + } +} + +impl TryFrom<WasmValue> for u32 { + type Error = (); + + #[inline] + fn try_from(value: WasmValue) -> Result<Self, Self::Error> { + if let WasmValue::I32(i) = value { + Ok(i as u32) + } else { + Err(()) + } + } +} + +impl From<u64> for WasmValue { + #[inline] + fn from(i: u64) -> Self { + Self::I64(i as i64) + } +} + +impl TryFrom<WasmValue> for u64 { + type Error = (); + + #[inline] + fn try_from(value: WasmValue) -> Result<Self, Self::Error> { + if let WasmValue::I64(i) = value { + Ok(i as u64) + } else { + Err(()) + } + } +} |
