Unify InstantiationError

This commit is contained in:
Syrus Akbary
2023-02-11 17:41:53 -08:00
parent f8176e3bb6
commit 58f27ae045
9 changed files with 65 additions and 92 deletions

39
lib/api/src/errors.rs Normal file
View File

@@ -0,0 +1,39 @@
use crate::{LinkError, RuntimeError};
use thiserror::Error;
/// An error while instantiating a module.
///
/// This is not a common WebAssembly error, however
/// we need to differentiate from a `LinkError` (an error
/// that happens while linking, on instantiation), a
/// Trap that occurs when calling the WebAssembly module
/// start function, and an error when initializing the user's
/// host environments.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum InstantiationError {
/// A linking ocurred during instantiation.
#[cfg_attr(feature = "std", error(transparent))]
Link(LinkError),
/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),
/// The module was compiled with a CPU feature that is not available on
/// the current host.
#[cfg_attr(feature = "std", error("missing required CPU features: {0:?}"))]
CpuFeature(String),
/// Import from a different [`Store`].
/// This error occurs when an import from a different store is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))]
DifferentStores,
}
#[cfg(feature = "core")]
impl std::fmt::Display for InstantiationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InstantiationError")
}
}

View File

@@ -116,40 +116,3 @@ pub enum LinkError {
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
}
/// An error while instantiating a module.
///
/// This is not a common WebAssembly error, however
/// we need to differentiate from a `LinkError` (an error
/// that happens while linking, on instantiation), a
/// Trap that occurs when calling the WebAssembly module
/// start function, and an error when initializing the user's
/// host environments.
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum InstantiationError {
/// A linking ocurred during instantiation.
#[cfg_attr(feature = "std", error(transparent))]
Link(LinkError),
/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),
/// The module was compiled with a CPU feature that is not available on
/// the current host.
#[cfg_attr(feature = "std", error("missing required CPU features: {0:?}"))]
CpuFeature(String),
/// Import from a different [`Store`].
/// This error occurs when an import from a different store is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))]
DifferentStores,
}
#[cfg(feature = "core")]
impl std::fmt::Display for InstantiationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InstantiationError")
}
}

View File

@@ -1,7 +1,7 @@
use crate::errors::InstantiationError;
use crate::exports::Exports;
use crate::imports::Imports;
use crate::js::as_js::AsJs;
use crate::js::error::InstantiationError;
use crate::js::externals::Extern;
use crate::js::vm::{VMExtern, VMInstance};
use crate::module::Module;

View File

@@ -40,7 +40,7 @@ mod wasm_bindgen_polyfill;
pub use crate::js::as_js::AsJs;
pub use crate::js::engine::Engine;
pub use crate::js::error::{DeserializeError, InstantiationError, LinkError, SerializeError};
pub use crate::js::error::{DeserializeError, LinkError, SerializeError};
pub use crate::js::externals::{
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView,
Table, WasmTypeList,

View File

@@ -1,5 +1,5 @@
use crate::errors::InstantiationError;
use crate::imports::Imports;
use crate::js::error::InstantiationError;
#[cfg(feature = "wat")]
use crate::js::error::WasmError;
use crate::js::externals::Extern;

View File

@@ -430,6 +430,7 @@ compile_error!(
);
mod engine;
mod errors;
mod exports;
mod extern_ref;
mod function_env;
@@ -456,6 +457,7 @@ mod js;
pub use js::*;
pub use engine::{AsEngineRef, Engine};
pub use errors::InstantiationError;
pub use exports::{ExportError, Exportable, Exports, ExportsIterator};
pub use extern_ref::ExternRef;
pub use function_env::{FunctionEnv, FunctionEnvMut};

View File

@@ -1,8 +1,7 @@
use crate::errors::InstantiationError;
use crate::exports::Exports;
use crate::module::Module;
use crate::sys::{LinkError, RuntimeError};
use std::fmt;
use thiserror::Error;
use wasmer_vm::{StoreHandle, VMInstance};
use crate::imports::Imports;
@@ -39,35 +38,6 @@ mod send_test {
}
}
/// An error while instantiating a module.
///
/// This is not a common WebAssembly error, however
/// we need to differentiate from a `LinkError` (an error
/// that happens while linking, on instantiation), a
/// Trap that occurs when calling the WebAssembly module
/// start function, and an error when initializing the user's
/// host environments.
#[derive(Error, Debug)]
pub enum InstantiationError {
/// A linking ocurred during instantiation.
#[error(transparent)]
Link(LinkError),
/// A runtime error occured while invoking the start function
#[error(transparent)]
Start(RuntimeError),
/// The module was compiled with a CPU feature that is not available on
/// the current host.
#[error("missing required CPU features: {0:?}")]
CpuFeature(String),
/// Import from a different Store.
/// This error occurs when an import from a different store is used.
#[error("cannot mix imports from different stores")]
DifferentStores,
}
impl From<wasmer_compiler::InstantiationError> for InstantiationError {
fn from(other: wasmer_compiler::InstantiationError) -> Self {
match other {
@@ -118,15 +88,7 @@ impl Instance {
.imports_for_module(module)
.map_err(InstantiationError::Link)?;
let mut handle = module.0.instantiate(store, &externs)?;
let mut 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 exports = Self::get_exports(store, module, &mut handle);
let instance = Self {
_handle: StoreHandle::new(store.objects_mut(), handle),
@@ -154,16 +116,7 @@ impl Instance {
) -> Result<Self, InstantiationError> {
let externs = externs.to_vec();
let mut handle = module.0.instantiate(store, &externs)?;
let mut 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 exports = Self::get_exports(store, module, &mut handle);
let instance = Self {
_handle: StoreHandle::new(store.objects_mut(), handle),
module: module.clone(),
@@ -173,6 +126,22 @@ impl Instance {
Ok(instance)
}
fn get_exports(
store: &mut impl AsStoreMut,
module: &Module,
handle: &mut VMInstance,
) -> 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>()
}
/// Gets the [`Module`] associated with this instance.
pub fn module(&self) -> &Module {
&self.module

View File

@@ -10,7 +10,7 @@ pub use crate::sys::externals::{
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, Table,
WasmTypeList,
};
pub use crate::sys::instance::{Instance, InstantiationError};
pub use crate::sys::instance::Instance;
pub use crate::sys::tunables::BaseTunables;
pub use target_lexicon::{Architecture, CallingConvention, OperatingSystem, Triple, HOST};

View File

@@ -9,7 +9,7 @@ use wasmer_types::{
};
use wasmer_types::{ExportType, ImportType};
use crate::{sys::InstantiationError, AsStoreMut, AsStoreRef, IntoBytes};
use crate::{AsStoreMut, AsStoreRef, InstantiationError, IntoBytes};
use wasmer_vm::VMInstance;
/// A WebAssembly Module contains stateless WebAssembly