1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -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 GitHub
parent e26fc195ef
commit d0a53c3ce5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.
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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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]

View file

@ -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()
));
}

View file

@ -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,
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}

View file

@ -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()
));
}