From c5215c74e4b419f0ce430ccc3e97b769d2f65c92 Mon Sep 17 00:00:00 2001 From: csh <458761603@qq.com> Date: Mon, 5 Feb 2024 01:55:34 +0800 Subject: [PATCH] Bump to wasmedge 0.14 Signed-off-by: csh <458761603@qq.com> --- .github/workflows/ci-build-release-lib.yml | 2 + crates/wasmedge-sys/src/ast_module.rs | 4 +- crates/wasmedge-sys/src/async/function.rs | 6 +- crates/wasmedge-sys/src/async/module.rs | 104 +-- crates/wasmedge-sys/src/instance/function.rs | 6 +- crates/wasmedge-sys/src/instance/global.rs | 5 +- crates/wasmedge-sys/src/instance/module.rs | 6 +- crates/wasmedge-sys/src/instance/table.rs | 15 +- crates/wasmedge-sys/src/types.rs | 160 ++-- crates/wasmedge-sys/src/utils.rs | 803 ++++++++++++------- crates/wasmedge-types/src/error.rs | 93 ++- crates/wasmedge-types/src/lib.rs | 89 +- src/types.rs | 4 +- 13 files changed, 748 insertions(+), 549 deletions(-) diff --git a/.github/workflows/ci-build-release-lib.yml b/.github/workflows/ci-build-release-lib.yml index 67e5d5c94..dad10ebc2 100644 --- a/.github/workflows/ci-build-release-lib.yml +++ b/.github/workflows/ci-build-release-lib.yml @@ -6,6 +6,8 @@ concurrency: on: push: + branches: + - '!bump/**' paths-ignore: - "**/*.md" - ".github/workflows/standalone.yml" diff --git a/crates/wasmedge-sys/src/ast_module.rs b/crates/wasmedge-sys/src/ast_module.rs index ba4275b92..01d52f1d4 100644 --- a/crates/wasmedge-sys/src/ast_module.rs +++ b/crates/wasmedge-sys/src/ast_module.rs @@ -202,7 +202,7 @@ impl<'module> ImportType<'module> { false => { // get the element type let elem_ty = unsafe { ffi::WasmEdge_TableTypeGetRefType(ctx_tab_ty) }; - let elem_ty: RefType = elem_ty.into(); + let elem_ty = RefType::from(ValType::from(elem_ty)); // get the limit let limit = unsafe { ffi::WasmEdge_TableTypeGetLimit(ctx_tab_ty) }; @@ -317,7 +317,7 @@ impl<'module> ExportType<'module> { false => { // get the element type let elem_ty = unsafe { ffi::WasmEdge_TableTypeGetRefType(ctx_tab_ty) }; - let elem_ty: RefType = elem_ty.into(); + let elem_ty = RefType::from(ValType::from(elem_ty)); // get the limit let limit = unsafe { ffi::WasmEdge_TableTypeGetLimit(ctx_tab_ty) }; diff --git a/crates/wasmedge-sys/src/async/function.rs b/crates/wasmedge-sys/src/async/function.rs index 64dd801d4..ffc5693e2 100644 --- a/crates/wasmedge-sys/src/async/function.rs +++ b/crates/wasmedge-sys/src/async/function.rs @@ -164,19 +164,19 @@ mod tests { println!("Rust: Entering Rust function real_add"); if input.len() != 2 { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); } let a = if input[0].ty() == ValType::I32 { input[0].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let b = if input[1].ty() == ValType::I32 { input[1].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; tokio::time::sleep(std::time::Duration::from_millis(300)).await; diff --git a/crates/wasmedge-sys/src/async/module.rs b/crates/wasmedge-sys/src/async/module.rs index 0d25f5644..84c2da66a 100644 --- a/crates/wasmedge-sys/src/async/module.rs +++ b/crates/wasmedge-sys/src/async/module.rs @@ -212,7 +212,7 @@ fn args_get( WasmPtr::from(argv_buf), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -236,7 +236,7 @@ fn args_sizes_get( WasmPtr::from(argv_buf_size), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -260,7 +260,7 @@ fn environ_get( WasmPtr::from(environ_buf), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -284,7 +284,7 @@ fn environ_sizes_get( WasmPtr::from(environ_buf_size), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -308,7 +308,7 @@ fn clock_res_get( WasmPtr::from(resolution_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -335,7 +335,7 @@ fn clock_time_get( WasmPtr::from(time_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -360,7 +360,7 @@ fn random_get( buf_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -385,7 +385,7 @@ fn fd_prestat_get( WasmPtr::from(prestat_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -412,7 +412,7 @@ fn fd_prestat_dir_name( path_max_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -437,7 +437,7 @@ fn fd_renumber( to, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -466,7 +466,7 @@ fn fd_advise( advice, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -493,7 +493,7 @@ fn fd_allocate( len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -516,7 +516,7 @@ fn fd_close( fd, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -545,7 +545,7 @@ fn fd_seek( WasmPtr::from(newoffset_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -568,7 +568,7 @@ fn fd_sync( fd, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -591,7 +591,7 @@ fn fd_datasync( fd, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -616,7 +616,7 @@ fn fd_tell( WasmPtr::from(offset), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -641,7 +641,7 @@ fn fd_fdstat_get( WasmPtr::from(buf_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -666,7 +666,7 @@ fn fd_fdstat_set_flags( flags, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -693,7 +693,7 @@ fn fd_fdstat_set_rights( fs_rights_inheriting, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -718,7 +718,7 @@ fn fd_filestat_get( WasmPtr::from(buf), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -743,7 +743,7 @@ fn fd_filestat_set_size( WasmPtr::from(buf), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -772,7 +772,7 @@ fn fd_filestat_set_times( fst_flags, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -801,7 +801,7 @@ fn fd_read( WasmPtr::from(nread), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -832,7 +832,7 @@ fn fd_pread( WasmPtr::from(nread), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -861,7 +861,7 @@ fn fd_write( WasmPtr::from(nwritten), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -892,7 +892,7 @@ fn fd_pwrite( WasmPtr::from(nwritten), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -923,7 +923,7 @@ fn fd_readdir( WasmPtr::from(bufused_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -950,7 +950,7 @@ fn path_create_directory( path_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -981,7 +981,7 @@ fn path_filestat_get( WasmPtr::from(file_stat_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1042,7 +1042,7 @@ fn path_open( WasmPtr::from(fd_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1080,7 +1080,7 @@ fn path_remove_directory( path_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1113,7 +1113,7 @@ fn path_rename( new_path_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1151,7 +1151,7 @@ fn path_unlink_file( path_len, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1170,7 +1170,7 @@ fn proc_exit( p::proc_exit(data, &mut mem as &mut Memory, code); Err(CoreError::Common(CoreCommonError::Terminated)) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1223,7 +1223,7 @@ fn sock_open( WasmPtr::from(ro_fd_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1249,7 +1249,7 @@ fn sock_bind( port, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1274,7 +1274,7 @@ fn sock_listen( backlog, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1308,7 +1308,7 @@ where .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } }) } @@ -1333,7 +1333,7 @@ async fn sock_connect( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1369,7 +1369,7 @@ async fn sock_recv( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1409,7 +1409,7 @@ async fn sock_recv_from( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1443,7 +1443,7 @@ async fn sock_send( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1481,7 +1481,7 @@ async fn sock_send_to( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1505,7 +1505,7 @@ fn sock_shutdown( how, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1533,7 +1533,7 @@ fn sock_getpeeraddr( WasmPtr::from(port_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1561,7 +1561,7 @@ fn sock_getlocaladdr( WasmPtr::from(port_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1591,7 +1591,7 @@ fn sock_getsockopt( WasmPtr::from(flag_size_ptr), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1621,7 +1621,7 @@ fn sock_setsockopt( flag_size, ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1658,7 +1658,7 @@ fn sock_getaddrinfo( WasmPtr::from(res_len), ))) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1690,7 +1690,7 @@ async fn poll_oneoff( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } @@ -1725,7 +1725,7 @@ async fn sock_lookup_ip( .await, )) } else { - Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)) + Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)) } } diff --git a/crates/wasmedge-sys/src/instance/function.rs b/crates/wasmedge-sys/src/instance/function.rs index 2cf3d3489..939744f4b 100644 --- a/crates/wasmedge-sys/src/instance/function.rs +++ b/crates/wasmedge-sys/src/instance/function.rs @@ -450,19 +450,19 @@ mod tests { println!("Rust: Entering Rust function real_add"); if input.len() != 2 { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); } let a = if input[0].ty() == ValType::I32 { input[0].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let b = if input[1].ty() == ValType::I32 { input[1].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let c = a + b; diff --git a/crates/wasmedge-sys/src/instance/global.rs b/crates/wasmedge-sys/src/instance/global.rs index 8b088e994..3fc46cc51 100644 --- a/crates/wasmedge-sys/src/instance/global.rs +++ b/crates/wasmedge-sys/src/instance/global.rs @@ -83,7 +83,10 @@ impl Global { GlobalError::UnmatchedValType, ))); } - unsafe { ffi::WasmEdge_GlobalInstanceSetValue(self.inner.0, val.as_raw()) } + unsafe { + let result = ffi::WasmEdge_GlobalInstanceSetValue(self.inner.0, val.as_raw()); + crate::utils::check(result)?; + } Ok(()) } diff --git a/crates/wasmedge-sys/src/instance/module.rs b/crates/wasmedge-sys/src/instance/module.rs index fc89988ec..99adf5e1c 100644 --- a/crates/wasmedge-sys/src/instance/module.rs +++ b/crates/wasmedge-sys/src/instance/module.rs @@ -1010,19 +1010,19 @@ mod tests { inputs: Vec, ) -> Result, CoreError> { if inputs.len() != 2 { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); } let a = if inputs[0].ty() == ValType::I32 { inputs[0].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let b = if inputs[1].ty() == ValType::I32 { inputs[1].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let c = a + b; diff --git a/crates/wasmedge-sys/src/instance/table.rs b/crates/wasmedge-sys/src/instance/table.rs index 6ddc9a468..b3e65202f 100644 --- a/crates/wasmedge-sys/src/instance/table.rs +++ b/crates/wasmedge-sys/src/instance/table.rs @@ -14,7 +14,7 @@ use crate::{ use wasmedge_types::{ error::{TableError, WasmEdgeError}, - RefType, + RefType, ValType, }; /// A WasmEdge [Table] defines a WebAssembly table instance described by its [type](crate::TableType). A table is an array-like structure and stores function references. @@ -189,11 +189,9 @@ impl TableType { /// ``` /// pub(crate) fn create(elem_ty: RefType, min: u32, max: Option) -> WasmEdgeResult { + let ty: ValType = elem_ty.into(); let ctx = unsafe { - ffi::WasmEdge_TableTypeCreate( - elem_ty.into(), - WasmEdgeLimit::new(min, max, false).into(), - ) + ffi::WasmEdge_TableTypeCreate(ty.into(), WasmEdgeLimit::new(min, max, false).into()) }; if ctx.is_null() { Err(Box::new(WasmEdgeError::TableTypeCreate)) @@ -207,6 +205,7 @@ impl TableType { /// Returns the element type. pub(crate) fn elem_ty(&self) -> RefType { let ty = unsafe { ffi::WasmEdge_TableTypeGetRefType(self.inner.0) }; + let ty: ValType = ty.into(); ty.into() } @@ -417,19 +416,19 @@ mod tests { println!("Rust: Entering Rust function real_add"); if input.len() != 2 { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); } let a = if input[0].ty() == ValType::I32 { input[0].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let b = if input[1].ty() == ValType::I32 { input[0].to_i32() } else { - return Err(CoreError::Execution(CoreExecutionError::FuncTypeMismatch)); + return Err(CoreError::Execution(CoreExecutionError::FuncSigMismatch)); }; let c = a + b; diff --git a/crates/wasmedge-sys/src/types.rs b/crates/wasmedge-sys/src/types.rs index fa473fd26..5ec59e272 100644 --- a/crates/wasmedge-sys/src/types.rs +++ b/crates/wasmedge-sys/src/types.rs @@ -3,7 +3,7 @@ use crate::{ffi, instance::function::AsFunc, FuncRef, Function}; use core::ffi::c_void; use std::ffi::CString; -use wasmedge_types::{RefType, ValType}; +use wasmedge_types::ValType; #[derive(Debug, Clone)] pub(crate) struct WasmEdgeLimit { @@ -273,21 +273,6 @@ impl WasmValue { unsafe { ffi::WasmEdge_ValueGetV128(self.ctx) } } - /// Creates a [WasmValue] from a [RefType](wasmedge_types::RefType) value. - /// - /// # Argument - /// - /// * `val` - The `[`RefType`] value. - pub fn from_null_ref(ref_ty: RefType) -> Self { - Self { - ctx: unsafe { ffi::WasmEdge_ValueGenNullRef(ref_ty.into()) }, - ty: match ref_ty { - RefType::FuncRef => ValType::FuncRef, - RefType::ExternRef => ValType::ExternRef, - }, - } - } - /// Checks if a [WasmValue] is NullRef or not. pub fn is_null_ref(&self) -> bool { unsafe { ffi::WasmEdge_ValueIsNullRef(self.ctx) } @@ -342,6 +327,18 @@ impl WasmValue { } } + /// Creates a null [WasmValue] with a [ExternRef](wasmedge_types::RefType::ExternRef). + /// + /// # Argument + /// + /// * `val` - The `[`RefType`] value. + pub fn null_extern_ref() -> Self { + Self { + ctx: unsafe { ffi::WasmEdge_ValueGenExternRef(std::ptr::null_mut()) }, + ty: ValType::ExternRef, + } + } + /// Returns the reference to an external object. /// /// If the [WasmValue] is a `NullRef`, then `None` is returned. @@ -360,36 +357,56 @@ impl WasmValue { } impl From for WasmValue { fn from(raw_val: ffi::WasmEdge_Value) -> Self { - match raw_val.Type { - ffi::WasmEdge_ValType_I32 => Self { - ctx: raw_val, - ty: ValType::I32, - }, - ffi::WasmEdge_ValType_I64 => Self { - ctx: raw_val, - ty: ValType::I64, - }, - ffi::WasmEdge_ValType_F32 => Self { - ctx: raw_val, - ty: ValType::F32, - }, - ffi::WasmEdge_ValType_F64 => Self { - ctx: raw_val, - ty: ValType::F64, - }, - ffi::WasmEdge_ValType_V128 => Self { - ctx: raw_val, - ty: ValType::V128, - }, - ffi::WasmEdge_ValType_FuncRef => Self { - ctx: raw_val, - ty: ValType::FuncRef, - }, - ffi::WasmEdge_ValType_ExternRef => Self { - ctx: raw_val, - ty: ValType::ExternRef, - }, - _ => panic!("unknown WasmEdge_ValType `{}`", raw_val.Type), + let ty = raw_val.Type.into(); + Self { ctx: raw_val, ty } + } +} + +impl From for ValType { + fn from(value: ffi::WasmEdge_ValType) -> Self { + unsafe { + if ffi::WasmEdge_ValTypeIsI32(value) { + ValType::I32 + } else if ffi::WasmEdge_ValTypeIsI64(value) { + ValType::I64 + } else if ffi::WasmEdge_ValTypeIsF32(value) { + ValType::F32 + } else if ffi::WasmEdge_ValTypeIsF64(value) { + ValType::F64 + } else if ffi::WasmEdge_ValTypeIsV128(value) { + ValType::V128 + } else if ffi::WasmEdge_ValTypeIsRef(value) { + if ffi::WasmEdge_ValTypeIsFuncRef(value) { + ValType::FuncRef + } else if ffi::WasmEdge_ValTypeIsExternRef(value) { + ValType::ExternRef + } else { + panic!( + "capi unsupport WasmEdge_RefType `{:x}`", + u64::from_be_bytes(value.Data) + ) + } + } else { + panic!( + "unknown WasmEdge_ValType `{:x}`", + u64::from_be_bytes(value.Data) + ) + } + } + } +} +impl From for ffi::WasmEdge_ValType { + fn from(value: ValType) -> Self { + unsafe { + match value { + ValType::I32 => ffi::WasmEdge_ValTypeGenI32(), + ValType::I64 => ffi::WasmEdge_ValTypeGenI64(), + ValType::F32 => ffi::WasmEdge_ValTypeGenF32(), + ValType::F64 => ffi::WasmEdge_ValTypeGenF64(), + ValType::V128 => ffi::WasmEdge_ValTypeGenV128(), + ValType::FuncRef => ffi::WasmEdge_ValTypeGenFuncRef(), + ValType::ExternRef => ffi::WasmEdge_ValTypeGenExternRef(), + } } } } @@ -438,18 +455,6 @@ mod tests { assert_eq!(value.ty(), ValType::ExternRef); assert!(value.extern_ref::().is_some()); - // NullRef(FuncRef) - let val = WasmValue::from_null_ref(RefType::FuncRef); - assert_eq!(val.ty(), ValType::FuncRef); - assert!(val.is_null_ref()); - assert_eq!(val.ty(), ValType::FuncRef); - - // NullRef(ExternRef) - let val = WasmValue::from_null_ref(RefType::ExternRef); - assert_eq!(val.ty(), ValType::ExternRef); - assert!(val.is_null_ref()); - assert_eq!(val.ty(), ValType::ExternRef); - let val1 = WasmValue::from_i32(1314); let val2 = WasmValue::from_i32(1314); assert_eq!(val1.to_i32(), val2.to_i32()); @@ -476,12 +481,6 @@ mod tests { let mut table = result.unwrap(); let val_extern_ref = WasmValue::from_extern_ref(&mut table); - // NullRef(FuncRef) - let val_null_func_ref = WasmValue::from_null_ref(RefType::FuncRef); - - // NullRef(ExternRef) - let val_null_extern_ref = WasmValue::from_null_ref(RefType::ExternRef); - let handle = thread::spawn(move || { let val_i32_c = val_i32; assert_eq!(val_i32_c.ty(), ValType::I32); @@ -501,16 +500,6 @@ mod tests { let val_extern_ref_c = val_extern_ref; assert_eq!(val_extern_ref_c.ty(), ValType::ExternRef); assert!(val_extern_ref_c.extern_ref::
().is_some()); - - let val_null_func_ref_c = val_null_func_ref; - assert_eq!(val_null_func_ref_c.ty(), ValType::FuncRef); - assert!(val_null_func_ref_c.is_null_ref()); - assert_eq!(val_null_func_ref_c.ty(), ValType::FuncRef); - - let val_null_extern_ref_c = val_null_extern_ref; - assert_eq!(val_null_extern_ref_c.ty(), ValType::ExternRef); - assert!(val_null_extern_ref_c.is_null_ref()); - assert_eq!(val_null_extern_ref_c.ty(), ValType::ExternRef); }); handle.join().unwrap(); @@ -547,15 +536,6 @@ mod tests { let val_extern_ref = Arc::new(Mutex::new(WasmValue::from_extern_ref(&mut table))); let val_extern_ref_cloned = Arc::clone(&val_extern_ref); - // NullRef(FuncRef) - let val_null_func_ref = Arc::new(Mutex::new(WasmValue::from_null_ref(RefType::FuncRef))); - let val_null_func_ref_cloned = Arc::clone(&val_null_func_ref); - - // NullRef(ExternRef) - let val_null_extern_ref = - Arc::new(Mutex::new(WasmValue::from_null_ref(RefType::ExternRef))); - let val_null_extern_ref_cloned = Arc::clone(&val_null_extern_ref); - let handle = thread::spawn(move || { let result = val_i32_cloned.lock(); assert!(result.is_ok()); @@ -587,20 +567,6 @@ mod tests { let val_extern_ref_c = result.unwrap(); assert_eq!(val_extern_ref_c.ty(), ValType::ExternRef); assert!(val_extern_ref_c.extern_ref::
().is_some()); - - let result = val_null_func_ref_cloned.lock(); - assert!(result.is_ok()); - let val_null_func_ref_c = result.unwrap(); - assert_eq!(val_null_func_ref_c.ty(), ValType::FuncRef); - assert!(val_null_func_ref_c.is_null_ref()); - assert_eq!(val_null_func_ref_c.ty(), ValType::FuncRef); - - let result = val_null_extern_ref_cloned.lock(); - assert!(result.is_ok()); - let val_null_extern_ref_c = result.unwrap(); - assert_eq!(val_null_extern_ref_c.ty(), ValType::ExternRef); - assert!(val_null_extern_ref_c.is_null_ref()); - assert_eq!(val_null_extern_ref_c.ty(), ValType::ExternRef); }); handle.join().unwrap(); diff --git a/crates/wasmedge-sys/src/utils.rs b/crates/wasmedge-sys/src/utils.rs index 16bce920f..30eec5451 100644 --- a/crates/wasmedge-sys/src/utils.rs +++ b/crates/wasmedge-sys/src/utils.rs @@ -9,8 +9,8 @@ use std::{ path::Path, }; use wasmedge_types::error::{ - CoreCommonError, CoreError, CoreExecutionError, CoreInstantiationError, CoreLoadError, - CoreValidationError, WasmEdgeError, + CoreCommonError, CoreComponentError, CoreError, CoreExecutionError, CoreInstantiationError, + CoreLoadError, CoreValidationError, WasmEdgeError, }; #[cfg(unix)] @@ -54,7 +54,7 @@ pub(crate) fn check(result: WasmEdge_Result) -> WasmEdgeResult<()> { } else { 0u32 } - }; + } as ffi::WasmEdge_ErrCode; match category { ffi::WasmEdge_ErrCategory_UserLevelError => Err(Box::new(WasmEdgeError::User(code))), @@ -63,348 +63,559 @@ pub(crate) fn check(result: WasmEdge_Result) -> WasmEdgeResult<()> { } } -fn gen_runtime_error(code: u32) -> WasmEdgeResult<()> { +fn gen_runtime_error(code: ffi::WasmEdge_ErrCode) -> WasmEdgeResult<()> { match code { // Success or terminated (exit and return success) - 0x00 => Ok(()), - 0x01 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( + ffi::WasmEdge_ErrCode_Success => Ok(()), + ffi::WasmEdge_ErrCode_Terminated => Err(Box::new(WasmEdgeError::Core(CoreError::Common( CoreCommonError::Terminated, )))), // Common errors - 0x02 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::RuntimeError, - )))), - 0x03 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::CostLimitExceeded, - )))), - 0x04 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::WrongVMWorkflow, - )))), - 0x05 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::FuncNotFound, - )))), - 0x06 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( + ffi::WasmEdge_ErrCode_RuntimeError => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::RuntimeError), + ))), + ffi::WasmEdge_ErrCode_CostLimitExceeded => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::CostLimitExceeded), + ))), + ffi::WasmEdge_ErrCode_WrongVMWorkflow => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::WrongVMWorkflow), + ))), + ffi::WasmEdge_ErrCode_FuncNotFound => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::FuncNotFound), + ))), + ffi::WasmEdge_ErrCode_AOTDisabled => Err(Box::new(WasmEdgeError::Core(CoreError::Common( CoreCommonError::AOTDisabled, )))), - 0x07 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( + ffi::WasmEdge_ErrCode_Interrupted => Err(Box::new(WasmEdgeError::Core(CoreError::Common( CoreCommonError::Interrupted, )))), - 0x08 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::NotValidated, - )))), - 0x09 => Err(Box::new(WasmEdgeError::Core(CoreError::Common( - CoreCommonError::UserDefError, - )))), + ffi::WasmEdge_ErrCode_NotValidated => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::NotValidated), + ))), + ffi::WasmEdge_ErrCode_NonNullRequired => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::NonNullRequired), + ))), + ffi::WasmEdge_ErrCode_SetValueToConst => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::SetValueToConst), + ))), + ffi::WasmEdge_ErrCode_SetValueErrorType => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::SetValueErrorType), + ))), + ffi::WasmEdge_ErrCode_UserDefError => Err(Box::new(WasmEdgeError::Core( + CoreError::Common(CoreCommonError::UserDefError), + ))), // Load phase - 0x20 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_IllegalPath => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::IllegalPath, )))), - 0x21 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_ReadError => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::ReadError, )))), - 0x22 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_UnexpectedEnd => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::UnexpectedEnd, )))), - 0x23 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedMagic, - )))), - 0x24 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedVersion, - )))), - 0x25 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedSection, - )))), - 0x26 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::SectionSizeMismatch, - )))), - 0x27 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::NameSizeOutOfBounds, - )))), - 0x28 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_MalformedMagic => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedMagic), + ))), + ffi::WasmEdge_ErrCode_MalformedVersion => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedVersion), + ))), + ffi::WasmEdge_ErrCode_MalformedSection => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedSection), + ))), + ffi::WasmEdge_ErrCode_SectionSizeMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::SectionSizeMismatch), + ))), + ffi::WasmEdge_ErrCode_LengthOutOfBounds => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::LengthOutOfBounds), + ))), + ffi::WasmEdge_ErrCode_JunkSection => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::JunkSection, )))), - 0x29 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::IncompatibleFuncCode, - )))), - 0x2A => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::IncompatibleDataCount, - )))), - 0x2B => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::DataCountRequired, - )))), - 0x2C => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedImportKind, - )))), - 0x2D => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedExportKind, - )))), - 0x2E => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::ExpectedZeroByte, - )))), - 0x2F => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_IncompatibleFuncCode => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IncompatibleFuncCode), + ))), + ffi::WasmEdge_ErrCode_IncompatibleDataCount => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IncompatibleDataCount), + ))), + ffi::WasmEdge_ErrCode_DataCountRequired => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::DataCountRequired), + ))), + ffi::WasmEdge_ErrCode_MalformedImportKind => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedImportKind), + ))), + ffi::WasmEdge_ErrCode_MalformedExportKind => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedExportKind), + ))), + ffi::WasmEdge_ErrCode_ExpectedZeroByte => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::ExpectedZeroByte), + ))), + ffi::WasmEdge_ErrCode_InvalidMut => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::InvalidMut, )))), - 0x30 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_TooManyLocals => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::TooManyLocals, )))), - 0x31 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedValType, - )))), - 0x32 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedElemType, - )))), - 0x33 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::MalformedRefType, - )))), - 0x34 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_MalformedValType => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedValType), + ))), + ffi::WasmEdge_ErrCode_MalformedElemType => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedElemType), + ))), + ffi::WasmEdge_ErrCode_MalformedRefType => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedRefType), + ))), + ffi::WasmEdge_ErrCode_MalformedUTF8 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::MalformedUTF8, )))), - 0x35 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::IntegerTooLarge, - )))), - 0x36 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::IntegerTooLong, - )))), - 0x37 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( + ffi::WasmEdge_ErrCode_IntegerTooLarge => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IntegerTooLarge), + ))), + ffi::WasmEdge_ErrCode_IntegerTooLong => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IntegerTooLong), + ))), + ffi::WasmEdge_ErrCode_IllegalOpCode => Err(Box::new(WasmEdgeError::Core(CoreError::Load( CoreLoadError::IllegalOpCode, )))), - 0x38 => Err(Box::new(WasmEdgeError::Core(CoreError::Load( - CoreLoadError::IllegalGrammar, - )))), + ffi::WasmEdge_ErrCode_IllegalGrammar => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IllegalGrammar), + ))), + ffi::WasmEdge_ErrCode_SharedMemoryNoMax => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::SharedMemoryNoMax), + ))), + ffi::WasmEdge_ErrCode_IntrinsicsTableNotFound => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::IntrinsicsTableNotFound), + ))), + ffi::WasmEdge_ErrCode_MalformedTable => Err(Box::new(WasmEdgeError::Core( + CoreError::Load(CoreLoadError::MalformedTable), + ))), // Validation phase - 0x40 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidAlignment, - )))), - 0x41 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::TypeCheckFailed, - )))), - 0x42 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidLabelIdx, - )))), - 0x43 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidLocalIdx, - )))), - 0x44 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidFuncTypeIdx, - )))), - 0x45 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidFuncIdx, - )))), - 0x46 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidTableIdx, - )))), - 0x47 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidMemoryIdx, - )))), - 0x48 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidGlobalIdx, - )))), - 0x49 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidElemIdx, - )))), - 0x4A => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidDataIdx, - )))), - 0x4B => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidRefIdx, - )))), - 0x4C => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::ConstExprRequired, - )))), - 0x4D => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::DupExportName, - )))), - 0x4E => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::ImmutableGlobal, - )))), - 0x4F => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidResultArity, - )))), - 0x50 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::MultiTables, - )))), - 0x51 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::MultiMemories, - )))), - 0x52 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidLimit, - )))), - 0x53 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidMemPages, - )))), - 0x54 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidStartFunc, - )))), - 0x55 => Err(Box::new(WasmEdgeError::Core(CoreError::Validation( - CoreValidationError::InvalidLaneIdx, - )))), + ffi::WasmEdge_ErrCode_InvalidAlignment => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidAlignment), + ))), + ffi::WasmEdge_ErrCode_TypeCheckFailed => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::TypeCheckFailed), + ))), + ffi::WasmEdge_ErrCode_InvalidLabelIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidLabelIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidLocalIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidLocalIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidFieldIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidFieldIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidFuncTypeIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidFuncTypeIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidFuncIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidFuncIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidTableIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidTableIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidMemoryIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidMemoryIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidGlobalIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidGlobalIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidElemIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidElemIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidDataIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidDataIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidRefIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidRefIdx), + ))), + ffi::WasmEdge_ErrCode_ConstExprRequired => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ConstExprRequired), + ))), + ffi::WasmEdge_ErrCode_DupExportName => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::DupExportName), + ))), + ffi::WasmEdge_ErrCode_ImmutableGlobal => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ImmutableGlobal), + ))), + ffi::WasmEdge_ErrCode_ImmutableField => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ImmutableField), + ))), + ffi::WasmEdge_ErrCode_ImmutableArray => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ImmutableArray), + ))), + ffi::WasmEdge_ErrCode_InvalidResultArity => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidResultArity), + ))), + ffi::WasmEdge_ErrCode_MultiTables => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::MultiTables), + ))), + ffi::WasmEdge_ErrCode_MultiMemories => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::MultiMemories), + ))), + ffi::WasmEdge_ErrCode_InvalidLimit => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidLimit), + ))), + ffi::WasmEdge_ErrCode_InvalidMemPages => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidMemPages), + ))), + ffi::WasmEdge_ErrCode_InvalidStartFunc => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidStartFunc), + ))), + ffi::WasmEdge_ErrCode_InvalidLaneIdx => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidLaneIdx), + ))), + ffi::WasmEdge_ErrCode_InvalidUninitLocal => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidUninitLocal), + ))), + ffi::WasmEdge_ErrCode_InvalidNotDefaultableField => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidNotDefaultableField), + ))), + ffi::WasmEdge_ErrCode_InvalidNotDefaultableArray => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidNotDefaultableArray), + ))), + ffi::WasmEdge_ErrCode_InvalidPackedField => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidPackedField), + ))), + ffi::WasmEdge_ErrCode_InvalidPackedArray => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidPackedArray), + ))), + ffi::WasmEdge_ErrCode_InvalidUnpackedField => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidUnpackedField), + ))), + ffi::WasmEdge_ErrCode_InvalidUnpackedArray => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidUnpackedArray), + ))), + ffi::WasmEdge_ErrCode_InvalidBrRefType => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidBrRefType), + ))), + ffi::WasmEdge_ErrCode_ArrayTypesMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ArrayTypesMismatch), + ))), + ffi::WasmEdge_ErrCode_ArrayTypesNumtypeRequired => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::ArrayTypesNumtypeRequired), + ))), + ffi::WasmEdge_ErrCode_InvalidSubType => Err(Box::new(WasmEdgeError::Core( + CoreError::Validation(CoreValidationError::InvalidSubType), + ))), // Instantiation phase - 0x60 => Err(Box::new(WasmEdgeError::Core(CoreError::Instantiation( - CoreInstantiationError::ModuleNameConflict, - )))), - 0x61 => Err(Box::new(WasmEdgeError::Core(CoreError::Instantiation( - CoreInstantiationError::IncompatibleImportType, - )))), - 0x62 => Err(Box::new(WasmEdgeError::Core(CoreError::Instantiation( - CoreInstantiationError::UnknownImport, - )))), - 0x63 => Err(Box::new(WasmEdgeError::Core(CoreError::Instantiation( - CoreInstantiationError::DataSegDoesNotFit, - )))), - 0x64 => Err(Box::new(WasmEdgeError::Core(CoreError::Instantiation( - CoreInstantiationError::ElemSegDoesNotFit, - )))), + ffi::WasmEdge_ErrCode_ModuleNameConflict => Err(Box::new(WasmEdgeError::Core( + CoreError::Instantiation(CoreInstantiationError::ModuleNameConflict), + ))), + ffi::WasmEdge_ErrCode_IncompatibleImportType => Err(Box::new(WasmEdgeError::Core( + CoreError::Instantiation(CoreInstantiationError::IncompatibleImportType), + ))), + ffi::WasmEdge_ErrCode_UnknownImport => Err(Box::new(WasmEdgeError::Core( + CoreError::Instantiation(CoreInstantiationError::UnknownImport), + ))), + ffi::WasmEdge_ErrCode_DataSegDoesNotFit => Err(Box::new(WasmEdgeError::Core( + CoreError::Instantiation(CoreInstantiationError::DataSegDoesNotFit), + ))), + ffi::WasmEdge_ErrCode_ElemSegDoesNotFit => Err(Box::new(WasmEdgeError::Core( + CoreError::Instantiation(CoreInstantiationError::ElemSegDoesNotFit), + ))), // Execution phase - 0x80 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::WrongInstanceAddress, - )))), - 0x81 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::WrongInstanceIndex, - )))), - 0x82 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::InstrTypeMismatch, - )))), - 0x83 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::FuncTypeMismatch, - )))), - 0x84 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::DivideByZero, - )))), - 0x85 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::IntegerOverflow, - )))), - 0x86 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::InvalidConvToInt, - )))), - 0x87 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::TableOutOfBounds, - )))), - 0x88 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::MemoryOutOfBounds, - )))), - 0x89 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::Unreachable, - )))), - 0x8A => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::UninitializedElement, - )))), - 0x8B => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::UndefinedElement, - )))), - 0x8C => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::IndirectCallTypeMismatch, - )))), - 0x8D => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::HostFuncFailed, - )))), - 0x8E => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::RefTypeMismatch, - )))), - 0x8F => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::UnalignedAtomicAccess, - )))), - 0x90 => Err(Box::new(WasmEdgeError::Core(CoreError::Execution( - CoreExecutionError::ExpectSharedMemory, - )))), + ffi::WasmEdge_ErrCode_WrongInstanceAddress => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::WrongInstanceAddress), + ))), + ffi::WasmEdge_ErrCode_WrongInstanceIndex => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::WrongInstanceIndex), + ))), + ffi::WasmEdge_ErrCode_InstrTypeMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::InstrTypeMismatch), + ))), + ffi::WasmEdge_ErrCode_FuncSigMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::FuncSigMismatch), + ))), + ffi::WasmEdge_ErrCode_DivideByZero => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::DivideByZero), + ))), + ffi::WasmEdge_ErrCode_IntegerOverflow => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::IntegerOverflow), + ))), + ffi::WasmEdge_ErrCode_InvalidConvToInt => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::InvalidConvToInt), + ))), + ffi::WasmEdge_ErrCode_TableOutOfBounds => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::TableOutOfBounds), + ))), + ffi::WasmEdge_ErrCode_MemoryOutOfBounds => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::MemoryOutOfBounds), + ))), + ffi::WasmEdge_ErrCode_ArrayOutOfBounds => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::ArrayOutOfBounds), + ))), + ffi::WasmEdge_ErrCode_Unreachable => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::Unreachable), + ))), + ffi::WasmEdge_ErrCode_UninitializedElement => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::UninitializedElement), + ))), + ffi::WasmEdge_ErrCode_UndefinedElement => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::UndefinedElement), + ))), + ffi::WasmEdge_ErrCode_IndirectCallTypeMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::IndirectCallTypeMismatch), + ))), + ffi::WasmEdge_ErrCode_HostFuncError => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::HostFuncFailed), + ))), + ffi::WasmEdge_ErrCode_RefTypeMismatch => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::RefTypeMismatch), + ))), + ffi::WasmEdge_ErrCode_UnalignedAtomicAccess => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::UnalignedAtomicAccess), + ))), + ffi::WasmEdge_ErrCode_ExpectSharedMemory => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::ExpectSharedMemory), + ))), + ffi::WasmEdge_ErrCode_CastNullToNonNull => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::CastNullToNonNull), + ))), + ffi::WasmEdge_ErrCode_AccessNullFunc => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::AccessNullFunc), + ))), + ffi::WasmEdge_ErrCode_AccessNullStruct => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::AccessNullStruct), + ))), + ffi::WasmEdge_ErrCode_AccessNullArray => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::AccessNullArray), + ))), + ffi::WasmEdge_ErrCode_AccessNullI31 => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::AccessNullI31), + ))), + ffi::WasmEdge_ErrCode_CastFailed => Err(Box::new(WasmEdgeError::Core( + CoreError::Execution(CoreExecutionError::CastFailed), + ))), - _ => panic!("unknown error code: {code}"), + // Component model phase + ffi::WasmEdge_ErrCode_MalformedSort => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedSort), + ))), + ffi::WasmEdge_ErrCode_MalformedAliasTarget => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedAliasTarget), + ))), + ffi::WasmEdge_ErrCode_MalformedCoreInstance => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedCoreInstance), + ))), + ffi::WasmEdge_ErrCode_MalformedInstance => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedInstance), + ))), + ffi::WasmEdge_ErrCode_MalformedDefType => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedDefType), + ))), + ffi::WasmEdge_ErrCode_MalformedRecordType => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedRecordType), + ))), + ffi::WasmEdge_ErrCode_MalformedVariantType => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedVariantType), + ))), + ffi::WasmEdge_ErrCode_MalformedTupleType => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedTupleType), + ))), + ffi::WasmEdge_ErrCode_MalformedFlagsType => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedFlagsType), + ))), + ffi::WasmEdge_ErrCode_MalformedCanonical => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedCanonical), + ))), + ffi::WasmEdge_ErrCode_UnknownCanonicalOption => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::UnknownCanonicalOption), + ))), + ffi::WasmEdge_ErrCode_MalformedName => Err(Box::new(WasmEdgeError::Core( + CoreError::Component(CoreComponentError::MalformedName), + ))), + c => Err(Box::new(WasmEdgeError::Core(CoreError::UnknownError( + c as _, + )))), } } impl From for WasmEdge_Result { fn from(val: CoreError) -> WasmEdge_Result { let code = match val { - CoreError::Common(CoreCommonError::Terminated) => 0x01, // Common errors - CoreError::Common(CoreCommonError::RuntimeError) => 0x02, - CoreError::Common(CoreCommonError::CostLimitExceeded) => 0x03, - CoreError::Common(CoreCommonError::WrongVMWorkflow) => 0x04, - CoreError::Common(CoreCommonError::FuncNotFound) => 0x05, - CoreError::Common(CoreCommonError::AOTDisabled) => 0x06, - CoreError::Common(CoreCommonError::Interrupted) => 0x07, - CoreError::Common(CoreCommonError::NotValidated) => 0x08, - CoreError::Common(CoreCommonError::UserDefError) => 0x09, + CoreError::Common(e) => match e { + CoreCommonError::Terminated => ffi::WasmEdge_ErrCode_Terminated, + CoreCommonError::RuntimeError => ffi::WasmEdge_ErrCode_RuntimeError, + CoreCommonError::CostLimitExceeded => ffi::WasmEdge_ErrCode_CostLimitExceeded, + CoreCommonError::WrongVMWorkflow => ffi::WasmEdge_ErrCode_WrongVMWorkflow, + CoreCommonError::FuncNotFound => ffi::WasmEdge_ErrCode_FuncNotFound, + CoreCommonError::AOTDisabled => ffi::WasmEdge_ErrCode_AOTDisabled, + CoreCommonError::Interrupted => ffi::WasmEdge_ErrCode_Interrupted, + CoreCommonError::UserDefError => ffi::WasmEdge_ErrCode_UserDefError, + CoreCommonError::NotValidated => ffi::WasmEdge_ErrCode_NotValidated, + CoreCommonError::NonNullRequired => ffi::WasmEdge_ErrCode_NonNullRequired, + CoreCommonError::SetValueToConst => ffi::WasmEdge_ErrCode_SetValueToConst, + CoreCommonError::SetValueErrorType => ffi::WasmEdge_ErrCode_SetValueErrorType, + }, // Load phase - CoreError::Load(CoreLoadError::IllegalPath) => 0x20, - CoreError::Load(CoreLoadError::ReadError) => 0x21, - CoreError::Load(CoreLoadError::UnexpectedEnd) => 0x22, - CoreError::Load(CoreLoadError::MalformedMagic) => 0x23, - CoreError::Load(CoreLoadError::MalformedVersion) => 0x24, - CoreError::Load(CoreLoadError::MalformedSection) => 0x25, - CoreError::Load(CoreLoadError::SectionSizeMismatch) => 0x26, - CoreError::Load(CoreLoadError::NameSizeOutOfBounds) => 0x27, - CoreError::Load(CoreLoadError::JunkSection) => 0x28, - CoreError::Load(CoreLoadError::IncompatibleFuncCode) => 0x29, - CoreError::Load(CoreLoadError::IncompatibleDataCount) => 0x2A, - CoreError::Load(CoreLoadError::DataCountRequired) => 0x2B, - CoreError::Load(CoreLoadError::MalformedImportKind) => 0x2C, - CoreError::Load(CoreLoadError::MalformedExportKind) => 0x2D, - CoreError::Load(CoreLoadError::ExpectedZeroByte) => 0x2E, - CoreError::Load(CoreLoadError::InvalidMut) => 0x2F, - CoreError::Load(CoreLoadError::TooManyLocals) => 0x30, - CoreError::Load(CoreLoadError::MalformedValType) => 0x31, - CoreError::Load(CoreLoadError::MalformedElemType) => 0x32, - CoreError::Load(CoreLoadError::MalformedRefType) => 0x33, - CoreError::Load(CoreLoadError::MalformedUTF8) => 0x34, - CoreError::Load(CoreLoadError::IntegerTooLarge) => 0x35, - CoreError::Load(CoreLoadError::IntegerTooLong) => 0x36, - CoreError::Load(CoreLoadError::IllegalOpCode) => 0x37, - CoreError::Load(CoreLoadError::IllegalGrammar) => 0x38, + CoreError::Load(e) => match e { + CoreLoadError::IllegalPath => ffi::WasmEdge_ErrCode_IllegalPath, + CoreLoadError::ReadError => ffi::WasmEdge_ErrCode_ReadError, + CoreLoadError::UnexpectedEnd => ffi::WasmEdge_ErrCode_UnexpectedEnd, + CoreLoadError::MalformedMagic => ffi::WasmEdge_ErrCode_MalformedMagic, + CoreLoadError::MalformedVersion => ffi::WasmEdge_ErrCode_MalformedVersion, + CoreLoadError::MalformedSection => ffi::WasmEdge_ErrCode_MalformedSection, + CoreLoadError::SectionSizeMismatch => ffi::WasmEdge_ErrCode_SectionSizeMismatch, + CoreLoadError::LengthOutOfBounds => ffi::WasmEdge_ErrCode_LengthOutOfBounds, + CoreLoadError::JunkSection => ffi::WasmEdge_ErrCode_JunkSection, + CoreLoadError::IncompatibleFuncCode => ffi::WasmEdge_ErrCode_IncompatibleFuncCode, + CoreLoadError::IncompatibleDataCount => ffi::WasmEdge_ErrCode_IncompatibleDataCount, + CoreLoadError::DataCountRequired => ffi::WasmEdge_ErrCode_DataCountRequired, + CoreLoadError::MalformedImportKind => ffi::WasmEdge_ErrCode_MalformedImportKind, + CoreLoadError::MalformedExportKind => ffi::WasmEdge_ErrCode_MalformedExportKind, + CoreLoadError::ExpectedZeroByte => ffi::WasmEdge_ErrCode_ExpectedZeroByte, + CoreLoadError::InvalidMut => ffi::WasmEdge_ErrCode_InvalidMut, + CoreLoadError::TooManyLocals => ffi::WasmEdge_ErrCode_TooManyLocals, + CoreLoadError::MalformedValType => ffi::WasmEdge_ErrCode_MalformedValType, + CoreLoadError::MalformedElemType => ffi::WasmEdge_ErrCode_MalformedElemType, + CoreLoadError::MalformedRefType => ffi::WasmEdge_ErrCode_MalformedRefType, + CoreLoadError::MalformedUTF8 => ffi::WasmEdge_ErrCode_MalformedUTF8, + CoreLoadError::IntegerTooLarge => ffi::WasmEdge_ErrCode_IntegerTooLarge, + CoreLoadError::IntegerTooLong => ffi::WasmEdge_ErrCode_IntegerTooLong, + CoreLoadError::IllegalOpCode => ffi::WasmEdge_ErrCode_IllegalOpCode, + CoreLoadError::IllegalGrammar => ffi::WasmEdge_ErrCode_IllegalGrammar, + CoreLoadError::SharedMemoryNoMax => ffi::WasmEdge_ErrCode_SharedMemoryNoMax, + CoreLoadError::IntrinsicsTableNotFound => { + ffi::WasmEdge_ErrCode_IntrinsicsTableNotFound + } + CoreLoadError::MalformedTable => ffi::WasmEdge_ErrCode_MalformedTable, + }, // Validation phase - CoreError::Validation(CoreValidationError::InvalidAlignment) => 0x40, - CoreError::Validation(CoreValidationError::TypeCheckFailed) => 0x41, - CoreError::Validation(CoreValidationError::InvalidLabelIdx) => 0x42, - CoreError::Validation(CoreValidationError::InvalidLocalIdx) => 0x43, - CoreError::Validation(CoreValidationError::InvalidFuncTypeIdx) => 0x44, - CoreError::Validation(CoreValidationError::InvalidFuncIdx) => 0x45, - CoreError::Validation(CoreValidationError::InvalidTableIdx) => 0x46, - CoreError::Validation(CoreValidationError::InvalidMemoryIdx) => 0x47, - CoreError::Validation(CoreValidationError::InvalidGlobalIdx) => 0x48, - CoreError::Validation(CoreValidationError::InvalidElemIdx) => 0x49, - CoreError::Validation(CoreValidationError::InvalidDataIdx) => 0x4A, - CoreError::Validation(CoreValidationError::InvalidRefIdx) => 0x4B, - CoreError::Validation(CoreValidationError::ConstExprRequired) => 0x4C, - CoreError::Validation(CoreValidationError::DupExportName) => 0x4D, - CoreError::Validation(CoreValidationError::ImmutableGlobal) => 0x4E, - CoreError::Validation(CoreValidationError::InvalidResultArity) => 0x4F, - CoreError::Validation(CoreValidationError::MultiTables) => 0x50, - CoreError::Validation(CoreValidationError::MultiMemories) => 0x51, - CoreError::Validation(CoreValidationError::InvalidLimit) => 0x52, - CoreError::Validation(CoreValidationError::InvalidMemPages) => 0x53, - CoreError::Validation(CoreValidationError::InvalidStartFunc) => 0x54, - CoreError::Validation(CoreValidationError::InvalidLaneIdx) => 0x55, + CoreError::Validation(e) => match e { + CoreValidationError::InvalidAlignment => ffi::WasmEdge_ErrCode_InvalidAlignment, + CoreValidationError::TypeCheckFailed => ffi::WasmEdge_ErrCode_TypeCheckFailed, + CoreValidationError::InvalidLabelIdx => ffi::WasmEdge_ErrCode_InvalidLabelIdx, + CoreValidationError::InvalidLocalIdx => ffi::WasmEdge_ErrCode_InvalidLocalIdx, + CoreValidationError::InvalidFieldIdx => ffi::WasmEdge_ErrCode_InvalidFieldIdx, + CoreValidationError::InvalidFuncTypeIdx => ffi::WasmEdge_ErrCode_InvalidFuncTypeIdx, + CoreValidationError::InvalidFuncIdx => ffi::WasmEdge_ErrCode_InvalidFuncIdx, + CoreValidationError::InvalidTableIdx => ffi::WasmEdge_ErrCode_InvalidTableIdx, + CoreValidationError::InvalidMemoryIdx => ffi::WasmEdge_ErrCode_InvalidMemoryIdx, + CoreValidationError::InvalidGlobalIdx => ffi::WasmEdge_ErrCode_InvalidGlobalIdx, + CoreValidationError::InvalidElemIdx => ffi::WasmEdge_ErrCode_InvalidElemIdx, + CoreValidationError::InvalidDataIdx => ffi::WasmEdge_ErrCode_InvalidDataIdx, + CoreValidationError::InvalidRefIdx => ffi::WasmEdge_ErrCode_InvalidRefIdx, + CoreValidationError::ConstExprRequired => ffi::WasmEdge_ErrCode_ConstExprRequired, + CoreValidationError::DupExportName => ffi::WasmEdge_ErrCode_DupExportName, + CoreValidationError::ImmutableGlobal => ffi::WasmEdge_ErrCode_ImmutableGlobal, + CoreValidationError::ImmutableField => ffi::WasmEdge_ErrCode_ImmutableField, + CoreValidationError::ImmutableArray => ffi::WasmEdge_ErrCode_ImmutableArray, + CoreValidationError::InvalidResultArity => ffi::WasmEdge_ErrCode_InvalidResultArity, + CoreValidationError::MultiTables => ffi::WasmEdge_ErrCode_MultiTables, + CoreValidationError::MultiMemories => ffi::WasmEdge_ErrCode_MultiMemories, + CoreValidationError::InvalidLimit => ffi::WasmEdge_ErrCode_InvalidLimit, + CoreValidationError::InvalidMemPages => ffi::WasmEdge_ErrCode_InvalidMemPages, + CoreValidationError::InvalidStartFunc => ffi::WasmEdge_ErrCode_InvalidStartFunc, + CoreValidationError::InvalidLaneIdx => ffi::WasmEdge_ErrCode_InvalidLaneIdx, + CoreValidationError::InvalidUninitLocal => ffi::WasmEdge_ErrCode_InvalidUninitLocal, + CoreValidationError::InvalidNotDefaultableField => { + ffi::WasmEdge_ErrCode_InvalidNotDefaultableField + } + CoreValidationError::InvalidNotDefaultableArray => { + ffi::WasmEdge_ErrCode_InvalidNotDefaultableArray + } + CoreValidationError::InvalidPackedField => ffi::WasmEdge_ErrCode_InvalidPackedField, + CoreValidationError::InvalidPackedArray => ffi::WasmEdge_ErrCode_InvalidPackedArray, + CoreValidationError::InvalidUnpackedField => { + ffi::WasmEdge_ErrCode_InvalidUnpackedField + } + CoreValidationError::InvalidUnpackedArray => { + ffi::WasmEdge_ErrCode_InvalidUnpackedArray + } + CoreValidationError::InvalidBrRefType => ffi::WasmEdge_ErrCode_InvalidBrRefType, + CoreValidationError::ArrayTypesMismatch => ffi::WasmEdge_ErrCode_ArrayTypesMismatch, + CoreValidationError::ArrayTypesNumtypeRequired => { + ffi::WasmEdge_ErrCode_ArrayTypesNumtypeRequired + } + CoreValidationError::InvalidSubType => ffi::WasmEdge_ErrCode_InvalidSubType, + }, // Instantiation phase - CoreError::Instantiation(CoreInstantiationError::ModuleNameConflict) => 0x60, - CoreError::Instantiation(CoreInstantiationError::IncompatibleImportType) => 0x61, - CoreError::Instantiation(CoreInstantiationError::UnknownImport) => 0x62, - CoreError::Instantiation(CoreInstantiationError::DataSegDoesNotFit) => 0x63, - CoreError::Instantiation(CoreInstantiationError::ElemSegDoesNotFit) => 0x64, + CoreError::Instantiation(e) => match e { + CoreInstantiationError::ModuleNameConflict => { + ffi::WasmEdge_ErrCode_ModuleNameConflict + } + CoreInstantiationError::IncompatibleImportType => { + ffi::WasmEdge_ErrCode_IncompatibleImportType + } + CoreInstantiationError::UnknownImport => ffi::WasmEdge_ErrCode_UnknownImport, + CoreInstantiationError::DataSegDoesNotFit => { + ffi::WasmEdge_ErrCode_DataSegDoesNotFit + } + CoreInstantiationError::ElemSegDoesNotFit => { + ffi::WasmEdge_ErrCode_ElemSegDoesNotFit + } + }, // Execution phase - CoreError::Execution(CoreExecutionError::WrongInstanceAddress) => 0x80, - CoreError::Execution(CoreExecutionError::WrongInstanceIndex) => 0x81, - CoreError::Execution(CoreExecutionError::InstrTypeMismatch) => 0x82, - CoreError::Execution(CoreExecutionError::FuncTypeMismatch) => 0x83, - CoreError::Execution(CoreExecutionError::DivideByZero) => 0x84, - CoreError::Execution(CoreExecutionError::IntegerOverflow) => 0x85, - CoreError::Execution(CoreExecutionError::InvalidConvToInt) => 0x86, - CoreError::Execution(CoreExecutionError::TableOutOfBounds) => 0x87, - CoreError::Execution(CoreExecutionError::MemoryOutOfBounds) => 0x88, - CoreError::Execution(CoreExecutionError::Unreachable) => 0x89, - CoreError::Execution(CoreExecutionError::UninitializedElement) => 0x8A, - CoreError::Execution(CoreExecutionError::UndefinedElement) => 0x8B, - CoreError::Execution(CoreExecutionError::IndirectCallTypeMismatch) => 0x8C, - CoreError::Execution(CoreExecutionError::HostFuncFailed) => 0x8D, - CoreError::Execution(CoreExecutionError::RefTypeMismatch) => 0x8E, - CoreError::Execution(CoreExecutionError::UnalignedAtomicAccess) => 0x8F, - CoreError::Execution(CoreExecutionError::ExpectSharedMemory) => 0x90, + CoreError::Execution(e) => match e { + CoreExecutionError::WrongInstanceAddress => { + ffi::WasmEdge_ErrCode_WrongInstanceAddress + } + CoreExecutionError::WrongInstanceIndex => ffi::WasmEdge_ErrCode_WrongInstanceIndex, + CoreExecutionError::InstrTypeMismatch => ffi::WasmEdge_ErrCode_InstrTypeMismatch, + CoreExecutionError::FuncSigMismatch => ffi::WasmEdge_ErrCode_FuncSigMismatch, + CoreExecutionError::DivideByZero => ffi::WasmEdge_ErrCode_DivideByZero, + CoreExecutionError::IntegerOverflow => ffi::WasmEdge_ErrCode_IntegerOverflow, + CoreExecutionError::InvalidConvToInt => ffi::WasmEdge_ErrCode_InvalidConvToInt, + CoreExecutionError::TableOutOfBounds => ffi::WasmEdge_ErrCode_TableOutOfBounds, + CoreExecutionError::MemoryOutOfBounds => ffi::WasmEdge_ErrCode_MemoryOutOfBounds, + CoreExecutionError::ArrayOutOfBounds => ffi::WasmEdge_ErrCode_ArrayOutOfBounds, + CoreExecutionError::Unreachable => ffi::WasmEdge_ErrCode_Unreachable, + CoreExecutionError::UninitializedElement => { + ffi::WasmEdge_ErrCode_UninitializedElement + } + CoreExecutionError::UndefinedElement => ffi::WasmEdge_ErrCode_UndefinedElement, + CoreExecutionError::IndirectCallTypeMismatch => { + ffi::WasmEdge_ErrCode_IndirectCallTypeMismatch + } + CoreExecutionError::HostFuncFailed => ffi::WasmEdge_ErrCode_HostFuncError, + CoreExecutionError::RefTypeMismatch => ffi::WasmEdge_ErrCode_RefTypeMismatch, + CoreExecutionError::UnalignedAtomicAccess => { + ffi::WasmEdge_ErrCode_UnalignedAtomicAccess + } + CoreExecutionError::ExpectSharedMemory => ffi::WasmEdge_ErrCode_ExpectSharedMemory, + CoreExecutionError::CastNullToNonNull => ffi::WasmEdge_ErrCode_CastNullToNonNull, + CoreExecutionError::AccessNullFunc => ffi::WasmEdge_ErrCode_AccessNullFunc, + CoreExecutionError::AccessNullStruct => ffi::WasmEdge_ErrCode_AccessNullStruct, + CoreExecutionError::AccessNullArray => ffi::WasmEdge_ErrCode_AccessNullArray, + CoreExecutionError::AccessNullI31 => ffi::WasmEdge_ErrCode_AccessNullI31, + CoreExecutionError::CastFailed => ffi::WasmEdge_ErrCode_CastFailed, + }, + + CoreError::Component(e) => match e { + CoreComponentError::MalformedSort => ffi::WasmEdge_ErrCode_MalformedSort, + CoreComponentError::MalformedAliasTarget => { + ffi::WasmEdge_ErrCode_MalformedAliasTarget + } + CoreComponentError::MalformedCoreInstance => { + ffi::WasmEdge_ErrCode_MalformedCoreInstance + } + CoreComponentError::MalformedInstance => ffi::WasmEdge_ErrCode_MalformedInstance, + CoreComponentError::MalformedDefType => ffi::WasmEdge_ErrCode_MalformedDefType, + CoreComponentError::MalformedRecordType => { + ffi::WasmEdge_ErrCode_MalformedRecordType + } + CoreComponentError::MalformedVariantType => { + ffi::WasmEdge_ErrCode_MalformedVariantType + } + CoreComponentError::MalformedTupleType => ffi::WasmEdge_ErrCode_MalformedTupleType, + CoreComponentError::MalformedFlagsType => ffi::WasmEdge_ErrCode_MalformedFlagsType, + CoreComponentError::MalformedCanonical => ffi::WasmEdge_ErrCode_MalformedCanonical, + CoreComponentError::UnknownCanonicalOption => { + ffi::WasmEdge_ErrCode_UnknownCanonicalOption + } + CoreComponentError::MalformedName => ffi::WasmEdge_ErrCode_MalformedName, + }, + CoreError::UnknownError(c) => c as ffi::WasmEdge_ErrCode, }; unsafe { ffi::WasmEdge_ResultGen(ffi::WasmEdge_ErrCategory_WASM, code) } } diff --git a/crates/wasmedge-types/src/error.rs b/crates/wasmedge-types/src/error.rs index bc212d6ca..6a81448a5 100644 --- a/crates/wasmedge-types/src/error.rs +++ b/crates/wasmedge-types/src/error.rs @@ -299,6 +299,10 @@ pub enum CoreError { Instantiation(CoreInstantiationError), #[error("{0}")] Execution(CoreExecutionError), + #[error("{0}")] + Component(CoreComponentError), + #[error("unknown error code {0}")] + UnknownError(u32), } /// The error type for the common errors from WasmEdge Core. @@ -322,6 +326,12 @@ pub enum CoreCommonError { UserDefError, #[error("wasm module hasn't passed validation yet")] NotValidated, + #[error("set null value into non-nullable value type")] + NonNullRequired, + #[error("set value into const")] + SetValueToConst, + #[error("set value type mismatch")] + SetValueErrorType, } /// The error type for the load phase from WasmEdge Core. @@ -342,7 +352,7 @@ pub enum CoreLoadError { #[error("section size mismatch")] SectionSizeMismatch, #[error("length out of bounds")] - NameSizeOutOfBounds, + LengthOutOfBounds, #[error("unexpected content after last section")] JunkSection, #[error("function and code section have inconsistent lengths")] @@ -377,6 +387,12 @@ pub enum CoreLoadError { IllegalOpCode, #[error("invalid wasm grammar")] IllegalGrammar, + #[error("shared memory must have maximum")] + SharedMemoryNoMax, + #[error("intrinsics table not found")] + IntrinsicsTableNotFound, + #[error("malformed table")] + MalformedTable, } /// The error type for the validation phase from WasmEdge Core. @@ -390,6 +406,8 @@ pub enum CoreValidationError { InvalidLabelIdx, #[error("unknown local")] InvalidLocalIdx, + #[error("unknown field")] + InvalidFieldIdx, #[error("unknown type")] InvalidFuncTypeIdx, #[error("unknown function")] @@ -412,6 +430,10 @@ pub enum CoreValidationError { DupExportName, #[error("global is immutable")] ImmutableGlobal, + #[error("field is immutable")] + ImmutableField, + #[error("array is immutable")] + ImmutableArray, #[error("invalid result arity")] InvalidResultArity, #[error("multiple tables")] @@ -426,6 +448,28 @@ pub enum CoreValidationError { InvalidStartFunc, #[error("invalid lane index")] InvalidLaneIdx, + #[error("uninitialized local")] + InvalidUninitLocal, + #[error("field type is not defaultable")] + InvalidNotDefaultableField, + #[error("array type is not defaultable")] + InvalidNotDefaultableArray, + #[error("field is packed")] + InvalidPackedField, + #[error("array is packed")] + InvalidPackedArray, + #[error("field is unpacked")] + InvalidUnpackedField, + #[error("array is unpacked")] + InvalidUnpackedArray, + #[error("invalid br ref type")] + InvalidBrRefType, + #[error("array types do not match")] + ArrayTypesMismatch, + #[error("array type is not numeric or vector")] + ArrayTypesNumtypeRequired, + #[error("sub type")] + InvalidSubType, } /// The error type for the instantiation phase from WasmEdge Core. @@ -452,8 +496,8 @@ pub enum CoreExecutionError { WrongInstanceIndex, #[error("instruction type mismatch")] InstrTypeMismatch, - #[error("function type mismatch")] - FuncTypeMismatch, + #[error("function signature mismatch")] + FuncSigMismatch, #[error("integer divide by zero")] DivideByZero, #[error("integer overflow")] @@ -464,6 +508,8 @@ pub enum CoreExecutionError { TableOutOfBounds, #[error("out of bounds memory access")] MemoryOutOfBounds, + #[error("out of bounds array access")] + ArrayOutOfBounds, #[error("unreachable")] Unreachable, #[error("uninitialized element")] @@ -480,6 +526,47 @@ pub enum CoreExecutionError { UnalignedAtomicAccess, #[error("expected shared memory")] ExpectSharedMemory, + #[error("null reference")] + CastNullToNonNull, + #[error("null function reference")] + AccessNullFunc, + #[error("null structure reference")] + AccessNullStruct, + #[error("null array reference")] + AccessNullArray, + #[error("null i31 reference")] + AccessNullI31, + #[error("cast failure")] + CastFailed, +} + +/// The error type for the component model phase from WasmEdge Core. +#[derive(Error, Clone, Debug, PartialEq, Eq)] +pub enum CoreComponentError { + #[error("malformed sort")] + MalformedSort, + #[error("malformed alias target")] + MalformedAliasTarget, + #[error("malformed core instance")] + MalformedCoreInstance, + #[error("malformed instance")] + MalformedInstance, + #[error("malformed defined type")] + MalformedDefType, + #[error("malformed record type")] + MalformedRecordType, + #[error("malformed variant type")] + MalformedVariantType, + #[error("malformed tuple type")] + MalformedTupleType, + #[error("malformed flags type")] + MalformedFlagsType, + #[error("malformed canonical")] + MalformedCanonical, + #[error("unknown canonical option")] + UnknownCanonicalOption, + #[error("malformed name")] + MalformedName, } /// The error type for the host function definition. diff --git a/crates/wasmedge-types/src/lib.rs b/crates/wasmedge-types/src/lib.rs index 14be61aa1..b7077fe9c 100644 --- a/crates/wasmedge-types/src/lib.rs +++ b/crates/wasmedge-types/src/lib.rs @@ -20,37 +20,22 @@ pub enum RefType { /// Refers to the infinite union of all references to objects and that can be passed into WebAssembly under this type. ExternRef, } -impl From for RefType { - fn from(value: u32) -> Self { - match value { - 112 => RefType::FuncRef, - 111 => RefType::ExternRef, - _ => panic!("[wasmedge-types] Invalid WasmEdge_RefType: {value:#X}"), - } - } -} -impl From for u32 { - fn from(value: RefType) -> Self { - match value { - RefType::FuncRef => 112, - RefType::ExternRef => 111, - } - } -} -impl From for RefType { - fn from(value: i32) -> Self { + +impl From for RefType { + fn from(value: ValType) -> Self { match value { - 112 => RefType::FuncRef, - 111 => RefType::ExternRef, - _ => panic!("[wasmedge-types] Invalid WasmEdge_RefType: {value:#X}"), + ValType::FuncRef => RefType::FuncRef, + ValType::ExternRef => RefType::ExternRef, + _ => panic!("[wasmedge-types] Invalid WasmEdge_RefType: {value:#X?}"), } } } -impl From for i32 { + +impl From for ValType { fn from(value: RefType) -> Self { match value { - RefType::FuncRef => 112, - RefType::ExternRef => 111, + RefType::FuncRef => ValType::FuncRef, + RefType::ExternRef => ValType::ExternRef, } } } @@ -80,60 +65,6 @@ pub enum ValType { /// A reference to object. ExternRef, } -impl From for ValType { - fn from(value: u32) -> Self { - match value { - 127 => ValType::I32, - 126 => ValType::I64, - 125 => ValType::F32, - 124 => ValType::F64, - 123 => ValType::V128, - 112 => ValType::FuncRef, - 111 => ValType::ExternRef, - _ => panic!("[wasmedge-types] Invalid WasmEdge_ValType: {value:#X}"), - } - } -} -impl From for u32 { - fn from(value: ValType) -> Self { - match value { - ValType::I32 => 127, - ValType::I64 => 126, - ValType::F32 => 125, - ValType::F64 => 124, - ValType::V128 => 123, - ValType::FuncRef => 112, - ValType::ExternRef => 111, - } - } -} -impl From for ValType { - fn from(value: i32) -> Self { - match value { - 127 => ValType::I32, - 126 => ValType::I64, - 125 => ValType::F32, - 124 => ValType::F64, - 123 => ValType::V128, - 112 => ValType::FuncRef, - 111 => ValType::ExternRef, - _ => panic!("[wasmedge-types] Invalid WasmEdge_ValType: {value:#X}"), - } - } -} -impl From for i32 { - fn from(value: ValType) -> Self { - match value { - ValType::I32 => 127, - ValType::I64 => 126, - ValType::F32 => 125, - ValType::F64 => 124, - ValType::V128 => 123, - ValType::FuncRef => 112, - ValType::ExternRef => 111, - } - } -} /// Defines the mutability property of WasmEdge Global variables. /// diff --git a/src/types.rs b/src/types.rs index 618572b79..17df5c334 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,7 @@ //! Defines the general types. use wasmedge_sys::WasmValue; -use wasmedge_types::{self, RefType}; +use wasmedge_types::{self}; /// Defines runtime values that a WebAssembly module can either consume or produce. #[derive(Debug, Clone)] @@ -45,7 +45,7 @@ impl From for WasmValue { // Val::FuncRef(Some(func_ref)) => WasmValue::from_func_ref(func_ref.inner), // Val::FuncRef(None) => WasmValue::from_null_ref(RefType::FuncRef), Val::ExternRef(Some(extern_ref)) => extern_ref.inner, - Val::ExternRef(None) => WasmValue::from_null_ref(RefType::ExternRef), + Val::ExternRef(None) => WasmValue::null_extern_ref(), } } }