Removed Instance::new_no_memory_init, use custom LinearMemory with custom memory_init method to get the same result

This commit is contained in:
ptitSeb
2022-10-04 14:48:51 +02:00
parent d581152641
commit c27547e41e
6 changed files with 53 additions and 76 deletions

View File

@@ -118,47 +118,7 @@ impl Instance {
let imports = imports
.imports_for_module(module)
.map_err(InstantiationError::Link)?;
let mut handle = module.instantiate(store, &imports, true)?;
let exports = module
.exports()
.map(|export| {
let name = export.name().to_string();
let export = handle.lookup(&name).expect("export");
let extern_ = Extern::from_vm_extern(store, export);
(name, extern_)
})
.collect::<Exports>();
let instance = Self {
_handle: StoreHandle::new(store.objects_mut(), handle),
module: module.clone(),
exports,
};
Ok(instance)
}
#[cfg(feature = "compiler")]
/// Creates a new `Instance` from a WebAssembly [`Module`] and a
/// set of imports using [`Imports`] or the [`imports`] macro helper.
/// The instance memory will not be initialized
///
/// ## Errors
///
/// The function can return [`InstantiationError`]s.
///
/// Those are, as defined by the spec:
/// * Link errors that happen when plugging the imports into the instance
/// * Runtime errors that happen when running the module `start` function.
pub fn new_no_memory_init(
store: &mut impl AsStoreMut,
module: &Module,
imports: &Imports,
) -> Result<Self, InstantiationError> {
let imports = imports
.imports_for_module(module)
.map_err(InstantiationError::Link)?;
let mut handle = module.instantiate(store, &imports, false)?;
let mut handle = module.instantiate(store, &imports)?;
let exports = module
.exports()
.map(|export| {
@@ -195,7 +155,7 @@ impl Instance {
externs: &[Extern],
) -> Result<Self, InstantiationError> {
let imports = externs.to_vec();
let mut handle = module.instantiate(store, &imports, true)?;
let mut handle = module.instantiate(store, &imports)?;
let exports = module
.exports()
.map(|export| {

View File

@@ -343,7 +343,6 @@ impl Module {
&self,
store: &mut impl AsStoreMut,
imports: &[crate::Extern],
initialize_memory: bool,
) -> Result<InstanceHandle, InstantiationError> {
// Ensure all imports come from the same context.
for import in imports {
@@ -371,7 +370,6 @@ impl Module {
self.artifact.finish_instantiation(
store.as_store_ref().signal_handler(),
&mut instance_handle,
initialize_memory,
)?;
Ok(instance_handle)

View File

@@ -245,6 +245,13 @@ mod tests {
fn try_clone(&self) -> Option<Box<dyn LinearMemory + 'static>> {
None
}
/*
// this code allow custom memory to be ignoring init_memory
use wasmer_vm::Trap;
unsafe fn initialize_with_data(&self, _start: usize, _data: &[u8]) -> Result<(), Trap> {
Ok(())
}
*/
}
impl From<VMTinyMemory> for wasmer_vm::VMMemory {