Skip to content

Commit

Permalink
Fixed RuntimeError start when instantiating wasmer-js
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 22, 2021
1 parent c398cae commit 14c75e7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
20 changes: 14 additions & 6 deletions lib/js-api/src/instance.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/js-api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
))
}
Expand Down
10 changes: 8 additions & 2 deletions lib/js-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
}
}

Expand All @@ -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
),
}
}
}
6 changes: 6 additions & 0 deletions lib/js-api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ fn global_get() {
let store = Store::default();
let global_i32 = Global::new(&store, Value::I32(10));
assert_eq!(global_i32.get(), Value::I32(10));
// 64-bit values are not yet fully supported in some versions of Node
// Commenting this tests for now:

// let global_i64 = Global::new(&store, Value::I64(20));
// assert_eq!(global_i64.get(), Value::I64(20));
let global_f32 = Global::new(&store, Value::F32(10.0));
Expand Down Expand Up @@ -76,6 +79,9 @@ fn table_new() {
// assert_eq!(*table.ty(), table_type);
}

// Tables are not yet fully supported in Wasm
// Commenting this tests for now

// #[test]
// #[ignore]
// fn table_get() -> Result<()> {
Expand Down
26 changes: 26 additions & 0 deletions lib/js-api/tests/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,29 @@ fn test_custom_error() {
let run_func = instance.exports.get_function("run").unwrap();
test_result(run_func.call(&[Val::I32(1), Val::I32(7)]));
}

#[wasm_bindgen_test]
fn test_start_function_fails() {
let store = Store::default();
let module = Module::new(
&store,
br#"
(module
(func $start_function
(i32.div_u
(i32.const 1)
(i32.const 0)
)
drop
)
(start $start_function)
)
"#,
)
.unwrap();

let import_object = imports! {};
let result = Instance::new(&module, &import_object);
let err = result.unwrap_err();
assert!(format!("{:?}", err).contains("zero"))
}
7 changes: 5 additions & 2 deletions lib/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ pub use crate::instance::{
};
pub use crate::memory::{LinearMemory, Memory, MemoryError, MemoryStyle};
pub use crate::mmap::Mmap;
#[deprecated(since = "2.1.0", note = "ModuleInfo, ExportsIterator, ImportsIterator should be imported from wasmer_types.")]
pub use wasmer_types::{ModuleInfo, ExportsIterator, ImportsIterator};
pub use crate::probestack::PROBESTACK;
pub use crate::sig_registry::SignatureRegistry;
pub use crate::table::{LinearTable, Table, TableElement, TableStyle};
Expand All @@ -62,6 +60,11 @@ pub use crate::vmcontext::{
pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets};
use loupe::MemoryUsage;
pub use wasmer_types::VMExternRef;
#[deprecated(
since = "2.1.0",
note = "ModuleInfo, ExportsIterator, ImportsIterator should be imported from wasmer_types."
)]
pub use wasmer_types::{ExportsIterator, ImportsIterator, ModuleInfo};

/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down

0 comments on commit 14c75e7

Please sign in to comment.