diff --git a/src/function.rs b/src/function.rs index 400456f..a622494 100644 --- a/src/function.rs +++ b/src/function.rs @@ -10,11 +10,14 @@ use std::ffi::CString; use wamr_sys::{ wasm_exec_env_t, wasm_func_get_result_count, wasm_func_get_result_types, wasm_function_inst_t, wasm_runtime_call_wasm, wasm_runtime_get_exception, wasm_runtime_get_exec_env_singleton, - wasm_runtime_lookup_function, wasm_valkind_enum_WASM_F32, wasm_valkind_enum_WASM_F64, - wasm_valkind_enum_WASM_I32, wasm_valkind_enum_WASM_I64, wasm_valkind_t, + wasm_runtime_get_wasi_exit_code, wasm_runtime_lookup_function, wasm_valkind_enum_WASM_F32, + wasm_valkind_enum_WASM_F64, wasm_valkind_enum_WASM_I32, wasm_valkind_enum_WASM_I64, + wasm_valkind_t, }; -use crate::{helper::exception_to_string, instance::Instance, value::WasmValue, RuntimeError}; +use crate::{ + helper::exception_to_string, instance::Instance, value::WasmValue, ExecError, RuntimeError, +}; pub struct Function { function: wasm_function_inst_t, @@ -95,9 +98,11 @@ impl Function { if !call_result { unsafe { let exception_c = wasm_runtime_get_exception(instance.get_inner_instance()); - return Err(RuntimeError::ExecutionError(exception_to_string( - exception_c, - ))); + let error_info = ExecError { + message: exception_to_string(exception_c), + exit_code: wasm_runtime_get_wasi_exit_code(instance.get_inner_instance()), + }; + return Err(RuntimeError::ExecutionError(error_info)); } } diff --git a/src/lib.rs b/src/lib.rs index 83daf78..2383a59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,6 +155,12 @@ pub mod runtime; pub mod value; pub mod wasi_context; +#[derive(Debug)] +pub struct ExecError { + pub message: String, + pub exit_code: u32, +} + /// all kinds of exceptions raised by WAMR #[derive(Debug)] pub enum RuntimeError { @@ -168,7 +174,7 @@ pub enum RuntimeError { /// instantiation failure InstantiationFailure(String), /// Error during execute wasm functions - ExecutionError(String), + ExecutionError(ExecError), /// usually returns by `find_export_func()` FunctionNotFound, } @@ -181,7 +187,11 @@ impl fmt::Display for RuntimeError { RuntimeError::WasmFileFSError(e) => write!(f, "Wasm file operation error: {}", e), RuntimeError::CompilationError(e) => write!(f, "Wasm compilation error: {}", e), RuntimeError::InstantiationFailure(e) => write!(f, "Wasm instantiation failure: {}", e), - RuntimeError::ExecutionError(e) => write!(f, "Wasm execution error: {}", e), + RuntimeError::ExecutionError(info) => write!( + f, + "Wasm execution error: {} and {}", + info.message, info.exit_code + ), RuntimeError::FunctionNotFound => write!(f, "Function not found"), } }