Fixed RuntimeError start when instantiating wasmer-js

This commit is contained in:
Syrus Akbary
2021-07-22 16:49:20 -07:00
parent c398cae110
commit 14c75e7b51
6 changed files with 62 additions and 11 deletions

View File

@@ -1,9 +1,11 @@
use crate::env::HostEnvInitError;
use crate::export::Export;
use crate::exports::Exports;
use crate::externals::Extern;
use crate::module::Module;
use crate::store::Store;
use crate::resolver::Resolver;
use crate::store::Store;
use crate::trap::RuntimeError;
use js_sys::WebAssembly;
use std::fmt;
use thiserror::Error;
@@ -39,10 +41,13 @@ pub enum InstantiationError {
Link(String),
/// A runtime error occured while invoking the start function
#[cfg_attr(feature = "std", error("Start error: {0}"))]
Start(String),
}
#[error(transparent)]
Start(RuntimeError),
/// Error occurred when initializing the host environment.
#[error(transparent)]
HostEnvInitialization(HostEnvInitError),
}
impl Instance {
/// Creates a new `Instance` from a WebAssembly [`Module`] and a
@@ -80,7 +85,9 @@ impl Instance {
/// * Runtime errors that happen when running the module `start` function.
pub fn new(module: &Module, resolver: &dyn Resolver) -> Result<Self, InstantiationError> {
let store = module.store();
let (instance, functions) = module.instantiate(resolver).unwrap();
let (instance, functions) = module
.instantiate(resolver)
.map_err(|e| InstantiationError::Start(e))?;
let instance_exports = instance.exports();
let exports = module
.exports()
@@ -100,7 +107,8 @@ impl Instance {
exports,
};
for func in functions {
func.init_envs(&self_instance).unwrap();
func.init_envs(&self_instance)
.map_err(|e| InstantiationError::HostEnvInitialization(e))?;
}
Ok(self_instance)
}

View File

@@ -12,6 +12,7 @@ use std::fmt;
use std::io;
use std::path::Path;
use thiserror::Error;
use wasm_bindgen::JsValue;
use wasmer_types::{
ExportsIterator, ExternType, FunctionType, GlobalType, ImportsIterator, MemoryType, Mutability,
Pages, TableType, Type,
@@ -244,7 +245,8 @@ impl Module {
// the error for us, so we don't need to handle it
}
Ok((
WebAssembly::Instance::new(&self.module, &imports).unwrap(),
WebAssembly::Instance::new(&self.module, &imports)
.map_err(|e: JsValue| -> RuntimeError { e.into() })?,
functions,
))
}

View File

@@ -28,7 +28,10 @@ pub fn param_from_js(ty: &ValType, js_val: &JsValue) -> Val {
ValType::I64 => Val::I64(js_val.as_f64().unwrap() as _),
ValType::F32 => Val::F32(js_val.as_f64().unwrap() as _),
ValType::F64 => Val::F64(js_val.as_f64().unwrap()),
t => unimplemented!("The type `{:?}` is not yet supported in the JS Function API", t),
t => unimplemented!(
"The type `{:?}` is not yet supported in the JS Function API",
t
),
}
}
@@ -40,7 +43,10 @@ impl AsJs for Val {
Self::F32(f) => JsValue::from_f64(*f as f64),
Self::F64(f) => JsValue::from_f64(*f),
Self::FuncRef(func) => func.as_ref().unwrap().exported.function.clone().into(),
v => unimplemented!("The value `{:?}` is not yet supported in the JS Function API", v),
v => unimplemented!(
"The value `{:?}` is not yet supported in the JS Function API",
v
),
}
}
}