Skip to content

Commit

Permalink
feat(vm) Remove InstanceHandle::from_vmctx.
Browse files Browse the repository at this point in the history
The `InstanceHandle::from_vmctx` method is only used by
`CallThreadState::any_instance`, where a given closure expect an
`&InstanceHandle`. However, the only closure that exists in the code
only reads an `&Instance` from the `InstanceHandle`.

So… this patch updates this only closure and `any_instance` to work on
an `&Instance` directly, which allows us to remove
`InstanceHandle::from_vmctx`. It results to less code and less pointer
arithmetic.
  • Loading branch information
Hywan committed Nov 26, 2020
1 parent 7f06d4f commit f968b24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
19 changes: 0 additions & 19 deletions lib/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,25 +1052,6 @@ impl InstanceHandle {
Ok(handle)
}

/// Create a new `InstanceHandle` pointing at the instance
/// pointed to by the given `VMContext` pointer.
///
/// # Safety
/// This is unsafe because it doesn't work on just any `VMContext`, it must
/// be a `VMContext` allocated as part of an `Instance`.
pub(crate) unsafe fn from_vmctx(vmctx: *const VMContext) -> Self {
/*
let instance = (&*vmctx).instance();
Self {
instance: Arc::from_raw(instance as *const InstanceAllocator),
// We consider `vmctx` owns the `Instance`, not `Self`.
//owned_instance: false,
}
*/
todo!()
}

/// Return a reference to the contained `Instance`.
pub(crate) fn instance(&self) -> &InstanceAllocator {
&self.instance
Expand Down
19 changes: 12 additions & 7 deletions lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! signalhandling mechanisms.
use super::trapcode::TrapCode;
use crate::instance::{InstanceHandle, SignalHandler};
use crate::instance::{Instance, SignalHandler};
use crate::vmcontext::{VMFunctionBody, VMFunctionEnvironment, VMTrampoline};
use backtrace::Backtrace;
use std::any::Any;
Expand Down Expand Up @@ -577,9 +577,15 @@ impl CallThreadState {
})
}

fn any_instance(&self, func: impl Fn(&InstanceHandle) -> bool) -> bool {
fn any_instance(&self, func: impl Fn(&Instance) -> bool) -> bool {
unsafe {
if func(&InstanceHandle::from_vmctx(self.vmctx.vmctx)) {
if func(
self.vmctx
.vmctx
.as_ref()
.expect("`VMContext` is null in `any_instance`")
.instance(),
) {
return true;
}
match self.prev {
Expand Down Expand Up @@ -632,14 +638,13 @@ impl CallThreadState {
// First up see if any instance registered has a custom trap handler,
// in which case run them all. If anything handles the trap then we
// return that the trap was handled.
let any_instance = self.any_instance(|i| {
let instance_ref = i.instance().instance_ref();
let handler = match instance_ref.signal_handler.replace(None) {
let any_instance = self.any_instance(|instance: &Instance| {
let handler = match instance.signal_handler.replace(None) {
Some(handler) => handler,
None => return false,
};
let result = call_handler(&handler);
instance_ref.signal_handler.set(Some(handler));
instance.signal_handler.set(Some(handler));
result
});

Expand Down

0 comments on commit f968b24

Please sign in to comment.