Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename NativeFunc to TypedFunction #2933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C

### Changed
- #2864 wasmer-cli: remove wasi-experimental-io-devices from default builds
- [#2933](https://github.com/wasmerio/wasmer/pull/2933) Rename NativeFunc to TypedFunction.
### Changed
- #2868 Removed loupe crate dependency

Expand Down
4 changes: 2 additions & 2 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
};
let instance = Instance::new(&module, &import_object).unwrap();
let dyn_f: &Function = instance.exports.get("add").unwrap();
let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap();
let f: TypedFunction<(i32, i32), i32> = dyn_f.native().unwrap();

c.bench_function(&format!("basic static func {}", compiler_name), |b| {
b.iter(|| {
Expand All @@ -50,7 +50,7 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
});

let dyn_f_many: &Function = instance.exports.get("add20").unwrap();
let f_many: NativeFunc<
let f_many: TypedFunction<
(
i32,
i32,
Expand Down
4 changes: 2 additions & 2 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use anyhow::bail;
use std::fmt;
use wasmer::{imports, wat2wasm, Function, Instance, Module, NativeFunc, Store};
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;

Expand Down Expand Up @@ -82,7 +82,7 @@ fn main() -> anyhow::Result<()> {
//
// Get the `run` function which we'll use as our entrypoint.
println!("Calling `run` function...");
let run_func: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("run")?;
let run_func: TypedFunction<(i32, i32), i32> = instance.exports.get_native_function("run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(1, 7) {
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(result.to_vec(), vec![Value::I32(3)]);

// That was fun. But what if we can get rid of the `Value`s? Well,
// that's possible with the `NativeFunction` API. The function
// that's possible with the `TypedFunction` API. The function
// will use native Rust values.
//
// Note that `native` takes 2 generic parameters: `Args` and
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! cargo run --example hello-world --release --features "cranelift"
//! ```
use wasmer::{imports, wat2wasm, Function, Instance, Module, NativeFunc, Store};
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;

Expand Down Expand Up @@ -73,11 +73,11 @@ fn main() -> anyhow::Result<()> {
// and is ready to execute.
let instance = Instance::new(&module, &import_object)?;

// We get the `NativeFunc` with no parameters and no results from the instance.
// We get the `TypedFunction` with no parameters and no results from the instance.
//
// Recall that the Wasm module exported a function named "run", this is getting
// that exported function from the `Instance`.
let run_func: NativeFunc<(), ()> = instance.exports.get_native_function("run")?;
let run_func: TypedFunction<(), ()> = instance.exports.get_native_function("run")?;

// Finally, we call our exported Wasm function which will call our "say_hello"
// function and return.
Expand Down
8 changes: 4 additions & 4 deletions examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! Ready?
use std::mem;
use wasmer::{imports, wat2wasm, Bytes, Instance, Module, NativeFunc, Pages, Store};
use wasmer::{imports, wat2wasm, Bytes, Instance, Module, Pages, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;

Expand Down Expand Up @@ -73,9 +73,9 @@ fn main() -> anyhow::Result<()> {
// The module exports some utility functions, let's get them.
//
// These function will be used later in this example.
let mem_size: NativeFunc<(), i32> = instance.exports.get_native_function("mem_size")?;
let get_at: NativeFunc<i32, i32> = instance.exports.get_native_function("get_at")?;
let set_at: NativeFunc<(i32, i32), ()> = instance.exports.get_native_function("set_at")?;
let mem_size: TypedFunction<(), i32> = instance.exports.get_native_function("mem_size")?;
let get_at: TypedFunction<i32, i32> = instance.exports.get_native_function("get_at")?;
let set_at: TypedFunction<(i32, i32), ()> = instance.exports.get_native_function("set_at")?;
let memory = instance.exports.get_memory("memory")?;

// We now have an instance ready to be used.
Expand Down
4 changes: 2 additions & 2 deletions examples/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasmer::{
imports, wat2wasm, Function, Instance, Module, NativeFunc, Store, TableType, Type, Value,
imports, wat2wasm, Function, Instance, Module, Store, TableType, Type, TypedFunction, Value,
};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_universal::Universal;
Expand Down Expand Up @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
// We get our function that calls (i32, i32) -> i32 functions via table.
// The first argument is the table index and the next 2 are the 2 arguments
// to be passed to the function found in the table.
let call_via_table: NativeFunc<(i32, i32, i32), i32> =
let call_via_table: TypedFunction<(i32, i32, i32), i32> =
instance.exports.get_native_function("call_callback")?;

// And then call it with table index 1 and arguments 2 and 7.
Expand Down
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
11 changes: 7 additions & 4 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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::RuntimeError;
use crate::js::TypedFunction;
use crate::js::WasmerEnv;
pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, WithoutEnv};
use js_sys::{Array, Function as JSFunction};
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,10 @@ impl Function {
}
}

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

#[track_caller]
Expand Down
9 changes: 8 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,10 @@ 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
Loading