1use std::mem::MaybeUninit;
2
3use super::{
4 bindings as napi,
5 raw::{Env, Local},
6};
7
8pub(crate) struct HandleScope {
9 env: Env,
10 scope: napi::HandleScope,
11}
12
13impl HandleScope {
14 pub(crate) unsafe fn new(env: Env) -> Self {
15 let mut scope = MaybeUninit::uninit();
16
17 napi::open_handle_scope(env, scope.as_mut_ptr()).unwrap();
18
19 Self {
20 env,
21 scope: scope.assume_init(),
22 }
23 }
24}
25
26impl Drop for HandleScope {
27 fn drop(&mut self) {
28 unsafe {
29 let _status = napi::close_handle_scope(self.env, self.scope);
30
31 debug_assert_eq!(_status, Ok(()),);
32 }
33 }
34}
35
36pub(crate) struct EscapableHandleScope {
37 env: Env,
38 scope: napi::EscapableHandleScope,
39}
40
41impl EscapableHandleScope {
42 pub(crate) unsafe fn new(env: Env) -> Self {
43 let mut scope = MaybeUninit::uninit();
44
45 napi::open_escapable_handle_scope(env, scope.as_mut_ptr()).unwrap();
46
47 Self {
48 env,
49 scope: scope.assume_init(),
50 }
51 }
52
53 pub(crate) unsafe fn escape(&self, value: napi::Value) -> napi::Value {
54 let mut escapee = MaybeUninit::uninit();
55
56 napi::escape_handle(self.env, self.scope, value, escapee.as_mut_ptr()).unwrap();
57
58 escapee.assume_init()
59 }
60}
61
62impl Drop for EscapableHandleScope {
63 fn drop(&mut self) {
64 unsafe {
65 let _status = napi::close_escapable_handle_scope(self.env, self.scope);
66
67 debug_assert_eq!(_status, Ok(()),);
68 }
69 }
70}
71
72pub unsafe fn get_global(env: Env, out: &mut Local) {
73 super::get_global(env, out as *mut _).unwrap();
74}