mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 13:18:20 +00:00
committed by
Manos Pitsidianakis
parent
59ee09bccf
commit
da4e9fe617
@@ -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 {
|
||||
@@ -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>,
|
||||
/// }
|
||||
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
@@ -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.
|
||||
|
||||
11
lib/api/src/js/externals/function.rs
vendored
11
lib/api/src/js/externals/function.rs
vendored
@@ -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};
|
||||
@@ -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
|
||||
///
|
||||
@@ -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,
|
||||
@@ -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]
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user