mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 16:57:58 +00:00
Support u128/i128 in runtime interface (#4703)
* Support `u128`/`i128` in runtime interface This implements support for `u128`/`i128` as parameters/return value in runtime interfaces. As we can not pass them as identity, as for the other primitives types, we pass them as an pointer to an `[u8; 16]` array. * Remove some unsafe code usage
This commit is contained in:
@@ -471,3 +471,55 @@ impl<T: sp_wasm_interface::PointerType> IntoFFIValue for Pointer<T> {
|
||||
Ok(self.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement the traits for `u128`/`i128`
|
||||
macro_rules! for_u128_i128 {
|
||||
($type:ty) => {
|
||||
/// `u128`/`i128` is passed as `u32`.
|
||||
///
|
||||
/// The `u32` is a pointer to an `[u8; 16]` array.
|
||||
impl RIType for $type {
|
||||
type FFIType = u32;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
impl IntoFFIValue for $type {
|
||||
type Owned = ();
|
||||
|
||||
fn into_ffi_value(&self) -> WrappedFFIValue<u32> {
|
||||
unsafe { (mem::transmute::<&Self, *const u8>(self) as u32).into() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
impl FromFFIValue for $type {
|
||||
fn from_ffi_value(arg: u32) -> $type {
|
||||
<$type>::from_le_bytes(<[u8; mem::size_of::<$type>()]>::from_ffi_value(arg))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl FromFFIValue for $type {
|
||||
type SelfInstance = $type;
|
||||
|
||||
fn from_ffi_value(context: &mut dyn FunctionContext, arg: u32) -> Result<$type> {
|
||||
let data = context.read_memory(Pointer::new(arg), mem::size_of::<$type>() as u32)?;
|
||||
let mut res = [0u8; mem::size_of::<$type>()];
|
||||
res.copy_from_slice(&data);
|
||||
Ok(<$type>::from_le_bytes(res))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl IntoFFIValue for $type {
|
||||
fn into_ffi_value(self, context: &mut dyn FunctionContext) -> Result<u32> {
|
||||
let addr = context.allocate_memory(mem::size_of::<$type>() as u32)?;
|
||||
context.write_memory(addr, &self.to_le_bytes())?;
|
||||
Ok(addr.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for_u128_i128!(u128);
|
||||
for_u128_i128!(i128);
|
||||
|
||||
Reference in New Issue
Block a user