Statically register host WASM functions (#10394)

* Statically register host WASM functions

* Fix `substrate-test-client` compilation

* Move `ExtendedHostFunctions` to `sp-wasm-interface`

* Fix `sp-runtime-interface` tests' compilation

* Fix `sc-executor-wasmtime` tests' compilation

* Use `runtime_interface` macro in `test-runner`

* Fix `sc-executor` tests' compilation

* Reformatting/`rustfmt`

* Add an extra comment regarding the `H` generic arg in `create_runtime`

* Even more `rustfmt`

* Depend on `wasmtime` without default features in `sp-wasm-interface`

* Bump version of `sp-wasm-interface` to 4.0.1

* Bump `sp-wasm-interface` in `Cargo.lock` too

* Bump all of the `sp-wasm-interface` requirements to 4.0.1

Maybe this will appease cargo-unleash?

* Revert "Bump all of the `sp-wasm-interface` requirements to 4.0.1"

This reverts commit 0f7ccf8e0f371542861121b145ab87af6541ac30.

* Make `cargo-unleash` happy (maybe)

* Use `cargo-unleash` to bump the crates' versions

* Align to review comments
This commit is contained in:
Koute
2021-12-14 17:26:40 +09:00
committed by GitHub
parent 23c5b6755b
commit 7711f5266e
36 changed files with 742 additions and 570 deletions
@@ -95,36 +95,36 @@ macro_rules! impl_traits_for_primitives {
}
impl_traits_for_primitives! {
u8, u8,
u16, u16,
u8, u32,
u16, u32,
u32, u32,
u64, u64,
i8, i8,
i16, i16,
i8, i32,
i16, i32,
i32, i32,
i64, i64,
}
/// `bool` is passed as `u8`.
/// `bool` is passed as `u32`.
///
/// - `1`: true
/// - `0`: false
impl RIType for bool {
type FFIType = u8;
type FFIType = u32;
}
#[cfg(not(feature = "std"))]
impl IntoFFIValue for bool {
type Owned = ();
fn into_ffi_value(&self) -> WrappedFFIValue<u8> {
fn into_ffi_value(&self) -> WrappedFFIValue<u32> {
if *self { 1 } else { 0 }.into()
}
}
#[cfg(not(feature = "std"))]
impl FromFFIValue for bool {
fn from_ffi_value(arg: u8) -> bool {
fn from_ffi_value(arg: u32) -> bool {
arg == 1
}
}
@@ -133,14 +133,14 @@ impl FromFFIValue for bool {
impl FromFFIValue for bool {
type SelfInstance = bool;
fn from_ffi_value(_: &mut dyn FunctionContext, arg: u8) -> Result<bool> {
fn from_ffi_value(_: &mut dyn FunctionContext, arg: u32) -> Result<bool> {
Ok(arg == 1)
}
}
#[cfg(feature = "std")]
impl IntoFFIValue for bool {
fn into_ffi_value(self, _: &mut dyn FunctionContext) -> Result<u8> {
fn into_ffi_value(self, _: &mut dyn FunctionContext) -> Result<u32> {
Ok(if self { 1 } else { 0 })
}
}
@@ -80,17 +80,17 @@
//!
//! | Type | FFI type | Conversion |
//! |----|----|----|
//! | `u8` | `u8` | `Identity` |
//! | `u16` | `u16` | `Identity` |
//! | `u8` | `u32` | zero-extended to 32-bits |
//! | `u16` | `u32` | zero-extended to 32-bits |
//! | `u32` | `u32` | `Identity` |
//! | `u64` | `u64` | `Identity` |
//! | `i128` | `u32` | `v.as_ptr()` (pointer to a 16 byte array) |
//! | `i8` | `i8` | `Identity` |
//! | `i16` | `i16` | `Identity` |
//! | `i8` | `i32` | sign-extended to 32-bits |
//! | `i16` | `i32` | sign-extended to 32-bits |
//! | `i32` | `i32` | `Identity` |
//! | `i64` | `i64` | `Identity` |
//! | `u128` | `u32` | `v.as_ptr()` (pointer to a 16 byte array) |
//! | `bool` | `u8` | `if v { 1 } else { 0 }` |
//! | `bool` | `u32` | `if v { 1 } else { 0 }` |
//! | `&str` | `u64` | <code>v.len() 32bit << 32 &#124; v.as_ptr() 32bit</code> |
//! | `&[u8]` | `u64` | <code>v.len() 32bit << 32 &#124; v.as_ptr() 32bit</code> |
//! | `Vec<u8>` | `u64` | <code>v.len() 32bit << 32 &#124; v.as_ptr() 32bit</code> |
@@ -325,7 +325,9 @@ pub use util::{pack_ptr_and_len, unpack_ptr_and_len};
pub trait RIType {
/// The ffi type that is used to represent `Self`.
#[cfg(feature = "std")]
type FFIType: sp_wasm_interface::IntoValue + sp_wasm_interface::TryFromValue;
type FFIType: sp_wasm_interface::IntoValue
+ sp_wasm_interface::TryFromValue
+ sp_wasm_interface::WasmTy;
#[cfg(not(feature = "std"))]
type FFIType;
}
@@ -403,11 +403,11 @@ pub struct Enum<T: Copy + Into<u8> + TryFrom<u8>>(PhantomData<T>);
#[cfg(feature = "std")]
impl<T: Copy + Into<u8> + TryFrom<u8>> PassByImpl<T> for Enum<T> {
fn into_ffi_value(instance: T, _: &mut dyn FunctionContext) -> Result<Self::FFIType> {
Ok(instance.into())
Ok(instance.into() as u32)
}
fn from_ffi_value(_: &mut dyn FunctionContext, arg: Self::FFIType) -> Result<T> {
T::try_from(arg).map_err(|_| format!("Invalid enum discriminant: {}", arg))
T::try_from(arg as u8).map_err(|_| format!("Invalid enum discriminant: {}", arg))
}
}
@@ -417,17 +417,17 @@ impl<T: Copy + Into<u8> + TryFrom<u8, Error = ()>> PassByImpl<T> for Enum<T> {
fn into_ffi_value(instance: &T) -> WrappedFFIValue<Self::FFIType, Self::Owned> {
let value: u8 = (*instance).into();
value.into()
(value as u32).into()
}
fn from_ffi_value(arg: Self::FFIType) -> T {
T::try_from(arg).expect("Host to wasm provides a valid enum discriminant; qed")
T::try_from(arg as u8).expect("Host to wasm provides a valid enum discriminant; qed")
}
}
/// The type is passed as `u8`.
/// The type is passed as `u32`.
///
/// The value is corresponds to the discriminant of the variant.
impl<T: Copy + Into<u8> + TryFrom<u8>> RIType for Enum<T> {
type FFIType = u8;
type FFIType = u32;
}