Skip to content

Commit

Permalink
Refactor Instance's methods to just be wrappers around InstanceConten…
Browse files Browse the repository at this point in the history
…ts methods.
  • Loading branch information
sunfishcode committed Feb 26, 2019
1 parent 1ab9e17 commit a8cd5ef
Showing 1 changed file with 68 additions and 31 deletions.
99 changes: 68 additions & 31 deletions lib/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,62 @@ impl InstanceContents {
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> {
// TODO: Check that the callee's calling convention matches what we expect.

Expand Down Expand Up @@ -532,11 +588,6 @@ 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 {
&mut *self.host_state
}

pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> {
let cell: &RefCell<HashMap<std::string::String, core::option::Option<Export>>> =
self.global_exports.borrow();
Expand Down Expand Up @@ -754,38 +805,19 @@ impl Instance {

/// Lookup an export with the given name.
pub fn lookup(&mut self, field: &str) -> Option<Export> {
let export = if let Some(export) = self.mmap_field.contents().module.exports.get(field) {
export.clone()
} else {
return None;
};
Some(self.lookup_by_declaration(&export))
self.mmap_field.contents_mut().lookup(field)
}

/// 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)
}

/// 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()
self.mmap_field.contents().lookup_immutable(field)
}

/// Lookup an export with the given export declaration.
pub fn lookup_by_declaration(&mut self, export: &wasmtime_environ::Export) -> Export {
let contents = self.mmap_field.contents_mut();
lookup_by_declaration(
&contents.module,
&mut contents.vmctx,
&contents.offsets,
&contents.finished_functions,
export,
)
self.mmap_field.contents_mut().lookup_by_declaration(export)
}

/// Lookup an export with the given export declaration. This takes an immutable
Expand All @@ -795,9 +827,9 @@ impl Instance {
&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)
self.mmap_field
.contents()
.lookup_immutable_by_declaration(export)
}

/// Return an iterator over the exports of this instance.
Expand All @@ -806,7 +838,12 @@ impl Instance {
/// 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.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()
}
}

Expand Down

0 comments on commit a8cd5ef

Please sign in to comment.