Skip to content

Commit

Permalink
Rename NativeFunc to TypedFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
silwol committed Jun 6, 2022
1 parent 09d5d6d commit e30098b
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 116 deletions.
4 changes: 2 additions & 2 deletions lib/api/src/js/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<ExportError> for HostEnvInitError {
/// This trait can be derived like so:
///
/// ```
/// use wasmer::{WasmerEnv, LazyInit, Memory, NativeFunc};
/// use wasmer::{WasmerEnv, LazyInit, Memory, TypedFunction};
///
/// #[derive(WasmerEnv, Clone)]
/// pub struct MyEnvWithNoInstanceData {
Expand All @@ -41,7 +41,7 @@ impl From<ExportError> for HostEnvInitError {
/// #[wasmer(export)]
/// memory: LazyInit<Memory>,
/// #[wasmer(export(name = "real_name"))]
/// func: LazyInit<NativeFunc<(i32, i32), i32>>,
/// func: LazyInit<TypedFunction<(i32, i32), i32>>,
/// #[wasmer(export(optional = true, alias = "memory2", alias = "_memory2"))]
/// optional_memory: LazyInit<Memory>,
/// }
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/js/exports.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::js::export::Export;
use crate::js::externals::{Extern, Function, Global, Memory, Table};
use crate::js::native::NativeFunc;
use crate::js::native::TypedFunction;
use crate::js::WasmTypeList;
use indexmap::IndexMap;
use std::fmt;
Expand Down Expand Up @@ -134,11 +134,11 @@ impl Exports {
self.get(name)
}

/// Get an export as a `NativeFunc`.
/// Get an export as a `TypedFunction`.
pub fn get_native_function<Args, Rets>(
&self,
name: &str,
) -> Result<NativeFunc<Args, Rets>, ExportError>
) -> Result<TypedFunction<Args, Rets>, ExportError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand Down Expand Up @@ -309,7 +309,7 @@ pub trait Exportable<'a>: Sized {
}

/// A trait for accessing exports (like [`Exportable`]) but it takes generic
/// `Args` and `Rets` parameters so that `NativeFunc` can be accessed directly
/// `Args` and `Rets` parameters so that `TypedFunction` can be accessed directly
/// as well.
pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Sized {
/// Get an export with the given generics.
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::js::externals::Extern;
use crate::js::store::Store;
use crate::js::types::{param_from_js, AsJs /* ValFuncRef */, Val};
use crate::js::FunctionType;
use crate::js::NativeFunc;
use crate::js::TypedFunction;
use crate::js::RuntimeError;
use crate::js::WasmerEnv;
pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
Expand Down Expand Up @@ -490,7 +490,7 @@ impl Function {
}

/// Transform this WebAssembly function into a function with the
/// native ABI. See [`NativeFunc`] to learn more.
/// native ABI. See [`TypedFunction`] to learn more.
///
/// # Examples
///
Expand Down Expand Up @@ -564,7 +564,7 @@ impl Function {
/// // This results in an error: `RuntimeError`
/// let sum_native = sum.native::<(i32, i32), i64>().unwrap();
/// ```
pub fn native<Args, Rets>(&self) -> Result<NativeFunc<Args, Rets>, RuntimeError>
pub fn native<Args, Rets>(&self) -> Result<TypedFunction<Args, Rets>, RuntimeError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand Down Expand Up @@ -597,7 +597,7 @@ impl Function {
}
}

Ok(NativeFunc::new(self.store.clone(), self.exported.clone()))
Ok(TypedFunction::new(self.store.clone(), self.exported.clone()))
}

#[track_caller]
Expand Down
6 changes: 5 additions & 1 deletion lib/api/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub use crate::js::instance::{Instance, InstantiationError};
pub use crate::js::js_import_object::JsImportObject;
pub use crate::js::mem_access::{MemoryAccessError, WasmRef, WasmSlice, WasmSliceIter};
pub use crate::js::module::{Module, ModuleTypeHints};
pub use crate::js::native::NativeFunc;
pub use crate::js::native::TypedFunction;
pub use crate::js::ptr::{Memory32, Memory64, MemorySize, WasmPtr, WasmPtr64};
pub use crate::js::trap::RuntimeError;

Expand All @@ -82,3 +82,7 @@ pub use wat::parse_bytes as wat2wasm;

/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// This type is deprecated, it has been replaced by TypedFunction.
#[deprecated(since = "3.0.0", note = "NativeFunc has been replaced by TypedFunction")]
pub type NativeFunc<Args = (), Rets = ()> = TypedFunction<Args, Rets>;
22 changes: 11 additions & 11 deletions lib/api/src/js/native.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Native Functions.
//!
//! This module creates the helper `NativeFunc` that let us call WebAssembly
//! This module creates the helper `TypedFunction` that let us call WebAssembly
//! functions with the native ABI, that is:
//!
//! ```ignore
//! let add_one = instance.exports.get_function("function_name")?;
//! let add_one_native: NativeFunc<i32, i32> = add_one.native().unwrap();
//! let add_one_native: TypedFunction<i32, i32> = add_one.native().unwrap();
//! ```
use std::marker::PhantomData;

Expand All @@ -21,15 +21,15 @@ use wasmer_types::NativeWasmType;
/// A WebAssembly function that can be called natively
/// (using the Native ABI).
#[derive(Clone)]
pub struct NativeFunc<Args = (), Rets = ()> {
pub struct TypedFunction<Args = (), Rets = ()> {
store: Store,
exported: VMFunction,
_phantom: PhantomData<(Args, Rets)>,
}

unsafe impl<Args, Rets> Send for NativeFunc<Args, Rets> {}
unsafe impl<Args, Rets> Send for TypedFunction<Args, Rets> {}

impl<Args, Rets> NativeFunc<Args, Rets>
impl<Args, Rets> TypedFunction<Args, Rets>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand All @@ -43,22 +43,22 @@ where
}
}

impl<Args, Rets> From<&NativeFunc<Args, Rets>> for VMFunction
impl<Args, Rets> From<&TypedFunction<Args, Rets>> for VMFunction
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
fn from(other: &NativeFunc<Args, Rets>) -> Self {
fn from(other: &TypedFunction<Args, Rets>) -> Self {
other.exported.clone()
}
}

impl<Args, Rets> From<NativeFunc<Args, Rets>> for Function
impl<Args, Rets> From<TypedFunction<Args, Rets>> for Function
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
fn from(other: NativeFunc<Args, Rets>) -> Self {
fn from(other: TypedFunction<Args, Rets>) -> Self {
Self {
store: other.store,
exported: other.exported,
Expand All @@ -69,7 +69,7 @@ where
macro_rules! impl_native_traits {
( $( $x:ident ),* ) => {
#[allow(unused_parens, non_snake_case)]
impl<$( $x , )* Rets> NativeFunc<( $( $x ),* ), Rets>
impl<$( $x , )* Rets> TypedFunction<( $( $x ),* ), Rets>
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
Expand Down Expand Up @@ -107,7 +107,7 @@ macro_rules! impl_native_traits {
}

#[allow(unused_parens)]
impl<'a, $( $x, )* Rets> crate::js::exports::ExportableWithGenerics<'a, ($( $x ),*), Rets> for NativeFunc<( $( $x ),* ), Rets>
impl<'a, $( $x, )* Rets> crate::js::exports::ExportableWithGenerics<'a, ($( $x ),*), Rets> for TypedFunction<( $( $x ),* ), Rets>
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
Expand Down
6 changes: 3 additions & 3 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@
//! from any instance via `instance.exports`:
//!
//! ```
//! # use wasmer::{imports, Instance, Function, Memory, NativeFunc};
//! # use wasmer::{imports, Instance, Function, Memory, TypedFunction};
//! # fn exports_example(instance: &Instance) -> anyhow::Result<()> {
//! let memory = instance.exports.get_memory("memory")?;
//! let memory: &Memory = instance.exports.get("some_other_memory")?;
//! let add: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("add")?;
//! let add: TypedFunction<(i32, i32), i32> = instance.exports.get_native_function("add")?;
//! let result = add.call(5, 37)?;
//! assert_eq!(result, 42);
//! # Ok(())
Expand Down Expand Up @@ -215,7 +215,7 @@
//!
//! In the `wasmer` API we support functions which take their arguments and
//! return their results dynamically, [`Function`], and functions which
//! take their arguments and return their results statically, [`NativeFunc`].
//! take their arguments and return their results statically, [`TypedFunction`].
//!
//! ### Memories
//!
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/sys/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<ExportError> for HostEnvInitError {
/// This trait can be derived like so:
///
/// ```
/// use wasmer::{WasmerEnv, LazyInit, Memory, NativeFunc};
/// use wasmer::{WasmerEnv, LazyInit, Memory, TypedFunction};
///
/// #[derive(WasmerEnv, Clone)]
/// pub struct MyEnvWithNoInstanceData {
Expand All @@ -41,7 +41,7 @@ impl From<ExportError> for HostEnvInitError {
/// #[wasmer(export)]
/// memory: LazyInit<Memory>,
/// #[wasmer(export(name = "real_name"))]
/// func: LazyInit<NativeFunc<(i32, i32), i32>>,
/// func: LazyInit<TypedFunction<(i32, i32), i32>>,
/// #[wasmer(export(optional = true, alias = "memory2", alias = "_memory2"))]
/// optional_memory: LazyInit<Memory>,
/// }
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/sys/exports.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::sys::externals::{Extern, Function, Global, Memory, Table};
use crate::sys::native::NativeFunc;
use crate::sys::native::TypedFunction;
use crate::sys::WasmTypeList;
use indexmap::IndexMap;
use std::fmt;
Expand Down Expand Up @@ -134,11 +134,11 @@ impl Exports {
self.get(name)
}

/// Get an export as a `NativeFunc`.
/// Get an export as a `TypedFunction`.
pub fn get_native_function<Args, Rets>(
&self,
name: &str,
) -> Result<NativeFunc<Args, Rets>, ExportError>
) -> Result<TypedFunction<Args, Rets>, ExportError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand Down Expand Up @@ -315,7 +315,7 @@ pub trait Exportable<'a>: Sized {
}

/// A trait for accessing exports (like [`Exportable`]) but it takes generic
/// `Args` and `Rets` parameters so that `NativeFunc` can be accessed directly
/// `Args` and `Rets` parameters so that `TypedFunction` can be accessed directly
/// as well.
pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Sized {
/// Get an export with the given generics.
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::sys::externals::Extern;
use crate::sys::store::Store;
use crate::sys::types::{Val, ValFuncRef};
use crate::sys::FunctionType;
use crate::sys::NativeFunc;
use crate::sys::TypedFunction;
use crate::sys::RuntimeError;
use crate::sys::WasmerEnv;
pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
Expand Down Expand Up @@ -561,7 +561,7 @@ impl Function {
}

/// Transform this WebAssembly function into a function with the
/// native ABI. See [`NativeFunc`] to learn more.
/// native ABI. See [`TypedFunction`] to learn more.
///
/// # Examples
///
Expand Down Expand Up @@ -635,7 +635,7 @@ impl Function {
/// // This results in an error: `RuntimeError`
/// let sum_native = sum.native::<(i32, i32), i64>().unwrap();
/// ```
pub fn native<Args, Rets>(&self) -> Result<NativeFunc<Args, Rets>, RuntimeError>
pub fn native<Args, Rets>(&self) -> Result<TypedFunction<Args, Rets>, RuntimeError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand Down Expand Up @@ -668,7 +668,7 @@ impl Function {
}
}

Ok(NativeFunc::new(self.store.clone(), self.exported.clone()))
Ok(TypedFunction::new(self.store.clone(), self.exported.clone()))
}

#[track_caller]
Expand Down
7 changes: 6 additions & 1 deletion lib/api/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub use crate::sys::imports::Imports;
pub use crate::sys::instance::{Instance, InstantiationError};
pub use crate::sys::mem_access::{MemoryAccessError, WasmRef, WasmSlice, WasmSliceIter};
pub use crate::sys::module::Module;
pub use crate::sys::native::NativeFunc;
pub use crate::sys::native::TypedFunction;

pub use crate::sys::ptr::{Memory32, Memory64, MemorySize, WasmPtr, WasmPtr64};
pub use crate::sys::store::{Store, StoreObject};
pub use crate::sys::tunables::BaseTunables;
Expand Down Expand Up @@ -126,3 +127,7 @@ pub type JIT = Universal;
#[cfg(feature = "native")]
#[deprecated(since = "2.0.0", note = "Please use the `native` feature instead")]
pub type Native = Dylib;

/// This type is deprecated, it has been replaced by TypedFunction.
#[deprecated(since = "3.0.0", note = "NativeFunc has been replaced by TypedFunction")]
pub type NativeFunc<Args = (), Rets = ()> = TypedFunction<Args, Rets>;
Loading

0 comments on commit e30098b

Please sign in to comment.