diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/cli/src/value_parse.rs | 5 | ||||
| -rw-r--r-- | crates/cli/src/wast_runner.rs | 8 | ||||
| -rw-r--r-- | crates/parser/src/conversion.rs | 2 | ||||
| -rw-r--r-- | crates/parser/src/visit.rs | 4 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/stack/value_stack.rs | 2 | ||||
| -rw-r--r-- | crates/tinywasm/src/interpreter/values.rs | 6 | ||||
| -rw-r--r-- | crates/types/src/instructions.rs | 2 | ||||
| -rw-r--r-- | crates/types/src/value.rs | 16 |
8 files changed, 22 insertions, 23 deletions
diff --git a/crates/cli/src/value_parse.rs b/crates/cli/src/value_parse.rs index f64d335..f9ffbbb 100644 --- a/crates/cli/src/value_parse.rs +++ b/crates/cli/src/value_parse.rs @@ -17,7 +17,10 @@ fn parse_arg(index: usize, ty: WasmType, value: &str) -> Result<WasmValue> { WasmType::I64 => value.parse::<i64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?, WasmType::F32 => value.parse::<f32>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?, WasmType::F64 => value.parse::<f64>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?, - WasmType::V128 => value.parse::<i128>().map(WasmValue::from).map_err(|e| format_error(index, ty, value, e))?, + WasmType::V128 => value + .parse::<i128>() + .map(|v| WasmValue::V128(v.to_le_bytes())) + .map_err(|e| format_error(index, ty, value, e))?, WasmType::RefFunc | WasmType::RefExtern => { bail!( "unsupported CLI argument type at position {}: {}; use the embedding API for reference values", diff --git a/crates/cli/src/wast_runner.rs b/crates/cli/src/wast_runner.rs index 8a759b7..9170dce 100644 --- a/crates/cli/src/wast_runner.rs +++ b/crates/cli/src/wast_runner.rs @@ -745,7 +745,7 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> { F64(f) => WasmValue::F64(f64::from_bits(f.bits)), I32(i) => WasmValue::I32(i), I64(i) => WasmValue::I64(i), - V128(i) => WasmValue::V128(i128::from_le_bytes(i.to_le_bytes())), + V128(i) => WasmValue::V128(i.to_le_bytes()), RefExtern(v) => WasmValue::RefExtern(ExternRef::new(Some(v))), RefNull(t) => match t { wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func } => { @@ -760,7 +760,7 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> { }) } -fn wast_i128_to_i128(i: wast::core::V128Pattern) -> i128 { +fn wast_v128_to_bytes(i: wast::core::V128Pattern) -> [u8; 16] { let res: Vec<u8> = match i { wast::core::V128Pattern::F32x4(f) => { f.iter().flat_map(|v| nanpattern2tinywasmvalue(*v).unwrap().as_f32().unwrap().to_le_bytes()).collect() @@ -773,7 +773,7 @@ fn wast_i128_to_i128(i: wast::core::V128Pattern) -> i128 { wast::core::V128Pattern::I64x2(f) => f.iter().flat_map(|v| v.to_le_bytes()).collect(), wast::core::V128Pattern::I8x16(f) => f.iter().flat_map(|v| v.to_le_bytes()).collect(), }; - i128::from_le_bytes(res.try_into().unwrap()) + res.try_into().unwrap() } fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<WasmValue>> { @@ -793,7 +793,7 @@ fn wastretcore2tinywasmvalue(ret: wast::core::WastRetCore) -> Result<WasmValue> F64(f) => nanpattern2tinywasmvalue(f)?, I32(i) => WasmValue::I32(i), I64(i) => WasmValue::I64(i), - V128(i) => WasmValue::V128(wast_i128_to_i128(i)), + V128(i) => WasmValue::V128(wast_v128_to_bytes(i)), RefNull(t) => match t { Some(wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func }) => { WasmValue::RefFunc(FuncRef::null()) diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 8a12daa..fea43ba 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -271,7 +271,7 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C wasmparser::Operator::I64Const { value } => ConstInstruction::I64Const(*value), wasmparser::Operator::F32Const { value } => ConstInstruction::F32Const(f32::from_bits(value.bits())), wasmparser::Operator::F64Const { value } => ConstInstruction::F64Const(f64::from_bits(value.bits())), - wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(value.i128()), + wasmparser::Operator::V128Const { value } => ConstInstruction::V128Const(*value.bytes()), wasmparser::Operator::GlobalGet { global_index } => ConstInstruction::GlobalGet(*global_index), wasmparser::Operator::I32Add => ConstInstruction::I32Add, wasmparser::Operator::I32Sub => ConstInstruction::I32Sub, diff --git a/crates/parser/src/visit.rs b/crates/parser/src/visit.rs index 62db307..d1612ed 100644 --- a/crates/parser/src/visit.rs +++ b/crates/parser/src/visit.rs @@ -400,11 +400,11 @@ impl<'a, R: WasmModuleResources> wasmparser::VisitOperator<'a> for FunctionBuild } fn visit_f32_const(&mut self, val: wasmparser::Ieee32) -> Self::Output { - self.instructions.push(Instruction::Const32(i32::from_ne_bytes(val.bits().to_ne_bytes()))); + self.instructions.push(Instruction::Const32(val.bits() as i32)); } fn visit_f64_const(&mut self, val: wasmparser::Ieee64) -> Self::Output { - self.instructions.push(Instruction::Const64(i64::from_ne_bytes(val.bits().to_ne_bytes()))); + self.instructions.push(Instruction::Const64(val.bits() as i64)); } fn visit_table_copy(&mut self, dst_table: u32, src_table: u32) -> Self::Output { diff --git a/crates/tinywasm/src/interpreter/stack/value_stack.rs b/crates/tinywasm/src/interpreter/stack/value_stack.rs index e84b8b6..9be804a 100644 --- a/crates/tinywasm/src/interpreter/stack/value_stack.rs +++ b/crates/tinywasm/src/interpreter/stack/value_stack.rs @@ -261,7 +261,7 @@ impl ValueStack { WasmType::F64 => WasmValue::F64(f64::stack_pop(self)), WasmType::RefExtern => WasmValue::RefExtern(ExternRef::from_raw(ValueRef::stack_pop(self).raw())), WasmType::RefFunc => WasmValue::RefFunc(FuncRef::from_raw(ValueRef::stack_pop(self).raw())), - WasmType::V128 => WasmValue::V128(Value128::stack_pop(self).into()), + WasmType::V128 => WasmValue::V128(Value128::stack_pop(self).0), } } diff --git a/crates/tinywasm/src/interpreter/values.rs b/crates/tinywasm/src/interpreter/values.rs index a73528a..ddda317 100644 --- a/crates/tinywasm/src/interpreter/values.rs +++ b/crates/tinywasm/src/interpreter/values.rs @@ -105,7 +105,7 @@ impl TinyWasmValue { (Self::Value64(v), WasmType::F64) => Some(WasmValue::F64(f64::from_bits(v))), (Self::ValueRef(v), WasmType::RefExtern) => Some(WasmValue::RefExtern(ExternRef::from_raw(v.raw()))), (Self::ValueRef(v), WasmType::RefFunc) => Some(WasmValue::RefFunc(FuncRef::from_raw(v.raw()))), - (Self::Value128(v), WasmType::V128) => Some(WasmValue::V128((v).into())), + (Self::Value128(v), WasmType::V128) => Some(WasmValue::V128(v.0)), (_, WasmType::I32 | WasmType::F32) => None, (_, WasmType::I64 | WasmType::F64) => None, (_, WasmType::RefExtern | WasmType::RefFunc) => None, @@ -134,8 +134,8 @@ impl From<WasmValue> for TinyWasmValue { } } -impl From<i128> for TinyWasmValue { - fn from(value: i128) -> Self { +impl From<[u8; 16]> for TinyWasmValue { + fn from(value: [u8; 16]) -> Self { Self::Value128(Value128::from(value)) } } diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index 2afe3d8..c8ffe2c 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -36,7 +36,7 @@ pub enum ConstInstruction { I64Const(i64), F32Const(f32), F64Const(f64), - V128Const(i128), + V128Const([u8; 16]), GlobalGet(GlobalAddr), RefFunc(Option<FuncAddr>), RefExtern(Option<ExternAddr>), diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index c57edc0..d01214f 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -17,7 +17,7 @@ pub enum WasmValue { /// A 64-bit float. F64(f64), // /// A 128-bit vector - V128(i128), + V128([u8; 16]), RefExtern(ExternRef), RefFunc(FuncRef), } @@ -176,7 +176,7 @@ impl WasmValue { WasmType::I64 => Self::I64(0), WasmType::F32 => Self::F32(0.0), WasmType::F64 => Self::F64(0.0), - WasmType::V128 => Self::V128(0), + WasmType::V128 => Self::V128([0; 16]), WasmType::RefFunc => Self::RefFunc(FuncRef::null()), WasmType::RefExtern => Self::RefExtern(ExternRef::null()), } @@ -188,11 +188,7 @@ impl WasmValue { match (self, other) { (Self::I32(a), Self::I32(b)) => a == b, (Self::I64(a), Self::I64(b)) => a == b, - (Self::V128(a), Self::V128(b)) => { - let a_bytes = a.to_le_bytes(); - let b_bytes = b.to_le_bytes(); - a_bytes == b_bytes || Self::v128_nan_eq(a_bytes, b_bytes) - } + (Self::V128(a), Self::V128(b)) => a == b || Self::v128_nan_eq(*a, *b), (Self::RefExtern(addr), Self::RefExtern(addr2)) => addr == addr2, (Self::RefFunc(addr), Self::RefFunc(addr2)) => addr == addr2, (Self::F32(a), Self::F32(b)) => { @@ -293,8 +289,8 @@ impl WasmValue { } } - /// Return the `i128` from a `WasmValue`, if it is a `V128`. - pub const fn as_v128(&self) -> Option<i128> { + /// Return the raw little-endian bytes from a `WasmValue`, if it is a `V128`. + pub const fn as_v128(&self) -> Option<[u8; 16]> { match self { Self::V128(i) => Some(*i), _ => None, @@ -394,4 +390,4 @@ macro_rules! impl_conversion_for_wasmvalue { } } -impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, i128 => V128, ExternRef => RefExtern, FuncRef => RefFunc } +impl_conversion_for_wasmvalue! { i32 => I32, i64 => I64, f32 => F32, f64 => F64, [u8; 16] => V128, ExternRef => RefExtern, FuncRef => RefFunc } |
