mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 05:31:32 +00:00
Instantiate takes reference
This commit is contained in:
@ -296,7 +296,7 @@ pub struct ImportBacking {
|
|||||||
impl ImportBacking {
|
impl ImportBacking {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
module: &ModuleInner,
|
module: &ModuleInner,
|
||||||
imports: &mut ImportObject,
|
imports: &ImportObject,
|
||||||
vmctx: *mut vm::Ctx,
|
vmctx: *mut vm::Ctx,
|
||||||
) -> LinkResult<Self> {
|
) -> LinkResult<Self> {
|
||||||
let mut failed = false;
|
let mut failed = false;
|
||||||
@ -349,7 +349,7 @@ impl ImportBacking {
|
|||||||
|
|
||||||
fn import_functions(
|
fn import_functions(
|
||||||
module: &ModuleInner,
|
module: &ModuleInner,
|
||||||
imports: &mut ImportObject,
|
imports: &ImportObject,
|
||||||
vmctx: *mut vm::Ctx,
|
vmctx: *mut vm::Ctx,
|
||||||
) -> LinkResult<BoxedMap<ImportedFuncIndex, vm::ImportedFunc>> {
|
) -> LinkResult<BoxedMap<ImportedFuncIndex, vm::ImportedFunc>> {
|
||||||
let mut link_errors = vec![];
|
let mut link_errors = vec![];
|
||||||
@ -416,7 +416,7 @@ fn import_functions(
|
|||||||
|
|
||||||
fn import_memories(
|
fn import_memories(
|
||||||
module: &ModuleInner,
|
module: &ModuleInner,
|
||||||
imports: &mut ImportObject,
|
imports: &ImportObject,
|
||||||
) -> LinkResult<(
|
) -> LinkResult<(
|
||||||
BoxedMap<ImportedMemoryIndex, Memory>,
|
BoxedMap<ImportedMemoryIndex, Memory>,
|
||||||
BoxedMap<ImportedMemoryIndex, *mut vm::LocalMemory>,
|
BoxedMap<ImportedMemoryIndex, *mut vm::LocalMemory>,
|
||||||
@ -477,7 +477,7 @@ fn import_memories(
|
|||||||
|
|
||||||
fn import_tables(
|
fn import_tables(
|
||||||
module: &ModuleInner,
|
module: &ModuleInner,
|
||||||
imports: &mut ImportObject,
|
imports: &ImportObject,
|
||||||
) -> LinkResult<(
|
) -> LinkResult<(
|
||||||
BoxedMap<ImportedTableIndex, Table>,
|
BoxedMap<ImportedTableIndex, Table>,
|
||||||
BoxedMap<ImportedTableIndex, *mut vm::LocalTable>,
|
BoxedMap<ImportedTableIndex, *mut vm::LocalTable>,
|
||||||
@ -536,7 +536,7 @@ fn import_tables(
|
|||||||
|
|
||||||
fn import_globals(
|
fn import_globals(
|
||||||
module: &ModuleInner,
|
module: &ModuleInner,
|
||||||
imports: &mut ImportObject,
|
imports: &ImportObject,
|
||||||
) -> LinkResult<(
|
) -> LinkResult<(
|
||||||
BoxedMap<ImportedGlobalIndex, Global>,
|
BoxedMap<ImportedGlobalIndex, Global>,
|
||||||
BoxedMap<ImportedGlobalIndex, *mut vm::LocalGlobal>,
|
BoxedMap<ImportedGlobalIndex, *mut vm::LocalGlobal>,
|
||||||
|
@ -108,7 +108,7 @@ impl Global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IsExport for Global {
|
impl IsExport for Global {
|
||||||
fn to_export(&mut self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
Export::Global(self.clone())
|
Export::Global(self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,15 @@ use crate::export::Export;
|
|||||||
use hashbrown::{hash_map::Entry, HashMap};
|
use hashbrown::{hash_map::Entry, HashMap};
|
||||||
|
|
||||||
pub trait LikeNamespace {
|
pub trait LikeNamespace {
|
||||||
fn get_export(&mut self, name: &str) -> Option<Export>;
|
fn get_export(&self, name: &str) -> Option<Export>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IsExport {
|
pub trait IsExport {
|
||||||
fn to_export(&mut self) -> Export;
|
fn to_export(&self) -> Export;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IsExport for Export {
|
impl IsExport for Export {
|
||||||
fn to_export(&mut self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
self.clone()
|
self.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,10 +76,8 @@ impl ImportObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn LikeNamespace + 'static)> {
|
pub fn get_namespace(&self, namespace: &str) -> Option<&(dyn LikeNamespace + 'static)> {
|
||||||
self.map
|
self.map.get(namespace).map(|namespace| &**namespace)
|
||||||
.get_mut(namespace)
|
|
||||||
.map(|namespace| &mut **namespace)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +102,7 @@ impl Namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LikeNamespace for Namespace {
|
impl LikeNamespace for Namespace {
|
||||||
fn get_export(&mut self, name: &str) -> Option<Export> {
|
fn get_export(&self, name: &str) -> Option<Export> {
|
||||||
self.map
|
self.map.get(name).map(|is_export| is_export.to_export())
|
||||||
.get_mut(name)
|
|
||||||
.map(|is_export| is_export.to_export())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,21 +38,16 @@ impl Drop for InstanceInner {
|
|||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
module: Arc<ModuleInner>,
|
module: Arc<ModuleInner>,
|
||||||
inner: Box<InstanceInner>,
|
inner: Box<InstanceInner>,
|
||||||
#[allow(dead_code)]
|
|
||||||
imports: Box<ImportObject>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(module: Arc<ModuleInner>, imports: &ImportObject) -> Result<Instance> {
|
||||||
module: Arc<ModuleInner>,
|
|
||||||
mut imports: Box<ImportObject>,
|
|
||||||
) -> Result<Instance> {
|
|
||||||
// We need the backing and import_backing to create a vm::Ctx, but we need
|
// 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
|
// 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.
|
// uninitialized vm::Ctx and then initialize it in-place.
|
||||||
let mut vmctx = unsafe { Box::new(mem::uninitialized()) };
|
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);
|
let backing = LocalBacking::new(&module, &import_backing, &mut *vmctx);
|
||||||
|
|
||||||
// When Pin is stablized, this will use `Box::pinned` instead of `Box::new`.
|
// 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)
|
*inner.vmctx = vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module)
|
||||||
};
|
};
|
||||||
|
|
||||||
let instance = Instance {
|
let instance = Instance { module, inner };
|
||||||
module,
|
|
||||||
inner,
|
|
||||||
imports,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(start_index) = instance.module.start_func {
|
if let Some(start_index) = instance.module.start_func {
|
||||||
instance.call_with_index(start_index, &[])?;
|
instance.call_with_index(start_index, &[])?;
|
||||||
@ -414,7 +405,7 @@ impl InstanceInner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LikeNamespace for Instance {
|
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)?;
|
let export_index = self.module.exports.get(name)?;
|
||||||
|
|
||||||
Some(self.inner.get_export_from_index(&self.module, export_index))
|
Some(self.inner.get_export_from_index(&self.module, export_index))
|
||||||
|
@ -236,7 +236,7 @@ impl Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IsExport for Memory {
|
impl IsExport for Memory {
|
||||||
fn to_export(&mut self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
Export::Memory(self.clone())
|
Export::Memory(self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,8 +79,8 @@ impl Module {
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn instantiate(&self, import_object: ImportObject) -> Result<Instance> {
|
pub fn instantiate(&self, import_object: &ImportObject) -> Result<Instance> {
|
||||||
Instance::new(Arc::clone(&self.0), Box::new(import_object))
|
Instance::new(Arc::clone(&self.0), import_object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ impl Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IsExport for Table {
|
impl IsExport for Table {
|
||||||
fn to_export(&mut self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
Export::Table(self.clone())
|
Export::Table(self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,7 +190,7 @@ where
|
|||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
Safety: Safeness,
|
Safety: Safeness,
|
||||||
{
|
{
|
||||||
fn to_export(&mut self) -> Export {
|
fn to_export(&self) -> Export {
|
||||||
let func = unsafe { FuncPointer::new(self.f as _) };
|
let func = unsafe { FuncPointer::new(self.f as _) };
|
||||||
let ctx = Context::Internal;
|
let ctx = Context::Internal;
|
||||||
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));
|
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));
|
||||||
|
@ -141,7 +141,7 @@ pub fn compile(wasm: &[u8]) -> error::CompileResult<Module> {
|
|||||||
/// `error::RuntimeError` (all combined into an `error::Error`),
|
/// `error::RuntimeError` (all combined into an `error::Error`),
|
||||||
/// depending on the cause of the failure.
|
/// depending on the cause of the failure.
|
||||||
#[cfg(feature = "wasmer-clif-backend")]
|
#[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)?;
|
let module = compile(wasm)?;
|
||||||
module.instantiate(import_object)
|
module.instantiate(import_object)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user