From 97521a739c67a8d8bcf9c2733e6505e95d05a9c8 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 18 Sep 2020 15:25:03 +0200 Subject: [PATCH] feat(c-api) Implement `wasm_module_validate`. --- lib/c-api/src/wasm_c_api/mod.rs | 22 +++++++++++++++++++++- lib/c-api/src/wasm_c_api/utils.rs | 13 ++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/mod.rs b/lib/c-api/src/wasm_c_api/mod.rs index 8e14c34cd2f..efd022409f1 100644 --- a/lib/c-api/src/wasm_c_api/mod.rs +++ b/lib/c-api/src/wasm_c_api/mod.rs @@ -199,7 +199,7 @@ pub unsafe extern "C" fn wasm_module_new( ) -> Option> { // TODO: review lifetime of byte slice let wasm_byte_slice: &[u8] = slice::from_raw_parts_mut(bytes.data, bytes.size); - let store_ptr: NonNull = store_ptr?.cast::(); + let store_ptr = store_ptr?.cast::(); let store = store_ptr.as_ref(); let module = c_try!(Module::from_binary(store, wasm_byte_slice)); @@ -208,6 +208,26 @@ pub unsafe extern "C" fn wasm_module_new( })) } +#[no_mangle] +pub unsafe extern "C" fn wasm_module_validate( + store_ptr: Option>, + bytes: &wasm_byte_vec_t, +) -> bool { + // TODO: review lifetime of byte slice + let wasm_byte_slice: &[u8] = slice::from_raw_parts_mut(bytes.data, bytes.size); + + if store_ptr.is_none() { + return false; + } + + let store_ptr = store_ptr.unwrap().cast::(); + let store = store_ptr.as_ref(); + + c_try!(Module::validate(store, wasm_byte_slice); otherwise return false); + + true +} + #[no_mangle] pub unsafe extern "C" fn wasm_module_delete(_module: Option>) {} diff --git a/lib/c-api/src/wasm_c_api/utils.rs b/lib/c-api/src/wasm_c_api/utils.rs index fa041ee70a4..bfecd533b9a 100644 --- a/lib/c-api/src/wasm_c_api/utils.rs +++ b/lib/c-api/src/wasm_c_api/utils.rs @@ -1,17 +1,20 @@ #[macro_export] macro_rules! c_try { ($expr:expr) => {{ + c_try!($expr; otherwise return None) + }}; + ($expr:expr, $e:expr) => {{ + let opt: Option<_> = $expr; + c_try!(opt.ok_or_else(|| $e)) + }}; + ($expr:expr; otherwise return $ret:expr) => {{ let res: Result<_, _> = $expr; match res { Ok(val) => val, Err(err) => { crate::error::update_last_error(err); - return None; + return $ret; } } }}; - ($expr:expr, $e:expr) => {{ - let opt: Option<_> = $expr; - c_try!(opt.ok_or_else(|| $e)) - }}; }