Skip to content

Commit

Permalink
Move TypedVal to wasmi_core crate (#1154)
Browse files Browse the repository at this point in the history
move TypedVal to wasmi_core crate
  • Loading branch information
Robbepop authored Aug 27, 2024
1 parent 37d1449 commit 74b9a4d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod hint;
mod host_error;
mod nan_preserving_float;
mod trap;
mod typed;
mod units;
mod untyped;
mod value;
Expand All @@ -40,6 +41,7 @@ pub use self::{
host_error::HostError,
nan_preserving_float::{F32, F64},
trap::{Trap, TrapCode},
typed::{Typed, TypedVal},
units::Pages,
untyped::{DecodeUntypedSlice, EncodeUntypedSlice, UntypedError, UntypedVal},
value::ValType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::{
core::{TrapCode, UntypedVal, ValType, F32, F64},
ExternRef,
FuncRef,
};
use crate::{TrapCode, UntypedVal, ValType, F32, F64};

/// Types that are associated to a static Wasm type.
pub trait Typed {
Expand All @@ -28,8 +24,6 @@ impl_typed_for! {
f64 => ValType::F64;
F32 => ValType::F32;
F64 => ValType::F64;
FuncRef => ValType::FuncRef;
ExternRef => ValType::ExternRef;
}

impl From<TypedVal> for UntypedVal {
Expand Down Expand Up @@ -67,6 +61,11 @@ impl TypedVal {
self.ty
}

/// Returns the [`UntypedVal`] of the [`TypedVal`].
pub fn untyped(&self) -> UntypedVal {
self.value
}

/// Changes the [`ValType`] of `self` to `ty`.
///
/// # Note
Expand Down Expand Up @@ -127,8 +126,6 @@ impl_from_typed_value_for! {
impl From<TypedValue> for f64;
impl From<TypedValue> for F32;
impl From<TypedValue> for F64;
impl From<TypedValue> for FuncRef;
impl From<TypedValue> for ExternRef;
}

macro_rules! impl_forwarding {
Expand All @@ -138,6 +135,15 @@ macro_rules! impl_forwarding {
)*
};
( @impl #[fallible] fn $name:ident($lhs_ty:ty, $rhs_ty:ty) -> $result_ty:ty ) => {
#[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = concat!("If [`UntypedVal::", stringify!($name), "`] returns an error.")]
#[doc = ""]
#[doc = "# Panics (Debug)"]
#[doc = ""]
#[doc = "If type checks fail."]
pub fn $name(self, other: Self) -> Result<Self, TrapCode> {
debug_assert!(matches!(self.ty(), <$lhs_ty as Typed>::TY));
debug_assert!(matches!(other.ty(), <$rhs_ty as Typed>::TY));
Expand All @@ -148,6 +154,11 @@ macro_rules! impl_forwarding {
}
};
( @impl fn $name:ident($lhs_ty:ty, $rhs_ty:ty) -> $result_ty:ty ) => {
#[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
#[doc = ""]
#[doc = "# Panics (Debug)"]
#[doc = ""]
#[doc = "If type checks fail."]
pub fn $name(self, other: Self) -> Self {
debug_assert!(matches!(self.ty(), <$lhs_ty as Typed>::TY));
debug_assert!(matches!(other.ty(), <$rhs_ty as Typed>::TY));
Expand All @@ -158,6 +169,15 @@ macro_rules! impl_forwarding {
}
};
( @impl #[fallible] fn $name:ident($input_ty:ty) -> $result_ty:ty ) => {
#[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = concat!("If [`UntypedVal::", stringify!($name), "`] returns an error.")]
#[doc = ""]
#[doc = "# Panics (Debug)"]
#[doc = ""]
#[doc = "If type checks fail."]
pub fn $name(self) -> Result<Self, TrapCode> {
debug_assert!(matches!(self.ty(), <$input_ty as Typed>::TY));
Ok(Self::new(
Expand All @@ -167,6 +187,11 @@ macro_rules! impl_forwarding {
}
};
( @impl fn $name:ident($input_ty:ty) -> $result_ty:ty ) => {
#[doc = concat!("Forwards to [`UntypedVal::", stringify!($name), "`] with debug type checks.")]
#[doc = ""]
#[doc = "# Panics (Debug)"]
#[doc = ""]
#[doc = "If type checks fail."]
pub fn $name(self) -> Self {
debug_assert!(matches!(self.ty(), <$input_ty as Typed>::TY));
Self::new(
Expand Down
3 changes: 2 additions & 1 deletion crates/wasmi/src/engine/translator/control_stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{typed_value::TypedVal, ControlFrame};
use super::ControlFrame;
use crate::{
core::TypedVal,
engine::bytecode::{Provider, ProviderSliceStack},
Error,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/translator/instr_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,7 +1473,7 @@ impl Instruction {
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::translator::typed_value::TypedVal;
use crate::core::TypedVal;

#[test]
fn has_overlapping_copies_works() {
Expand Down
33 changes: 30 additions & 3 deletions crates/wasmi/src/engine/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod instr_encoder;
mod labels;
mod relink_result;
mod stack;
mod typed_value;
mod utils;
mod visit;
mod visit_register;
Expand All @@ -27,7 +26,6 @@ use self::{
control_stack::AcquiredTarget,
labels::{LabelRef, LabelRegistry},
stack::ValueStack,
typed_value::TypedVal,
utils::{WasmFloat, WasmInteger},
};
pub use self::{
Expand All @@ -40,7 +38,7 @@ pub use self::{
};
use super::code_map::CompiledFuncEntity;
use crate::{
core::{TrapCode, UntypedVal, ValType},
core::{TrapCode, Typed, TypedVal, UntypedVal, ValType},
engine::{
bytecode::{
AnyConst32,
Expand All @@ -60,6 +58,8 @@ use crate::{
module::{FuncIdx, FuncTypeIdx, ModuleHeader},
Engine,
Error,
ExternRef,
FuncRef,
FuncType,
};
use core::fmt;
Expand All @@ -73,6 +73,33 @@ use wasmparser::{
VisitOperator,
};

macro_rules! impl_typed_for {
( $( $ty:ident ),* $(,)? ) => {
$(
impl Typed for $ty {
const TY: ValType = crate::core::ValType::$ty;
}

impl From<TypedVal> for $ty {
fn from(typed_value: TypedVal) -> Self {
// # Note
//
// We only use a `debug_assert` here instead of a proper `assert`
// since the whole translation process assumes that Wasm validation
// was already performed and thus type checking does not necessarily
// need to happen redundantly outside of debug builds.
debug_assert!(matches!(typed_value.ty(), <$ty as Typed>::TY));
Self::from(typed_value.untyped())
}
}
)*
};
}
impl_typed_for! {
FuncRef,
ExternRef,
}

/// Reusable allocations of a [`FuncTranslator`].
#[derive(Debug, Default)]
pub struct FuncTranslatorAllocations {
Expand Down

0 comments on commit 74b9a4d

Please sign in to comment.