Rename Instance to InstanceHandle.

This commit is contained in:
Dan Gohman
2019-02-22 14:11:02 -08:00
parent 21abecb158
commit 5f201f6d73
13 changed files with 82 additions and 79 deletions

View File

@@ -1,4 +1,4 @@
use crate::instance::Instance;
use crate::instance::InstanceHandle;
use crate::vmcontext::{VMFunctionImport, VMGlobalImport, VMMemoryImport, VMTableImport};
use cranelift_entity::{BoxedSlice, PrimaryMap};
use cranelift_wasm::{FuncIndex, GlobalIndex, MemoryIndex, TableIndex};
@@ -8,7 +8,7 @@ use std::collections::HashSet;
#[derive(Clone)]
pub struct Imports {
/// The set of instances that the imports depend on.
pub dependencies: HashSet<Instance>,
pub dependencies: HashSet<InstanceHandle>,
/// Resolved addresses for imported functions.
pub functions: BoxedSlice<FuncIndex, VMFunctionImport>,
@@ -26,7 +26,7 @@ pub struct Imports {
impl Imports {
/// Construct a new `Imports` instance.
pub fn new(
dependencies: HashSet<Instance>,
dependencies: HashSet<InstanceHandle>,
function_imports: PrimaryMap<FuncIndex, VMFunctionImport>,
table_imports: PrimaryMap<TableIndex, VMTableImport>,
memory_imports: PrimaryMap<MemoryIndex, VMMemoryImport>,

View File

@@ -1,5 +1,6 @@
//! An `Instance` contains all the runtime state used by execution of a wasm
//! module.
//! An `InstanceContents` contains all the runtime state used by execution
//! of a wasm module. An `InstanceHandle` is a reference-counting handle
//! for an `InstanceContents`.
use crate::export::Export;
use crate::imports::Imports;
@@ -177,25 +178,23 @@ fn global_mut<'vmctx>(
/// The actual contents of an instance.
///
/// `Instance` is just a handle containing a pointer to an `InstanceContents`,
/// which is specially allocated.
/// `InstanceContents` instances are specially allocated.
///
/// This is repr(C) to ensure that the vmctx field is last.
/// FIXME: Should this be pub(crate)?
#[repr(C)]
pub struct InstanceContents {
pub(crate) struct InstanceContents {
/// The number of references to this `InstanceContents`.
refcount: usize,
/// Instances from which this `InstanceContents` imports. These won't
/// create reference cycles because wasm instances can't cyclically
/// import from each other.
dependencies: HashSet<Instance>,
dependencies: HashSet<InstanceHandle>,
/// The allocated contents.
mmap: Mmap,
/// The `Module` this `Instance` was instantiated from.
/// The `Module` this `InstanceContents` was instantiated from.
module: Rc<Module>,
/// Offsets in the `vmctx` region.
@@ -497,7 +496,7 @@ impl InstanceContents {
}
}
/// Return the offset from the vmctx pointer to its containing Instance.
/// Return the offset from the vmctx pointer to its containing InstanceContents.
pub(crate) fn vmctx_offset() -> isize {
offset_of!(Self, vmctx) as isize
}
@@ -611,17 +610,14 @@ impl InstanceContents {
}
}
/// An Instance of a WebAssembly module.
///
/// Note that compiled wasm code passes around raw pointers to `Instance`, so
/// this shouldn't be moved.
/// A handle holding an `InstanceContents` of a WebAssembly module.
#[derive(Hash, PartialEq, Eq)]
pub struct Instance {
pub struct InstanceHandle {
instance: *mut InstanceContents,
}
impl Instance {
/// Create a new `Instance`.
impl InstanceHandle {
/// Create a new `InstanceHandle` pointing at a new `InstanceContents`.
pub fn new(
module: Rc<Module>,
global_exports: Rc<RefCell<HashMap<String, Option<Export>>>>,
@@ -753,7 +749,7 @@ impl Instance {
}
// Ensure that our signal handlers are ready for action.
// TODO: Move these calls out of `Instance`.
// TODO: Move these calls out of `InstanceHandle`.
wasmtime_init_eager();
wasmtime_init_finish(contents.vmctx_mut());
@@ -834,28 +830,28 @@ impl Instance {
}
}
impl Instance {
impl InstanceHandle {
/// Return the contained contents.
pub fn contents(&self) -> &InstanceContents {
fn contents(&self) -> &InstanceContents {
unsafe { &*(self.instance as *const InstanceContents) }
}
/// Return the contained contents.
pub fn contents_mut(&mut self) -> &mut InstanceContents {
fn contents_mut(&mut self) -> &mut InstanceContents {
unsafe { &mut *(self.instance as *mut InstanceContents) }
}
}
impl Clone for Instance {
impl Clone for InstanceHandle {
fn clone(&self) -> Self {
unsafe { &mut *(self.instance as *mut InstanceContents) }.refcount += 1;
Instance {
InstanceHandle {
instance: self.instance,
}
}
}
impl Drop for Instance {
impl Drop for InstanceHandle {
fn drop(&mut self) {
let contents = self.contents_mut();
contents.refcount -= 1;

View File

@@ -46,7 +46,7 @@ pub mod libcalls;
pub use crate::export::Export;
pub use crate::imports::Imports;
pub use crate::instance::{Instance, InstanceContents, InstantiationError, LinkError};
pub use crate::instance::{InstanceHandle, InstantiationError, LinkError};
pub use crate::mmap::Mmap;
pub use crate::sig_registry::SignatureRegistry;
pub use crate::signalhandlers::{wasmtime_init_eager, wasmtime_init_finish};

View File

@@ -478,21 +478,20 @@ impl Default for VMCallerCheckedAnyfunc {
pub struct VMContext {}
impl VMContext {
/// Return a mutable reference to the associated `Instance`.
/// Return a mutable reference to the associated `InstanceContents`.
///
/// This is unsafe because it doesn't work on just any `VMContext`, it must
/// be a `VMContext` allocated as part of an `Instance`.
/// FIXME: make this pub(crate)?
/// be a `VMContext` allocated as part of an `InstanceContents`.
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn instance_contents(&mut self) -> &mut InstanceContents {
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`.
/// Return a mutable reference to the host state associated with `InstanceContents`.
///
/// This is unsafe because it doesn't work on just any `VMContext`, it must
/// be a `VMContext` allocated as part of an `Instance`.
/// be a `VMContext` allocated as part of an `InstanceContents`.
pub unsafe fn host_state(&mut self) -> &mut Any {
self.instance_contents().host_state()
}