Skip to content

Commit

Permalink
Merge #1226
Browse files Browse the repository at this point in the history
1226: chore(runtime-core) Some code clean ups r=syrusakbary a=Hywan

Extracted from #1018. Opinionated patch.

Some code clean ups, mostly renaming `ExternalFunction` to `HostFunction`.

Co-authored-by: Ivan Enderlin <[email protected]>
Co-authored-by: Ivan Enderlin <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
4 people authored Feb 17, 2020
2 parents 7cf3830 + abd44e9 commit bcb1f04
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct Wasm {
pub(crate) invoke_env: Option<NonNull<c_void>>,
}

impl Kind for Wasm {}

impl Wasm {
/// Create new `Wasm` from given parts.
pub unsafe fn from_raw_parts(
Expand All @@ -70,7 +72,6 @@ impl Wasm {
/// by the host.
pub struct Host(());

impl Kind for Wasm {}
impl Kind for Host {}

/// Represents a list of WebAssembly values.
Expand Down Expand Up @@ -110,14 +111,15 @@ pub trait WasmTypeList {
Rets: WasmTypeList;
}

/// Empty trait to specify the kind of `ExternalFunction`: With or
/// Empty trait to specify the kind of `HostFunction`: With or
/// without a `vm::Ctx` argument. See the `ExplicitVmCtx` and the
/// `ImplicitVmCtx` structures.
///
/// This type is never aimed to be used by a user. It is used by the
/// This trait is never aimed to be used by a user. It is used by the
/// trait system to automatically generate an appropriate `wrap`
/// function.
pub trait ExternalFunctionKind {}
#[doc(hidden)]
pub trait HostFunctionKind {}

/// This empty structure indicates that an external function must
/// contain an explicit `vm::Ctx` argument (at first position).
Expand All @@ -127,8 +129,11 @@ pub trait ExternalFunctionKind {}
/// x + 1
/// }
/// ```
#[doc(hidden)]
pub struct ExplicitVmCtx {}

impl HostFunctionKind for ExplicitVmCtx {}

/// This empty structure indicates that an external function has no
/// `vm::Ctx` argument (at first position). Its signature is:
///
Expand All @@ -139,14 +144,13 @@ pub struct ExplicitVmCtx {}
/// ```
pub struct ImplicitVmCtx {}

impl ExternalFunctionKind for ExplicitVmCtx {}
impl ExternalFunctionKind for ImplicitVmCtx {}
impl HostFunctionKind for ImplicitVmCtx {}

/// Represents a function that can be converted to a `vm::Func`
/// (function pointer) that can be called within WebAssembly.
pub trait ExternalFunction<Kind, Args, Rets>
pub trait HostFunction<Kind, Args, Rets>
where
Kind: ExternalFunctionKind,
Kind: HostFunctionKind,
Args: WasmTypeList,
Rets: WasmTypeList,
{
Expand Down Expand Up @@ -227,8 +231,8 @@ where
/// Creates a new `Func`.
pub fn new<F, Kind>(func: F) -> Func<'a, Args, Rets, Host>
where
Kind: ExternalFunctionKind,
F: ExternalFunction<Kind, Args, Rets>,
Kind: HostFunctionKind,
F: HostFunction<Kind, Args, Rets>,
{
let (func, func_env) = func.to_raw();

Expand Down Expand Up @@ -382,7 +386,7 @@ macro_rules! impl_traits {
}

#[allow(unused_parens)]
impl< $( $x, )* Rets, Trap, FN > ExternalFunction<ExplicitVmCtx, ( $( $x ),* ), Rets> for FN
impl< $( $x, )* Rets, Trap, FN > HostFunction<ExplicitVmCtx, ( $( $x ),* ), Rets> for FN
where
$( $x: WasmExternType, )*
Rets: WasmTypeList,
Expand Down Expand Up @@ -498,7 +502,7 @@ macro_rules! impl_traits {
}

#[allow(unused_parens)]
impl< $( $x, )* Rets, Trap, FN > ExternalFunction<ImplicitVmCtx, ( $( $x ),* ), Rets> for FN
impl< $( $x, )* Rets, Trap, FN > HostFunction<ImplicitVmCtx, ( $( $x ),* ), Rets> for FN
where
$( $x: WasmExternType, )*
Rets: WasmTypeList,
Expand Down

0 comments on commit bcb1f04

Please sign in to comment.