napi 3.12.0-alpha.0

N-API bindings
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
use std::cell::{LazyCell, RefCell};
use std::ffi::c_void;
use std::hash::BuildHasherDefault;
use std::ops::Deref;
use std::ptr;
use std::sync::{
  atomic::{AtomicBool, Ordering},
  Arc, Weak,
};

use nohash_hasher::NoHashHasher;

use crate::{
  bindgen_runtime::{
    FromNapiValue, MaybeTypeTag, NapiValueOwner, PersistedPerInstanceHashMap, ToNapiValue,
  },
  check_status, Env, Error, Result, Status,
};

type RefInformation = (
  /* wrapped_value */ *mut c_void,
  /* napi_ref */ crate::sys::napi_ref,
  /* finalize_callbacks */ *const FinalizeCallbacks,
  /* owner */ NapiValueOwner,
);

pub(crate) type FinalizeCallback = Box<dyn FnMut()>;

pub(crate) struct FinalizeCallbacks {
  alive: AtomicBool,
  callbacks: RefCell<Vec<FinalizeCallback>>,
}

impl FinalizeCallbacks {
  pub(crate) fn new(callback: FinalizeCallback) -> Self {
    Self {
      alive: AtomicBool::new(true),
      callbacks: RefCell::new(vec![callback]),
    }
  }

  pub(crate) fn push(&self, callback: FinalizeCallback) {
    self.callbacks.borrow_mut().push(callback);
  }

  pub(crate) fn is_alive(&self) -> bool {
    self.alive.load(Ordering::Acquire)
  }

  pub(crate) fn close(&self) {
    self.alive.store(false, Ordering::Release);
  }

  /// Shared values may depend on earlier shared values, so callers must consume
  /// this list in reverse registration order.
  pub(crate) fn take(&self) -> Vec<FinalizeCallback> {
    std::mem::take(&mut *self.callbacks.borrow_mut())
  }

  #[cfg(test)]
  pub(crate) fn len(&self) -> usize {
    self.callbacks.borrow().len()
  }
}

impl Drop for FinalizeCallbacks {
  fn drop(&mut self) {
    self.alive.store(false, Ordering::Release);
    for callback in std::mem::take(self.callbacks.get_mut()).into_iter().rev() {
      crate::bindgen_runtime::catch_unwind_safely(|| drop(callback));
    }
  }
}

thread_local! {
  pub(crate) static REFERENCE_MAP: LazyCell<
    PersistedPerInstanceHashMap<*mut c_void, RefInformation, BuildHasherDefault<NoHashHasher<usize>>>,
  > = LazyCell::new(Default::default);
}

/// Create a [`napi_ref`](https://nodejs.org/api/n-api.html#napi_ref) from `Class` instance.
///
/// Unref the [`napi_ref`](https://nodejs.org/api/n-api.html#napi_ref) when the `Reference` is dropped.
///
/// `Reference` is neither `Send` nor `Sync`: it owns a thread-affine Node-API reference and points
/// at a class value that JavaScript may access through another wrapper.
pub struct Reference<T> {
  raw: *mut T,
  napi_ref: crate::sys::napi_ref,
  owner: NapiValueOwner,
  // the finalize callbacks can only be written with the `Env` passed in
  // So we can use `RefCell` rather than a lock here
  finalize_callbacks: Arc<FinalizeCallbacks>,
}

pub(crate) fn add_ref(
  env: crate::sys::napi_env,
  key: *mut c_void,
  wrapped_value: *mut c_void,
  napi_ref: crate::sys::napi_ref,
  finalize_callbacks: *const FinalizeCallbacks,
) {
  REFERENCE_MAP.with(|cell| {
    cell.borrow_mut(|map| {
      if let Some((_, previous_ref, previous_callbacks, previous_owner)) = map.insert(
        key,
        (
          wrapped_value,
          napi_ref,
          finalize_callbacks,
          NapiValueOwner::new(env),
        ),
      ) {
        let previous_callbacks = unsafe { Arc::from_raw(previous_callbacks) };
        previous_callbacks.close();
        unsafe { crate::sys::napi_delete_reference(previous_owner.env(), previous_ref) };
      }
    })
  });
}

impl<T> Drop for Reference<T> {
  fn drop(&mut self) {
    let rc_strong_count = Arc::strong_count(&self.finalize_callbacks);
    let mut ref_count = 0;
    // If Rc strong count == 1, then the referenced object is dropped on GC
    // It would happen when the process is exiting
    // In general, the `drop` of the `Reference` would happen first
    if rc_strong_count > 1
      && self.finalize_callbacks.is_alive()
      && self
        .owner
        .ensure_access(self.owner.env(), "Reference")
        .is_ok()
    {
      let status = unsafe {
        crate::sys::napi_reference_unref(self.owner.env(), self.napi_ref, &mut ref_count)
      };
      debug_assert!(
        status == crate::sys::Status::napi_ok,
        "Reference unref failed, status code: {}",
        crate::Status::from(status)
      );
    };
  }
}

impl<T> Reference<T> {
  #[allow(clippy::not_unsafe_ptr_arg_deref)]
  pub(crate) fn add_ref(
    env: crate::sys::napi_env,
    t: *mut c_void,
    value: (*mut c_void, crate::sys::napi_ref, *const FinalizeCallbacks),
  ) {
    add_ref(env, t, value.0, value.1, value.2);
  }

  #[doc(hidden)]
  pub unsafe fn from_value_ptr(t: *mut c_void, env: crate::sys::napi_env) -> Result<Self> {
    if let Some((wrapped_value, napi_ref, finalize_callbacks_ptr, owner)) =
      REFERENCE_MAP.with(|cell| cell.borrow_mut(|map| map.get(&t).cloned()))
    {
      owner.ensure_access(env, "Reference")?;
      let mut ref_count = 0;
      check_status!(
        unsafe { crate::sys::napi_reference_ref(env, napi_ref, &mut ref_count) },
        "Failed to ref napi reference"
      )?;
      let finalize_callbacks_raw = unsafe { Arc::from_raw(finalize_callbacks_ptr) };
      let finalize_callbacks = finalize_callbacks_raw.clone();
      // Leak the raw finalize callbacks
      let _ = Arc::into_raw(finalize_callbacks_raw);
      Ok(Self {
        raw: wrapped_value.cast(),
        napi_ref,
        owner,
        finalize_callbacks,
      })
    } else {
      Err(Error::new(
        Status::InvalidArg,
        format!("Class for Type {t:?} not found"),
      ))
    }
  }
}

impl<T> ToNapiValue for Reference<T> {
  unsafe fn to_napi_value(env: crate::sys::napi_env, val: Self) -> Result<crate::sys::napi_value> {
    val.get_value(env)
  }
}

impl<T: MaybeTypeTag> FromNapiValue for Reference<T> {
  unsafe fn from_napi_value(
    env: crate::sys::napi_env,
    napi_val: crate::sys::napi_value,
  ) -> Result<Self> {
    let mut value = ptr::null_mut();
    check_status!(
      unsafe { crate::sys::napi_unwrap(env, napi_val, &mut value) },
      "Unwrap value [{}] from class Reference failed",
      std::any::type_name::<T>(),
    )?;

    // Reject a wrong-class / prototype-spoofed object before adopting it as a
    // `Reference<T>`. Compiled only on napi8 NATIVE targets (the `T: MaybeTypeTag`
    // bound provides `T::type_tag()` only there; elsewhere this is the pre-tag
    // path).
    #[cfg(all(feature = "napi8", not(target_family = "wasm")))]
    unsafe {
      crate::bindgen_runtime::validate_type_tag(
        env,
        napi_val,
        &T::type_tag(),
        std::any::type_name::<T>(),
      )?
    };

    unsafe { Reference::from_value_ptr(value.cast(), env) }
  }
}

impl<T> Reference<T> {
  fn ensure_access(&self, env: crate::sys::napi_env) -> Result<()> {
    self.owner.ensure_access(env, "Reference")?;
    if !self.finalize_callbacks.is_alive() {
      return Err(Error::new(
        Status::InvalidArg,
        "A JavaScript Reference cannot be accessed after its object has been finalized".to_owned(),
      ));
    }
    Ok(())
  }

  pub(crate) fn get_value(&self, env: crate::sys::napi_env) -> Result<crate::sys::napi_value> {
    self.ensure_access(env)?;
    let mut result = ptr::null_mut();
    check_status!(
      unsafe { crate::sys::napi_get_reference_value(env, self.napi_ref, &mut result) },
      "Failed to get reference value"
    )?;
    Ok(result)
  }

  pub fn clone(&self, env: Env) -> Result<Self> {
    self.ensure_access(env.0)?;
    let mut ref_count = 0;
    check_status!(
      unsafe { crate::sys::napi_reference_ref(env.0, self.napi_ref, &mut ref_count) },
      "Failed to ref napi reference"
    )?;
    Ok(Self {
      raw: self.raw,
      napi_ref: self.napi_ref,
      owner: self.owner.clone(),
      finalize_callbacks: self.finalize_callbacks.clone(),
    })
  }

  pub fn downgrade(&self) -> WeakReference<T>
  where
    T: 'static,
  {
    WeakReference {
      raw: self.raw,
      napi_ref: self.napi_ref,
      owner: self.owner.clone(),
      finalize_callbacks: Arc::downgrade(&self.finalize_callbacks),
    }
  }

  /// Access the referenced class value while preventing overlapping mutable callbacks.
  pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> Result<R> {
    self.ensure_access(self.owner.env())?;
    let _borrow = crate::bindgen_runtime::acquire_native_borrow(self.raw, false)?;
    Ok(f(unsafe { &*self.raw }))
  }

  /// Mutably access the referenced class value while preventing all overlapping callbacks.
  pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
    self.ensure_access(self.owner.env())?;
    let _borrow = crate::bindgen_runtime::acquire_native_borrow(self.raw, true)?;
    Ok(f(unsafe { &mut *self.raw }))
  }

  /// Create an owner-tied value that may borrow this class value.
  ///
  /// # Safety
  ///
  /// The caller must guarantee exclusive access to `T` for the duration of `f`. If the returned
  /// `S` contains references to `T`, no mutable access to `T` may occur for the lifetime of the
  /// returned `SharedReference`. Those references must remain reachable only through that
  /// `SharedReference` and must not escape through any other side effect. The supplied `env` must be
  /// the owning environment.
  pub unsafe fn share_with<S: 'static, F: FnOnce(&'static mut T) -> Result<S>>(
    self,
    env: Env,
    f: F,
  ) -> Result<SharedReference<T, S>>
  where
    T: 'static,
  {
    self.ensure_access(env.0)?;
    let s = f(unsafe { &mut *self.raw })?;
    let mut s = Some(Box::new(s));
    let s_ptr = s
      .as_deref_mut()
      .expect("shared reference value must be present") as *mut S;
    self.finalize_callbacks.push(Box::new(move || {
      if let Some(s) = s.take() {
        drop(s);
      }
    }));
    Ok(SharedReference {
      raw: s_ptr,
      owner: self,
    })
  }
}

pub struct WeakReference<T: 'static> {
  raw: *mut T,
  napi_ref: crate::sys::napi_ref,
  owner: NapiValueOwner,
  finalize_callbacks: Weak<FinalizeCallbacks>,
}

impl<T> Clone for WeakReference<T> {
  fn clone(&self) -> Self {
    Self {
      raw: self.raw,
      napi_ref: self.napi_ref,
      owner: self.owner.clone(),
      finalize_callbacks: self.finalize_callbacks.clone(),
    }
  }
}

impl<T: 'static> ToNapiValue for WeakReference<T> {
  unsafe fn to_napi_value(env: crate::sys::napi_env, val: Self) -> Result<crate::sys::napi_value> {
    val.owner.ensure_access(env, "WeakReference")?;
    if Weak::strong_count(&val.finalize_callbacks) == 0 {
      return Err(Error::new(
        Status::GenericFailure,
        format!(
          "The original reference that WeakReference<{}> is pointing to is dropped",
          std::any::type_name::<T>()
        ),
      ));
    };
    if !val
      .finalize_callbacks
      .upgrade()
      .is_some_and(|callbacks| callbacks.is_alive())
    {
      return Err(Error::new(
        Status::InvalidArg,
        "A JavaScript WeakReference cannot be accessed after its object has been finalized"
          .to_owned(),
      ));
    }
    let mut result = ptr::null_mut();
    check_status!(
      unsafe { crate::sys::napi_get_reference_value(env, val.napi_ref, &mut result) },
      "Failed to get reference value"
    )?;
    Ok(result)
  }
}

impl<T: 'static> WeakReference<T> {
  pub fn upgrade(&self, env: Env) -> Result<Option<Reference<T>>> {
    self.owner.ensure_access(env.0, "WeakReference")?;
    if let Some(finalize_callbacks) = self.finalize_callbacks.upgrade() {
      if !finalize_callbacks.is_alive() {
        return Ok(None);
      }
      let mut ref_count = 0;
      check_status!(
        unsafe { crate::sys::napi_reference_ref(env.0, self.napi_ref, &mut ref_count) },
        "Failed to ref napi reference"
      )?;
      Ok(Some(Reference {
        raw: self.raw,
        napi_ref: self.napi_ref,
        owner: self.owner.clone(),
        finalize_callbacks,
      }))
    } else {
      Ok(None)
    }
  }

  /// Access the value if its owning JavaScript object is still alive.
  pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> Result<Option<R>> {
    match self.upgrade(Env::from_raw(self.owner.env()))? {
      Some(reference) => reference.with(f).map(Some),
      None => Ok(None),
    }
  }
}

/// ### Experimental feature
///
/// Create a `SharedReference` from an existed `Reference`.
pub struct SharedReference<T: 'static, S: 'static> {
  raw: *mut S,
  owner: Reference<T>,
}

impl<T: 'static, S: 'static> SharedReference<T, S> {
  pub fn clone(&self, env: Env) -> Result<Self> {
    Ok(SharedReference {
      raw: self.raw,
      owner: self.owner.clone(env)?,
    })
  }

  pub fn clone_owner(&self, env: Env) -> Result<Reference<T>> {
    self.owner.clone(env)
  }

  /// Create another owner-tied value that may borrow this shared value.
  ///
  /// # Safety
  ///
  /// The caller must guarantee exclusive access to `S` for the duration of `f`. If the returned
  /// `U` contains references to `S`, no mutable access to `S` may occur for the lifetime of the
  /// returned `SharedReference`. Those references must remain reachable only through that
  /// `SharedReference` and must not escape through any other side effect. The supplied `env` must be
  /// the owning environment.
  pub unsafe fn share_with<U: 'static, F: FnOnce(&'static mut S) -> Result<U>>(
    self,
    env: Env,
    f: F,
  ) -> Result<SharedReference<T, U>> {
    self.owner.ensure_access(env.0)?;
    let value = f(unsafe { &mut *self.raw })?;
    let mut value = Some(Box::new(value));
    let raw = value
      .as_deref_mut()
      .expect("shared reference value must be present") as *mut U;
    self.owner.finalize_callbacks.push(Box::new(move || {
      if let Some(value) = value.take() {
        drop(value);
      }
    }));
    Ok(SharedReference {
      raw,
      owner: self.owner,
    })
  }
}

impl<T: 'static, S: 'static> ToNapiValue for SharedReference<T, S> {
  unsafe fn to_napi_value(env: crate::sys::napi_env, val: Self) -> Result<crate::sys::napi_value> {
    val.owner.ensure_access(env)?;
    let mut result = ptr::null_mut();
    check_status!(
      unsafe { crate::sys::napi_get_reference_value(env, val.owner.napi_ref, &mut result) },
      "Failed to get reference value"
    )?;
    Ok(result)
  }
}

impl<T: 'static, S: 'static> Deref for SharedReference<T, S> {
  type Target = S;

  fn deref(&self) -> &Self::Target {
    assert!(
      self.owner.finalize_callbacks.is_alive(),
      "A JavaScript SharedReference cannot be accessed after its object has been finalized"
    );
    unsafe { &*self.raw }
  }
}