2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-10-05 10:06:44 -04:00
|
|
|
#![allow(clippy::all)]
|
|
|
|
#![allow(clippy::undocumented_unsafe_blocks)]
|
|
|
|
|
2023-01-10 13:15:10 -05:00
|
|
|
use std::ffi::c_void;
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
use napi_sys::*;
|
|
|
|
|
|
|
|
pub mod array;
|
2022-10-30 13:13:46 -04:00
|
|
|
pub mod arraybuffer;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod r#async;
|
2023-05-18 06:49:56 -04:00
|
|
|
pub mod bigint;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod callback;
|
|
|
|
pub mod coerce;
|
2023-01-13 06:53:45 -05:00
|
|
|
pub mod date;
|
2023-02-01 09:41:04 -05:00
|
|
|
pub mod env;
|
2023-01-14 22:36:55 -05:00
|
|
|
pub mod error;
|
2023-05-18 09:45:47 -04:00
|
|
|
pub mod finalizer;
|
2023-05-24 09:41:43 -04:00
|
|
|
pub mod make_callback;
|
2023-01-23 08:30:01 -05:00
|
|
|
pub mod mem;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod numbers;
|
2024-05-06 15:22:50 -04:00
|
|
|
pub mod object;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod object_wrap;
|
2022-10-07 06:54:01 -04:00
|
|
|
pub mod primitives;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod promise;
|
|
|
|
pub mod properties;
|
|
|
|
pub mod strings;
|
2023-05-21 15:14:48 -04:00
|
|
|
pub mod symbol;
|
2023-01-12 07:47:55 -05:00
|
|
|
pub mod tsfn;
|
2022-10-05 10:06:44 -04:00
|
|
|
pub mod typedarray;
|
|
|
|
|
2023-01-14 22:36:55 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! cstr {
|
|
|
|
($s: literal) => {{
|
2023-02-10 07:50:47 -05:00
|
|
|
std::ffi::CString::new($s).unwrap().into_raw()
|
2023-01-14 22:36:55 -05:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
#[macro_export]
|
2023-01-11 09:47:26 -05:00
|
|
|
macro_rules! assert_napi_ok {
|
|
|
|
($call: expr) => {{
|
|
|
|
assert_eq!(unsafe { $call }, napi_sys::Status::napi_ok);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! napi_get_callback_info {
|
2022-10-05 10:06:44 -04:00
|
|
|
($env: expr, $callback_info: expr, $size: literal) => {{
|
2023-01-10 13:15:10 -05:00
|
|
|
let mut args = [std::ptr::null_mut(); $size];
|
2022-10-05 10:06:44 -04:00
|
|
|
let mut argc = $size;
|
2023-01-10 13:15:10 -05:00
|
|
|
let mut this = std::ptr::null_mut();
|
2023-01-11 09:47:26 -05:00
|
|
|
crate::assert_napi_ok!(napi_get_cb_info(
|
|
|
|
$env,
|
|
|
|
$callback_info,
|
|
|
|
&mut argc,
|
|
|
|
args.as_mut_ptr(),
|
|
|
|
&mut this,
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
));
|
2022-10-05 10:06:44 -04:00
|
|
|
(args, argc, this)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2023-01-11 09:47:26 -05:00
|
|
|
macro_rules! napi_new_property {
|
2022-10-05 10:06:44 -04:00
|
|
|
($env: expr, $name: expr, $value: expr) => {
|
|
|
|
napi_property_descriptor {
|
2023-01-11 09:47:26 -05:00
|
|
|
utf8name: concat!($name, "\0").as_ptr() as *const std::os::raw::c_char,
|
2023-01-10 13:15:10 -05:00
|
|
|
name: std::ptr::null_mut(),
|
2022-10-05 10:06:44 -04:00
|
|
|
method: Some($value),
|
|
|
|
getter: None,
|
|
|
|
setter: None,
|
2023-01-10 13:15:10 -05:00
|
|
|
data: std::ptr::null_mut(),
|
2022-10-05 10:06:44 -04:00
|
|
|
attributes: 0,
|
2023-01-10 13:15:10 -05:00
|
|
|
value: std::ptr::null_mut(),
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:15:10 -05:00
|
|
|
extern "C" fn cleanup(arg: *mut c_void) {
|
|
|
|
println!("cleanup({})", arg as i64);
|
|
|
|
}
|
|
|
|
|
2023-01-13 16:17:25 -05:00
|
|
|
extern "C" fn remove_this_hook(arg: *mut c_void) {
|
|
|
|
let env = arg as napi_env;
|
|
|
|
unsafe { napi_remove_env_cleanup_hook(env, Some(remove_this_hook), arg) };
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:15:10 -05:00
|
|
|
static SECRET: i64 = 42;
|
|
|
|
static WRONG_SECRET: i64 = 17;
|
|
|
|
static THIRD_SECRET: i64 = 18;
|
|
|
|
|
|
|
|
extern "C" fn install_cleanup_hook(
|
|
|
|
env: napi_env,
|
|
|
|
info: napi_callback_info,
|
|
|
|
) -> napi_value {
|
2023-01-11 09:47:26 -05:00
|
|
|
let (_args, argc, _) = napi_get_callback_info!(env, info, 1);
|
2023-01-10 13:15:10 -05:00
|
|
|
assert_eq!(argc, 0);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
napi_add_env_cleanup_hook(env, Some(cleanup), WRONG_SECRET as *mut c_void);
|
|
|
|
napi_add_env_cleanup_hook(env, Some(cleanup), SECRET as *mut c_void);
|
|
|
|
napi_add_env_cleanup_hook(env, Some(cleanup), THIRD_SECRET as *mut c_void);
|
2023-01-13 16:17:25 -05:00
|
|
|
napi_add_env_cleanup_hook(env, Some(remove_this_hook), env as *mut c_void);
|
2023-01-10 13:15:10 -05:00
|
|
|
napi_remove_env_cleanup_hook(
|
|
|
|
env,
|
|
|
|
Some(cleanup),
|
|
|
|
WRONG_SECRET as *mut c_void,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_cleanup_hook(env: napi_env, exports: napi_value) {
|
2023-01-11 09:47:26 -05:00
|
|
|
let properties = &[napi_new_property!(
|
2023-01-10 13:15:10 -05:00
|
|
|
env,
|
2023-01-11 09:47:26 -05:00
|
|
|
"installCleanupHook",
|
2023-01-10 13:15:10 -05:00
|
|
|
install_cleanup_hook
|
|
|
|
)];
|
|
|
|
|
2023-01-11 09:47:26 -05:00
|
|
|
assert_napi_ok!(napi_define_properties(
|
|
|
|
env,
|
|
|
|
exports,
|
|
|
|
properties.len(),
|
|
|
|
properties.as_ptr()
|
|
|
|
));
|
2023-01-10 13:15:10 -05:00
|
|
|
}
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn napi_register_module_v1(
|
|
|
|
env: napi_env,
|
2023-01-23 08:29:46 -05:00
|
|
|
_: napi_value,
|
2022-10-05 10:06:44 -04:00
|
|
|
) -> napi_value {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
napi_sys::setup();
|
|
|
|
}
|
|
|
|
|
2023-01-23 08:29:46 -05:00
|
|
|
// We create a fresh exports object and leave the passed
|
|
|
|
// exports object empty.
|
|
|
|
//
|
|
|
|
// https://github.com/denoland/deno/issues/17349
|
|
|
|
let mut exports = std::ptr::null_mut();
|
|
|
|
assert_napi_ok!(napi_create_object(env, &mut exports));
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
strings::init(env, exports);
|
|
|
|
numbers::init(env, exports);
|
|
|
|
typedarray::init(env, exports);
|
2022-10-30 13:13:46 -04:00
|
|
|
arraybuffer::init(env, exports);
|
2022-10-05 10:06:44 -04:00
|
|
|
array::init(env, exports);
|
2023-02-01 09:41:04 -05:00
|
|
|
env::init(env, exports);
|
2023-01-14 22:36:55 -05:00
|
|
|
error::init(env, exports);
|
2023-05-18 09:45:47 -04:00
|
|
|
finalizer::init(env, exports);
|
2022-10-07 06:54:01 -04:00
|
|
|
primitives::init(env, exports);
|
2022-10-05 10:06:44 -04:00
|
|
|
properties::init(env, exports);
|
|
|
|
promise::init(env, exports);
|
|
|
|
coerce::init(env, exports);
|
|
|
|
object_wrap::init(env, exports);
|
|
|
|
callback::init(env, exports);
|
|
|
|
r#async::init(env, exports);
|
2023-01-13 06:53:45 -05:00
|
|
|
date::init(env, exports);
|
2023-01-12 07:47:55 -05:00
|
|
|
tsfn::init(env, exports);
|
2023-01-23 08:30:01 -05:00
|
|
|
mem::init(env, exports);
|
2023-05-18 06:49:56 -04:00
|
|
|
bigint::init(env, exports);
|
2023-05-21 15:14:48 -04:00
|
|
|
symbol::init(env, exports);
|
2023-05-24 09:41:43 -04:00
|
|
|
make_callback::init(env, exports);
|
2024-05-06 15:22:50 -04:00
|
|
|
object::init(env, exports);
|
2023-01-23 08:30:01 -05:00
|
|
|
|
2023-01-10 13:15:10 -05:00
|
|
|
init_cleanup_hook(env, exports);
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
exports
|
|
|
|
}
|