Instantiate takes reference

This commit is contained in:
Lachlan Sneff 2019-02-02 15:58:33 -08:00
parent eba66f3b33
commit ccd43df767
9 changed files with 23 additions and 36 deletions

View File

@ -296,7 +296,7 @@ pub struct ImportBacking {
impl ImportBacking {
pub fn new(
module: &ModuleInner,
imports: &mut ImportObject,
imports: &ImportObject,
vmctx: *mut vm::Ctx,
) -> LinkResult<Self> {
let mut failed = false;
@ -349,7 +349,7 @@ impl ImportBacking {
fn import_functions(
module: &ModuleInner,
imports: &mut ImportObject,
imports: &ImportObject,
vmctx: *mut vm::Ctx,
) -> LinkResult<BoxedMap<ImportedFuncIndex, vm::ImportedFunc>> {
let mut link_errors = vec![];
@ -416,7 +416,7 @@ fn import_functions(
fn import_memories(
module: &ModuleInner,
imports: &mut ImportObject,
imports: &ImportObject,
) -> LinkResult<(
BoxedMap<ImportedMemoryIndex, Memory>,
BoxedMap<ImportedMemoryIndex, *mut vm::LocalMemory>,
@ -477,7 +477,7 @@ fn import_memories(
fn import_tables(
module: &ModuleInner,
imports: &mut ImportObject,
imports: &ImportObject,
) -> LinkResult<(
BoxedMap<ImportedTableIndex, Table>,
BoxedMap<ImportedTableIndex, *mut vm::LocalTable>,
@ -536,7 +536,7 @@ fn import_tables(
fn import_globals(
module: &ModuleInner,
imports: &mut ImportObject,
imports: &ImportObject,
) -> LinkResult<(
BoxedMap<ImportedGlobalIndex, Global>,
BoxedMap<ImportedGlobalIndex, *mut vm::LocalGlobal>,

View File

@ -108,7 +108,7 @@ impl Global {
}
impl IsExport for Global {
fn to_export(&mut self) -> Export {
fn to_export(&self) -> Export {
Export::Global(self.clone())
}
}

View File

@ -2,15 +2,15 @@ use crate::export::Export;
use hashbrown::{hash_map::Entry, HashMap};
pub trait LikeNamespace {
fn get_export(&mut self, name: &str) -> Option<Export>;
fn get_export(&self, name: &str) -> Option<Export>;
}
pub trait IsExport {
fn to_export(&mut self) -> Export;
fn to_export(&self) -> Export;
}
impl IsExport for Export {
fn to_export(&mut self) -> Export {
fn to_export(&self) -> Export {
self.clone()
}
}
@ -76,10 +76,8 @@ impl ImportObject {
}
}
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn LikeNamespace + 'static)> {
self.map
.get_mut(namespace)
.map(|namespace| &mut **namespace)
pub fn get_namespace(&self, namespace: &str) -> Option<&(dyn LikeNamespace + 'static)> {
self.map.get(namespace).map(|namespace| &**namespace)
}
}
@ -104,9 +102,7 @@ impl Namespace {
}
impl LikeNamespace for Namespace {
fn get_export(&mut self, name: &str) -> Option<Export> {
self.map
.get_mut(name)
.map(|is_export| is_export.to_export())
fn get_export(&self, name: &str) -> Option<Export> {
self.map.get(name).map(|is_export| is_export.to_export())
}
}

View File

@ -38,21 +38,16 @@ impl Drop for InstanceInner {
pub struct Instance {
module: Arc<ModuleInner>,
inner: Box<InstanceInner>,
#[allow(dead_code)]
imports: Box<ImportObject>,
}
impl Instance {
pub(crate) fn new(
module: Arc<ModuleInner>,
mut imports: Box<ImportObject>,
) -> Result<Instance> {
pub(crate) fn new(module: Arc<ModuleInner>, imports: &ImportObject) -> Result<Instance> {
// We need the backing and import_backing to create a vm::Ctx, but we need
// a vm::Ctx to create a backing and an import_backing. The solution is to create an
// uninitialized vm::Ctx and then initialize it in-place.
let mut vmctx = unsafe { Box::new(mem::uninitialized()) };
let import_backing = ImportBacking::new(&module, &mut imports, &mut *vmctx)?;
let import_backing = ImportBacking::new(&module, &imports, &mut *vmctx)?;
let backing = LocalBacking::new(&module, &import_backing, &mut *vmctx);
// When Pin is stablized, this will use `Box::pinned` instead of `Box::new`.
@ -68,11 +63,7 @@ impl Instance {
*inner.vmctx = vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module)
};
let instance = Instance {
module,
inner,
imports,
};
let instance = Instance { module, inner };
if let Some(start_index) = instance.module.start_func {
instance.call_with_index(start_index, &[])?;
@ -414,7 +405,7 @@ impl InstanceInner {
}
impl LikeNamespace for Instance {
fn get_export(&mut self, name: &str) -> Option<Export> {
fn get_export(&self, name: &str) -> Option<Export> {
let export_index = self.module.exports.get(name)?;
Some(self.inner.get_export_from_index(&self.module, export_index))

View File

@ -236,7 +236,7 @@ impl Memory {
}
impl IsExport for Memory {
fn to_export(&mut self) -> Export {
fn to_export(&self) -> Export {
Export::Memory(self.clone())
}
}

View File

@ -79,8 +79,8 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn instantiate(&self, import_object: ImportObject) -> Result<Instance> {
Instance::new(Arc::clone(&self.0), Box::new(import_object))
pub fn instantiate(&self, import_object: &ImportObject) -> Result<Instance> {
Instance::new(Arc::clone(&self.0), import_object)
}
}

View File

@ -118,7 +118,7 @@ impl Table {
}
impl IsExport for Table {
fn to_export(&mut self) -> Export {
fn to_export(&self) -> Export {
Export::Table(self.clone())
}
}

View File

@ -190,7 +190,7 @@ where
Rets: WasmTypeList,
Safety: Safeness,
{
fn to_export(&mut self) -> Export {
fn to_export(&self) -> Export {
let func = unsafe { FuncPointer::new(self.f as _) };
let ctx = Context::Internal;
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));

View File

@ -141,7 +141,7 @@ pub fn compile(wasm: &[u8]) -> error::CompileResult<Module> {
/// `error::RuntimeError` (all combined into an `error::Error`),
/// depending on the cause of the failure.
#[cfg(feature = "wasmer-clif-backend")]
pub fn instantiate(wasm: &[u8], import_object: ImportObject) -> error::Result<Instance> {
pub fn instantiate(wasm: &[u8], import_object: &ImportObject) -> error::Result<Instance> {
let module = compile(wasm)?;
module.instantiate(import_object)
}