Add host_state() accessors to InstanceContents and VMContext too.

This commit is contained in:
Dan Gohman
2019-01-04 17:24:14 -08:00
parent 73b2c45313
commit 3ac4269dc5
2 changed files with 18 additions and 1 deletions

View File

@@ -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();
}
}

View File

@@ -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()
}
}