summaryrefslogtreecommitdiff
path: root/test_napi/src
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-05-24 15:41:43 +0200
committerGitHub <noreply@github.com>2023-05-24 15:41:43 +0200
commite56695daa89b7c53a88a691f35ee9a498caffbdf (patch)
tree4d02b4422e50ec0bc0ec5b733923f64206d4abba /test_napi/src
parent0bb5bbc7a0ff7565a4c7fa4ebc8c69e02f76e6b5 (diff)
fix(napi): add napi_async_init and napi_async_destroy (#19234)
We don't have support for "AsyncContext" in "node:async_hooks" module, so these two APIs are just noops. Towards https://github.com/denoland/deno/issues/18610.
Diffstat (limited to 'test_napi/src')
-rw-r--r--test_napi/src/lib.rs2
-rw-r--r--test_napi/src/make_callback.rs85
2 files changed, 87 insertions, 0 deletions
diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs
index 8fa7d9ef6..9342656fd 100644
--- a/test_napi/src/lib.rs
+++ b/test_napi/src/lib.rs
@@ -16,6 +16,7 @@ pub mod date;
pub mod env;
pub mod error;
pub mod finalizer;
+pub mod make_callback;
pub mod mem;
pub mod numbers;
pub mod object_wrap;
@@ -162,6 +163,7 @@ unsafe extern "C" fn napi_register_module_v1(
mem::init(env, exports);
bigint::init(env, exports);
symbol::init(env, exports);
+ make_callback::init(env, exports);
init_cleanup_hook(env, exports);
diff --git a/test_napi/src/make_callback.rs b/test_napi/src/make_callback.rs
new file mode 100644
index 000000000..c8d2b3342
--- /dev/null
+++ b/test_napi/src/make_callback.rs
@@ -0,0 +1,85 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+use crate::assert_napi_ok;
+use crate::cstr;
+use napi_sys::ValueType::napi_function;
+use napi_sys::*;
+use std::ptr;
+
+extern "C" fn make_callback(
+ env: napi_env,
+ info: napi_callback_info,
+) -> napi_value {
+ const MAX_ARGUMENTS: usize = 10;
+ const RESERVED_ARGUMENTS: usize = 3;
+
+ let mut args = [std::ptr::null_mut(); MAX_ARGUMENTS];
+ let mut argc = MAX_ARGUMENTS;
+ assert_napi_ok!(napi_get_cb_info(
+ env,
+ info,
+ &mut argc,
+ args.as_mut_ptr(),
+ ptr::null_mut(),
+ ptr::null_mut(),
+ ));
+
+ assert!(argc > 0);
+ let resource = args[0];
+ let recv = args[1];
+ let func = args[2];
+
+ let mut argv: Vec<napi_value> = Vec::new();
+ argv.resize(MAX_ARGUMENTS - RESERVED_ARGUMENTS, ptr::null_mut());
+ for i in RESERVED_ARGUMENTS..argc {
+ argv[i - RESERVED_ARGUMENTS] = args[i];
+ }
+
+ let mut func_type: napi_valuetype = -1;
+ assert_napi_ok!(napi_typeof(env, func, &mut func_type));
+
+ let mut resource_name = ptr::null_mut();
+ assert_napi_ok!(napi_create_string_utf8(
+ env,
+ cstr!("test"),
+ usize::MAX,
+ &mut resource_name
+ ));
+
+ let mut context: napi_async_context = ptr::null_mut();
+ assert_napi_ok!(napi_async_init(env, resource, resource_name, &mut context));
+
+ let mut result = ptr::null_mut();
+ assert_eq!(func_type, napi_function);
+ assert_napi_ok!(napi_make_callback(
+ env,
+ context,
+ recv,
+ func,
+ argc - RESERVED_ARGUMENTS,
+ argv.as_mut_ptr(),
+ &mut result
+ ));
+
+ assert_napi_ok!(napi_async_destroy(env, context));
+ result
+}
+
+pub fn init(env: napi_env, exports: napi_value) {
+ let mut fn_: napi_value = ptr::null_mut();
+
+ assert_napi_ok!(napi_create_function(
+ env,
+ ptr::null_mut(),
+ usize::MAX,
+ Some(make_callback),
+ ptr::null_mut(),
+ &mut fn_,
+ ));
+ assert_napi_ok!(napi_set_named_property(
+ env,
+ exports,
+ cstr!("makeCallback"),
+ fn_
+ ));
+}