Skip to content

Commit

Permalink
Simplify Func's closure state.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Apr 20, 2020
1 parent b2b5e8d commit e4b07c5
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions crates/api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,8 @@ macro_rules! getters {

// Pass the instance into the closure so that we keep it live for the lifetime
// of the closure. Pass the export in so that we can call it.
struct ClosureState {
_instance: InstanceHandle,
export: ExportFunction
}
let closure = ClosureState {
_instance: self.instance.clone(),
export: self.export.clone()
};
let instance = self.instance.clone();
let export = self.export.clone();

// ... and then once we've passed the typechecks we can hand out our
// object since our `transmute` below should be safe!
Expand All @@ -194,12 +188,17 @@ macro_rules! getters {
*mut VMContext,
$($args,)*
) -> R,
>(closure.export.address);
>(export.address);
let mut ret = None;
$(let $args = $args.into_abi();)*
wasmtime_runtime::catch_traps(closure.export.vmctx, || {
ret = Some(fnptr(closure.export.vmctx, ptr::null_mut(), $($args,)*));
wasmtime_runtime::catch_traps(export.vmctx, || {
ret = Some(fnptr(export.vmctx, ptr::null_mut(), $($args,)*));
}).map_err(Trap::from_jit)?;

// We're holding this handle just to ensure that the instance stays
// live while we call into it.
drop(&instance);

Ok(ret.unwrap())
}
})
Expand Down

0 comments on commit e4b07c5

Please sign in to comment.