chore(node-api): reuse SendPtr (#21567)

Pending review items from https://github.com/denoland/deno/pull/21406
This commit is contained in:
Divy Srivastava 2023-12-15 04:16:57 +05:30 committed by GitHub
parent c481ff7d81
commit 94c70fd719
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 10 deletions

View file

@ -3,6 +3,7 @@
use deno_runtime::deno_napi::*;
use crate::check_env;
use crate::napi::threadsafe_functions::SendPtr;
#[repr(C)]
pub struct AsyncWork {
@ -64,10 +65,6 @@ fn napi_queue_async_work(
return napi_invalid_arg;
};
#[repr(transparent)]
struct SendPtr<T>(*const T);
unsafe impl<T> Send for SendPtr<T> {}
unsafe impl<T> Sync for SendPtr<T> {}
let send_env = SendPtr(env_ptr);
#[inline(always)]

View file

@ -9,6 +9,12 @@ use std::ptr::NonNull;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
#[repr(transparent)]
pub struct SendPtr<T>(pub *const T);
unsafe impl<T> Send for SendPtr<T> {}
unsafe impl<T> Sync for SendPtr<T> {}
static TS_FN_ID_COUNTER: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
pub struct TsFn {
@ -86,11 +92,6 @@ impl TsFn {
pub fn call(&self, data: *mut c_void, is_blocking: bool) {
let js_func = self.maybe_func.clone();
#[repr(transparent)]
struct SendPtr<T>(*const T);
unsafe impl<T> Send for SendPtr<T> {}
unsafe impl<T> Sync for SendPtr<T> {}
let env = SendPtr(self.env);
let context = SendPtr(self.context);
let data = SendPtr(data);
@ -146,7 +147,8 @@ impl TsFn {
context: SendPtr<c_void>,
data: SendPtr<c_void>,
) {
// SAFETY: We're calling the provided callback with valid args
// SAFETY: env is valid for the duration of the callback.
// data lifetime is users responsibility.
unsafe {
call_js_cb(
env.0 as _,