From d5a758c0e13585743e8a3e3981fb8feec1fb50e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 11 Jan 2023 15:47:26 +0100 Subject: [PATCH] refactor(napi): Cleanup tests (#17347) --- test_napi/src/array.rs | 44 ++++++------ test_napi/src/arraybuffer.rs | 30 ++++---- test_napi/src/async.rs | 87 +++++++++++------------ test_napi/src/callback.rs | 85 +++++++++++----------- test_napi/src/coerce.rs | 43 ++++++------ test_napi/src/lib.rs | 49 +++++++------ test_napi/src/numbers.rs | 33 +++++---- test_napi/src/object_wrap.rs | 132 +++++++++++++++-------------------- test_napi/src/primitives.rs | 17 +++-- test_napi/src/promise.rs | 44 ++++++------ test_napi/src/properties.rs | 58 +++++++-------- test_napi/src/strings.rs | 25 ++++--- test_napi/src/typedarray.rs | 55 +++++++-------- 13 files changed, 340 insertions(+), 362 deletions(-) diff --git a/test_napi/src/array.rs b/test_napi/src/array.rs index adb2b426e1..065436df43 100644 --- a/test_napi/src/array.rs +++ b/test_napi/src/array.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::ValueType::napi_number; use napi_sys::ValueType::napi_object; use napi_sys::*; @@ -10,25 +12,23 @@ extern "C" fn test_array_new( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_object); let mut value: napi_value = ptr::null_mut(); - assert!(unsafe { napi_create_array(env, &mut value) } == napi_ok); + assert_napi_ok!(napi_create_array(env, &mut value)); let mut length: u32 = 0; - assert!( - unsafe { napi_get_array_length(env, args[0], &mut length) } == napi_ok - ); + assert_napi_ok!(napi_get_array_length(env, args[0], &mut length)); for i in 0..length { let mut e: napi_value = ptr::null_mut(); - assert!(unsafe { napi_get_element(env, args[0], i, &mut e) } == napi_ok); - assert!(unsafe { napi_set_element(env, value, i, e) } == napi_ok); + assert_napi_ok!(napi_get_element(env, args[0], i, &mut e)); + assert_napi_ok!(napi_set_element(env, value, i, e)); } value @@ -38,36 +38,36 @@ extern "C" fn test_array_new_with_length( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_number); let mut len: u32 = 0; - assert!(unsafe { napi_get_value_uint32(env, args[0], &mut len) } == napi_ok); + assert_napi_ok!(napi_get_value_uint32(env, args[0], &mut len)); let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_create_array_with_length(env, len as usize, &mut value) } - == napi_ok - ); + assert_napi_ok!(napi_create_array_with_length(env, len as usize, &mut value)); value } pub fn init(env: napi_env, exports: napi_value) { let properties = &[ - crate::new_property!(env, "test_array_new\0", test_array_new), - crate::new_property!( + napi_new_property!(env, "test_array_new", test_array_new), + napi_new_property!( env, - "test_array_new_with_length\0", + "test_array_new_with_length", test_array_new_with_length ), ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/arraybuffer.rs b/test_napi/src/arraybuffer.rs index 6765df4815..96d89f9d96 100644 --- a/test_napi/src/arraybuffer.rs +++ b/test_napi/src/arraybuffer.rs @@ -1,35 +1,33 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::*; extern "C" fn test_detached( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut value = false; - assert!( - unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) } - == napi_ok - ); + assert_napi_ok!(napi_is_detached_arraybuffer(env, args[0], &mut value)); assert!(!value); - assert!(unsafe { napi_detach_arraybuffer(env, args[0]) } == napi_ok); - assert!( - unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) } - == napi_ok - ); + assert_napi_ok!(napi_detach_arraybuffer(env, args[0])); + assert_napi_ok!(napi_is_detached_arraybuffer(env, args[0], &mut value)); assert!(value); args[0] } pub fn init(env: napi_env, exports: napi_value) { - let properties = - &[crate::new_property!(env, "test_detached\0", test_detached)]; + let properties = &[napi_new_property!(env, "test_detached", test_detached)]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/async.rs b/test_napi/src/async.rs index d825636e10..0df4990ab2 100644 --- a/test_napi/src/async.rs +++ b/test_napi/src/async.rs @@ -1,5 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::Status::napi_ok; use napi_sys::ValueType::napi_function; use napi_sys::*; @@ -31,82 +34,76 @@ unsafe extern "C" fn complete( assert!(!baton.func.is_null()); let mut global: napi_value = ptr::null_mut(); - assert!(napi_get_global(env, &mut global) == napi_ok); + assert_napi_ok!(napi_get_global(env, &mut global)); let mut callback: napi_value = ptr::null_mut(); - assert!(napi_get_reference_value(env, baton.func, &mut callback) == napi_ok); + assert_napi_ok!(napi_get_reference_value(env, baton.func, &mut callback)); let mut _result: napi_value = ptr::null_mut(); - assert!( - napi_call_function(env, global, callback, 0, ptr::null(), &mut _result) - == napi_ok - ); + assert_napi_ok!(napi_call_function( + env, + global, + callback, + 0, + ptr::null(), + &mut _result + )); - assert!(napi_delete_reference(env, baton.func) == napi_ok); - assert!(napi_delete_async_work(env, baton.task) == napi_ok); + assert_napi_ok!(napi_delete_reference(env, baton.func)); + assert_napi_ok!(napi_delete_async_work(env, baton.task)); } extern "C" fn test_async_work( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_function); let mut resource_name: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_create_string_utf8( - env, - "test_async_resource\0".as_ptr() as *const c_char, - usize::MAX, - &mut resource_name, - ) - } == napi_ok - ); + assert_napi_ok!(napi_create_string_utf8( + env, + "test_async_resource".as_ptr() as *const c_char, + usize::MAX, + &mut resource_name, + )); let mut async_work: napi_async_work = ptr::null_mut(); let mut func: napi_ref = ptr::null_mut(); - assert!( - unsafe { napi_create_reference(env, args[0], 1, &mut func) } == napi_ok - ); + assert_napi_ok!(napi_create_reference(env, args[0], 1, &mut func)); let baton = Box::new(Baton { called: false, func, task: async_work, }); - assert!( - unsafe { - napi_create_async_work( - env, - ptr::null_mut(), - resource_name, - Some(execute), - Some(complete), - Box::into_raw(baton) as *mut c_void, - &mut async_work, - ) - } == napi_ok - ); - assert!(unsafe { napi_queue_async_work(env, async_work) } == napi_ok); + assert_napi_ok!(napi_create_async_work( + env, + ptr::null_mut(), + resource_name, + Some(execute), + Some(complete), + Box::into_raw(baton) as *mut c_void, + &mut async_work, + )); + assert_napi_ok!(napi_queue_async_work(env, async_work)); ptr::null_mut() } pub fn init(env: napi_env, exports: napi_value) { - let properties = &[crate::new_property!( - env, - "test_async_work\0", - test_async_work - )]; + let properties = + &[napi_new_property!(env, "test_async_work", test_async_work)]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/callback.rs b/test_napi/src/callback.rs index 4b97f1c0b1..c863eb7a60 100644 --- a/test_napi/src/callback.rs +++ b/test_napi/src/callback.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::ValueType::napi_function; use napi_sys::ValueType::napi_object; use napi_sys::*; @@ -11,44 +13,38 @@ extern "C" fn test_callback_run( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 2); + let (args, argc, _) = napi_get_callback_info!(env, info, 2); assert_eq!(argc, 2); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_function); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[1], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[1], &mut ty)); assert_eq!(ty, napi_object); let mut len = 0; - assert!(unsafe { napi_get_array_length(env, args[1], &mut len) } == napi_ok); + assert_napi_ok!(napi_get_array_length(env, args[1], &mut len)); let mut argv = Vec::with_capacity(len as usize); for index in 0..len { let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_get_element(env, args[1], index, &mut value) } == napi_ok - ); + assert_napi_ok!(napi_get_element(env, args[1], index, &mut value)); argv.push(value); } let mut global: napi_value = ptr::null_mut(); - assert!(unsafe { napi_get_global(env, &mut global) } == napi_ok); + assert_napi_ok!(napi_get_global(env, &mut global)); let mut result: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_call_function( - env, - global, - args[0], - argv.len(), - argv.as_mut_ptr(), - &mut result, - ) - } == napi_ok - ); + assert_napi_ok!(napi_call_function( + env, + global, + args[0], + argv.len(), + argv.as_mut_ptr(), + &mut result, + )); result } @@ -57,57 +53,54 @@ extern "C" fn test_callback_run_with_recv( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 3); + let (args, argc, _) = napi_get_callback_info!(env, info, 3); assert_eq!(argc, 3); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_function); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[1], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[1], &mut ty)); assert_eq!(ty, napi_object); let mut len = 0; - assert!(unsafe { napi_get_array_length(env, args[1], &mut len) } == napi_ok); + assert_napi_ok!(napi_get_array_length(env, args[1], &mut len)); let mut argv = Vec::with_capacity(len as usize); for index in 0..len { let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_get_element(env, args[1], index, &mut value) } == napi_ok - ); + assert_napi_ok!(napi_get_element(env, args[1], index, &mut value)); argv.push(value); } let mut result: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_call_function( - env, - args[2], // recv - args[0], // cb - argv.len(), - argv.as_mut_ptr(), - &mut result, - ) - } == napi_ok - ); + assert_napi_ok!(napi_call_function( + env, + args[2], // recv + args[0], // cb + argv.len(), + argv.as_mut_ptr(), + &mut result, + )); result } pub fn init(env: napi_env, exports: napi_value) { let properties = &[ - crate::new_property!(env, "test_callback_run\0", test_callback_run), - crate::new_property!( + napi_new_property!(env, "test_callback_run", test_callback_run), + napi_new_property!( env, - "test_callback_run_with_recv\0", + "test_callback_run_with_recv", test_callback_run_with_recv ), ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/coerce.rs b/test_napi/src/coerce.rs index edce7f7672..80ee5aea1a 100644 --- a/test_napi/src/coerce.rs +++ b/test_napi/src/coerce.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::*; use std::ptr; @@ -8,11 +10,11 @@ extern "C" fn test_coerce_bool( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut value: napi_value = ptr::null_mut(); - assert!(unsafe { napi_coerce_to_bool(env, args[0], &mut value) } == napi_ok); + assert_napi_ok!(napi_coerce_to_bool(env, args[0], &mut value)); value } @@ -20,13 +22,11 @@ extern "C" fn test_coerce_number( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_coerce_to_number(env, args[0], &mut value) } == napi_ok - ); + assert_napi_ok!(napi_coerce_to_number(env, args[0], &mut value)); value } @@ -34,13 +34,11 @@ extern "C" fn test_coerce_object( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_coerce_to_object(env, args[0], &mut value) } == napi_ok - ); + assert_napi_ok!(napi_coerce_to_object(env, args[0], &mut value)); value } @@ -48,24 +46,25 @@ extern "C" fn test_coerce_string( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_coerce_to_string(env, args[0], &mut value) } == napi_ok - ); + assert_napi_ok!(napi_coerce_to_string(env, args[0], &mut value)); value } pub fn init(env: napi_env, exports: napi_value) { let properties = &[ - crate::new_property!(env, "test_coerce_bool\0", test_coerce_bool), - crate::new_property!(env, "test_coerce_number\0", test_coerce_number), - crate::new_property!(env, "test_coerce_object\0", test_coerce_object), - crate::new_property!(env, "test_coerce_string\0", test_coerce_string), + napi_new_property!(env, "test_coerce_bool", test_coerce_bool), + napi_new_property!(env, "test_coerce_number", test_coerce_number), + napi_new_property!(env, "test_coerce_object", test_coerce_object), + napi_new_property!(env, "test_coerce_string", test_coerce_string), ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs index 3ab1f3c846..3a28e4471a 100644 --- a/test_napi/src/lib.rs +++ b/test_napi/src/lib.rs @@ -2,7 +2,6 @@ #![allow(clippy::all)] #![allow(clippy::undocumented_unsafe_blocks)] -use napi_sys::Status::napi_ok; use std::ffi::c_void; use napi_sys::*; @@ -21,32 +20,35 @@ pub mod strings; pub mod typedarray; #[macro_export] -macro_rules! get_callback_info { +macro_rules! assert_napi_ok { + ($call: expr) => {{ + assert_eq!(unsafe { $call }, napi_sys::Status::napi_ok); + }}; +} + +#[macro_export] +macro_rules! napi_get_callback_info { ($env: expr, $callback_info: expr, $size: literal) => {{ let mut args = [std::ptr::null_mut(); $size]; let mut argc = $size; let mut this = std::ptr::null_mut(); - unsafe { - assert!( - napi_get_cb_info( - $env, - $callback_info, - &mut argc, - args.as_mut_ptr(), - &mut this, - std::ptr::null_mut(), - ) == napi_ok, - ) - }; + crate::assert_napi_ok!(napi_get_cb_info( + $env, + $callback_info, + &mut argc, + args.as_mut_ptr(), + &mut this, + std::ptr::null_mut(), + )); (args, argc, this) }}; } #[macro_export] -macro_rules! new_property { +macro_rules! napi_new_property { ($env: expr, $name: expr, $value: expr) => { napi_property_descriptor { - utf8name: $name.as_ptr() as *const std::os::raw::c_char, + utf8name: concat!($name, "\0").as_ptr() as *const std::os::raw::c_char, name: std::ptr::null_mut(), method: Some($value), getter: None, @@ -70,7 +72,7 @@ extern "C" fn install_cleanup_hook( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (_args, argc, _) = get_callback_info!(env, info, 1); + let (_args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 0); unsafe { @@ -88,15 +90,18 @@ extern "C" fn install_cleanup_hook( } pub fn init_cleanup_hook(env: napi_env, exports: napi_value) { - let properties = &[new_property!( + let properties = &[napi_new_property!( env, - "installCleanupHook\0", + "installCleanupHook", install_cleanup_hook )]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } #[no_mangle] diff --git a/test_napi/src/numbers.rs b/test_napi/src/numbers.rs index 8808659f97..829dd24c56 100644 --- a/test_napi/src/numbers.rs +++ b/test_napi/src/numbers.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::ValueType::napi_number; use napi_sys::*; use std::ptr; @@ -9,18 +11,18 @@ extern "C" fn test_int32( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_number); let mut int32 = -1; - assert!(unsafe { napi_get_value_int32(env, args[0], &mut int32) } == napi_ok); + assert_napi_ok!(napi_get_value_int32(env, args[0], &mut int32)); let mut value: napi_value = ptr::null_mut(); - assert!(unsafe { napi_create_int32(env, int32, &mut value) } == napi_ok); + assert_napi_ok!(napi_create_int32(env, int32, &mut value)); value } @@ -28,28 +30,31 @@ extern "C" fn test_int64( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_number); let mut int64 = -1; - assert!(unsafe { napi_get_value_int64(env, args[0], &mut int64) } == napi_ok); + assert_napi_ok!(napi_get_value_int64(env, args[0], &mut int64)); let mut value: napi_value = ptr::null_mut(); - assert!(unsafe { napi_create_int64(env, int64, &mut value) } == napi_ok); + assert_napi_ok!(napi_create_int64(env, int64, &mut value)); value } pub fn init(env: napi_env, exports: napi_value) { let properties = &[ - crate::new_property!(env, "test_int32\0", test_int32), - crate::new_property!(env, "test_int64\0", test_int64), + napi_new_property!(env, "test_int32", test_int32), + napi_new_property!(env, "test_int64", test_int64), ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/object_wrap.rs b/test_napi/src/object_wrap.rs index 5b15a16d7d..493cb05b39 100644 --- a/test_napi/src/object_wrap.rs +++ b/test_napi/src/object_wrap.rs @@ -1,7 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::PropertyAttributes; -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::ValueType::napi_number; use napi_sys::*; use std::os::raw::{c_char, c_void}; @@ -16,42 +17,34 @@ impl NapiObject { #[allow(clippy::new_ret_no_self)] pub extern "C" fn new(env: napi_env, info: napi_callback_info) -> napi_value { let mut new_target: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_get_new_target(env, info, &mut new_target) } == napi_ok - ); + assert_napi_ok!(napi_get_new_target(env, info, &mut new_target)); let is_constructor = !new_target.is_null(); - let (args, argc, this) = crate::get_callback_info!(env, info, 1); + let (args, argc, this) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); if is_constructor { let mut value = 0; let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_number); - assert!( - unsafe { napi_get_value_int32(env, args[0], &mut value) } == napi_ok - ); + assert_napi_ok!(napi_get_value_int32(env, args[0], &mut value)); let mut wrapper: napi_ref = ptr::null_mut(); let obj = Box::new(Self { counter: value, _wrapper: wrapper, }); - assert!( - unsafe { - napi_wrap( - env, - this, - Box::into_raw(obj) as *mut c_void, - None, - ptr::null_mut(), - &mut wrapper, - ) - } == napi_ok - ); + assert_napi_ok!(napi_wrap( + env, + this, + Box::into_raw(obj) as *mut c_void, + None, + ptr::null_mut(), + &mut wrapper, + )); return this; } @@ -63,18 +56,16 @@ impl NapiObject { env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, this) = crate::get_callback_info!(env, info, 1); + let (args, argc, this) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut obj: *mut Self = ptr::null_mut(); - assert!( - unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } - == napi_ok - ); + assert_napi_ok!(napi_unwrap( + env, + this, + &mut obj as *mut _ as *mut *mut c_void + )); - assert!( - unsafe { napi_get_value_int32(env, args[0], &mut (*obj).counter) } - == napi_ok - ); + assert_napi_ok!(napi_get_value_int32(env, args[0], &mut (*obj).counter)); ptr::null_mut() } @@ -83,18 +74,17 @@ impl NapiObject { env: napi_env, info: napi_callback_info, ) -> napi_value { - let (_args, argc, this) = crate::get_callback_info!(env, info, 0); + let (_args, argc, this) = napi_get_callback_info!(env, info, 0); assert_eq!(argc, 0); let mut obj: *mut Self = ptr::null_mut(); - assert!( - unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } - == napi_ok - ); + assert_napi_ok!(napi_unwrap( + env, + this, + &mut obj as *mut _ as *mut *mut c_void + )); let mut num: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_create_int32(env, (*obj).counter, &mut num) } == napi_ok - ); + assert_napi_ok!(napi_create_int32(env, (*obj).counter, &mut num)); num } @@ -103,13 +93,14 @@ impl NapiObject { env: napi_env, info: napi_callback_info, ) -> napi_value { - let (_args, argc, this) = crate::get_callback_info!(env, info, 0); + let (_args, argc, this) = napi_get_callback_info!(env, info, 0); assert_eq!(argc, 0); let mut obj: *mut Self = ptr::null_mut(); - assert!( - unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } - == napi_ok - ); + assert_napi_ok!(napi_unwrap( + env, + this, + &mut obj as *mut _ as *mut *mut c_void + )); unsafe { (*obj).counter += 1; @@ -122,52 +113,43 @@ impl NapiObject { env: napi_env, info: napi_callback_info, ) -> napi_value { - let (_args, argc, _this) = crate::get_callback_info!(env, info, 0); + let (_args, argc, _this) = napi_get_callback_info!(env, info, 0); assert_eq!(argc, 0); let int64 = 64; let mut value: napi_value = ptr::null_mut(); - assert!(unsafe { napi_create_int64(env, int64, &mut value) } == napi_ok); + assert_napi_ok!(napi_create_int64(env, int64, &mut value)); value } } pub fn init(env: napi_env, exports: napi_value) { - let mut static_prop = - crate::new_property!(env, "factory\0", NapiObject::factory); + let mut static_prop = napi_new_property!(env, "factory", NapiObject::factory); static_prop.attributes = PropertyAttributes::static_; let properties = &[ - crate::new_property!(env, "set_value\0", NapiObject::set_value), - crate::new_property!(env, "get_value\0", NapiObject::get_value), - crate::new_property!(env, "increment\0", NapiObject::increment), + napi_new_property!(env, "set_value", NapiObject::set_value), + napi_new_property!(env, "get_value", NapiObject::get_value), + napi_new_property!(env, "increment", NapiObject::increment), static_prop, ]; let mut cons: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_define_class( - env, - "NapiObject\0".as_ptr() as *mut c_char, - usize::MAX, - Some(NapiObject::new), - ptr::null_mut(), - properties.len(), - properties.as_ptr(), - &mut cons, - ) - } == napi_ok - ); + assert_napi_ok!(napi_define_class( + env, + "NapiObject\0".as_ptr() as *mut c_char, + usize::MAX, + Some(NapiObject::new), + ptr::null_mut(), + properties.len(), + properties.as_ptr(), + &mut cons, + )); - assert!( - unsafe { - napi_set_named_property( - env, - exports, - "NapiObject\0".as_ptr() as *const c_char, - cons, - ) - } == napi_ok - ); + assert_napi_ok!(napi_set_named_property( + env, + exports, + "NapiObject\0".as_ptr() as *const c_char, + cons, + )); } diff --git a/test_napi/src/primitives.rs b/test_napi/src/primitives.rs index c5fbbba041..2d951ebfa3 100644 --- a/test_napi/src/primitives.rs +++ b/test_napi/src/primitives.rs @@ -1,3 +1,5 @@ +use crate::assert_napi_ok; +use crate::napi_new_property; use napi_sys::*; use std::ptr; @@ -6,18 +8,21 @@ extern "C" fn test_get_undefined( _: napi_callback_info, ) -> napi_value { let mut result = ptr::null_mut(); - unsafe { napi_get_undefined(env, &mut result) }; + assert_napi_ok!(napi_get_undefined(env, &mut result)); result } pub fn init(env: napi_env, exports: napi_value) { - let properties = &[crate::new_property!( + let properties = &[napi_new_property!( env, - "test_get_undefined\0", + "test_get_undefined", test_get_undefined )]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/promise.rs b/test_napi/src/promise.rs index 02ea2f2375..d6da684ba9 100644 --- a/test_napi/src/promise.rs +++ b/test_napi/src/promise.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::*; use std::ptr; @@ -11,10 +13,7 @@ extern "C" fn test_promise_new( _info: napi_callback_info, ) -> napi_value { let mut value: napi_value = ptr::null_mut(); - assert!( - unsafe { napi_create_promise(env, &mut CURRENT_DEFERRED, &mut value) } - == napi_ok - ); + assert_napi_ok!(napi_create_promise(env, &mut CURRENT_DEFERRED, &mut value)); value } @@ -22,12 +21,10 @@ extern "C" fn test_promise_resolve( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); - assert!( - unsafe { napi_resolve_deferred(env, CURRENT_DEFERRED, args[0]) } == napi_ok - ); + assert_napi_ok!(napi_resolve_deferred(env, CURRENT_DEFERRED, args[0])); unsafe { CURRENT_DEFERRED = ptr::null_mut() }; ptr::null_mut() } @@ -36,12 +33,10 @@ extern "C" fn test_promise_reject( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); - assert!( - unsafe { napi_reject_deferred(env, CURRENT_DEFERRED, args[0]) } == napi_ok - ); + assert_napi_ok!(napi_reject_deferred(env, CURRENT_DEFERRED, args[0])); unsafe { CURRENT_DEFERRED = ptr::null_mut() }; ptr::null_mut() } @@ -50,27 +45,30 @@ extern "C" fn test_promise_is( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut is_promise: bool = false; - assert!(unsafe { napi_is_promise(env, args[0], &mut is_promise) } == napi_ok); + assert_napi_ok!(napi_is_promise(env, args[0], &mut is_promise)); let mut result: napi_value = ptr::null_mut(); - assert!(unsafe { napi_get_boolean(env, is_promise, &mut result) } == napi_ok); + assert_napi_ok!(napi_get_boolean(env, is_promise, &mut result)); result } pub fn init(env: napi_env, exports: napi_value) { let properties = &[ - crate::new_property!(env, "test_promise_new\0", test_promise_new), - crate::new_property!(env, "test_promise_resolve\0", test_promise_resolve), - crate::new_property!(env, "test_promise_reject\0", test_promise_reject), - crate::new_property!(env, "test_promise_is\0", test_promise_is), + napi_new_property!(env, "test_promise_new", test_promise_new), + napi_new_property!(env, "test_promise_resolve", test_promise_resolve), + napi_new_property!(env, "test_promise_reject", test_promise_reject), + napi_new_property!(env, "test_promise_is", test_promise_is), ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/properties.rs b/test_napi/src/properties.rs index dafa9ef961..9d90d0b7f2 100644 --- a/test_napi/src/properties.rs +++ b/test_napi/src/properties.rs @@ -1,49 +1,42 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::assert_napi_ok; use napi_sys::PropertyAttributes::*; -use napi_sys::Status::napi_ok; use napi_sys::*; use std::os::raw::c_char; use std::ptr; pub fn init(env: napi_env, exports: napi_value) { let mut number: napi_value = ptr::null_mut(); - assert!(unsafe { napi_create_double(env, 1.0, &mut number) } == napi_ok); + assert_napi_ok!(napi_create_double(env, 1.0, &mut number)); // Key name as napi_value representing `v8::String` let mut name_value: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_create_string_utf8( - env, - "key_v8_string".as_ptr() as *const c_char, - usize::MAX, - &mut name_value, - ) - } == napi_ok - ); + assert_napi_ok!(napi_create_string_utf8( + env, + "key_v8_string".as_ptr() as *const c_char, + usize::MAX, + &mut name_value, + )); // Key symbol let mut symbol_description: napi_value = ptr::null_mut(); let mut name_symbol: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_create_string_utf8( - env, - "key_v8_symbol".as_ptr() as *const c_char, - usize::MAX, - &mut symbol_description, - ) - } == napi_ok - ); - assert!( - unsafe { napi_create_symbol(env, symbol_description, &mut name_symbol) } - == napi_ok - ); + assert_napi_ok!(napi_create_string_utf8( + env, + "key_v8_symbol".as_ptr() as *const c_char, + usize::MAX, + &mut symbol_description, + )); + assert_napi_ok!(napi_create_symbol( + env, + symbol_description, + &mut name_symbol + )); let properties = &[ napi_property_descriptor { - utf8name: "test_property_rw\0".as_ptr() as *const c_char, + utf8name: "test_property_rw".as_ptr() as *const c_char, name: ptr::null_mut(), method: None, getter: None, @@ -53,7 +46,7 @@ pub fn init(env: napi_env, exports: napi_value) { value: number, }, napi_property_descriptor { - utf8name: "test_property_r\0".as_ptr() as *const c_char, + utf8name: "test_property_r".as_ptr() as *const c_char, name: ptr::null_mut(), method: None, getter: None, @@ -84,7 +77,10 @@ pub fn init(env: napi_env, exports: napi_value) { }, ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/strings.rs b/test_napi/src/strings.rs index af6f841890..70faa640d2 100644 --- a/test_napi/src/strings.rs +++ b/test_napi/src/strings.rs @@ -1,15 +1,17 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use napi_sys::Status::napi_ok; +use crate::assert_napi_ok; +use crate::napi_get_callback_info; +use crate::napi_new_property; use napi_sys::ValueType::napi_string; use napi_sys::*; extern "C" fn test_utf8(env: napi_env, info: napi_callback_info) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_string); args[0] @@ -19,11 +21,11 @@ extern "C" fn test_utf16( env: napi_env, info: napi_callback_info, ) -> napi_value { - let (args, argc, _) = crate::get_callback_info!(env, info, 1); + let (args, argc, _) = napi_get_callback_info!(env, info, 1); assert_eq!(argc, 1); let mut ty = -1; - assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok); + assert_napi_ok!(napi_typeof(env, args[0], &mut ty)); assert_eq!(ty, napi_string); args[0] @@ -32,13 +34,16 @@ extern "C" fn test_utf16( pub fn init(env: napi_env, exports: napi_value) { let properties = &[ // utf8 - crate::new_property!(env, "test_utf8\0", test_utf8), + napi_new_property!(env, "test_utf8", test_utf8), // utf16 - crate::new_property!(env, "test_utf16\0", test_utf16), + napi_new_property!(env, "test_utf16", test_utf16), // latin1 ]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); } diff --git a/test_napi/src/typedarray.rs b/test_napi/src/typedarray.rs index f8b5fd6746..034757dd01 100644 --- a/test_napi/src/typedarray.rs +++ b/test_napi/src/typedarray.rs @@ -1,7 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::assert_napi_ok; +use crate::napi_new_property; use core::ffi::c_void; -use napi_sys::Status::napi_ok; use napi_sys::TypedarrayType::uint8_array; use napi_sys::*; use std::ptr; @@ -12,42 +13,36 @@ extern "C" fn test_external( ) -> napi_value { let mut arraybuffer: napi_value = ptr::null_mut(); let mut external: Box<[u8; 4]> = Box::new([0, 1, 2, 3]); - assert!( - unsafe { - napi_create_external_arraybuffer( - env, - external.as_mut_ptr() as *mut c_void, - external.len(), - None, - ptr::null_mut(), - &mut arraybuffer, - ) - } == napi_ok - ); + assert_napi_ok!(napi_create_external_arraybuffer( + env, + external.as_mut_ptr() as *mut c_void, + external.len(), + None, + ptr::null_mut(), + &mut arraybuffer, + )); let mut typedarray: napi_value = ptr::null_mut(); - assert!( - unsafe { - napi_create_typedarray( - env, - uint8_array, - external.len(), - arraybuffer, - 0, - &mut typedarray, - ) - } == napi_ok - ); + assert_napi_ok!(napi_create_typedarray( + env, + uint8_array, + external.len(), + arraybuffer, + 0, + &mut typedarray, + )); std::mem::forget(external); // Leak into JS land typedarray } pub fn init(env: napi_env, exports: napi_value) { - let properties = - &[crate::new_property!(env, "test_external\0", test_external)]; + let properties = &[napi_new_property!(env, "test_external", test_external)]; - unsafe { - napi_define_properties(env, exports, properties.len(), properties.as_ptr()) - }; + assert_napi_ok!(napi_define_properties( + env, + exports, + properties.len(), + properties.as_ptr() + )); }