diff options
Diffstat (limited to 'crates/types')
| -rw-r--r-- | crates/types/src/instructions.rs | 4 | ||||
| -rw-r--r-- | crates/types/src/lib.rs | 53 | ||||
| -rw-r--r-- | crates/types/src/value.rs | 99 |
3 files changed, 85 insertions, 71 deletions
diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index 8c0680e..01398b5 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -1,4 +1,4 @@ -use super::{FuncAddr, GlobalAddr, LocalAddr, TableAddr, TypeAddr, ValType, ValueCounts}; +use super::{FuncAddr, GlobalAddr, LocalAddr, TableAddr, TypeAddr, ValueCounts, WasmType}; use crate::{ConstIdx, DataAddr, ElemAddr, ExternAddr, MemAddr}; /// Represents a memory immediate in a WebAssembly memory instruction. @@ -142,7 +142,7 @@ pub enum Instruction { F64Const(f64), // > Reference Types - RefNull(ValType), + RefNull(WasmType), RefFunc(FuncAddr), RefIsNull, diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index f74336c..ece6086 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -200,8 +200,27 @@ impl ExternVal { #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct FuncType { - pub params: Box<[ValType]>, - pub results: Box<[ValType]>, + data: Box<[WasmType]>, + param_count: u16, +} + +impl FuncType { + /// Create a new function type. + pub fn new(params: &[WasmType], results: &[WasmType]) -> Self { + let param_count = params.len() as u16; + let data: Box<[WasmType]> = params.iter().cloned().chain(results.iter().cloned()).collect(); + Self { data, param_count } + } + + /// Get the parameter types of this function type. + pub fn params(&self) -> &[WasmType] { + &self.data[..self.param_count as usize] + } + + /// Get the result types of this function type. + pub fn results(&self) -> &[WasmType] { + &self.data[self.param_count as usize..] + } } #[derive(Default, Clone, Copy, PartialEq, Eq)] @@ -221,15 +240,15 @@ impl ValueCounts { } } -impl<'a> FromIterator<&'a ValType> for ValueCounts { +impl<'a> FromIterator<&'a WasmType> for ValueCounts { #[inline] - fn from_iter<I: IntoIterator<Item = &'a ValType>>(iter: I) -> Self { + fn from_iter<I: IntoIterator<Item = &'a WasmType>>(iter: I) -> Self { iter.into_iter().fold(Self::default(), |mut counts, ty| { match ty { - ValType::I32 | ValType::F32 => counts.c32 += 1, - ValType::I64 | ValType::F64 => counts.c64 += 1, - ValType::V128 => counts.c128 += 1, - ValType::RefExtern | ValType::RefFunc => counts.cref += 1, + WasmType::I32 | WasmType::F32 => counts.c32 += 1, + WasmType::I64 | WasmType::F64 => counts.c64 += 1, + WasmType::V128 => counts.c128 += 1, + WasmType::RefExtern | WasmType::RefFunc => counts.cref += 1, } counts }) @@ -247,8 +266,8 @@ pub struct WasmFunction { pub ty: FuncType, } -#[derive(Clone, PartialEq)] #[doc(hidden)] +#[derive(Clone, PartialEq)] // wrapper around Arc<[T]> to support serde serialization and deserialization pub struct ArcSlice<T>(pub Arc<[T]>); @@ -333,17 +352,17 @@ pub struct Global { #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct GlobalType { pub mutable: bool, - pub ty: ValType, + pub ty: WasmType, } impl GlobalType { /// Create a new global type. - pub const fn new(ty: ValType, mutable: bool) -> Self { + pub const fn new(ty: WasmType, mutable: bool) -> Self { Self { mutable, ty } } /// Set a different value type. - pub const fn with_ty(mut self, ty: ValType) -> Self { + pub const fn with_ty(mut self, ty: WasmType) -> Self { self.ty = ty; self } @@ -357,7 +376,7 @@ impl GlobalType { impl Default for GlobalType { fn default() -> Self { - Self::new(ValType::I32, false) + Self::new(WasmType::I32, false) } } @@ -365,17 +384,17 @@ impl Default for GlobalType { #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct TableType { - pub element_type: ValType, + pub element_type: WasmType, pub size_initial: u32, pub size_max: Option<u32>, } impl TableType { pub fn empty() -> Self { - Self { element_type: ValType::RefFunc, size_initial: 0, size_max: None } + Self { element_type: WasmType::RefFunc, size_initial: 0, size_max: None } } - pub fn new(element_type: ValType, size_initial: u32, size_max: Option<u32>) -> Self { + pub fn new(element_type: WasmType, size_initial: u32, size_max: Option<u32>) -> Self { Self { element_type, size_initial, size_max } } } @@ -525,7 +544,7 @@ pub struct Element { pub kind: ElementKind, pub items: Box<[ElementItem]>, pub range: Range<usize>, - pub ty: ValType, + pub ty: WasmType, } #[derive(Clone, PartialEq)] diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index f468be5..462749b 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -18,7 +18,6 @@ pub enum WasmValue { F64(f64), // /// A 128-bit vector V128(i128), - RefExtern(ExternRef), RefFunc(FuncRef), } @@ -70,55 +69,52 @@ impl Debug for FuncRef { } impl FuncRef { - /// Create a new `FuncRef` from a `FuncAddr`. - /// Should only be used by the runtime. - #[doc(hidden)] #[inline] + /// Create a new [`FuncRef`] from a [`FuncAddr`]. pub const fn new(addr: Option<FuncAddr>) -> Self { Self(addr) } - /// Create a null `FuncRef`. #[inline] + /// Create a null [`FuncRef`]. pub const fn null() -> Self { Self(None) } - /// Check if the `FuncRef` is null. #[inline] + /// Check if the [`FuncRef`] is null. pub const fn is_null(&self) -> bool { self.0.is_none() } - /// Get the `FuncAddr` from the `FuncRef`. #[inline] + /// Get the [`FuncAddr`] from the [`FuncRef`]. pub const fn addr(&self) -> Option<FuncAddr> { self.0 } } impl ExternRef { - /// Create a new `ExternRef` from an `ExternAddr`. - /// Should only be used by the runtime. - #[doc(hidden)] #[inline] + /// Create a new [`ExternRef`] from an [`ExternAddr`]. + /// Should only be used by the runtime. pub const fn new(addr: Option<ExternAddr>) -> Self { Self(addr) } - /// Create a null `ExternRef`. + /// Create a null [`ExternRef`]. #[inline] pub const fn null() -> Self { Self(None) } - /// Check if the `ExternRef` is null. + /// Check if the [`ExternRef`] is null. #[inline] pub const fn is_null(&self) -> bool { self.0.is_none() } - /// Get the `ExternAddr` from the `ExternRef`. + /// Get the [`ExternAddr`] from the [`ExternRef`]. #[inline] pub const fn addr(&self) -> Option<ExternAddr> { self.0 @@ -126,8 +122,8 @@ impl ExternRef { } impl WasmValue { - #[doc(hidden)] #[inline] + /// Get the matching [`ConstInstruction`] for this value. pub fn const_instr(&self) -> alloc::boxed::Box<[ConstInstruction]> { alloc::boxed::Box::new([match self { Self::I32(i) => ConstInstruction::I32Const(*i), @@ -140,22 +136,22 @@ impl WasmValue { }]) } - /// Get the default value for a given type. #[inline] - pub const fn default_for(ty: ValType) -> Self { + /// Get the default value for a given type. + pub const fn default_for(ty: WasmType) -> Self { match ty { - ValType::I32 => Self::I32(0), - ValType::I64 => Self::I64(0), - ValType::F32 => Self::F32(0.0), - ValType::F64 => Self::F64(0.0), - ValType::V128 => Self::V128(0), - ValType::RefFunc => Self::RefFunc(FuncRef::null()), - ValType::RefExtern => Self::RefExtern(ExternRef::null()), + WasmType::I32 => Self::I32(0), + WasmType::I64 => Self::I64(0), + WasmType::F32 => Self::F32(0.0), + WasmType::F64 => Self::F64(0.0), + WasmType::V128 => Self::V128(0), + WasmType::RefFunc => Self::RefFunc(FuncRef::null()), + WasmType::RefExtern => Self::RefExtern(ExternRef::null()), } } - /// Check if two values are equal, ignoring differences in NaN values. #[inline] + /// Check if two values are equal, ignoring differences in NaN values. pub fn eq_loose(&self, other: &Self) -> bool { match (self, other) { (Self::I32(a), Self::I32(b)) => a == b, @@ -233,7 +229,7 @@ impl WasmValue { }) && a_f64x2.iter().any(|x| x.is_nan()) } - #[doc(hidden)] + /// Return the `i32` from a `WasmValue`, if it is an `I32`. pub const fn as_i32(&self) -> Option<i32> { match self { Self::I32(i) => Some(*i), @@ -241,7 +237,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the `i64` from a `WasmValue`, if it is an `I64`. pub const fn as_i64(&self) -> Option<i64> { match self { Self::I64(i) => Some(*i), @@ -249,7 +245,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the `f32` from a `WasmValue`, if it is a `F32`. pub const fn as_f32(&self) -> Option<f32> { match self { Self::F32(i) => Some(*i), @@ -257,7 +253,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the `f64` from a `WasmValue`, if it is a `F64`. pub const fn as_f64(&self) -> Option<f64> { match self { Self::F64(i) => Some(*i), @@ -265,7 +261,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the `i128` from a `WasmValue`, if it is a `V128`. pub const fn as_v128(&self) -> Option<i128> { match self { Self::V128(i) => Some(*i), @@ -273,7 +269,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the [[`ExternRef`]] from a `WasmValue`, if it is one pub const fn as_ref_extern(&self) -> Option<ExternRef> { match self { Self::RefExtern(ref_extern) => Some(*ref_extern), @@ -281,7 +277,7 @@ impl WasmValue { } } - #[doc(hidden)] + /// Return the [`FuncRef`] from a `WasmValue`, if it is one pub const fn as_ref_func(&self) -> Option<FuncRef> { match self { Self::RefFunc(ref_func) => Some(*ref_func), @@ -290,27 +286,33 @@ impl WasmValue { } } -impl WasmValue { - /// Get the type of a [`WasmValue`] +impl From<&WasmValue> for WasmType { #[inline] - pub const fn val_type(&self) -> ValType { - match self { - Self::I32(_) => ValType::I32, - Self::I64(_) => ValType::I64, - Self::F32(_) => ValType::F32, - Self::F64(_) => ValType::F64, - Self::V128(_) => ValType::V128, - Self::RefExtern(_) => ValType::RefExtern, - Self::RefFunc(_) => ValType::RefFunc, + fn from(value: &WasmValue) -> Self { + match value { + WasmValue::I32(_) => WasmType::I32, + WasmValue::I64(_) => WasmType::I64, + WasmValue::F32(_) => WasmType::F32, + WasmValue::F64(_) => WasmType::F64, + WasmValue::V128(_) => WasmType::V128, + WasmValue::RefExtern(_) => WasmType::RefExtern, + WasmValue::RefFunc(_) => WasmType::RefFunc, } } } +impl From<WasmValue> for WasmType { + #[inline] + fn from(value: WasmValue) -> Self { + Self::from(&value) + } +} + /// Type of a WebAssembly value. #[derive(Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] -pub enum ValType { +pub enum WasmType { /// A 32-bit integer. I32, /// A 64-bit integer. @@ -327,13 +329,12 @@ pub enum ValType { RefExtern, } -impl ValType { +impl WasmType { #[inline] pub const fn default_value(&self) -> WasmValue { WasmValue::default_for(*self) } - #[doc(hidden)] #[inline] pub const fn is_simd(&self) -> bool { matches!(self, Self::V128) @@ -343,7 +344,6 @@ impl ValType { macro_rules! impl_conversion_for_wasmvalue { ($($t:ty => $variant:ident),*) => { $( - // Implementing From<$t> for WasmValue impl From<$t> for WasmValue { #[inline] fn from(i: $t) -> Self { @@ -351,17 +351,12 @@ macro_rules! impl_conversion_for_wasmvalue { } } - // Implementing TryFrom<WasmValue> for $t impl TryFrom<WasmValue> for $t { type Error = (); #[inline] fn try_from(value: WasmValue) -> Result<Self, Self::Error> { - if let WasmValue::$variant(i) = value { - Ok(i) - } else { - Err(()) - } + if let WasmValue::$variant(i) = value { Ok(i) } else { Err(()) } } } )* |
