Remove setting memory for WASI compatibility

This commit is contained in:
Syrus Akbary
2023-01-26 17:00:48 +01:00
parent 9c04fc8138
commit 423ca7a9f5
3 changed files with 11 additions and 27 deletions

View File

@@ -4,7 +4,7 @@ use crate::js::externals::Extern;
use crate::js::imports::Imports; use crate::js::imports::Imports;
use crate::js::module::Module; use crate::js::module::Module;
use crate::js::store::{AsStoreMut, AsStoreRef}; use crate::js::store::{AsStoreMut, AsStoreRef};
use crate::js::vm::VMExtern; use crate::js::vm::{VMExtern, VMInstance};
use js_sys::WebAssembly; use js_sys::WebAssembly;
use std::fmt; use std::fmt;
@@ -18,7 +18,7 @@ use std::fmt;
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances> /// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
#[derive(Clone)] #[derive(Clone)]
pub struct Instance { pub struct Instance {
handle: WebAssembly::Instance, handle: VMInstance,
module: Module, module: Module,
/// The exports for an instance. /// The exports for an instance.
pub exports: Exports, pub exports: Exports,
@@ -63,12 +63,11 @@ impl Instance {
module: &Module, module: &Module,
imports: &Imports, imports: &Imports,
) -> Result<Self, InstantiationError> { ) -> Result<Self, InstantiationError> {
let (instance, externs) = module let instance = module
.instantiate(&mut store, imports) .instantiate(&mut store, imports)
.map_err(|e| InstantiationError::Start(e))?; .map_err(|e| InstantiationError::Start(e))?;
let mut self_instance = Self::from_module_and_instance(store, module, instance)?; let self_instance = Self::from_module_and_instance(store, module, instance)?;
self_instance.ensure_memory_export(store, externs);
Ok(self_instance) Ok(self_instance)
} }
@@ -135,22 +134,6 @@ impl Instance {
}) })
} }
/// This will check the memory is correctly setup
/// If the memory is imported then also export it for backwards compatibility reasons
/// (many will assume the memory is always exported) - later we can remove this
/// TODO: This is trialing from WASIX, we should remove this or move it to the wasmer-wasi crate
pub fn ensure_memory_export(&mut self, store: &mut impl AsStoreMut, externs: Vec<Extern>) {
if self.exports.get_memory("memory").is_err() {
if let Some(memory) = externs
.iter()
.filter(|a| a.ty(store).memory().is_some())
.next()
{
self.exports.insert("memory", memory.clone());
}
}
}
/// Gets the [`Module`] associated with this instance. /// Gets the [`Module`] associated with this instance.
pub fn module(&self) -> &Module { pub fn module(&self) -> &Module {
&self.module &self.module

View File

@@ -7,6 +7,7 @@ use crate::js::externals::Extern;
use crate::js::imports::Imports; use crate::js::imports::Imports;
use crate::js::store::AsStoreMut; use crate::js::store::AsStoreMut;
use crate::js::types::{AsJs, ExportType, ImportType}; use crate::js::types::{AsJs, ExportType, ImportType};
use crate::js::vm::VMInstance;
use crate::js::RuntimeError; use crate::js::RuntimeError;
use crate::AsStoreRef; use crate::AsStoreRef;
use crate::IntoBytes; use crate::IntoBytes;
@@ -246,7 +247,7 @@ impl Module {
&self, &self,
store: &mut impl AsStoreMut, store: &mut impl AsStoreMut,
imports: &Imports, imports: &Imports,
) -> Result<(WebAssembly::Instance, Vec<Extern>), RuntimeError> { ) -> Result<VMInstance, RuntimeError> {
// Ensure all imports come from the same store. // Ensure all imports come from the same store.
if imports if imports
.into_iter() .into_iter()
@@ -323,11 +324,8 @@ impl Module {
// in case the import is not found, the JS Wasm VM will handle // in case the import is not found, the JS Wasm VM will handle
// the error for us, so we don't need to handle it // the error for us, so we don't need to handle it
} }
Ok(( Ok(WebAssembly::Instance::new(&self.module, &imports_object)
WebAssembly::Instance::new(&self.module, &imports_object) .map_err(|e: JsValue| -> RuntimeError { e.into() })?)
.map_err(|e: JsValue| -> RuntimeError { e.into() })?,
import_externs,
))
} }
/// Returns the name of the current module. /// Returns the name of the current module.

View File

@@ -11,6 +11,7 @@ use crate::js::wasm_bindgen_polyfill::Global as JsGlobal;
use crate::MemoryView; use crate::MemoryView;
use js_sys::Function; use js_sys::Function;
use js_sys::Function as JsFunction; use js_sys::Function as JsFunction;
use js_sys::WebAssembly;
use js_sys::WebAssembly::{Memory, Table}; use js_sys::WebAssembly::{Memory, Table};
use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable}; use js_sys::WebAssembly::{Memory as JsMemory, Table as JsTable};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -252,3 +253,5 @@ impl VMExtern {
} }
} }
} }
pub type VMInstance = WebAssembly::Instance;