mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Improved code based on feedback
This commit is contained in:
@@ -77,7 +77,7 @@ impl From<ExportError> for HostEnvInitError {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait WasmerEnv: {
|
||||
pub trait WasmerEnv {
|
||||
/// The function that Wasmer will call on your type to let it finish
|
||||
/// setting up the environment with data from the `Instance`.
|
||||
///
|
||||
|
||||
@@ -65,6 +65,7 @@ impl VMFunction {
|
||||
environment: environment.map(|env| Arc::new(RefCell::new(env))),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn init_envs(&self, instance: &Instance) -> Result<(), HostEnvInitError> {
|
||||
if let Some(env) = &self.environment {
|
||||
let mut borrowed_env = env.borrow_mut();
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// use super::frame_info::{FrameInfo, GlobalFrameInfo, FRAME_INFO};
|
||||
use std::convert::TryInto;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use std::convert::TryInto;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
/// A struct representing an aborted instruction execution, with a message
|
||||
/// indicating the cause.
|
||||
@@ -61,8 +61,7 @@ impl RuntimeError {
|
||||
pub fn raise(error: Box<dyn Error + Send + Sync>) -> ! {
|
||||
let error = if error.is::<RuntimeError>() {
|
||||
*error.downcast::<RuntimeError>().unwrap()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
RuntimeError {
|
||||
inner: Arc::new(RuntimeErrorSource::User(error)),
|
||||
}
|
||||
@@ -131,13 +130,13 @@ impl From<JsValue> for RuntimeError {
|
||||
// Ok(rt) => rt,
|
||||
// Err(_) => RuntimeError {
|
||||
// inner: Arc::new(RuntimeErrorSource::Js(original)),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// match original.dyn_into::<RuntimeError>() {
|
||||
// Ok(rt) => rt,
|
||||
// Err(original) => RuntimeError {
|
||||
// inner: Arc::new(RuntimeErrorSource::Js(original)),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -603,19 +603,20 @@ fn test_custom_error() {
|
||||
local.get $y))
|
||||
(export "run" (func $run)))
|
||||
"#,
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct ExitCode(u32);
|
||||
|
||||
|
||||
impl fmt::Display for ExitCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl std::error::Error for ExitCode {}
|
||||
|
||||
fn early_exit() {
|
||||
@@ -629,11 +630,13 @@ fn test_custom_error() {
|
||||
};
|
||||
let instance = Instance::new(&module, &import_object).unwrap();
|
||||
|
||||
let run_func: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("run").unwrap();
|
||||
let run_func: NativeFunc<(i32, i32), i32> =
|
||||
instance.exports.get_native_function("run").unwrap();
|
||||
|
||||
match run_func.call(1, 7) {
|
||||
Ok(result) => {
|
||||
assert!(false,
|
||||
assert!(
|
||||
false,
|
||||
"Expected early termination with `ExitCode`, found: {}",
|
||||
result
|
||||
);
|
||||
@@ -641,13 +644,14 @@ fn test_custom_error() {
|
||||
Err(e) => {
|
||||
assert!(false, "Unknown error `{:?}`", e);
|
||||
match e.downcast::<ExitCode>() {
|
||||
// We found the exit code used to terminate execution.
|
||||
Ok(exit_code) => {
|
||||
assert_eq!(exit_code.0, 1);
|
||||
// We found the exit code used to terminate execution.
|
||||
Ok(exit_code) => {
|
||||
assert_eq!(exit_code.0, 1);
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, "Unknown error `{:?}` found. expected `ErrorCode`", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, "Unknown error `{:?}` found. expected `ErrorCode`", e);
|
||||
}
|
||||
}},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user