diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index c08544d605..bdeb213200 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -409,6 +409,11 @@ impl InstanceContents { foreign_instance_contents.memory_size(foreign_index) } + + /// Return a reference to the custom state attached to this instance. + pub fn host_state(&mut self) -> &mut Any { + return &mut *self.host_state; + } } /// A wrapper around an `Mmap` holding an `InstanceContents`. @@ -686,7 +691,7 @@ impl Instance { /// Return a reference to the custom state attached to this instance. pub fn host_state(&mut self) -> &mut Any { - return &mut *self.mmap_field.contents_mut().host_state; + return self.mmap_field.contents_mut().host_state(); } } diff --git a/lib/runtime/src/vmcontext.rs b/lib/runtime/src/vmcontext.rs index 3d8780e170..10e6e95471 100644 --- a/lib/runtime/src/vmcontext.rs +++ b/lib/runtime/src/vmcontext.rs @@ -2,6 +2,7 @@ //! fields that compiled wasm code accesses directly. use crate::instance::InstanceContents; +use core::any::Any; use core::{ptr, u32}; /// An imported function. @@ -478,9 +479,20 @@ pub struct VMContext {} impl VMContext { /// Return a mutable reference to the associated `Instance`. + /// + /// This is unsafe because it doesn't work on just any `VMContext`, it must + /// be a `VMContext` allocated as part of an `Instance`. #[allow(clippy::cast_ptr_alignment)] pub(crate) unsafe fn instance_contents(&mut self) -> &mut InstanceContents { &mut *((self as *mut Self as *mut u8).offset(-InstanceContents::vmctx_offset()) as *mut InstanceContents) } + + /// Return a mutable reference to the host state associated with `Instance`. + /// + /// This is unsafe because it doesn't work on just any `VMContext`, it must + /// be a `VMContext` allocated as part of an `Instance`. + pub unsafe fn host_state(&mut self) -> &mut Any { + self.instance_contents().host_state() + } }