Skip to content

Commit

Permalink
Merge #1772
Browse files Browse the repository at this point in the history
1772: Remove lifetime parameter from `NativeFunc` r=MarkMcCaskey a=MarkMcCaskey

Ran into this as an annoyance on #1739 ; will need to account for this when we fix the memory leak in `InstanceHandle`.

Note: the lifetime wasn't doing anything useful for us, this change doesn't make anything new possible (other than not having to deal with the lifetime parameter)

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
bors[bot] and Mark McCaskey authored Oct 27, 2020
2 parents 3663140 + 608fbdc commit 962f3be
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

### Changed

- [#1772](https://github.com/wasmerio/wasmer/pull/1772) Remove lifetime parameter from `NativeFunc`.
- [#1762](https://github.com/wasmerio/wasmer/pull/1762) Allow the `=` sign in a WASI environment variable value.

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl Function {
/// // This results in an error: `RuntimeError`
/// let sum_native = sum.native::<(i32, i32), i64>().unwrap();
/// ```
pub fn native<'a, Args, Rets>(&self) -> Result<NativeFunc<'a, Args, Rets>, RuntimeError>
pub fn native<Args, Rets>(&self) -> Result<NativeFunc<Args, Rets>, RuntimeError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand Down
18 changes: 9 additions & 9 deletions lib/api/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ use wasmer_vm::{

/// A WebAssembly function that can be called natively
/// (using the Native ABI).
pub struct NativeFunc<'a, Args = (), Rets = ()> {
pub struct NativeFunc<Args = (), Rets = ()> {
definition: FunctionDefinition,
store: Store,
address: *const VMFunctionBody,
vmctx: *mut VMContext,
arg_kind: VMFunctionKind,
// exported: ExportFunction,
_phantom: PhantomData<(&'a (), Args, Rets)>,
_phantom: PhantomData<(Args, Rets)>,
}

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

impl<'a, Args, Rets> NativeFunc<'a, Args, Rets>
impl<Args, Rets> NativeFunc<Args, Rets>
where
Args: WasmTypeList,
Rets: WasmTypeList,
Expand All @@ -57,12 +57,12 @@ where
}
}

impl<'a, Args, Rets> From<&NativeFunc<'a, Args, Rets>> for ExportFunction
impl<Args, Rets> From<&NativeFunc<Args, Rets>> for ExportFunction
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
fn from(other: &NativeFunc<'a, Args, Rets>) -> Self {
fn from(other: &NativeFunc<Args, Rets>) -> Self {
let signature = FunctionType::new(Args::wasm_types(), Rets::wasm_types());
Self {
address: other.address,
Expand All @@ -74,12 +74,12 @@ where
}
}

impl<'a, Args, Rets> From<NativeFunc<'a, Args, Rets>> for Function
impl<Args, Rets> From<NativeFunc<Args, Rets>> for Function
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
fn from(other: NativeFunc<'a, Args, Rets>) -> Self {
fn from(other: NativeFunc<Args, Rets>) -> Self {
let signature = FunctionType::new(Args::wasm_types(), Rets::wasm_types());
Self {
store: other.store,
Expand All @@ -98,7 +98,7 @@ where
macro_rules! impl_native_traits {
( $( $x:ident ),* ) => {
#[allow(unused_parens, non_snake_case)]
impl<'a $( , $x )*, Rets> NativeFunc<'a, ( $( $x ),* ), Rets>
impl<$( $x , )* Rets> NativeFunc<( $( $x ),* ), Rets>
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
Expand Down
26 changes: 26 additions & 0 deletions lib/api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,29 @@ fn native_function_works() -> Result<()> {

Ok(())
}

#[test]
fn function_outlives_instance() -> Result<()> {
let store = Store::default();
let wat = r#"(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add)
(export "sum" (func $sum_f)))
"#;

let f = {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &imports! {})?;
let f: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("sum")?;

assert_eq!(f.call(4, 5)?, 9);
f
};

assert_eq!(f.call(4, 5)?, 9);

Ok(())
}
123 changes: 60 additions & 63 deletions lib/emscripten/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,81 +122,78 @@ const STATIC_BASE: u32 = GLOBAL_BASE;
pub struct EmscriptenData<'a> {
pub globals: &'a EmscriptenGlobalsData,

pub malloc: Option<NativeFunc<'a, u32, u32>>,
pub free: Option<NativeFunc<'a, u32>>,
pub memalign: Option<NativeFunc<'a, (u32, u32), u32>>,
pub memset: Option<NativeFunc<'a, (u32, u32, u32), u32>>,
pub stack_alloc: Option<NativeFunc<'a, u32, u32>>,
pub malloc: Option<NativeFunc<u32, u32>>,
pub free: Option<NativeFunc<u32>>,
pub memalign: Option<NativeFunc<(u32, u32), u32>>,
pub memset: Option<NativeFunc<(u32, u32, u32), u32>>,
pub stack_alloc: Option<NativeFunc<u32, u32>>,
pub jumps: Vec<UnsafeCell<[u32; 27]>>,
pub opened_dirs: HashMap<i32, Box<*mut LibcDir>>,

pub dyn_call_i: Option<NativeFunc<'a, i32, i32>>,
pub dyn_call_ii: Option<NativeFunc<'a, (i32, i32), i32>>,
pub dyn_call_iii: Option<NativeFunc<'a, (i32, i32, i32), i32>>,
pub dyn_call_iiii: Option<NativeFunc<'a, (i32, i32, i32, i32), i32>>,
pub dyn_call_iifi: Option<NativeFunc<'a, (i32, i32, f64, i32), i32>>,
pub dyn_call_v: Option<NativeFunc<'a, i32>>,
pub dyn_call_vi: Option<NativeFunc<'a, (i32, i32)>>,
pub dyn_call_vii: Option<NativeFunc<'a, (i32, i32, i32)>>,
pub dyn_call_viii: Option<NativeFunc<'a, (i32, i32, i32, i32)>>,
pub dyn_call_viiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32)>>,
pub dyn_call_i: Option<NativeFunc<i32, i32>>,
pub dyn_call_ii: Option<NativeFunc<(i32, i32), i32>>,
pub dyn_call_iii: Option<NativeFunc<(i32, i32, i32), i32>>,
pub dyn_call_iiii: Option<NativeFunc<(i32, i32, i32, i32), i32>>,
pub dyn_call_iifi: Option<NativeFunc<(i32, i32, f64, i32), i32>>,
pub dyn_call_v: Option<NativeFunc<i32>>,
pub dyn_call_vi: Option<NativeFunc<(i32, i32)>>,
pub dyn_call_vii: Option<NativeFunc<(i32, i32, i32)>>,
pub dyn_call_viii: Option<NativeFunc<(i32, i32, i32, i32)>>,
pub dyn_call_viiii: Option<NativeFunc<(i32, i32, i32, i32, i32)>>,

// round 2
pub dyn_call_dii: Option<NativeFunc<'a, (i32, i32, i32), f64>>,
pub dyn_call_diiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32), f64>>,
pub dyn_call_iiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_dii: Option<NativeFunc<(i32, i32, i32), f64>>,
pub dyn_call_diiii: Option<NativeFunc<(i32, i32, i32, i32, i32), f64>>,
pub dyn_call_iiiii: Option<NativeFunc<(i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiiiiiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_vd: Option<NativeFunc<'a, (i32, f64)>>,
pub dyn_call_viiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_vd: Option<NativeFunc<(i32, f64)>>,
pub dyn_call_viiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiiiiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_iij: Option<NativeFunc<'a, (i32, i32, i32, i32), i32>>,
pub dyn_call_iji: Option<NativeFunc<'a, (i32, i32, i32, i32), i32>>,
pub dyn_call_iiji: Option<NativeFunc<'a, (i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiijj: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_j: Option<NativeFunc<'a, i32, i32>>,
pub dyn_call_ji: Option<NativeFunc<'a, (i32, i32), i32>>,
pub dyn_call_jii: Option<NativeFunc<'a, (i32, i32, i32), i32>>,
pub dyn_call_jij: Option<NativeFunc<'a, (i32, i32, i32, i32), i32>>,
pub dyn_call_jjj: Option<NativeFunc<'a, (i32, i32, i32, i32, i32), i32>>,
pub dyn_call_viiij: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiijiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_iij: Option<NativeFunc<(i32, i32, i32, i32), i32>>,
pub dyn_call_iji: Option<NativeFunc<(i32, i32, i32, i32), i32>>,
pub dyn_call_iiji: Option<NativeFunc<(i32, i32, i32, i32, i32), i32>>,
pub dyn_call_iiijj: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32), i32>>,
pub dyn_call_j: Option<NativeFunc<i32, i32>>,
pub dyn_call_ji: Option<NativeFunc<(i32, i32), i32>>,
pub dyn_call_jii: Option<NativeFunc<(i32, i32, i32), i32>>,
pub dyn_call_jij: Option<NativeFunc<(i32, i32, i32, i32), i32>>,
pub dyn_call_jjj: Option<NativeFunc<(i32, i32, i32, i32, i32), i32>>,
pub dyn_call_viiij: Option<NativeFunc<(i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiijiiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viiijiiiiii:
Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viij: Option<NativeFunc<'a, (i32, i32, i32, i32, i32)>>,
pub dyn_call_viiji: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viijiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viijj: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vj: Option<NativeFunc<'a, (i32, i32, i32)>>,
pub dyn_call_vjji: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vij: Option<NativeFunc<'a, (i32, i32, i32, i32)>>,
pub dyn_call_viji: Option<NativeFunc<'a, (i32, i32, i32, i32, i32)>>,
pub dyn_call_vijiii: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vijj: Option<NativeFunc<'a, (i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viid: Option<NativeFunc<'a, (i32, i32, i32, f64)>>,
pub dyn_call_vidd: Option<NativeFunc<'a, (i32, i32, f64, f64)>>,
pub dyn_call_viidii: Option<NativeFunc<'a, (i32, i32, i32, f64, i32, i32)>>,
Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viij: Option<NativeFunc<(i32, i32, i32, i32, i32)>>,
pub dyn_call_viiji: Option<NativeFunc<(i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viijiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viijj: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vj: Option<NativeFunc<(i32, i32, i32)>>,
pub dyn_call_vjji: Option<NativeFunc<(i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vij: Option<NativeFunc<(i32, i32, i32, i32)>>,
pub dyn_call_viji: Option<NativeFunc<(i32, i32, i32, i32, i32)>>,
pub dyn_call_vijiii: Option<NativeFunc<(i32, i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_vijj: Option<NativeFunc<(i32, i32, i32, i32, i32, i32)>>,
pub dyn_call_viid: Option<NativeFunc<(i32, i32, i32, f64)>>,
pub dyn_call_vidd: Option<NativeFunc<(i32, i32, f64, f64)>>,
pub dyn_call_viidii: Option<NativeFunc<(i32, i32, i32, f64, i32, i32)>>,
pub dyn_call_viidddddddd:
Option<NativeFunc<'a, (i32, i32, i32, f64, f64, f64, f64, f64, f64, f64, f64)>>,
Option<NativeFunc<(i32, i32, i32, f64, f64, f64, f64, f64, f64, f64, f64)>>,
pub temp_ret_0: i32,

pub stack_save: Option<NativeFunc<'a, (), i32>>,
pub stack_restore: Option<NativeFunc<'a, i32>>,
pub set_threw: Option<NativeFunc<'a, (i32, i32)>>,
pub stack_save: Option<NativeFunc<(), i32>>,
pub stack_restore: Option<NativeFunc<i32>>,
pub set_threw: Option<NativeFunc<(i32, i32)>>,
pub mapped_dirs: HashMap<String, PathBuf>,
}

Expand Down

0 comments on commit 962f3be

Please sign in to comment.