Refactor Instance's methods to just be wrappers around InstanceContents methods.

This commit is contained in:
Dan Gohman
2019-02-22 10:51:48 -08:00
parent 1ab9e17517
commit a8cd5ef613

View File

@@ -367,6 +367,62 @@ impl InstanceContents {
self.vmctx_mut() self.vmctx_mut()
} }
/// Lookup an export with the given name.
pub fn lookup(&mut self, field: &str) -> Option<Export> {
let export = if let Some(export) = self.module.exports.get(field) {
export.clone()
} else {
return None;
};
Some(self.lookup_by_declaration(&export))
}
/// Lookup an export with the given name. This takes an immutable reference,
/// and the result is an `Export` that the type system doesn't prevent from
/// being used to mutate the instance, so this function is unsafe.
pub unsafe fn lookup_immutable(&self, field: &str) -> Option<Export> {
#[allow(clippy::cast_ref_to_mut)]
let temporary_mut = &mut *(self as *const Self as *mut Self);
temporary_mut.lookup(field)
}
/// Lookup an export with the given export declaration.
pub fn lookup_by_declaration(&mut self, export: &wasmtime_environ::Export) -> Export {
lookup_by_declaration(
&self.module,
&mut self.vmctx,
&self.offsets,
&self.finished_functions,
export,
)
}
/// Lookup an export with the given export declaration. This takes an immutable
/// reference, and the result is an `Export` that the type system doesn't prevent
/// from being used to mutate the instance, so this function is unsafe.
pub unsafe fn lookup_immutable_by_declaration(
&self,
export: &wasmtime_environ::Export,
) -> Export {
#[allow(clippy::cast_ref_to_mut)]
let temporary_mut = &mut *(self as *const Self as *mut Self);
temporary_mut.lookup_by_declaration(export)
}
/// Return an iterator over the exports of this instance.
///
/// Specifically, it provides access to the key-value pairs, where they keys
/// are export names, and the values are export declarations which can be
/// resolved `lookup_by_declaration`.
pub fn exports(&self) -> indexmap::map::Iter<String, wasmtime_environ::Export> {
self.module.exports.iter()
}
/// Return a reference to the custom state attached to this instance.
pub fn host_state(&mut self) -> &mut Any {
&mut *self.host_state
}
fn invoke_function(&mut self, index: FuncIndex) -> Result<(), InstantiationError> { fn invoke_function(&mut self, index: FuncIndex) -> Result<(), InstantiationError> {
// TODO: Check that the callee's calling convention matches what we expect. // TODO: Check that the callee's calling convention matches what we expect.
@@ -532,11 +588,6 @@ impl InstanceContents {
foreign_instance_contents.memory_size(foreign_index) 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 {
&mut *self.host_state
}
pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> { pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> {
let cell: &RefCell<HashMap<std::string::String, core::option::Option<Export>>> = let cell: &RefCell<HashMap<std::string::String, core::option::Option<Export>>> =
self.global_exports.borrow(); self.global_exports.borrow();
@@ -754,38 +805,19 @@ impl Instance {
/// Lookup an export with the given name. /// Lookup an export with the given name.
pub fn lookup(&mut self, field: &str) -> Option<Export> { pub fn lookup(&mut self, field: &str) -> Option<Export> {
let export = if let Some(export) = self.mmap_field.contents().module.exports.get(field) { self.mmap_field.contents_mut().lookup(field)
export.clone()
} else {
return None;
};
Some(self.lookup_by_declaration(&export))
} }
/// Lookup an export with the given name. This takes an immutable reference, /// Lookup an export with the given name. This takes an immutable reference,
/// and the result is an `Export` that the type system doesn't prevent from /// and the result is an `Export` that the type system doesn't prevent from
/// being used to mutate the instance, so this function is unsafe. /// being used to mutate the instance, so this function is unsafe.
pub unsafe fn lookup_immutable(&self, field: &str) -> Option<Export> { pub unsafe fn lookup_immutable(&self, field: &str) -> Option<Export> {
#[allow(clippy::cast_ref_to_mut)] self.mmap_field.contents().lookup_immutable(field)
let temporary_mut = &mut *(self as *const Self as *mut Self);
temporary_mut.lookup(field)
}
/// Return a reference to the custom state attached to this instance.
pub fn host_state(&mut self) -> &mut Any {
self.mmap_field.contents_mut().host_state()
} }
/// Lookup an export with the given export declaration. /// Lookup an export with the given export declaration.
pub fn lookup_by_declaration(&mut self, export: &wasmtime_environ::Export) -> Export { pub fn lookup_by_declaration(&mut self, export: &wasmtime_environ::Export) -> Export {
let contents = self.mmap_field.contents_mut(); self.mmap_field.contents_mut().lookup_by_declaration(export)
lookup_by_declaration(
&contents.module,
&mut contents.vmctx,
&contents.offsets,
&contents.finished_functions,
export,
)
} }
/// Lookup an export with the given export declaration. This takes an immutable /// Lookup an export with the given export declaration. This takes an immutable
@@ -795,9 +827,9 @@ impl Instance {
&self, &self,
export: &wasmtime_environ::Export, export: &wasmtime_environ::Export,
) -> Export { ) -> Export {
#[allow(clippy::cast_ref_to_mut)] self.mmap_field
let temporary_mut = &mut *(self as *const Self as *mut Self); .contents()
temporary_mut.lookup_by_declaration(export) .lookup_immutable_by_declaration(export)
} }
/// Return an iterator over the exports of this instance. /// Return an iterator over the exports of this instance.
@@ -806,7 +838,12 @@ impl Instance {
/// are export names, and the values are export declarations which can be /// are export names, and the values are export declarations which can be
/// resolved `lookup_by_declaration`. /// resolved `lookup_by_declaration`.
pub fn exports(&self) -> indexmap::map::Iter<String, wasmtime_environ::Export> { pub fn exports(&self) -> indexmap::map::Iter<String, wasmtime_environ::Export> {
self.mmap_field.contents().module.exports.iter() self.mmap_field.contents().exports()
}
/// Return a reference to the custom state attached to this instance.
pub fn host_state(&mut self) -> &mut Any {
self.mmap_field.contents_mut().host_state()
} }
} }