1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00

refactor(napi): Cleanup tests (#17347)

This commit is contained in:
Bartek Iwańczuk 2023-01-11 15:47:26 +01:00 committed by David Sherret
parent e49f0bee57
commit d5a758c0e1
13 changed files with 340 additions and 362 deletions

View file

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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_number;
use napi_sys::ValueType::napi_object; use napi_sys::ValueType::napi_object;
use napi_sys::*; use napi_sys::*;
@ -10,25 +12,23 @@ extern "C" fn test_array_new(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_object);
let mut value: napi_value = ptr::null_mut(); 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; let mut length: u32 = 0;
assert!( assert_napi_ok!(napi_get_array_length(env, args[0], &mut length));
unsafe { napi_get_array_length(env, args[0], &mut length) } == napi_ok
);
for i in 0..length { for i in 0..length {
let mut e: napi_value = ptr::null_mut(); let mut e: napi_value = ptr::null_mut();
assert!(unsafe { napi_get_element(env, args[0], i, &mut e) } == napi_ok); assert_napi_ok!(napi_get_element(env, args[0], i, &mut e));
assert!(unsafe { napi_set_element(env, value, i, e) } == napi_ok); assert_napi_ok!(napi_set_element(env, value, i, e));
} }
value value
@ -38,36 +38,36 @@ extern "C" fn test_array_new_with_length(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_number);
let mut len: u32 = 0; 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(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_array_with_length(env, len as usize, &mut value));
unsafe { napi_create_array_with_length(env, len as usize, &mut value) }
== napi_ok
);
value value
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
crate::new_property!(env, "test_array_new\0", test_array_new), napi_new_property!(env, "test_array_new", test_array_new),
crate::new_property!( napi_new_property!(
env, env,
"test_array_new_with_length\0", "test_array_new_with_length",
test_array_new_with_length test_array_new_with_length
), ),
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,35 +1,33 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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 napi_sys::*;
extern "C" fn test_detached( extern "C" fn test_detached(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut value = false; let mut value = false;
assert!( assert_napi_ok!(napi_is_detached_arraybuffer(env, args[0], &mut value));
unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) }
== napi_ok
);
assert!(!value); assert!(!value);
assert!(unsafe { napi_detach_arraybuffer(env, args[0]) } == napi_ok); assert_napi_ok!(napi_detach_arraybuffer(env, args[0]));
assert!( assert_napi_ok!(napi_is_detached_arraybuffer(env, args[0], &mut value));
unsafe { napi_is_detached_arraybuffer(env, args[0], &mut value) }
== napi_ok
);
assert!(value); assert!(value);
args[0] args[0]
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = let properties = &[napi_new_property!(env, "test_detached", test_detached)];
&[crate::new_property!(env, "test_detached\0", test_detached)];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,5 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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::Status::napi_ok;
use napi_sys::ValueType::napi_function; use napi_sys::ValueType::napi_function;
use napi_sys::*; use napi_sys::*;
@ -31,59 +34,55 @@ unsafe extern "C" fn complete(
assert!(!baton.func.is_null()); assert!(!baton.func.is_null());
let mut global: napi_value = ptr::null_mut(); 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(); 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(); let mut _result: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_call_function(
napi_call_function(env, global, callback, 0, ptr::null(), &mut _result) env,
== napi_ok global,
); callback,
0,
ptr::null(),
&mut _result
));
assert!(napi_delete_reference(env, baton.func) == napi_ok); assert_napi_ok!(napi_delete_reference(env, baton.func));
assert!(napi_delete_async_work(env, baton.task) == napi_ok); assert_napi_ok!(napi_delete_async_work(env, baton.task));
} }
extern "C" fn test_async_work( extern "C" fn test_async_work(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_function);
let mut resource_name: napi_value = ptr::null_mut(); let mut resource_name: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_string_utf8(
unsafe {
napi_create_string_utf8(
env, env,
"test_async_resource\0".as_ptr() as *const c_char, "test_async_resource".as_ptr() as *const c_char,
usize::MAX, usize::MAX,
&mut resource_name, &mut resource_name,
) ));
} == napi_ok
);
let mut async_work: napi_async_work = ptr::null_mut(); let mut async_work: napi_async_work = ptr::null_mut();
let mut func: napi_ref = ptr::null_mut(); let mut func: napi_ref = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_reference(env, args[0], 1, &mut func));
unsafe { napi_create_reference(env, args[0], 1, &mut func) } == napi_ok
);
let baton = Box::new(Baton { let baton = Box::new(Baton {
called: false, called: false,
func, func,
task: async_work, task: async_work,
}); });
assert!( assert_napi_ok!(napi_create_async_work(
unsafe {
napi_create_async_work(
env, env,
ptr::null_mut(), ptr::null_mut(),
resource_name, resource_name,
@ -91,22 +90,20 @@ extern "C" fn test_async_work(
Some(complete), Some(complete),
Box::into_raw(baton) as *mut c_void, Box::into_raw(baton) as *mut c_void,
&mut async_work, &mut async_work,
) ));
} == napi_ok assert_napi_ok!(napi_queue_async_work(env, async_work));
);
assert!(unsafe { napi_queue_async_work(env, async_work) } == napi_ok);
ptr::null_mut() ptr::null_mut()
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[crate::new_property!( let properties =
env, &[napi_new_property!(env, "test_async_work", test_async_work)];
"test_async_work\0",
test_async_work
)];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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_function;
use napi_sys::ValueType::napi_object; use napi_sys::ValueType::napi_object;
use napi_sys::*; use napi_sys::*;
@ -11,44 +13,38 @@ extern "C" fn test_callback_run(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 2);
let mut ty = -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); assert_eq!(ty, napi_function);
let mut ty = -1; 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); assert_eq!(ty, napi_object);
let mut len = 0; 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); let mut argv = Vec::with_capacity(len as usize);
for index in 0..len { for index in 0..len {
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_get_element(env, args[1], index, &mut value));
unsafe { napi_get_element(env, args[1], index, &mut value) } == napi_ok
);
argv.push(value); argv.push(value);
} }
let mut global: napi_value = ptr::null_mut(); 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(); let mut result: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_call_function(
unsafe {
napi_call_function(
env, env,
global, global,
args[0], args[0],
argv.len(), argv.len(),
argv.as_mut_ptr(), argv.as_mut_ptr(),
&mut result, &mut result,
) ));
} == napi_ok
);
result result
} }
@ -57,57 +53,54 @@ extern "C" fn test_callback_run_with_recv(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 3);
let mut ty = -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); assert_eq!(ty, napi_function);
let mut ty = -1; 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); assert_eq!(ty, napi_object);
let mut len = 0; 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); let mut argv = Vec::with_capacity(len as usize);
for index in 0..len { for index in 0..len {
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_get_element(env, args[1], index, &mut value));
unsafe { napi_get_element(env, args[1], index, &mut value) } == napi_ok
);
argv.push(value); argv.push(value);
} }
let mut result: napi_value = ptr::null_mut(); let mut result: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_call_function(
unsafe {
napi_call_function(
env, env,
args[2], // recv args[2], // recv
args[0], // cb args[0], // cb
argv.len(), argv.len(),
argv.as_mut_ptr(), argv.as_mut_ptr(),
&mut result, &mut result,
) ));
} == napi_ok
);
result result
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
crate::new_property!(env, "test_callback_run\0", test_callback_run), napi_new_property!(env, "test_callback_run", test_callback_run),
crate::new_property!( napi_new_property!(
env, env,
"test_callback_run_with_recv\0", "test_callback_run_with_recv",
test_callback_run_with_recv test_callback_run_with_recv
), ),
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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 napi_sys::*;
use std::ptr; use std::ptr;
@ -8,11 +10,11 @@ extern "C" fn test_coerce_bool(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut value: napi_value = ptr::null_mut(); 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 value
} }
@ -20,13 +22,11 @@ extern "C" fn test_coerce_number(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_coerce_to_number(env, args[0], &mut value));
unsafe { napi_coerce_to_number(env, args[0], &mut value) } == napi_ok
);
value value
} }
@ -34,13 +34,11 @@ extern "C" fn test_coerce_object(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_coerce_to_object(env, args[0], &mut value));
unsafe { napi_coerce_to_object(env, args[0], &mut value) } == napi_ok
);
value value
} }
@ -48,24 +46,25 @@ extern "C" fn test_coerce_string(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_coerce_to_string(env, args[0], &mut value));
unsafe { napi_coerce_to_string(env, args[0], &mut value) } == napi_ok
);
value value
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
crate::new_property!(env, "test_coerce_bool\0", test_coerce_bool), napi_new_property!(env, "test_coerce_bool", test_coerce_bool),
crate::new_property!(env, "test_coerce_number\0", test_coerce_number), napi_new_property!(env, "test_coerce_number", test_coerce_number),
crate::new_property!(env, "test_coerce_object\0", test_coerce_object), napi_new_property!(env, "test_coerce_object", test_coerce_object),
crate::new_property!(env, "test_coerce_string\0", test_coerce_string), napi_new_property!(env, "test_coerce_string", test_coerce_string),
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -2,7 +2,6 @@
#![allow(clippy::all)] #![allow(clippy::all)]
#![allow(clippy::undocumented_unsafe_blocks)] #![allow(clippy::undocumented_unsafe_blocks)]
use napi_sys::Status::napi_ok;
use std::ffi::c_void; use std::ffi::c_void;
use napi_sys::*; use napi_sys::*;
@ -21,32 +20,35 @@ pub mod strings;
pub mod typedarray; pub mod typedarray;
#[macro_export] #[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) => {{ ($env: expr, $callback_info: expr, $size: literal) => {{
let mut args = [std::ptr::null_mut(); $size]; let mut args = [std::ptr::null_mut(); $size];
let mut argc = $size; let mut argc = $size;
let mut this = std::ptr::null_mut(); let mut this = std::ptr::null_mut();
unsafe { crate::assert_napi_ok!(napi_get_cb_info(
assert!(
napi_get_cb_info(
$env, $env,
$callback_info, $callback_info,
&mut argc, &mut argc,
args.as_mut_ptr(), args.as_mut_ptr(),
&mut this, &mut this,
std::ptr::null_mut(), std::ptr::null_mut(),
) == napi_ok, ));
)
};
(args, argc, this) (args, argc, this)
}}; }};
} }
#[macro_export] #[macro_export]
macro_rules! new_property { macro_rules! napi_new_property {
($env: expr, $name: expr, $value: expr) => { ($env: expr, $name: expr, $value: expr) => {
napi_property_descriptor { 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(), name: std::ptr::null_mut(),
method: Some($value), method: Some($value),
getter: None, getter: None,
@ -70,7 +72,7 @@ extern "C" fn install_cleanup_hook(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 0);
unsafe { unsafe {
@ -88,15 +90,18 @@ extern "C" fn install_cleanup_hook(
} }
pub fn init_cleanup_hook(env: napi_env, exports: napi_value) { pub fn init_cleanup_hook(env: napi_env, exports: napi_value) {
let properties = &[new_property!( let properties = &[napi_new_property!(
env, env,
"installCleanupHook\0", "installCleanupHook",
install_cleanup_hook install_cleanup_hook
)]; )];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }
#[no_mangle] #[no_mangle]

View file

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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_number;
use napi_sys::*; use napi_sys::*;
use std::ptr; use std::ptr;
@ -9,18 +11,18 @@ extern "C" fn test_int32(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_number);
let mut int32 = -1; 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(); 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 value
} }
@ -28,28 +30,31 @@ extern "C" fn test_int64(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_number);
let mut int64 = -1; 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(); 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 value
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
crate::new_property!(env, "test_int32\0", test_int32), napi_new_property!(env, "test_int32", test_int32),
crate::new_property!(env, "test_int64\0", test_int64), napi_new_property!(env, "test_int64", test_int64),
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,7 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use napi_sys::PropertyAttributes; use crate::assert_napi_ok;
use napi_sys::Status::napi_ok; use crate::napi_get_callback_info;
use crate::napi_new_property;
use napi_sys::ValueType::napi_number; use napi_sys::ValueType::napi_number;
use napi_sys::*; use napi_sys::*;
use std::os::raw::{c_char, c_void}; use std::os::raw::{c_char, c_void};
@ -16,42 +17,34 @@ impl NapiObject {
#[allow(clippy::new_ret_no_self)] #[allow(clippy::new_ret_no_self)]
pub extern "C" fn new(env: napi_env, info: napi_callback_info) -> napi_value { pub extern "C" fn new(env: napi_env, info: napi_callback_info) -> napi_value {
let mut new_target: napi_value = ptr::null_mut(); let mut new_target: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_get_new_target(env, info, &mut new_target));
unsafe { napi_get_new_target(env, info, &mut new_target) } == napi_ok
);
let is_constructor = !new_target.is_null(); 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); assert_eq!(argc, 1);
if is_constructor { if is_constructor {
let mut value = 0; let mut value = 0;
let mut ty = -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); assert_eq!(ty, napi_number);
assert!( assert_napi_ok!(napi_get_value_int32(env, args[0], &mut value));
unsafe { napi_get_value_int32(env, args[0], &mut value) } == napi_ok
);
let mut wrapper: napi_ref = ptr::null_mut(); let mut wrapper: napi_ref = ptr::null_mut();
let obj = Box::new(Self { let obj = Box::new(Self {
counter: value, counter: value,
_wrapper: wrapper, _wrapper: wrapper,
}); });
assert!( assert_napi_ok!(napi_wrap(
unsafe {
napi_wrap(
env, env,
this, this,
Box::into_raw(obj) as *mut c_void, Box::into_raw(obj) as *mut c_void,
None, None,
ptr::null_mut(), ptr::null_mut(),
&mut wrapper, &mut wrapper,
) ));
} == napi_ok
);
return this; return this;
} }
@ -63,18 +56,16 @@ impl NapiObject {
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 1);
let mut obj: *mut Self = ptr::null_mut(); let mut obj: *mut Self = ptr::null_mut();
assert!( assert_napi_ok!(napi_unwrap(
unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } env,
== napi_ok this,
); &mut obj as *mut _ as *mut *mut c_void
));
assert!( assert_napi_ok!(napi_get_value_int32(env, args[0], &mut (*obj).counter));
unsafe { napi_get_value_int32(env, args[0], &mut (*obj).counter) }
== napi_ok
);
ptr::null_mut() ptr::null_mut()
} }
@ -83,18 +74,17 @@ impl NapiObject {
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 0);
let mut obj: *mut Self = ptr::null_mut(); let mut obj: *mut Self = ptr::null_mut();
assert!( assert_napi_ok!(napi_unwrap(
unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } env,
== napi_ok this,
); &mut obj as *mut _ as *mut *mut c_void
));
let mut num: napi_value = ptr::null_mut(); let mut num: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_int32(env, (*obj).counter, &mut num));
unsafe { napi_create_int32(env, (*obj).counter, &mut num) } == napi_ok
);
num num
} }
@ -103,13 +93,14 @@ impl NapiObject {
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 0);
let mut obj: *mut Self = ptr::null_mut(); let mut obj: *mut Self = ptr::null_mut();
assert!( assert_napi_ok!(napi_unwrap(
unsafe { napi_unwrap(env, this, &mut obj as *mut _ as *mut *mut c_void) } env,
== napi_ok this,
); &mut obj as *mut _ as *mut *mut c_void
));
unsafe { unsafe {
(*obj).counter += 1; (*obj).counter += 1;
@ -122,32 +113,29 @@ impl NapiObject {
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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); assert_eq!(argc, 0);
let int64 = 64; let int64 = 64;
let mut value: napi_value = ptr::null_mut(); 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 value
} }
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let mut static_prop = let mut static_prop = napi_new_property!(env, "factory", NapiObject::factory);
crate::new_property!(env, "factory\0", NapiObject::factory);
static_prop.attributes = PropertyAttributes::static_; static_prop.attributes = PropertyAttributes::static_;
let properties = &[ let properties = &[
crate::new_property!(env, "set_value\0", NapiObject::set_value), napi_new_property!(env, "set_value", NapiObject::set_value),
crate::new_property!(env, "get_value\0", NapiObject::get_value), napi_new_property!(env, "get_value", NapiObject::get_value),
crate::new_property!(env, "increment\0", NapiObject::increment), napi_new_property!(env, "increment", NapiObject::increment),
static_prop, static_prop,
]; ];
let mut cons: napi_value = ptr::null_mut(); let mut cons: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_define_class(
unsafe {
napi_define_class(
env, env,
"NapiObject\0".as_ptr() as *mut c_char, "NapiObject\0".as_ptr() as *mut c_char,
usize::MAX, usize::MAX,
@ -156,18 +144,12 @@ pub fn init(env: napi_env, exports: napi_value) {
properties.len(), properties.len(),
properties.as_ptr(), properties.as_ptr(),
&mut cons, &mut cons,
) ));
} == napi_ok
);
assert!( assert_napi_ok!(napi_set_named_property(
unsafe {
napi_set_named_property(
env, env,
exports, exports,
"NapiObject\0".as_ptr() as *const c_char, "NapiObject\0".as_ptr() as *const c_char,
cons, cons,
) ));
} == napi_ok
);
} }

View file

@ -1,3 +1,5 @@
use crate::assert_napi_ok;
use crate::napi_new_property;
use napi_sys::*; use napi_sys::*;
use std::ptr; use std::ptr;
@ -6,18 +8,21 @@ extern "C" fn test_get_undefined(
_: napi_callback_info, _: napi_callback_info,
) -> napi_value { ) -> napi_value {
let mut result = ptr::null_mut(); let mut result = ptr::null_mut();
unsafe { napi_get_undefined(env, &mut result) }; assert_napi_ok!(napi_get_undefined(env, &mut result));
result result
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[crate::new_property!( let properties = &[napi_new_property!(
env, env,
"test_get_undefined\0", "test_get_undefined",
test_get_undefined test_get_undefined
)]; )];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,6 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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 napi_sys::*;
use std::ptr; use std::ptr;
@ -11,10 +13,7 @@ extern "C" fn test_promise_new(
_info: napi_callback_info, _info: napi_callback_info,
) -> napi_value { ) -> napi_value {
let mut value: napi_value = ptr::null_mut(); let mut value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_promise(env, &mut CURRENT_DEFERRED, &mut value));
unsafe { napi_create_promise(env, &mut CURRENT_DEFERRED, &mut value) }
== napi_ok
);
value value
} }
@ -22,12 +21,10 @@ extern "C" fn test_promise_resolve(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
assert!( assert_napi_ok!(napi_resolve_deferred(env, CURRENT_DEFERRED, args[0]));
unsafe { napi_resolve_deferred(env, CURRENT_DEFERRED, args[0]) } == napi_ok
);
unsafe { CURRENT_DEFERRED = ptr::null_mut() }; unsafe { CURRENT_DEFERRED = ptr::null_mut() };
ptr::null_mut() ptr::null_mut()
} }
@ -36,12 +33,10 @@ extern "C" fn test_promise_reject(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
assert!( assert_napi_ok!(napi_reject_deferred(env, CURRENT_DEFERRED, args[0]));
unsafe { napi_reject_deferred(env, CURRENT_DEFERRED, args[0]) } == napi_ok
);
unsafe { CURRENT_DEFERRED = ptr::null_mut() }; unsafe { CURRENT_DEFERRED = ptr::null_mut() };
ptr::null_mut() ptr::null_mut()
} }
@ -50,27 +45,30 @@ extern "C" fn test_promise_is(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut is_promise: bool = false; 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(); 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 result
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
crate::new_property!(env, "test_promise_new\0", test_promise_new), napi_new_property!(env, "test_promise_new", test_promise_new),
crate::new_property!(env, "test_promise_resolve\0", test_promise_resolve), napi_new_property!(env, "test_promise_resolve", test_promise_resolve),
crate::new_property!(env, "test_promise_reject\0", test_promise_reject), napi_new_property!(env, "test_promise_reject", test_promise_reject),
crate::new_property!(env, "test_promise_is\0", test_promise_is), napi_new_property!(env, "test_promise_is", test_promise_is),
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,49 +1,42 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::assert_napi_ok;
use napi_sys::PropertyAttributes::*; use napi_sys::PropertyAttributes::*;
use napi_sys::Status::napi_ok;
use napi_sys::*; use napi_sys::*;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::ptr; use std::ptr;
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let mut number: napi_value = ptr::null_mut(); 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` // Key name as napi_value representing `v8::String`
let mut name_value: napi_value = ptr::null_mut(); let mut name_value: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_string_utf8(
unsafe {
napi_create_string_utf8(
env, env,
"key_v8_string".as_ptr() as *const c_char, "key_v8_string".as_ptr() as *const c_char,
usize::MAX, usize::MAX,
&mut name_value, &mut name_value,
) ));
} == napi_ok
);
// Key symbol // Key symbol
let mut symbol_description: napi_value = ptr::null_mut(); let mut symbol_description: napi_value = ptr::null_mut();
let mut name_symbol: napi_value = ptr::null_mut(); let mut name_symbol: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_string_utf8(
unsafe {
napi_create_string_utf8(
env, env,
"key_v8_symbol".as_ptr() as *const c_char, "key_v8_symbol".as_ptr() as *const c_char,
usize::MAX, usize::MAX,
&mut symbol_description, &mut symbol_description,
) ));
} == napi_ok assert_napi_ok!(napi_create_symbol(
); env,
assert!( symbol_description,
unsafe { napi_create_symbol(env, symbol_description, &mut name_symbol) } &mut name_symbol
== napi_ok ));
);
let properties = &[ let properties = &[
napi_property_descriptor { 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(), name: ptr::null_mut(),
method: None, method: None,
getter: None, getter: None,
@ -53,7 +46,7 @@ pub fn init(env: napi_env, exports: napi_value) {
value: number, value: number,
}, },
napi_property_descriptor { 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(), name: ptr::null_mut(),
method: None, method: None,
getter: None, getter: None,
@ -84,7 +77,10 @@ pub fn init(env: napi_env, exports: napi_value) {
}, },
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,15 +1,17 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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::ValueType::napi_string;
use napi_sys::*; use napi_sys::*;
extern "C" fn test_utf8(env: napi_env, info: napi_callback_info) -> napi_value { 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); assert_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_string);
args[0] args[0]
@ -19,11 +21,11 @@ extern "C" fn test_utf16(
env: napi_env, env: napi_env,
info: napi_callback_info, info: napi_callback_info,
) -> napi_value { ) -> 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_eq!(argc, 1);
let mut ty = -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); assert_eq!(ty, napi_string);
args[0] args[0]
@ -32,13 +34,16 @@ extern "C" fn test_utf16(
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = &[ let properties = &[
// utf8 // utf8
crate::new_property!(env, "test_utf8\0", test_utf8), napi_new_property!(env, "test_utf8", test_utf8),
// utf16 // utf16
crate::new_property!(env, "test_utf16\0", test_utf16), napi_new_property!(env, "test_utf16", test_utf16),
// latin1 // latin1
]; ];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }

View file

@ -1,7 +1,8 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // 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 core::ffi::c_void;
use napi_sys::Status::napi_ok;
use napi_sys::TypedarrayType::uint8_array; use napi_sys::TypedarrayType::uint8_array;
use napi_sys::*; use napi_sys::*;
use std::ptr; use std::ptr;
@ -12,42 +13,36 @@ extern "C" fn test_external(
) -> napi_value { ) -> napi_value {
let mut arraybuffer: napi_value = ptr::null_mut(); let mut arraybuffer: napi_value = ptr::null_mut();
let mut external: Box<[u8; 4]> = Box::new([0, 1, 2, 3]); let mut external: Box<[u8; 4]> = Box::new([0, 1, 2, 3]);
assert!( assert_napi_ok!(napi_create_external_arraybuffer(
unsafe {
napi_create_external_arraybuffer(
env, env,
external.as_mut_ptr() as *mut c_void, external.as_mut_ptr() as *mut c_void,
external.len(), external.len(),
None, None,
ptr::null_mut(), ptr::null_mut(),
&mut arraybuffer, &mut arraybuffer,
) ));
} == napi_ok
);
let mut typedarray: napi_value = ptr::null_mut(); let mut typedarray: napi_value = ptr::null_mut();
assert!( assert_napi_ok!(napi_create_typedarray(
unsafe {
napi_create_typedarray(
env, env,
uint8_array, uint8_array,
external.len(), external.len(),
arraybuffer, arraybuffer,
0, 0,
&mut typedarray, &mut typedarray,
) ));
} == napi_ok
);
std::mem::forget(external); // Leak into JS land std::mem::forget(external); // Leak into JS land
typedarray typedarray
} }
pub fn init(env: napi_env, exports: napi_value) { pub fn init(env: napi_env, exports: napi_value) {
let properties = let properties = &[napi_new_property!(env, "test_external", test_external)];
&[crate::new_property!(env, "test_external\0", test_external)];
unsafe { assert_napi_ok!(napi_define_properties(
napi_define_properties(env, exports, properties.len(), properties.as_ptr()) env,
}; exports,
properties.len(),
properties.as_ptr()
));
} }