support u128

This commit is contained in:
vms
2021-04-23 10:13:11 +03:00
parent 9095389a5a
commit 34bf8a196a
22 changed files with 264 additions and 32 deletions

View File

@ -37,6 +37,7 @@ pub fn ser_type_size(ty: &IType) -> usize {
// Vec-like types are passed by pointer and size
IType::String | IType::ByteArray | IType::Array(_) => 2 * WASM_POINTER_SIZE,
IType::S64 | IType::U64 | IType::I64 | IType::F64 => 8,
IType::U128 => 16,
}
}
@ -47,6 +48,7 @@ pub fn ser_value_size(value: &IValue) -> usize {
IValue::S16(_) | IValue::U16(_) => 2,
IValue::S32(_) | IValue::U32(_) | IValue::F32(_) | IValue::I32(_) => 4,
IValue::S64(_) | IValue::U64(_) | IValue::F64(_) | IValue::I64(_) => 8,
IValue::U128(_) => 16,
IValue::String(_) | IValue::ByteArray(_) | IValue::Array(_) => 2 * 4,
IValue::Record(_) => 4,
}
@ -70,12 +72,13 @@ pub fn type_tag_form_itype(itype: &IType) -> u32 {
IType::U16 => 2, // u16
IType::U32 => 3, // u32
IType::U64 => 4, // u64
IType::S8 => 5, // i8
IType::S16 => 6, // i16
IType::S32 | IType::I32 => 7, // i32
IType::S64 | IType::I64 => 8, // i64
IType::F32 => 9, // f32
IType::F64 => 10, // f64
IType::U128 => 5, // u128
IType::S8 => 6, // i8
IType::S16 => 7, // i16
IType::S32 | IType::I32 => 8, // i32
IType::S64 | IType::I64 => 9, // i64
IType::F32 => 10, // f32
IType::F64 => 11, // f64
IType::ByteArray | IType::Array(_) | IType::Record(_) | IType::String => POINTER_CODE,
}
}
@ -89,12 +92,13 @@ pub fn type_tag_form_ivalue(itype: &IValue) -> u32 {
IValue::U16(_) => 2, // u16
IValue::U32(_) => 3, // u32
IValue::U64(_) => 4, // u64
IValue::S8(_) => 5, // i8
IValue::S16(_) => 6, // i16
IValue::S32(_) | IValue::I32(_) => 7, // i32
IValue::S64(_) | IValue::I64(_) => 8, // i64
IValue::F32(_) => 9, // f32
IValue::F64(_) => 10, // f64
IValue::U128(_) => 5, // u128
IValue::S8(_) => 6, // i8
IValue::S16(_) => 7, // i16
IValue::S32(_) | IValue::I32(_) => 8, // i32
IValue::S64(_) | IValue::I64(_) => 9, // i64
IValue::F32(_) => 10, // f32
IValue::F64(_) => 11, // f64
IValue::ByteArray(_) | IValue::Array(_) | IValue::Record(_) | IValue::String(_) => {
POINTER_CODE
}

View File

@ -35,6 +35,10 @@ macro_rules! value_der {
($self:expr, $offset:expr, 8) => {
crate::value_der!($self, $offset, @seq_start 0, 1, 2, 3, 4, 5, 6, 7 @seq_end);
};
($self:expr, $offset:expr, 16) => {
crate::value_der!($self, $offset, @seq_start 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 @seq_end);
};
}
#[macro_export]
@ -78,6 +82,16 @@ macro_rules! read_ty {
result
}
};
($func_name:ident, $ty:ty, 16) => {
pub fn $func_name(&self) -> $ty {
let offset = self.offset.get();
let result = <$ty>::from_le_bytes(crate::value_der!(self, offset, 16));
self.offset.set(offset + 16);
result
}
};
}
#[macro_export]

View File

@ -103,6 +103,7 @@ impl<'m> MemoryReader<'m> {
read_array_ty!(read_s64_array, i64, S64);
read_array_ty!(read_i64_array, i64, I64);
read_array_ty!(read_f64_array, f64, F64);
read_array_ty!(read_u128_array, u128, U128);
}
impl<'r, 'm> SequentialReader<'r, 'm> {
@ -129,4 +130,5 @@ impl<'r, 'm> SequentialReader<'r, 'm> {
read_ty!(read_u64, u64, 8);
read_ty!(read_i64, i64, 8);
read_ty!(read_f64, f64, 8);
read_ty!(read_u128, u128, 16);
}

View File

@ -29,6 +29,7 @@ where
IType::U64 => 0x07_u8.to_bytes(writer),
IType::F32 => 0x08_u8.to_bytes(writer),
IType::F64 => 0x09_u8.to_bytes(writer),
IType::U128 => 0x46_u8.to_bytes(writer),
IType::String => 0x0a_u8.to_bytes(writer),
IType::ByteArray => 0x3C_u8.to_bytes(writer),
IType::Array(ty) => {
@ -87,6 +88,7 @@ mod keyword {
custom_keyword!(u16);
custom_keyword!(u32);
custom_keyword!(u64);
custom_keyword!(u128);
custom_keyword!(string);
custom_keyword!(array);
}
@ -138,6 +140,10 @@ impl Parse<'_> for IType {
parser.parse::<keyword::f64>()?;
Ok(IType::F64)
} else if lookahead.peek::<keyword::u128>() {
parser.parse::<keyword::u128>()?;
Ok(IType::U128)
} else if lookahead.peek::<keyword::string>() {
parser.parse::<keyword::string>()?;

View File

@ -22,14 +22,14 @@ macro_rules! native {
}
}
impl TryFrom<&IValue> for $native_type {
impl TryFrom<IValue> for $native_type {
type Error = WasmValueNativeCastError;
fn try_from(w: &IValue) -> Result<Self, Self::Error> {
fn try_from(w: IValue) -> Result<Self, Self::Error> {
match w {
IValue::$variant(n) => Ok(n.clone()),
IValue::$variant(n) => Ok(n),
_ => Err(WasmValueNativeCastError {
from: w.clone(),
from: w,
to: <$native_type>::INTERFACE_TYPE,
}),
}
@ -48,4 +48,6 @@ native!(u32, U32);
native!(u64, U64);
native!(f32, F32);
native!(f64, F64);
native!(u128, U128);
native!(String, String);
native!(Vec<u8>, ByteArray);

View File

@ -41,6 +41,9 @@ pub enum IType {
/// A 64-bits float.
F64,
/// A 128-bit unsigned integer.
U128,
/// A string.
String,
@ -112,6 +115,7 @@ impl ToString for &IType {
IType::U64 => "u64".to_string(),
IType::F32 => "f32".to_string(),
IType::F64 => "f64".to_string(),
IType::U128 => "u128".to_string(),
IType::String => "string".to_string(),
IType::ByteArray => "array (u8)".to_string(),
IType::Array(ty) => format!("array ({})", ty.as_ref().to_string()),

View File

@ -38,6 +38,9 @@ pub enum IValue {
/// A 64-bits float.
F64(f64),
/// A 128-bits integer.
U128(u128),
/// A string.
String(String),