deno_node 0.183.0

Node compatibility for Deno
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2018-2026 the Deno authors. MIT license.

use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;

use deno_core::CppgcBase;
use deno_core::CppgcInherits;
use deno_core::GarbageCollected;
use deno_core::OpState;
use deno_core::ResourceId;
use deno_core::error::ResourceError;
use deno_core::op2;
use deno_core::uv_compat;
use deno_core::uv_compat::uv_handle_t;
use deno_core::v8;

// ---------------------------------------------------------------------------
// OwnedPtr — a raw-pointer wrapper that owns heap memory without Box's
// uniqueness/noalias guarantees.  Needed when the pointee is accessed
// through raw pointers during reentrant JS calls.
// ---------------------------------------------------------------------------

pub struct OwnedPtr<T>(*mut T);

impl<T> OwnedPtr<T> {
  pub fn from_box(b: Box<T>) -> Self {
    Self(Box::into_raw(b))
  }

  pub fn as_mut_ptr(&self) -> *mut T {
    self.0
  }

  pub fn as_ptr(&self) -> *const T {
    self.0
  }

  /// # Safety
  /// Caller must ensure no mutable references exist to the pointee,
  /// and the pointer is valid.
  pub unsafe fn as_ref(&self) -> &T {
    // SAFETY: upheld by the caller per the method contract above.
    unsafe { &*self.0 }
  }

  #[allow(
    clippy::mut_from_ref,
    reason = "OwnedPtr represents uniquely owned heap memory whose address must remain stable across reentrant FFI callbacks."
  )]
  /// # Safety
  /// Caller must ensure no other references (shared or mutable) exist
  /// to the pointee, and the pointer is valid.
  pub unsafe fn as_mut(&self) -> &mut T {
    // SAFETY: upheld by the caller per the method contract above.
    unsafe { &mut *self.0 }
  }

  /// # Safety
  /// Caller must ensure T and U have identical memory layout.
  pub unsafe fn cast<U>(self) -> OwnedPtr<U> {
    const {
      assert!(size_of::<T>() == size_of::<U>());
      assert!(align_of::<T>() == align_of::<U>());
    }
    let ptr = self.0.cast();
    std::mem::forget(self);
    OwnedPtr(ptr)
  }
}

impl<T> Drop for OwnedPtr<T> {
  fn drop(&mut self) {
    // SAFETY: self.0 was created from Box::into_raw and has not been freed.
    unsafe {
      let _ = Box::from_raw(self.0);
    }
  }
}

// ---------------------------------------------------------------------------
// GlobalHandle — mirrors Node's BaseObject::persistent_handle_ weak/strong
// switching.
//
// In Node, BaseObject holds a v8::Global that can be switched between strong
// (prevents GC of the JS object) and weak (allows GC, triggering cleanup).
// With cppgc the C++ object's lifetime is managed by the GC, but we still
// need to hold a reference back to the JS wrapper object for use in
// callbacks. A strong Global would create a reference cycle (JS -> cppgc
// -> Global -> JS) and leak. A Weak reference allows the GC to collect
// the pair when nothing else references them.
//
// The handle starts Strong after construction (the active libuv handle
// should keep the object alive). It should be made Weak when the handle
// is closed or no longer actively referenced.
// ---------------------------------------------------------------------------

/// A reference to a JS object that can switch between strong (GC root),
/// weak (allows collection), or empty. This mirrors Node's pattern of
/// calling `MakeWeak()` / `ClearWeak()` on `BaseObject::persistent_handle_`.
#[derive(Default)]
pub enum GlobalHandle<T> {
  Strong(v8::Global<T>),
  Weak(v8::Weak<T>),
  #[default]
  None,
}

impl<T> GlobalHandle<T>
where
  v8::Global<T>: v8::Handle<Data = T>,
{
  /// Create a new strong handle.
  pub fn new_strong(scope: &mut v8::PinScope, value: v8::Local<T>) -> Self {
    GlobalHandle::Strong(v8::Global::new(scope, value))
  }

  /// Create a new weak handle.
  pub fn new_weak(scope: &mut v8::PinScope, value: v8::Local<T>) -> Self {
    GlobalHandle::Weak(v8::Weak::new(scope, value))
  }

  /// Make the handle weak, allowing the GC to collect the JS object.
  /// Mirrors Node's `BaseObject::MakeWeak()`.
  pub fn make_weak(&mut self, scope: &mut v8::PinScope) {
    match std::mem::take(self) {
      GlobalHandle::Strong(global) => {
        let local = v8::Local::new(scope, &global);
        *self = GlobalHandle::Weak(v8::Weak::new(scope, local));
      }
      other => *self = other,
    }
  }

  /// Make the handle strong, preventing the GC from collecting the JS object.
  /// Mirrors Node's `BaseObject::ClearWeak()`.
  pub fn make_strong(&mut self, scope: &mut v8::PinScope) {
    match std::mem::take(self) {
      GlobalHandle::Weak(weak) => {
        if let Some(local) = weak.to_local(scope) {
          *self = GlobalHandle::Strong(v8::Global::new(scope, local));
        }
        // If already collected, stays None
      }
      other => *self = other,
    }
  }

  /// Get a Global clone if the reference is still alive.
  /// Returns None if empty or if the weak reference has been collected.
  pub fn to_global(&self, scope: &mut v8::PinScope) -> Option<v8::Global<T>> {
    match self {
      GlobalHandle::Strong(global) => Some(global.clone()),
      GlobalHandle::Weak(weak) => weak
        .to_local(scope)
        .map(|local| v8::Global::new(scope, local)),
      GlobalHandle::None => None,
    }
  }

  /// Returns true if this is a weak reference.
  pub fn is_weak(&self) -> bool {
    matches!(self, GlobalHandle::Weak(_))
  }

  /// Returns true if this handle is empty.
  pub fn is_none(&self) -> bool {
    matches!(self, GlobalHandle::None)
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ProviderType {
  None = 0,
  DirHandle,
  DnsChannel,
  EldHistogram,
  FileHandle,
  FileHandleCloseReq,
  FixedSizeBlobCopy,
  FsEventWrap,
  FsReqCallback,
  FsReqPromise,
  GetAddrInfoReqWrap,
  GetNameInfoReqWrap,
  HeapSnapshot,
  Http2Session,
  Http2Stream,
  Http2Ping,
  Http2Settings,
  HttpIncomingMessage,
  HttpClientRequest,
  JsStream,
  JsUdpWrap,
  MessagePort,
  PipeConnectWrap,
  PipeServerWrap,
  PipeWrap,
  ProcessWrap,
  Promise,
  QueryWrap,
  ShutdownWrap,
  SignalWrap,
  StatWatcher,
  StreamPipe,
  TcpConnectWrap,
  TcpServerWrap,
  TcpWrap,
  TlsWrap,
  TtyWrap,
  UdpSendWrap,
  UdpWrap,
  SigIntWatchdog,
  Worker,
  WorkerHeapSnapshot,
  WriteWrap,
  Zlib,
}

impl From<ProviderType> for i32 {
  fn from(provider: ProviderType) -> Self {
    provider as i32
  }
}

pub use deno_core::uv_compat::AsyncId;

fn next_async_id(state: &mut OpState) -> i64 {
  state.borrow_mut::<AsyncId>().next()
}

#[op2(fast)]
pub fn op_node_new_async_id(state: &mut OpState) -> f64 {
  next_async_id(state) as f64
}

#[derive(CppgcBase)]
#[repr(C)]
pub struct AsyncWrap {
  provider: i32,
  async_id: i64,
}

// SAFETY: we're sure this can be GCed
unsafe impl GarbageCollected for AsyncWrap {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"AsyncWrap"
  }
}

impl AsyncWrap {
  pub(crate) fn create(state: &mut OpState, provider: i32) -> Self {
    let async_id = next_async_id(state);

    Self { provider, async_id }
  }
}

#[op2(base)]
impl AsyncWrap {
  #[getter]
  fn provider(&self) -> i32 {
    self.provider
  }

  #[fast]
  fn get_async_id(&self) -> f64 {
    self.async_id as f64
  }

  #[fast]
  fn get_provider_type(&self) -> i32 {
    self.provider
  }
}

#[derive(Copy, Clone, PartialEq, Eq, Default)]
enum State {
  #[default]
  Initialized,
  Closing,
  Closed,
}

/// A handle to a libuv resource. `New` stores a raw pointer to a `uv_handle_t`
/// whose lifetime is managed by the owning cppgc object (e.g. `TTY`). The
/// pointer is valid as long as the handle has not been closed via `uv_close`.
/// This mirrors Node's approach where `HandleWrap` stores a `uv_handle_t*`
/// that becomes null after close.
#[derive(PartialEq, Eq)]
pub enum Handle {
  Old(ResourceId),
  New(*const uv_handle_t),
}

#[derive(CppgcBase, CppgcInherits)]
#[cppgc_inherits_from(AsyncWrap)]
#[repr(C)]
pub struct HandleWrap {
  base: AsyncWrap,
  handle: Option<Handle>,
  state: Rc<Cell<State>>,
}

// SAFETY: we're sure this can be GCed
unsafe impl GarbageCollected for HandleWrap {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"HandleWrap"
  }
}

impl HandleWrap {
  pub(crate) fn create(base: AsyncWrap, handle: Option<Handle>) -> Self {
    Self {
      base,
      handle,
      state: Rc::new(Cell::new(State::Initialized)),
    }
  }

  pub(crate) fn is_alive(&self) -> bool {
    self.state.get() != State::Closed
  }

  #[allow(dead_code, reason = "used by upcoming TCPWrap/TLSWrap")]
  pub(crate) fn set_state_closing(&self) {
    self.state.set(State::Closing);
  }

  /// Run the JS-side close callback without closing the native handle.
  /// Used by `reset` which has already closed the native handle via
  /// `uv_tcp_close_reset`.
  pub(crate) fn run_close_callback(
    &self,
    op_state: Rc<RefCell<OpState>>,
    this: v8::Global<v8::Object>,
    scope: &mut v8::PinScope<'_, '_>,
    cb: Option<v8::Global<v8::Function>>,
  ) {
    let state = self.state.clone();
    let on_close = move |scope: &mut v8::PinScope<'_, '_>| {
      assert!(state.get() == State::Closing);
      state.set(State::Closed);

      if let Some(cb) = cb {
        let recv = v8::undefined(scope);
        cb.open(scope).call(scope, recv.into(), &[]);
      }
    };

    uv_close(scope, op_state, this, on_close);
  }

  pub(crate) fn close_handle(
    &self,
    op_state: Rc<RefCell<OpState>>,
    this: v8::Global<v8::Object>,
    scope: &mut v8::PinScope<'_, '_>,
    cb: Option<v8::Global<v8::Function>>,
  ) -> Result<(), ResourceError> {
    if self.state.get() != State::Initialized {
      return Ok(());
    }

    // Close the native libuv handle if we have one, so it gets removed
    // from the event loop and stops keeping the process alive.
    if let Some(Handle::New(handle)) = self.handle {
      // SAFETY: handle was initialized by the corresponding uv_*_init and
      // is still valid (state == Initialized).
      unsafe {
        uv_compat::uv_close(handle.cast_mut(), None);
      }
    }

    self.state.set(State::Closing);
    self.run_close_callback(op_state, this, scope, cb);

    Ok(())
  }

  pub(crate) fn has_ref_handle(&self, state: &mut OpState) -> bool {
    if let Some(handle) = &self.handle {
      return match handle {
        Handle::Old(resource_id) => state.has_ref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe { uv_compat::uv_has_ref(*handle) != 0 },
      };
    }

    true
  }

  pub(crate) fn ref_handle(&self, state: &mut OpState) {
    if self.is_alive()
      && let Some(handle) = &self.handle
    {
      match handle {
        Handle::Old(resource_id) => state.uv_ref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe { uv_compat::uv_ref(handle.cast_mut()) },
      }
    }
  }

  pub(crate) fn unref_handle(&self, state: &mut OpState) {
    if self.is_alive()
      && let Some(handle) = &self.handle
    {
      match handle {
        Handle::Old(resource_id) => state.uv_unref(*resource_id),
        // SAFETY: handle is a valid uv_handle_t pointer set during construction and remains live while HandleWrap is alive.
        Handle::New(handle) => unsafe {
          uv_compat::uv_unref(handle.cast_mut())
        },
      }
    }
  }
}

static ON_CLOSE_STR: deno_core::FastStaticString =
  deno_core::ascii_str!("_onClose");

#[op2(base, inherit = AsyncWrap)]
impl HandleWrap {
  #[constructor]
  #[cppgc]
  fn new(
    state: &mut OpState,
    #[smi] provider: i32,
    #[smi] handle: Option<ResourceId>,
  ) -> HandleWrap {
    HandleWrap::create(
      AsyncWrap::create(state, provider),
      handle.map(Handle::Old),
    )
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L65-L85
  #[reentrant]
  fn close(
    &self,
    op_state: Rc<RefCell<OpState>>,
    #[this] this: v8::Global<v8::Object>,
    scope: &mut v8::PinScope<'_, '_>,
    #[scoped] cb: Option<v8::Global<v8::Function>>,
  ) -> Result<(), ResourceError> {
    self.close_handle(op_state, this, scope, cb)
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L58-L62
  #[fast]
  fn has_ref(&self, state: &mut OpState) -> bool {
    self.has_ref_handle(state)
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L40-L46
  #[fast]
  #[rename("ref")]
  fn ref_method(&self, state: &mut OpState) {
    self.ref_handle(state)
  }

  // Ported from Node.js
  //
  // https://github.com/nodejs/node/blob/038d82980ab26cd79abe4409adc2fecad94d7c93/src/handle_wrap.cc#L49-L55
  #[fast]
  fn unref(&self, state: &mut OpState) {
    self.unref_handle(state)
  }
}

fn uv_close<F>(
  scope: &mut v8::PinScope<'_, '_>,
  op_state: Rc<RefCell<OpState>>,
  this: v8::Global<v8::Object>,
  on_close: F,
) where
  F: FnOnce(&mut v8::PinScope<'_, '_>) + 'static,
{
  // Call _onClose() on the JS handles. Not needed for Rust handles.
  let this = v8::Local::new(scope, this);
  let on_close_str = ON_CLOSE_STR.v8_string(scope).unwrap();
  let onclose = this.get(scope, on_close_str.into());

  if let Some(onclose) = onclose
    && let Ok(fn_) = v8::Local::<v8::Function>::try_from(onclose)
  {
    fn_.call(scope, this.into(), &[]);
  }

  op_state
    .borrow()
    .borrow::<deno_core::V8TaskSpawner>()
    .spawn(on_close);
}

#[cfg(test)]
mod tests {
  use std::future::poll_fn;
  use std::task::Poll;

  use deno_core::JsRuntime;
  use deno_core::RuntimeOptions;

  async fn js_test(source_code: &'static str) {
    deno_core::extension!(
      test_ext,
      objects = [super::AsyncWrap, super::HandleWrap,],
      state = |state| {
        state.put::<super::AsyncId>(super::AsyncId::default());
      }
    );

    let mut runtime = JsRuntime::new(RuntimeOptions {
      extensions: vec![test_ext::init()],
      ..Default::default()
    });

    poll_fn(move |cx| {
      runtime
        .execute_script("file://handle_wrap_test.js", source_code)
        .unwrap();

      let result = runtime.poll_event_loop(cx, Default::default());
      assert!(matches!(result, Poll::Ready(Ok(()))));
      Poll::Ready(())
    })
    .await;
  }

  #[tokio::test]
  async fn test_handle_wrap() {
    js_test(
      r#"
        const { HandleWrap } = Deno.core.ops;

        let called = false;
        class MyHandleWrap extends HandleWrap {
          constructor() {
            super(0, null);
          }

          _onClose() {
            called = true;
          }
        }

        const handleWrap = new MyHandleWrap();
        handleWrap.close();

        if (!called) {
          throw new Error("HandleWrap._onClose was not called");
        }
      "#,
    )
    .await;
  }
}