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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
use std::any::type_name;
use std::ffi::CString;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::ptr;

use crate::{
  bindgen_runtime::{
    raw_finalize_unchecked, FromNapiValue, MaybeTypeTag, Object, ObjectFinalize, Reference, Result,
    ToNapiValue, TypeName, ValidateNapiValue,
  },
  check_status, sys, Env, JsValue, Property, PropertyAttributes, Value, ValueType,
};

#[derive(Clone, Copy)]
pub struct This<'env, T = Object<'env>> {
  pub object: T,
  _phantom: &'env PhantomData<()>,
}

impl<T> From<T> for This<'_, T> {
  fn from(value: T) -> Self {
    Self {
      object: value,
      _phantom: &PhantomData,
    }
  }
}

impl<T> Deref for This<'_, T> {
  type Target = T;

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

impl<T> DerefMut for This<'_, T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.object
  }
}

impl<'env, T: JsValue<'env>> JsValue<'env> for This<'_, T> {
  fn value(&self) -> Value {
    self.object.value()
  }
}

impl<T: FromNapiValue> FromNapiValue for This<'_, T> {
  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
    Ok(Self {
      object: T::from_napi_value(env, napi_val)?,
      _phantom: &PhantomData,
    })
  }
}

pub struct ClassInstance<'env, T: 'env> {
  reference: Reference<T>,
  _phantom: &'env PhantomData<()>,
}

impl<'env, T: 'env> ClassInstance<'env, T> {
  /// Compatibility constructor for code generated by earlier `napi-derive` releases.
  #[doc(hidden)]
  pub unsafe fn new(value: sys::napi_value, env: sys::napi_env, inner: *mut T) -> Self {
    unsafe { Self::try_new(value, env, inner) }
      .unwrap_or_else(|err| panic!("Failed to create a ClassInstance reference: {err}"))
  }

  #[doc(hidden)]
  pub unsafe fn try_new(
    _value: sys::napi_value,
    env: sys::napi_env,
    inner: *mut T,
  ) -> Result<Self> {
    Ok(Self {
      reference: unsafe { Reference::from_value_ptr(inner.cast(), env)? },
      _phantom: &PhantomData,
    })
  }

  /// Access the native class value while preventing overlapping mutable callbacks.
  pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> Result<R> {
    self.reference.with(f)
  }

  /// Mutably access the native class value while preventing all overlapping callbacks.
  pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
    self.reference.with_mut(f)
  }

  /// Clone the strong JavaScript reference owned by this class instance.
  pub fn clone_reference(&self, env: Env) -> Result<Reference<T>> {
    self.reference.clone(env)
  }

  /// Convert this class instance into its strong JavaScript reference.
  pub fn into_reference(self) -> Reference<T> {
    self.reference
  }

  /// View this class instance as an [`Object`] in the current callback scope.
  pub fn as_object<'scope>(&self, env: &'scope Env) -> Result<Object<'scope>> {
    Ok(Object(
      Value {
        env: env.raw(),
        value: self.reference.get_value(env.raw())?,
        value_type: ValueType::Object,
      },
      PhantomData,
    ))
  }

  /// Assign this `ClassInstance` to another `This` object.
  pub fn assign_to_this<'this, U>(
    &self,
    name: &str,
    this: &mut This<'this, U>,
  ) -> Result<ClassInstance<'this, T>>
  where
    T: 'this,
    U: JsValue<'this>,
  {
    let this_value = this.object.value();
    let env = Env::from_raw(this_value.env);
    let reference = self.reference.clone(env)?;
    let instance_value = reference.get_value(this_value.env)?;
    let name = CString::new(name)?;
    check_status!(
      unsafe {
        sys::napi_set_named_property(
          this_value.env,
          this_value.value,
          name.as_ptr(),
          instance_value,
        )
      },
      "Failed to assign ClassInstance<{}> to this",
      std::any::type_name::<T>()
    )?;
    Ok(ClassInstance {
      reference,
      _phantom: &PhantomData,
    })
  }

  /// Assign this `ClassInstance` to another `This` object with `PropertyAttributes`.
  pub fn assign_to_this_with_attributes<'this, U>(
    &self,
    name: &str,
    attributes: PropertyAttributes,
    this: &mut This<'this, U>,
  ) -> Result<ClassInstance<'this, T>>
  where
    T: 'this,
    U: JsValue<'this>,
  {
    let this_value = this.object.value();
    let env = Env::from_raw(this_value.env);
    let reference = self.reference.clone(env)?;
    let property = Property::new()
      .with_utf8_name(name)?
      .with_napi_value(&env, self)?
      .with_property_attributes(attributes);

    check_status!(
      unsafe {
        sys::napi_define_properties(
          this_value.env,
          this_value.value,
          1,
          [property.raw()].as_ptr(),
        )
      },
      "Failed to define properties on This in `assign_to_this_with_attributes`"
    )?;

    Ok(ClassInstance {
      reference,
      _phantom: &PhantomData,
    })
  }
}

impl<'env, T: 'env> TypeName for ClassInstance<'env, T>
where
  &'env T: TypeName,
{
  fn type_name() -> &'static str {
    type_name::<&T>()
  }

  fn value_type() -> ValueType {
    <&T>::value_type()
  }
}

impl<'env, T: 'env> ValidateNapiValue for ClassInstance<'env, T>
where
  &'env T: ValidateNapiValue,
{
  unsafe fn validate(
    env: sys::napi_env,
    napi_val: sys::napi_value,
  ) -> crate::Result<sys::napi_value> {
    unsafe { <&'env T>::validate(env, napi_val) }
  }
}

impl<'env, T: 'env + MaybeTypeTag> FromNapiValue for ClassInstance<'env, T> {
  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
    let mut value = ptr::null_mut();
    check_status!(
      unsafe { sys::napi_unwrap(env, napi_val, &mut value) },
      "Unwrap value [{}] from class failed",
      type_name::<T>(),
    )?;
    // Reject a wrong-class / prototype-spoofed object before the blind cast.
    // Compiled only on napi8 NATIVE targets (the `T: MaybeTypeTag` bound provides
    // `T::type_tag()` only there; elsewhere this is the pre-tag unchecked cast).
    #[cfg(all(feature = "napi8", not(target_family = "wasm")))]
    unsafe {
      crate::bindgen_runtime::validate_type_tag(env, napi_val, &T::type_tag(), type_name::<T>())?;
    }

    let value = value.cast::<T>();
    Ok(Self {
      reference: unsafe { Reference::from_value_ptr(value.cast(), env)? },
      _phantom: &PhantomData,
    })
  }
}

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

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

pub trait JavaScriptClassExt: Sized {
  fn into_instance(self, env: &Env) -> Result<ClassInstance<'_, Self>>;
  fn into_reference(self, env: Env) -> Result<Reference<Self>>;
  fn instance_of<'env, V: JsValue<'env>>(env: &Env, value: &V) -> Result<bool>;
}

struct FactoryCallGuard {
  previous: bool,
}

impl FactoryCallGuard {
  fn new() -> Self {
    let previous =
      crate::__private::___CALL_FROM_FACTORY.with(|factory_call| factory_call.replace(true));
    Self { previous }
  }
}

impl Drop for FactoryCallGuard {
  fn drop(&mut self) {
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| factory_call.set(self.previous));
  }
}

struct PendingFinalizeCallbacks {
  callbacks: Option<std::sync::Arc<super::value_ref::FinalizeCallbacks>>,
}

impl PendingFinalizeCallbacks {
  fn with_callback(callback: super::value_ref::FinalizeCallback) -> Self {
    // The raw callback-list owner and public references share ownership, while list mutation stays
    // on the JavaScript thread.
    #[allow(clippy::arc_with_non_send_sync)]
    let callbacks = std::sync::Arc::new(super::value_ref::FinalizeCallbacks::new(callback));
    Self {
      callbacks: Some(callbacks),
    }
  }

  fn as_ptr(&self) -> *const super::value_ref::FinalizeCallbacks {
    std::sync::Arc::as_ptr(
      self
        .callbacks
        .as_ref()
        .expect("callbacks already transferred"),
    )
  }

  fn transfer(mut self) {
    let callbacks = self
      .callbacks
      .take()
      .expect("callbacks already transferred");
    let _ = std::sync::Arc::into_raw(callbacks);
  }
}

impl Drop for PendingFinalizeCallbacks {
  fn drop(&mut self) {
    drop(self.callbacks.take());
  }
}

trait NewInstanceOps {
  fn initial_finalize_callback() -> super::value_ref::FinalizeCallback {
    Box::new(|| {})
  }

  unsafe fn get_reference_value(
    env: sys::napi_env,
    ctor_ref: sys::napi_ref,
    ctor: *mut sys::napi_value,
  ) -> sys::napi_status;

  unsafe fn new_instance(
    env: sys::napi_env,
    ctor: sys::napi_value,
    result: *mut sys::napi_value,
  ) -> sys::napi_status;

  unsafe fn wrap(
    env: sys::napi_env,
    instance: sys::napi_value,
    wrapped_value: *mut std::ffi::c_void,
    finalize: sys::napi_finalize,
    object_ref: *mut sys::napi_ref,
  ) -> sys::napi_status;

  unsafe fn delete_reference(env: sys::napi_env, object_ref: sys::napi_ref) -> sys::napi_status {
    unsafe { sys::napi_delete_reference(env, object_ref) }
  }
}

struct NodeApiNewInstanceOps;

impl NewInstanceOps for NodeApiNewInstanceOps {
  unsafe fn get_reference_value(
    env: sys::napi_env,
    ctor_ref: sys::napi_ref,
    ctor: *mut sys::napi_value,
  ) -> sys::napi_status {
    unsafe { sys::napi_get_reference_value(env, ctor_ref, ctor) }
  }

  unsafe fn new_instance(
    env: sys::napi_env,
    ctor: sys::napi_value,
    result: *mut sys::napi_value,
  ) -> sys::napi_status {
    unsafe { sys::napi_new_instance(env, ctor, 0, std::ptr::null_mut(), result) }
  }

  unsafe fn wrap(
    env: sys::napi_env,
    instance: sys::napi_value,
    wrapped_value: *mut std::ffi::c_void,
    finalize: sys::napi_finalize,
    object_ref: *mut sys::napi_ref,
  ) -> sys::napi_status {
    unsafe {
      sys::napi_wrap(
        env,
        instance,
        wrapped_value,
        finalize,
        std::ptr::null_mut(),
        object_ref,
      )
    }
  }
}

unsafe fn construct_instance<T, O: NewInstanceOps>(
  env: sys::napi_env,
  ctor_ref: sys::napi_ref,
) -> Result<sys::napi_value> {
  let mut ctor = std::ptr::null_mut();
  check_status!(
    unsafe { O::get_reference_value(env, ctor_ref, &mut ctor) },
    "Failed to get constructor reference of class `{}`",
    type_name::<T>(),
  )?;

  let mut result = std::ptr::null_mut();
  {
    let _factory_call = FactoryCallGuard::new();
    check_status!(
      unsafe { O::new_instance(env, ctor, &mut result) },
      "Failed to construct class `{}`",
      type_name::<T>(),
    )?;
  }
  Ok(result)
}

unsafe fn wrap_instance<T, O: NewInstanceOps>(
  env: sys::napi_env,
  instance: sys::napi_value,
  wrapped_value: *mut std::ffi::c_void,
  finalize: sys::napi_finalize,
) -> Result<(sys::napi_ref, PendingFinalizeCallbacks)> {
  let finalize_callbacks = PendingFinalizeCallbacks::with_callback(O::initial_finalize_callback());
  let mut object_ref = std::ptr::null_mut();
  check_status!(
    unsafe { O::wrap(env, instance, wrapped_value, finalize, &mut object_ref) },
    "Failed to wrap native object of class `{}`",
    type_name::<T>(),
  )?;
  Ok((object_ref, finalize_callbacks))
}

struct OwnedClassValue<T> {
  raw: *mut T,
  is_zst: bool,
}

impl<T> OwnedClassValue<T> {
  fn new(value: T) -> Self {
    if std::mem::size_of::<T>() == 0 {
      let layout = Self::zst_layout();
      let raw = unsafe { std::alloc::alloc(layout) }.cast::<T>();
      if raw.is_null() {
        std::alloc::handle_alloc_error(layout);
      }
      unsafe { raw.write(value) };
      Self { raw, is_zst: true }
    } else {
      Self {
        raw: Box::into_raw(Box::new(value)),
        is_zst: false,
      }
    }
  }

  unsafe fn from_raw(raw: *mut T) -> Self {
    Self {
      raw,
      is_zst: std::mem::size_of::<T>() == 0,
    }
  }

  fn as_ptr(&self) -> *mut T {
    self.raw
  }

  fn into_raw(mut self) -> *mut T {
    let raw = self.raw;
    self.raw = std::ptr::null_mut();
    raw
  }

  fn zst_layout() -> std::alloc::Layout {
    std::alloc::Layout::from_size_align(1, std::mem::align_of::<T>())
      .expect("class ZST alignment must form a valid allocation layout")
  }
}

impl<T> Drop for OwnedClassValue<T> {
  fn drop(&mut self) {
    if self.raw.is_null() {
      return;
    }
    unsafe {
      if self.is_zst {
        std::ptr::drop_in_place(self.raw);
        std::alloc::dealloc(self.raw.cast(), Self::zst_layout());
      } else {
        drop(Box::from_raw(self.raw));
      }
    }
  }
}

unsafe extern "C" fn raw_finalize_owned_class<T: ObjectFinalize, O: NewInstanceOps>(
  env: sys::napi_env,
  finalize_data: *mut std::ffi::c_void,
  _finalize_hint: *mut std::ffi::c_void,
) {
  let data = unsafe { OwnedClassValue::<T>::from_raw(finalize_data.cast()) };
  let data_ptr = data.as_ptr();
  unsafe {
    crate::bindgen_runtime::finalize_object_with(
      env,
      data_ptr,
      finalize_data,
      |env, object_ref| O::delete_reference(env, object_ref),
      move || drop(data),
    );
  }
}

unsafe fn new_instance_with_owned_value_and_ops<T: ObjectFinalize, O: NewInstanceOps>(
  env: sys::napi_env,
  value: T,
  ctor_ref: sys::napi_ref,
) -> Result<(sys::napi_value, *mut T)> {
  let result = unsafe { construct_instance::<T, O>(env, ctor_ref)? };
  let wrapped_value = unsafe { wrap_owned_class_value_with_ops::<T, O>(env, result, value)? };
  Ok((result, wrapped_value))
}

unsafe fn wrap_owned_class_value_with_ops<T: ObjectFinalize, O: NewInstanceOps>(
  env: sys::napi_env,
  instance: sys::napi_value,
  value: T,
) -> Result<*mut T> {
  let value = OwnedClassValue::new(value);
  let wrapped_value = value.as_ptr();
  let (object_ref, finalize_callbacks) = unsafe {
    wrap_instance::<T, O>(
      env,
      instance,
      wrapped_value.cast(),
      Some(raw_finalize_owned_class::<T, O>),
    )?
  };

  // `napi_wrap` now owns the native value. Disarm its rollback guard before registering
  // reference metadata so an unexpected panic cannot double-drop the value.
  let wrapped_value = value.into_raw();
  let finalize_callbacks_ptr = finalize_callbacks.as_ptr();
  super::value_ref::add_ref(
    env,
    wrapped_value.cast(),
    wrapped_value.cast(),
    object_ref,
    finalize_callbacks_ptr,
  );
  finalize_callbacks.transfer();
  Ok(wrapped_value)
}

/// Wraps an owned native class value in an existing JavaScript instance.
///
/// # Safety
///
/// `env` and `instance` must belong to the current JavaScript environment, and `instance` must not
/// already wrap another native value.
#[doc(hidden)]
pub unsafe fn wrap_owned_class_value<T: ObjectFinalize + MaybeTypeTag>(
  env: sys::napi_env,
  instance: sys::napi_value,
  value: T,
) -> Result<*mut T> {
  let wrapped_value =
    unsafe { wrap_owned_class_value_with_ops::<T, NodeApiNewInstanceOps>(env, instance, value)? };

  // Stamp the class type tag AFTER the wrap+`add_ref`+`transfer` above have fully
  // registered the object (so a tag failure cannot leak it — GC reclaims a fully
  // registered object). This is pin's owned-value analog of the third wrap site
  // main tagged in `new_instance` (#3405); `_construct`/`_factory` reach it too,
  // and the derive codegen drives it via `new_instance_with_owned_value`. Tagging
  // in the public wrapper (not the `*_with_ops` internals) keeps those internals
  // usable by non-`#[napi]` types. Compiled only on napi8 NATIVE targets
  // (`T: MaybeTypeTag` provides `T::type_tag()` only there; no-op otherwise).
  #[cfg(all(feature = "napi8", not(target_family = "wasm")))]
  unsafe {
    crate::bindgen_runtime::tag_object(env, instance, &T::type_tag())?;
  }

  Ok(wrapped_value)
}

/// Creates a JavaScript class instance while retaining ownership of `value` until `napi_wrap`
/// succeeds.
///
/// # Safety
///
/// `env` and `ctor_ref` must belong to the current JavaScript environment.
#[doc(hidden)]
pub unsafe fn new_instance_with_owned_value<T: ObjectFinalize + MaybeTypeTag>(
  env: sys::napi_env,
  value: T,
  ctor_ref: sys::napi_ref,
) -> Result<(sys::napi_value, *mut T)> {
  let (result, wrapped_value) = unsafe {
    new_instance_with_owned_value_and_ops::<T, NodeApiNewInstanceOps>(env, value, ctor_ref)?
  };

  // Stamp the class type tag AFTER the instance is fully registered (see
  // `wrap_owned_class_value`). This is the by-value / factory / `into_instance`
  // wrap site the derive codegen calls; tagging here (not in the `*_with_ops`
  // internals) keeps the internals usable by non-`#[napi]` types. napi8 NATIVE
  // only (`T: MaybeTypeTag` provides `T::type_tag()` there; no-op otherwise).
  #[cfg(all(feature = "napi8", not(target_family = "wasm")))]
  unsafe {
    crate::bindgen_runtime::tag_object(env, result, &T::type_tag())?;
  }

  Ok((result, wrapped_value))
}

/// # Safety
///
/// create instance of class
#[doc(hidden)]
pub unsafe fn new_instance<T: 'static + ObjectFinalize + MaybeTypeTag>(
  env: sys::napi_env,
  wrapped_value: *mut std::ffi::c_void,
  ctor_ref: sys::napi_ref,
) -> Result<sys::napi_value> {
  let result = unsafe { construct_instance::<T, NodeApiNewInstanceOps>(env, ctor_ref)? };
  let (object_ref, finalize_callbacks) = unsafe {
    wrap_instance::<T, NodeApiNewInstanceOps>(
      env,
      result,
      wrapped_value,
      Some(raw_finalize_unchecked::<T>),
    )?
  };
  let finalize_callbacks_ptr = finalize_callbacks.as_ptr();
  Reference::<T>::add_ref(
    env,
    wrapped_value,
    (wrapped_value, object_ref, finalize_callbacks_ptr),
  );
  finalize_callbacks.transfer();

  // Stamp the type tag AFTER `add_ref`/`transfer` has adopted the Arc + napi_ref
  // into `REFERENCE_MAP`, so a tag failure cannot leak them: the object is fully
  // registered and GC reclaims value_ref + object_ref + Arc (see
  // `CallbackInfo::_construct`). Compiled only on napi8 NATIVE targets (the
  // `T: MaybeTypeTag` bound provides `T::type_tag()` only there).
  #[cfg(all(feature = "napi8", not(target_family = "wasm")))]
  unsafe {
    crate::bindgen_runtime::tag_object(env, result, &T::type_tag())?;
  }

  Ok(result)
}

#[cfg(test)]
mod tests {
  use std::{
    cell::Cell,
    sync::{
      atomic::{AtomicUsize, Ordering},
      Arc,
    },
  };

  use super::*;

  struct DropCounter(Arc<AtomicUsize>);

  impl Drop for DropCounter {
    fn drop(&mut self) {
      self.0.fetch_add(1, Ordering::SeqCst);
    }
  }

  impl ObjectFinalize for DropCounter {}

  static WRAP_FAILURE_CALLBACK_DROPS: AtomicUsize = AtomicUsize::new(0);
  static SUCCESS_VALUE_DROPS: AtomicUsize = AtomicUsize::new(0);
  static SUCCESS_FINALIZE_CALLS: AtomicUsize = AtomicUsize::new(0);
  static SUCCESS_CALLBACK_CALLS: AtomicUsize = AtomicUsize::new(0);
  static SUCCESS_REFERENCE_DELETES: AtomicUsize = AtomicUsize::new(0);

  thread_local! {
    static SUCCESS_FINALIZER: Cell<sys::napi_finalize> = const { Cell::new(None) };
  }

  const SUCCESS_OBJECT_REF: usize = 0x1001;

  struct StaticDropCounter(&'static AtomicUsize);

  impl Drop for StaticDropCounter {
    fn drop(&mut self) {
      self.0.fetch_add(1, Ordering::SeqCst);
    }
  }

  struct EndToEndValue;

  impl Drop for EndToEndValue {
    fn drop(&mut self) {
      SUCCESS_VALUE_DROPS.fetch_add(1, Ordering::SeqCst);
    }
  }

  impl ObjectFinalize for EndToEndValue {
    fn finalize(&mut self, _env: Env) -> Result<()> {
      SUCCESS_FINALIZE_CALLS.fetch_add(1, Ordering::SeqCst);
      Ok(())
    }
  }

  struct ConstructionFailureOps;

  impl NewInstanceOps for ConstructionFailureOps {
    unsafe fn get_reference_value(
      _env: sys::napi_env,
      _ctor_ref: sys::napi_ref,
      ctor: *mut sys::napi_value,
    ) -> sys::napi_status {
      unsafe { ctor.write(std::ptr::NonNull::dangling().as_ptr()) };
      sys::Status::napi_ok
    }

    unsafe fn new_instance(
      _env: sys::napi_env,
      _ctor: sys::napi_value,
      _result: *mut sys::napi_value,
    ) -> sys::napi_status {
      sys::Status::napi_generic_failure
    }

    unsafe fn wrap(
      _env: sys::napi_env,
      _instance: sys::napi_value,
      _wrapped_value: *mut std::ffi::c_void,
      _finalize: sys::napi_finalize,
      _object_ref: *mut sys::napi_ref,
    ) -> sys::napi_status {
      panic!("wrap must not run after construction failure")
    }
  }

  struct WrapFailureOps;

  impl NewInstanceOps for WrapFailureOps {
    fn initial_finalize_callback() -> super::super::value_ref::FinalizeCallback {
      let mut callback_drop = Some(StaticDropCounter(&WRAP_FAILURE_CALLBACK_DROPS));
      Box::new(move || drop(callback_drop.take()))
    }

    unsafe fn get_reference_value(
      _env: sys::napi_env,
      _ctor_ref: sys::napi_ref,
      ctor: *mut sys::napi_value,
    ) -> sys::napi_status {
      unsafe { ctor.write(std::ptr::NonNull::dangling().as_ptr()) };
      sys::Status::napi_ok
    }

    unsafe fn new_instance(
      _env: sys::napi_env,
      _ctor: sys::napi_value,
      result: *mut sys::napi_value,
    ) -> sys::napi_status {
      unsafe { result.write(std::ptr::NonNull::dangling().as_ptr()) };
      sys::Status::napi_ok
    }

    unsafe fn wrap(
      _env: sys::napi_env,
      _instance: sys::napi_value,
      _wrapped_value: *mut std::ffi::c_void,
      _finalize: sys::napi_finalize,
      _object_ref: *mut sys::napi_ref,
    ) -> sys::napi_status {
      sys::Status::napi_generic_failure
    }
  }

  struct SuccessfulOps;

  impl NewInstanceOps for SuccessfulOps {
    fn initial_finalize_callback() -> super::super::value_ref::FinalizeCallback {
      Box::new(|| {
        SUCCESS_CALLBACK_CALLS.fetch_add(1, Ordering::SeqCst);
      })
    }

    unsafe fn get_reference_value(
      _env: sys::napi_env,
      _ctor_ref: sys::napi_ref,
      ctor: *mut sys::napi_value,
    ) -> sys::napi_status {
      unsafe { ctor.write(0x1002usize as sys::napi_value) };
      sys::Status::napi_ok
    }

    unsafe fn new_instance(
      _env: sys::napi_env,
      _ctor: sys::napi_value,
      result: *mut sys::napi_value,
    ) -> sys::napi_status {
      unsafe { result.write(0x1003usize as sys::napi_value) };
      sys::Status::napi_ok
    }

    unsafe fn wrap(
      _env: sys::napi_env,
      _instance: sys::napi_value,
      _wrapped_value: *mut std::ffi::c_void,
      finalize: sys::napi_finalize,
      object_ref: *mut sys::napi_ref,
    ) -> sys::napi_status {
      assert!(finalize.is_some());
      SUCCESS_FINALIZER.with(|slot| slot.set(finalize));
      unsafe { object_ref.write(SUCCESS_OBJECT_REF as sys::napi_ref) };
      sys::Status::napi_ok
    }

    unsafe fn delete_reference(_env: sys::napi_env, object_ref: sys::napi_ref) -> sys::napi_status {
      assert_eq!(object_ref as usize, SUCCESS_OBJECT_REF);
      SUCCESS_REFERENCE_DELETES.fetch_add(1, Ordering::SeqCst);
      sys::Status::napi_ok
    }
  }

  #[test]
  fn factory_call_guard_restores_nested_prior_true_state() {
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| factory_call.set(true));

    {
      let _outer = FactoryCallGuard::new();
      crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(factory_call.get()));
      {
        let _inner = FactoryCallGuard::new();
        crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(factory_call.get()));
      }
      crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(factory_call.get()));
    }

    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| {
      assert!(factory_call.get());
      factory_call.set(false);
    });
  }

  #[test]
  fn factory_call_guard_restores_state_during_unwind() {
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| factory_call.set(false));

    let result = std::panic::catch_unwind(|| {
      let _guard = FactoryCallGuard::new();
      crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(factory_call.get()));
      panic!("factory construction panic");
    });

    assert!(result.is_err());
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(!factory_call.get()));
  }

  #[test]
  fn owned_class_value_is_dropped_when_construction_fails_and_factory_state_is_restored() {
    let drops = Arc::new(AtomicUsize::new(0));
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| factory_call.set(false));

    let result = unsafe {
      new_instance_with_owned_value_and_ops::<DropCounter, ConstructionFailureOps>(
        std::ptr::null_mut(),
        DropCounter(drops.clone()),
        std::ptr::null_mut(),
      )
    };

    assert!(result.is_err());
    assert_eq!(drops.load(Ordering::SeqCst), 1);
    crate::__private::___CALL_FROM_FACTORY.with(|factory_call| assert!(!factory_call.get()));
  }

  #[test]
  fn owned_class_value_and_finalize_callbacks_are_reclaimed_when_wrap_fails() {
    WRAP_FAILURE_CALLBACK_DROPS.store(0, Ordering::SeqCst);
    let value_drops = Arc::new(AtomicUsize::new(0));

    let result = unsafe {
      new_instance_with_owned_value_and_ops::<DropCounter, WrapFailureOps>(
        std::ptr::null_mut(),
        DropCounter(value_drops.clone()),
        std::ptr::null_mut(),
      )
    };

    assert!(result.is_err());
    assert_eq!(value_drops.load(Ordering::SeqCst), 1);
    assert_eq!(WRAP_FAILURE_CALLBACK_DROPS.load(Ordering::SeqCst), 1);
  }

  #[test]
  fn successful_owned_class_conversion_finalizes_all_transferred_state_once() {
    SUCCESS_VALUE_DROPS.store(0, Ordering::SeqCst);
    SUCCESS_FINALIZE_CALLS.store(0, Ordering::SeqCst);
    SUCCESS_CALLBACK_CALLS.store(0, Ordering::SeqCst);
    SUCCESS_REFERENCE_DELETES.store(0, Ordering::SeqCst);
    SUCCESS_FINALIZER.with(|slot| slot.set(None));
    let env = 0x1004usize as sys::napi_env;

    let (_, wrapped_value) = unsafe {
      new_instance_with_owned_value_and_ops::<EndToEndValue, SuccessfulOps>(
        env,
        EndToEndValue,
        0x1005usize as sys::napi_ref,
      )
    }
    .expect("the fake Node-API operations must complete");

    assert_eq!(SUCCESS_VALUE_DROPS.load(Ordering::SeqCst), 0);
    assert_eq!(SUCCESS_FINALIZE_CALLS.load(Ordering::SeqCst), 0);
    assert_eq!(SUCCESS_CALLBACK_CALLS.load(Ordering::SeqCst), 0);
    assert_eq!(SUCCESS_REFERENCE_DELETES.load(Ordering::SeqCst), 0);
    assert!(super::super::value_ref::REFERENCE_MAP.with(|references| {
      references.borrow_mut(|references| references.contains_key(&wrapped_value.cast()))
    }));

    let finalize = SUCCESS_FINALIZER
      .with(Cell::take)
      .expect("napi_wrap must receive the owned-value raw finalizer");
    unsafe { finalize(env, wrapped_value.cast(), std::ptr::null_mut()) };

    assert_eq!(SUCCESS_FINALIZE_CALLS.load(Ordering::SeqCst), 1);
    assert_eq!(SUCCESS_VALUE_DROPS.load(Ordering::SeqCst), 1);
    assert_eq!(SUCCESS_CALLBACK_CALLS.load(Ordering::SeqCst), 1);
    assert_eq!(SUCCESS_REFERENCE_DELETES.load(Ordering::SeqCst), 1);
    assert!(!super::super::value_ref::REFERENCE_MAP.with(|references| {
      references.borrow_mut(|references| references.contains_key(&wrapped_value.cast()))
    }));
  }

  #[test]
  fn transferred_owned_class_value_is_finalized_exactly_once() {
    let drops = Arc::new(AtomicUsize::new(0));
    let value = OwnedClassValue::new(DropCounter(drops.clone()));
    let raw = value.into_raw();

    assert_eq!(drops.load(Ordering::SeqCst), 0);
    let value = unsafe { OwnedClassValue::from_raw(raw) };
    assert_eq!(value.as_ptr(), raw);
    assert_eq!(drops.load(Ordering::SeqCst), 0);
    drop(value);
    assert_eq!(drops.load(Ordering::SeqCst), 1);
  }

  static ZST_DROPS: AtomicUsize = AtomicUsize::new(0);

  #[repr(align(64))]
  struct AlignedZst;

  impl Drop for AlignedZst {
    fn drop(&mut self) {
      ZST_DROPS.fetch_add(1, Ordering::SeqCst);
    }
  }

  #[test]
  fn owned_zst_class_values_have_unique_aligned_storage_and_drop_once() {
    ZST_DROPS.store(0, Ordering::SeqCst);
    let first = OwnedClassValue::new(AlignedZst);
    let second = OwnedClassValue::new(AlignedZst);

    assert_ne!(first.as_ptr(), second.as_ptr());
    assert_eq!(
      first.as_ptr() as usize % std::mem::align_of::<AlignedZst>(),
      0
    );
    assert_eq!(
      second.as_ptr() as usize % std::mem::align_of::<AlignedZst>(),
      0
    );

    drop(first);
    drop(second);
    assert_eq!(ZST_DROPS.load(Ordering::SeqCst), 2);
  }
}