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
|
|
|
|
|
|
|
use deno_runtime::deno_napi::*;
|
2022-10-09 22:29:31 -04:00
|
|
|
use std::os::raw::c_char;
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// It's an N-API symbol
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn napi_fatal_error(
|
|
|
|
location: *const c_char,
|
|
|
|
location_len: isize,
|
|
|
|
message: *const c_char,
|
|
|
|
message_len: isize,
|
|
|
|
) -> ! {
|
|
|
|
let location = if location.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(if location_len < 0 {
|
|
|
|
std::ffi::CStr::from_ptr(location).to_str().unwrap()
|
|
|
|
} else {
|
|
|
|
let slice = std::slice::from_raw_parts(
|
|
|
|
location as *const u8,
|
|
|
|
location_len as usize,
|
|
|
|
);
|
|
|
|
std::str::from_utf8(slice).unwrap()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let message = if message_len < 0 {
|
|
|
|
std::ffi::CStr::from_ptr(message).to_str().unwrap()
|
|
|
|
} else {
|
|
|
|
let slice =
|
|
|
|
std::slice::from_raw_parts(message as *const u8, message_len as usize);
|
|
|
|
std::str::from_utf8(slice).unwrap()
|
|
|
|
};
|
|
|
|
panic!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Fatal exception triggered by napi_fatal_error!\nLocation: {location:?}\n{message}"
|
2022-10-05 10:06:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// napi-3
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-18 10:17:53 -04:00
|
|
|
fn napi_fatal_exception(env: *mut Env, value: napi_value) -> napi_status {
|
|
|
|
let Some(env) = env.as_mut() else {
|
|
|
|
return napi_invalid_arg;
|
|
|
|
};
|
2022-10-05 10:06:44 -04:00
|
|
|
let value = transmute::<napi_value, v8::Local<v8::Value>>(value);
|
|
|
|
let error = value.to_rust_string_lossy(&mut env.scope());
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Fatal exception triggered by napi_fatal_exception!\n{error}");
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_add_env_cleanup_hook(
|
2023-01-10 13:15:10 -05:00
|
|
|
env: *mut Env,
|
|
|
|
hook: extern "C" fn(*const c_void),
|
|
|
|
data: *const c_void,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
|
|
|
let Some(env) = env.as_mut() else {
|
|
|
|
return napi_invalid_arg;
|
|
|
|
};
|
2023-01-10 13:15:10 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut env_cleanup_hooks = env.cleanup_hooks.borrow_mut();
|
|
|
|
if env_cleanup_hooks
|
|
|
|
.iter()
|
|
|
|
.any(|pair| pair.0 == hook && pair.1 == data)
|
|
|
|
{
|
|
|
|
panic!("Cleanup hook with this data already registered");
|
|
|
|
}
|
|
|
|
env_cleanup_hooks.push((hook, data));
|
|
|
|
}
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_remove_env_cleanup_hook(
|
2023-01-10 13:15:10 -05:00
|
|
|
env: *mut Env,
|
|
|
|
hook: extern "C" fn(*const c_void),
|
|
|
|
data: *const c_void,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
|
|
|
let Some(env) = env.as_mut() else {
|
|
|
|
return napi_invalid_arg;
|
|
|
|
};
|
2023-01-10 13:15:10 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut env_cleanup_hooks = env.cleanup_hooks.borrow_mut();
|
|
|
|
// Hooks are supposed to be removed in LIFO order
|
|
|
|
let maybe_index = env_cleanup_hooks
|
|
|
|
.iter()
|
|
|
|
.rposition(|&pair| pair.0 == hook && pair.1 == data);
|
|
|
|
|
|
|
|
if let Some(index) = maybe_index {
|
|
|
|
env_cleanup_hooks.remove(index);
|
|
|
|
} else {
|
|
|
|
panic!("Cleanup hook with this data not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_open_callback_scope(
|
|
|
|
_env: *mut Env,
|
|
|
|
_resource_object: napi_value,
|
|
|
|
_context: napi_value,
|
|
|
|
_result: *mut napi_callback_scope,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
// we open scope automatically when it's needed
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_close_callback_scope(
|
|
|
|
_env: *mut Env,
|
|
|
|
_scope: napi_callback_scope,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2022-10-05 10:06:44 -04:00
|
|
|
// we close scope automatically when it's needed
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn node_api_get_module_file_name(
|
|
|
|
env: *mut Env,
|
|
|
|
result: *mut *const c_char,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
|
|
|
let Some(env) = env.as_mut() else {
|
|
|
|
return napi_invalid_arg;
|
|
|
|
};
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
let shared = env.shared();
|
|
|
|
*result = shared.filename;
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-18 10:17:53 -04:00
|
|
|
fn napi_module_register(module: *const NapiModule) -> napi_status {
|
2023-05-26 00:40:17 -04:00
|
|
|
MODULE_TO_REGISTER.with(|cell| {
|
2022-10-05 10:06:44 -04:00
|
|
|
let mut slot = cell.borrow_mut();
|
2023-05-26 00:40:17 -04:00
|
|
|
let prev = slot.replace(module);
|
|
|
|
assert!(prev.is_none());
|
2022-10-05 10:06:44 -04:00
|
|
|
});
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
2023-05-18 10:17:53 -04:00
|
|
|
fn napi_get_uv_event_loop(
|
|
|
|
_env: *mut Env,
|
|
|
|
uv_loop: *mut *mut (),
|
|
|
|
) -> napi_status {
|
2023-01-14 08:02:42 -05:00
|
|
|
// There is no uv_loop in Deno
|
2022-10-05 10:06:44 -04:00
|
|
|
*uv_loop = std::ptr::null_mut();
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const NODE_VERSION: napi_node_version = napi_node_version {
|
2023-01-13 16:10:53 -05:00
|
|
|
major: 18,
|
|
|
|
minor: 13,
|
2022-10-05 10:06:44 -04:00
|
|
|
patch: 0,
|
2022-10-09 22:29:31 -04:00
|
|
|
release: "Deno\0".as_ptr() as *const c_char,
|
2022-10-05 10:06:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#[napi_sym::napi_sym]
|
|
|
|
fn napi_get_node_version(
|
|
|
|
env: *mut Env,
|
|
|
|
result: *mut *const napi_node_version,
|
2023-05-18 10:17:53 -04:00
|
|
|
) -> napi_status {
|
2023-01-14 22:36:55 -05:00
|
|
|
crate::check_env!(env);
|
|
|
|
crate::check_arg!(env, result);
|
2022-10-05 10:06:44 -04:00
|
|
|
|
|
|
|
*result = &NODE_VERSION as *const napi_node_version;
|
2023-05-18 10:17:53 -04:00
|
|
|
napi_ok
|
2022-10-05 10:06:44 -04:00
|
|
|
}
|