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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
use std::{
  any::{Any, TypeId},
  cell::{RefCell, UnsafeCell},
  collections::HashMap,
  ffi::c_void,
  ops::{Deref, DerefMut},
  ptr,
  sync::{
    atomic::{AtomicBool, AtomicUsize, Ordering},
    Arc, LazyLock, Mutex, Weak,
  },
};

use crate::{
  bindgen_runtime::{
    sys, Env, FromNapiRef, FromNapiValue, Result, Status, ToNapiValue, TypeName, Unknown,
    ValidateNapiValue,
  },
  check_status, check_status_or_throw, Error, JsExternal,
};

type EnvId = usize;

thread_local! {
  static EXTERNAL_ENV_STATES: RefCell<HashMap<EnvId, Weak<ExternalEnvState>>> =
    RefCell::new(HashMap::new());
}

struct ExternalEnvState {
  env: EnvId,
  owner_thread: std::thread::ThreadId,
  closed: AtomicBool,
}

impl ExternalEnvState {
  fn new(env: sys::napi_env) -> Self {
    Self {
      env: env as EnvId,
      owner_thread: std::thread::current().id(),
      closed: AtomicBool::new(false),
    }
  }

  fn is_open_for(&self, env: sys::napi_env) -> bool {
    self.env == env as EnvId && !self.closed.load(Ordering::Acquire)
  }

  fn is_open_on_owner_thread(&self) -> bool {
    self.owner_thread == std::thread::current().id() && !self.closed.load(Ordering::Acquire)
  }

  fn ensure_open_for(&self, env: sys::napi_env) -> Result<()> {
    if self.env != env as EnvId {
      return Err(Error::new(
        Status::InvalidArg,
        "An ExternalRef cannot be used with a different napi_env".to_owned(),
      ));
    }
    if self.owner_thread != std::thread::current().id() {
      return Err(Error::new(
        Status::InvalidArg,
        "An ExternalRef cannot be used outside its owner thread".to_owned(),
      ));
    }
    if self.closed.load(Ordering::Acquire) {
      return Err(Error::new(
        Status::InvalidArg,
        "An ExternalRef cannot be used after its owner environment has closed".to_owned(),
      ));
    }
    Ok(())
  }

  #[cfg(not(feature = "noop"))]
  fn close(&self) {
    self.closed.store(true, Ordering::Release);
  }
}

type ExternalToken = usize;

struct ExternalProvenance {
  type_id: TypeId,
  env: Arc<ExternalEnvState>,
  owner_ref: sys::napi_ref,
  control: Box<dyn Any>,
}

// Registry entries are inserted, resolved, and removed only by the owning N-API environment.
// The unsafe Send implementation permits the process-wide registry to hold non-Send External<T>
// values without allowing those values to be accessed from a foreign environment.
unsafe impl Send for ExternalProvenance {}

static NEXT_EXTERNAL_TOKEN: AtomicUsize = AtomicUsize::new(1);
static EXTERNAL_PROVENANCE: LazyLock<Mutex<HashMap<ExternalToken, ExternalProvenance>>> =
  LazyLock::new(|| Mutex::new(HashMap::new()));

fn allocate_external_token() -> Result<ExternalToken> {
  let mut current = NEXT_EXTERNAL_TOKEN.load(Ordering::Relaxed);
  loop {
    let next = current.checked_add(1).ok_or_else(|| {
      Error::new(
        Status::GenericFailure,
        "External token space has been exhausted".to_owned(),
      )
    })?;
    match NEXT_EXTERNAL_TOKEN.compare_exchange_weak(
      current,
      next,
      Ordering::Relaxed,
      Ordering::Relaxed,
    ) {
      Ok(_) => return Ok(current),
      Err(observed) => current = observed,
    }
  }
}

fn external_token(pointer: *mut c_void) -> Option<ExternalToken> {
  let token = pointer as ExternalToken;
  (token != 0).then_some(token)
}

fn register_external_provenance<T: 'static>(
  token: ExternalToken,
  env: Arc<ExternalEnvState>,
  owner_ref: sys::napi_ref,
  control: Arc<ExternalControlBlock<T>>,
) -> Result<()> {
  let mut registry = EXTERNAL_PROVENANCE
    .lock()
    .unwrap_or_else(std::sync::PoisonError::into_inner);
  if registry.contains_key(&token) {
    return Err(Error::new(
      Status::GenericFailure,
      "External token was already registered".to_owned(),
    ));
  }
  registry.insert(
    token,
    ExternalProvenance {
      type_id: TypeId::of::<T>(),
      env,
      owner_ref,
      control: Box::new(control),
    },
  );
  Ok(())
}

fn unregister_external_provenance(pointer: *mut c_void) -> Option<ExternalProvenance> {
  let token = external_token(pointer)?;
  EXTERNAL_PROVENANCE
    .lock()
    .unwrap_or_else(std::sync::PoisonError::into_inner)
    .remove(&token)
}

fn clone_external_control_unchecked<T: 'static>(
  pointer: *mut c_void,
) -> Option<Arc<ExternalControlBlock<T>>> {
  let token = external_token(pointer)?;
  let registry = EXTERNAL_PROVENANCE
    .lock()
    .unwrap_or_else(std::sync::PoisonError::into_inner);
  let provenance = registry.get(&token)?;
  if provenance.type_id != TypeId::of::<T>() || !provenance.env.is_open_on_owner_thread() {
    return None;
  }
  provenance
    .control
    .downcast_ref::<Arc<ExternalControlBlock<T>>>()
    .map(Arc::clone)
}

fn clone_external_control<T: 'static>(
  pointer: *mut c_void,
  env: sys::napi_env,
  napi_val: sys::napi_value,
) -> Result<Arc<ExternalControlBlock<T>>> {
  let Some(token) = external_token(pointer) else {
    return Err(Error::new(
      Status::InvalidArg,
      "The External value is not backed by a shareable napi-rs allocation".to_owned(),
    ));
  };
  let registry = EXTERNAL_PROVENANCE
    .lock()
    .unwrap_or_else(std::sync::PoisonError::into_inner);
  let Some(provenance) = registry.get(&token) else {
    return Err(Error::new(
      Status::InvalidArg,
      "The External value is not backed by a shareable napi-rs allocation".to_owned(),
    ));
  };
  if provenance.type_id != TypeId::of::<T>() {
    return Err(Error::new(
      Status::InvalidArg,
      format!(
        "<{}> on `External` is not the type of wrapped object",
        std::any::type_name::<T>()
      ),
    ));
  }
  provenance.env.ensure_open_for(env)?;

  let mut owner_value = ptr::null_mut();
  check_status!(
    unsafe { sys::napi_get_reference_value(env, provenance.owner_ref, &mut owner_value) },
    "Failed to resolve the owning External value"
  )?;
  if owner_value.is_null() {
    return Err(Error::new(
      Status::InvalidArg,
      "The External value is no longer owned by a live napi-rs allocation".to_owned(),
    ));
  }
  let mut is_owner_value = false;
  check_status!(
    unsafe { sys::napi_strict_equals(env, owner_value, napi_val, &mut is_owner_value) },
    "Failed to validate the owning External value"
  )?;
  if !is_owner_value {
    return Err(Error::new(
      Status::InvalidArg,
      "The External token does not belong to this JavaScript External value".to_owned(),
    ));
  }

  provenance
    .control
    .downcast_ref::<Arc<ExternalControlBlock<T>>>()
    .map(Arc::clone)
    .ok_or_else(|| {
      Error::new(
        Status::InvalidArg,
        "The External value has an invalid napi-rs control block".to_owned(),
      )
    })
}

fn external_env_state(env: sys::napi_env) -> Result<Arc<ExternalEnvState>> {
  let existing =
    EXTERNAL_ENV_STATES.with(|states| states.borrow().get(&(env as EnvId)).and_then(Weak::upgrade));
  if let Some(state) = existing {
    state.ensure_open_for(env)?;
    return Ok(state);
  }

  let state = Arc::new(ExternalEnvState::new(env));
  register_external_env_cleanup(env, Arc::clone(&state))?;
  EXTERNAL_ENV_STATES.with(|states| {
    states
      .borrow_mut()
      .insert(env as EnvId, Arc::downgrade(&state));
  });
  Ok(state)
}

#[cfg(all(feature = "napi3", not(feature = "noop")))]
fn register_external_env_cleanup(env: sys::napi_env, state: Arc<ExternalEnvState>) -> Result<()> {
  let data = Box::into_raw(Box::new(state));
  #[cfg(not(target_family = "wasm"))]
  let status =
    unsafe { sys::napi_add_env_cleanup_hook(env, Some(external_env_cleanup), data.cast()) };
  #[cfg(target_family = "wasm")]
  let status =
    unsafe { crate::napi_add_env_cleanup_hook(env, Some(external_env_cleanup), data.cast()) };
  if status != sys::Status::napi_ok {
    drop(unsafe { Box::from_raw(data) });
  }
  check_status!(status, "Failed to add External environment cleanup hook")
}

#[cfg(all(not(feature = "napi3"), not(feature = "noop")))]
fn register_external_env_cleanup(env: sys::napi_env, state: Arc<ExternalEnvState>) -> Result<()> {
  let data = Box::into_raw(Box::new(state));
  let mut sentinel = ptr::null_mut();
  let status = unsafe {
    sys::napi_create_external(
      env,
      data.cast(),
      Some(external_env_sentinel_finalize),
      ptr::null_mut(),
      &mut sentinel,
    )
  };
  if status != sys::Status::napi_ok {
    drop(unsafe { Box::from_raw(data) });
    return Err(Error::new(
      Status::from(status),
      "Failed to create External environment cleanup sentinel".to_owned(),
    ));
  }

  let mut sentinel_ref = ptr::null_mut();
  check_status!(
    unsafe { sys::napi_create_reference(env, sentinel, 1, &mut sentinel_ref) },
    "Failed to retain External environment cleanup sentinel"
  )
}

#[cfg(feature = "noop")]
fn register_external_env_cleanup(_env: sys::napi_env, _state: Arc<ExternalEnvState>) -> Result<()> {
  Ok(())
}

#[cfg(not(feature = "noop"))]
fn close_external_env(data: *mut c_void) {
  let state = unsafe { Box::from_raw(data.cast::<Arc<ExternalEnvState>>()) };
  state.close();
  let identity = Arc::as_ptr(&state);
  let _ = EXTERNAL_ENV_STATES.try_with(|states| {
    let mut states = states.borrow_mut();
    if states
      .get(&state.env)
      .is_some_and(|registered| registered.as_ptr() == identity)
    {
      states.remove(&state.env);
    }
  });
}

#[cfg(all(feature = "napi3", not(feature = "noop")))]
unsafe extern "C" fn external_env_cleanup(data: *mut c_void) {
  crate::bindgen_runtime::with_runtime_teardown_guard(|| {
    crate::bindgen_runtime::catch_unwind_safely(|| close_external_env(data));
  });
}

#[cfg(all(not(feature = "napi3"), not(feature = "noop")))]
unsafe extern "C" fn external_env_sentinel_finalize(
  env: sys::napi_env,
  data: *mut c_void,
  _hint: *mut c_void,
) {
  crate::bindgen_runtime::with_runtime_finalizer_guard(env, || {
    crate::bindgen_runtime::catch_unwind_safely(|| close_external_env(data));
  });
}

#[repr(C)]
pub struct External<T: 'static> {
  type_id: TypeId,
  obj: T,
  size_hint: usize,
  pub adjusted_size: i64,
}

#[repr(C)]
struct ExternalControlBlock<T: 'static> {
  external: UnsafeCell<External<T>>,
  env: Arc<ExternalEnvState>,
}

impl<T: 'static> ExternalControlBlock<T> {
  fn new(external: External<T>, env: Arc<ExternalEnvState>) -> Arc<Self> {
    Arc::new(Self {
      external: UnsafeCell::new(external),
      env,
    })
  }
}

impl<T: 'static> TypeName for &External<T> {
  fn type_name() -> &'static str {
    "External"
  }

  fn value_type() -> crate::ValueType {
    crate::ValueType::External
  }
}

impl<T: 'static> TypeName for &mut External<T> {
  fn type_name() -> &'static str {
    "External"
  }

  fn value_type() -> crate::ValueType {
    crate::ValueType::External
  }
}

impl<T: 'static> From<T> for External<T> {
  fn from(t: T) -> Self {
    External::new(t)
  }
}

impl<T: 'static> ValidateNapiValue for &External<T> {}

impl<T: 'static> External<T> {
  pub fn new(value: T) -> Self {
    Self {
      type_id: TypeId::of::<T>(),
      obj: value,
      size_hint: 0,
      adjusted_size: 0,
    }
  }

  pub(crate) unsafe fn from_napi_value_impl(
    env: sys::napi_env,
    napi_val: sys::napi_value,
    unknown_tagged_object: *mut c_void,
  ) -> Result<&'static Self> {
    let control = clone_external_control::<T>(unknown_tagged_object, env, napi_val)?;
    let external = control.external.get();
    Ok(unsafe { &*external })
  }

  /// Turn a raw pointer (from napi) pointing to an External into a mutable reference to the inner object.
  ///
  /// # Safety
  /// The token must come from a live napi-rs External value, and the caller must guarantee
  /// exclusive access to it for the returned reference's lifetime.
  pub unsafe fn inner_from_raw_mut(unknown_tagged_object: *mut c_void) -> Option<&'static mut T> {
    let control = clone_external_control_unchecked::<T>(unknown_tagged_object)?;
    let external = control.external.get();
    Some(unsafe { &mut (*external).obj })
  }

  pub(crate) unsafe fn inner_from_napi_value_mut(
    env: sys::napi_env,
    napi_val: sys::napi_value,
    unknown_tagged_object: *mut c_void,
  ) -> Result<&'static mut T> {
    let control = clone_external_control::<T>(unknown_tagged_object, env, napi_val)?;
    let external = control.external.get();
    Ok(unsafe { &mut (*external).obj })
  }

  /// Turn a raw pointer (from napi) pointing to an External into a reference inner object.
  ///
  /// # Safety
  /// The token must come from a live napi-rs External value that remains rooted for the returned
  /// reference's lifetime.
  pub unsafe fn inner_from_raw(unknown_tagged_object: *mut c_void) -> Option<&'static T> {
    let control = clone_external_control_unchecked::<T>(unknown_tagged_object)?;
    let external = control.external.get();
    Some(unsafe { &(*external).obj })
  }

  /// `size_hint` is a value to tell Node.js GC how much memory is used by this `External` object.
  ///
  /// If getting the exact `size_hint` is difficult, you can provide an approximate value, it's only effect to the GC.
  ///
  /// If your `External` object is not effect to GC, you can use `External::new` instead.
  pub fn new_with_size_hint(value: T, size_hint: usize) -> Self {
    Self {
      type_id: TypeId::of::<T>(),
      obj: value,
      size_hint,
      adjusted_size: 0,
    }
  }

  /// convert `External<T>` to `Unknown`
  pub fn into_unknown(self, env: &Env) -> Result<Unknown<'_>> {
    let napi_value = unsafe { ToNapiValue::to_napi_value(env.0, self)? };
    Ok(unsafe { Unknown::from_raw_unchecked(env.0, napi_value) })
  }

  /// Convert `External<T>` to `JsExternal`
  pub fn into_js_external(self, env: &Env) -> Result<JsExternal<'_>> {
    let napi_value = unsafe { ToNapiValue::to_napi_value(env.0, self)? };
    unsafe { JsExternal::from_napi_value(env.0, napi_value) }
  }

  #[allow(clippy::wrong_self_convention)]
  unsafe fn to_napi_value_impl(
    self,
    env: sys::napi_env,
  ) -> Result<(sys::napi_value, Arc<ExternalControlBlock<T>>)> {
    let mut napi_value = ptr::null_mut();
    #[cfg(not(target_family = "wasm"))]
    let size_hint = self.size_hint as i64;
    let control = ExternalControlBlock::new(self, external_env_state(env)?);
    let token = allocate_external_token()?;
    let token_ptr = ptr::without_provenance_mut::<c_void>(token);
    #[cfg(not(target_family = "wasm"))]
    let size_hint_ptr = Box::into_raw(Box::new(0i64));
    #[cfg(target_family = "wasm")]
    let size_hint_ptr: *mut i64 = ptr::null_mut();
    let status = unsafe {
      sys::napi_create_external(
        env,
        token_ptr,
        Some(raw_finalize_external),
        size_hint_ptr.cast(),
        &mut napi_value,
      )
    };
    if status != sys::Status::napi_ok {
      #[cfg(not(target_family = "wasm"))]
      drop(unsafe { Box::from_raw(size_hint_ptr) });
      return Err(Error::new(
        Status::from(status),
        "Create external value failed".to_owned(),
      ));
    }

    let mut owner_ref = ptr::null_mut();
    let status = unsafe { sys::napi_create_reference(env, napi_value, 0, &mut owner_ref) };
    if status != sys::Status::napi_ok {
      return Err(Error::new(
        Status::from(status),
        "Failed to create External owner reference".to_owned(),
      ));
    }
    if let Err(error) = register_external_provenance(
      token,
      Arc::clone(&control.env),
      owner_ref,
      Arc::clone(&control),
    ) {
      let _ = unsafe { sys::napi_delete_reference(env, owner_ref) };
      return Err(error);
    }

    #[cfg(not(target_family = "wasm"))]
    {
      let mut adjusted_external_memory_size = std::mem::MaybeUninit::new(0);

      if size_hint != 0 {
        let status = unsafe {
          sys::napi_adjust_external_memory(
            env,
            size_hint,
            adjusted_external_memory_size.as_mut_ptr(),
          )
        };
        check_status!(status, "Adjust external memory failed")?;
        unsafe { *size_hint_ptr = size_hint };
      };

      unsafe {
        (*control.external.get()).adjusted_size = adjusted_external_memory_size.assume_init();
      }
    }

    Ok((napi_value, control))
  }
}

unsafe extern "C" fn raw_finalize_external(
  env: sys::napi_env,
  finalize_data: *mut c_void,
  finalize_hint: *mut c_void,
) {
  #[cfg(target_family = "wasm")]
  let _ = finalize_hint;
  crate::bindgen_runtime::with_runtime_finalizer_guard(env, || {
    if !finalize_data.is_null() {
      crate::bindgen_runtime::catch_unwind_safely(|| {
        if let Some(provenance) = unregister_external_provenance(finalize_data) {
          debug_assert_eq!(provenance.env.env, env as EnvId);
          if !provenance.owner_ref.is_null() {
            let status = unsafe { sys::napi_delete_reference(env, provenance.owner_ref) };
            debug_assert!(
              status == sys::Status::napi_ok || status == sys::Status::napi_closing,
              "Deleting External owner reference failed"
            );
          }
          drop(provenance);
        }
      });
    }
    #[cfg(not(target_family = "wasm"))]
    if !finalize_hint.is_null() {
      crate::bindgen_runtime::catch_unwind_safely(|| {
        let size_hint = unsafe { *Box::from_raw(finalize_hint.cast::<i64>()) };
        if size_hint != 0 {
          let mut adjusted = 0i64;
          let status = unsafe { sys::napi_adjust_external_memory(env, -size_hint, &mut adjusted) };
          debug_assert!(
            status == sys::Status::napi_ok,
            "Calling napi_adjust_external_memory failed"
          );
        }
      });
    }
  });
}

impl<T: 'static> FromNapiRef for External<T> {
  unsafe fn from_napi_ref(
    env: sys::napi_env,
    napi_val: sys::napi_value,
  ) -> crate::Result<&'static Self> {
    let mut unknown_tagged_object = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_get_value_external(env, napi_val, &mut unknown_tagged_object) },
      "Failed to get external value"
    )?;
    let control = clone_external_control::<T>(unknown_tagged_object, env, napi_val)?;
    unsafe {
      crate::bindgen_runtime::register_legacy_native_borrow_with_value(
        env,
        napi_val,
        control.external.get(),
        false,
      )
    }?;
    let external = control.external.get();
    Ok(unsafe { &*external })
  }
}

impl<T: 'static> AsRef<T> for External<T> {
  fn as_ref(&self) -> &T {
    &self.obj
  }
}

impl<T: 'static> AsMut<T> for External<T> {
  fn as_mut(&mut self) -> &mut T {
    &mut self.obj
  }
}

impl<T: 'static> Deref for External<T> {
  type Target = T;

  fn deref(&self) -> &Self::Target {
    self.as_ref()
  }
}

impl<T: 'static> DerefMut for External<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    self.as_mut()
  }
}

impl<T: 'static> ToNapiValue for External<T> {
  unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> crate::Result<sys::napi_value> {
    let (napi_value, _) = unsafe { val.to_napi_value_impl(env)? };
    Ok(napi_value)
  }
}

/// `ExternalRef` keeps both the JavaScript `External` and its Rust allocation alive.
///
/// The reference can only be converted back to JavaScript through its owning environment. During
/// environment teardown, the JavaScript reference is closed before finalizers run; the Rust value
/// remains available through immutable `Deref` access until the last `ExternalRef` is dropped.
/// Mutable access is deliberately not provided because multiple `ExternalRef` values can point to
/// the same allocation; wrap `T` in an interior-mutability type when shared mutation is required.
pub struct ExternalRef<T: 'static> {
  control: Arc<ExternalControlBlock<T>>,
  pub(crate) raw: sys::napi_ref,
  pub(crate) env: sys::napi_env,
}

impl<T: 'static> TypeName for ExternalRef<T> {
  fn type_name() -> &'static str {
    "External"
  }

  fn value_type() -> crate::ValueType {
    crate::ValueType::External
  }
}

impl<T: 'static> ValidateNapiValue for ExternalRef<T> {}

impl<T: 'static> Drop for ExternalRef<T> {
  fn drop(&mut self) {
    if !self.control.env.is_open_for(self.env) {
      return;
    }
    check_status_or_throw!(
      self.env,
      unsafe { sys::napi_delete_reference(self.env, self.raw) },
      "Failed to delete reference on external value"
    );
  }
}

impl<T: 'static> ExternalRef<T> {
  pub fn new(env: &Env, value: T) -> Result<Self> {
    let external = External::new(value);
    let mut ref_ptr = ptr::null_mut();
    let (napi_val, control) = unsafe { external.to_napi_value_impl(env.0)? };
    check_status!(
      unsafe { sys::napi_create_reference(env.0, napi_val, 1, &mut ref_ptr) },
      "Failed to create reference on external value"
    )?;
    Ok(ExternalRef {
      control,
      raw: ref_ptr,
      env: env.0,
    })
  }

  /// Get the raw JsExternal value from the reference
  pub fn get_value(&self) -> Result<JsExternal<'_>> {
    self.control.env.ensure_open_for(self.env)?;
    let mut napi_val = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_get_reference_value(self.env, self.raw, &mut napi_val) },
      "Failed to get reference value on external value"
    )?;
    unsafe { JsExternal::from_napi_value(self.env, napi_val) }
  }
}

impl<T: 'static> FromNapiValue for ExternalRef<T> {
  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
    let mut unknown_tagged_object = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_get_value_external(env, napi_val, &mut unknown_tagged_object) },
      "Failed to get external value"
    )?;

    let control = clone_external_control::<T>(unknown_tagged_object, env, napi_val)?;

    let mut ref_ptr = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_create_reference(env, napi_val, 1, &mut ref_ptr) },
      "Failed to create reference on external value"
    )?;

    Ok(ExternalRef {
      control,
      raw: ref_ptr,
      env,
    })
  }
}

impl<T: 'static> ToNapiValue for ExternalRef<T> {
  unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> crate::Result<sys::napi_value> {
    val.control.env.ensure_open_for(env)?;
    let mut value = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_get_reference_value(val.env, val.raw, &mut value) },
      "Failed to get reference value on external value"
    )?;
    Ok(value)
  }
}

impl<T: 'static> ToNapiValue for &ExternalRef<T> {
  unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> crate::Result<sys::napi_value> {
    val.control.env.ensure_open_for(env)?;
    let mut value = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_get_reference_value(val.env, val.raw, &mut value) },
      "Failed to get reference value on external value"
    )?;
    Ok(value)
  }
}

impl<T: 'static> Deref for ExternalRef<T> {
  type Target = T;

  fn deref(&self) -> &Self::Target {
    unsafe { &(*self.control.external.get()).obj }
  }
}

#[cfg(all(test, not(feature = "noop")))]
mod tests {
  use std::{cell::Cell, rc::Rc, sync::Arc};

  use super::{External, ExternalControlBlock, ExternalEnvState, ExternalRef};

  struct DropProbe(Rc<Cell<bool>>);

  impl Drop for DropProbe {
    fn drop(&mut self) {
      self.0.set(true);
    }
  }

  #[test]
  fn external_ref_control_keeps_value_alive_after_js_owner_is_released() {
    let env = 0x1000usize as crate::sys::napi_env;
    let dropped = Rc::new(Cell::new(false));
    let env_state = Arc::new(ExternalEnvState::new(env));
    let control = ExternalControlBlock::new(
      External::new(DropProbe(Rc::clone(&dropped))),
      Arc::clone(&env_state),
    );
    let js_owner = Arc::clone(&control);
    let reference = ExternalRef {
      control,
      raw: std::ptr::null_mut(),
      env,
    };

    drop(js_owner);
    assert!(!dropped.get());
    assert!(!reference.0.get());

    env_state.close();
    drop(reference);
    assert!(dropped.get());
  }

  #[test]
  fn external_ref_environment_rejects_foreign_and_closed_access() {
    let env = 0x1000usize as crate::sys::napi_env;
    let foreign_env = 0x2000usize as crate::sys::napi_env;
    let state = ExternalEnvState::new(env);

    let foreign = state.ensure_open_for(foreign_env).unwrap_err();
    assert_eq!(foreign.status, crate::Status::InvalidArg);
    assert!(foreign.reason.contains("different napi_env"));

    state.close();
    let closed = state.ensure_open_for(env).unwrap_err();
    assert_eq!(closed.status, crate::Status::InvalidArg);
    assert!(closed.reason.contains("owner environment has closed"));
  }
}