Skip to content

Commit

Permalink
refactor: impl ExtendInto and WrapInto for Self (#541)
Browse files Browse the repository at this point in the history
* feat: impl ExtendInto for Self

* feat: impl WrapInto for Self
  • Loading branch information
yjhmelody authored Oct 31, 2022
1 parent ca8c5c9 commit 0d087e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
12 changes: 12 additions & 0 deletions crates/core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ impl_wrap_into!(u64, f32, F32);
// largest or smallest finite value representable by f32. This is a bug and will be fixed.
impl_wrap_into!(f64, f32);

// Casting to self
impl_wrap_into!(i32, i32);
impl_wrap_into!(i64, i64);
impl_wrap_into!(F32, F32);
impl_wrap_into!(F64, F64);

impl WrapInto<F32> for F64 {
#[inline]
fn wrap_into(self) -> F32 {
Expand Down Expand Up @@ -626,6 +632,12 @@ impl_extend_into!(i64, f64, F64);
impl_extend_into!(u64, f64, F64);
impl_extend_into!(f32, f64, F64);

// Casting to self
impl_extend_into!(i32, i32);
impl_extend_into!(i64, i64);
impl_extend_into!(F32, F32);
impl_extend_into!(F64, F64);

impl ExtendInto<F64> for F32 {
#[inline]
fn extend_into(self) -> F64 {
Expand Down
27 changes: 4 additions & 23 deletions crates/wasmi/src/engine/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,10 @@ impl<'ctx, 'engine, 'func, HostData> Executor<'ctx, 'engine, 'func, HostData> {
/// - `f64.load`
fn execute_load<T>(&mut self, offset: Offset) -> Result<(), TrapCode>
where
T: ExtendInto<T> + LittleEndianConvert,
UntypedValue: From<T>,
T: LittleEndianConvert,
{
self.value_stack.try_eval_top(|address| {
let raw_address = u32::from(address);
let address = Self::effective_address(offset, raw_address)?;
let mut bytes = <<T as LittleEndianConvert>::Bytes as Default>::default();
self.cache
.default_memory_bytes(self.ctx.as_context_mut())
.read(address, bytes.as_mut())?;
let value = <T as LittleEndianConvert>::from_le_bytes(bytes);
Ok(value.into())
})?;
self.next_instr();
Ok(())
self.execute_load_extend::<T, T>(offset)
}

/// Loads a value of type `U` from the default memory at the given address offset and extends it into `T`.
Expand Down Expand Up @@ -397,17 +386,9 @@ impl<'ctx, 'engine, 'func, HostData> Executor<'ctx, 'engine, 'func, HostData> {
/// - `f64.store`
fn execute_store<T>(&mut self, offset: Offset) -> Result<(), TrapCode>
where
T: LittleEndianConvert + From<UntypedValue>,
T: WrapInto<T> + LittleEndianConvert + From<UntypedValue>,
{
let (address, value) = self.value_stack.pop2();
let value = T::from(value);
let address = Self::effective_address(offset, u32::from(address))?;
let bytes = <T as LittleEndianConvert>::into_le_bytes(value);
self.cache
.default_memory_bytes(self.ctx.as_context_mut())
.write(address, bytes.as_ref())?;
self.next_instr();
Ok(())
self.execute_store_wrap::<T, T>(offset)
}

/// Stores a value of type `T` wrapped to type `U` into the default memory at the given address offset.
Expand Down

0 comments on commit 0d087e2

Please sign in to comment.