Skip to content

Commit

Permalink
chore(node-api): reuse SendPtr (#21567)
Browse files Browse the repository at this point in the history
Pending review items from #21406
  • Loading branch information
littledivy authored Dec 14, 2023
1 parent c481ff7 commit 94c70fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
5 changes: 1 addition & 4 deletions cli/napi/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use deno_runtime::deno_napi::*;

use crate::check_env;
use crate::napi::threadsafe_functions::SendPtr;

#[repr(C)]
pub struct AsyncWork {
Expand Down Expand Up @@ -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)]
Expand Down
14 changes: 8 additions & 6 deletions cli/napi/threadsafe_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 _,
Expand Down

0 comments on commit 94c70fd

Please sign in to comment.