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
use std::ffi::c_void;
use std::marker::PhantomData;
#[cfg(feature = "napi-6")]
use std::sync::Arc;
#[cfg(feature = "napi-6")]
use neon_runtime::tsfn::ThreadsafeFunction;
use neon_runtime::{raw, reference};
use crate::context::Context;
use crate::handle::Handle;
#[cfg(feature = "napi-6")]
use crate::lifecycle::{DropData, InstanceData};
use crate::object::Object;
use crate::types::boxed::Finalize;
#[repr(transparent)]
#[derive(Clone)]
pub(crate) struct NapiRef(*mut c_void);
impl NapiRef {
pub(crate) unsafe fn unref(self, env: raw::Env) {
reference::unreference(env, self.0.cast());
}
}
unsafe impl Send for NapiRef {}
unsafe impl Sync for NapiRef {}
pub struct Root<T> {
internal: Option<NapiRef>,
#[cfg(feature = "napi-6")]
drop_queue: Arc<ThreadsafeFunction<DropData>>,
_phantom: PhantomData<T>,
}
impl<T> std::fmt::Debug for Root<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Root<{}>", std::any::type_name::<T>())
}
}
unsafe impl<T> Send for Root<T> {}
unsafe impl<T> Sync for Root<T> {}
impl<T: Object> Root<T> {
pub fn new<'a, C: Context<'a>>(cx: &mut C, value: &T) -> Self {
let env = cx.env().to_raw();
let internal = unsafe { reference::new(env, value.to_raw()) };
Self {
internal: Some(NapiRef(internal as *mut _)),
#[cfg(feature = "napi-6")]
drop_queue: InstanceData::drop_queue(cx),
_phantom: PhantomData,
}
}
pub fn clone<'a, C: Context<'a>>(&self, cx: &mut C) -> Self {
let env = cx.env();
let internal = self.as_napi_ref().0 as *mut _;
unsafe {
reference::reference(env.to_raw(), internal);
};
Self {
internal: self.internal.clone(),
#[cfg(feature = "napi-6")]
drop_queue: Arc::clone(&self.drop_queue),
_phantom: PhantomData,
}
}
pub fn drop<'a, C: Context<'a>>(self, cx: &mut C) {
let env = cx.env().to_raw();
unsafe {
self.into_napi_ref().unref(env);
}
}
pub fn into_inner<'a, C: Context<'a>>(self, cx: &mut C) -> Handle<'a, T> {
let env = cx.env();
let internal = self.into_napi_ref();
let local = unsafe { reference::get(env.to_raw(), internal.0.cast()) };
unsafe {
internal.unref(env.to_raw());
}
Handle::new_internal(T::from_raw(env, local))
}
pub fn to_inner<'a, C: Context<'a>>(&self, cx: &mut C) -> Handle<'a, T> {
let env = cx.env();
let local = unsafe { reference::get(env.to_raw(), self.as_napi_ref().0 as *mut _) };
Handle::new_internal(T::from_raw(env, local))
}
fn as_napi_ref(&self) -> &NapiRef {
self.internal
.as_ref()
.unwrap()
}
fn into_napi_ref(mut self) -> NapiRef {
self.internal
.take()
.unwrap()
}
}
impl<T: Object> Finalize for Root<T> {
fn finalize<'a, C: Context<'a>>(self, cx: &mut C) {
self.drop(cx);
}
}
impl<T> Drop for Root<T> {
#[cfg(not(feature = "napi-6"))]
fn drop(&mut self) {
if self.internal.is_none() {
return;
}
if std::thread::panicking() {
eprintln!("Warning: neon::handle::Root leaked during a panic");
return;
}
if let Ok(true) = crate::context::internal::IS_RUNNING.try_with(|v| *v.borrow()) {
panic!("Must call `into_inner` or `drop` on `neon::handle::Root`");
}
}
#[cfg(feature = "napi-6")]
fn drop(&mut self) {
if let Some(internal) = self.internal.take() {
let _ = self.drop_queue.call(DropData::Ref(internal), None);
}
}
}