neon_runtime/nan/
scope.rs

1//! Facilities for working with `v8::HandleScope`s and `v8::EscapableHandleScope`s.
2
3use crate::raw::{EscapableHandleScope, HandleScope, InheritedHandleScope, Isolate};
4
5pub trait Root {
6    /// # Safety
7    /// Allocates an uninitialized scope. See `enter` and `exit`.
8    unsafe fn allocate() -> Self;
9    /// # Safety
10    /// Must be called exactly once after creating a `Root` and before usage
11    unsafe fn enter(&mut self, _: Isolate);
12    /// # Safety
13    /// Must be called exactly once, if and only if `enter` succeeds
14    unsafe fn exit(&mut self, _: Isolate);
15}
16
17impl Root for HandleScope {
18    unsafe fn allocate() -> Self {
19        HandleScope::new()
20    }
21    unsafe fn enter(&mut self, isolate: Isolate) {
22        enter(self, isolate)
23    }
24    unsafe fn exit(&mut self, _: Isolate) {
25        exit(self)
26    }
27}
28
29impl Root for EscapableHandleScope {
30    unsafe fn allocate() -> Self {
31        EscapableHandleScope::new()
32    }
33    unsafe fn enter(&mut self, isolate: Isolate) {
34        enter_escapable(self, isolate)
35    }
36    unsafe fn exit(&mut self, _: Isolate) {
37        exit_escapable(self)
38    }
39}
40
41impl Root for InheritedHandleScope {
42    unsafe fn allocate() -> Self {
43        InheritedHandleScope
44    }
45    unsafe fn enter(&mut self, _: Isolate) {}
46    unsafe fn exit(&mut self, _: Isolate) {}
47}
48
49/// Mutates the `out` argument provided to refer to the newly escaped `v8::Local` value.
50pub use neon_sys::Neon_Scope_Escape as escape;
51
52/// Creates a `v8::EscapableHandleScope` and calls the `callback` provided with the argument
53/// signature `(out, parent_scope, &v8_scope, closure)`.
54pub use neon_sys::Neon_Scope_Chained as chained;
55
56/// Creates a `v8::HandleScope` and calls the `callback` provided with the argument signature
57/// `(out, realm, closure)`.
58pub use neon_sys::Neon_Scope_Nested as nested;
59
60/// Instantiates a new `v8::HandleScope`.
61pub use neon_sys::Neon_Scope_Enter as enter;
62
63/// Destructs a `v8::HandleScope`.
64pub use neon_sys::Neon_Scope_Exit as exit;
65
66/// Instantiates a new `v8::HandleScope`.
67pub use neon_sys::Neon_Scope_Enter_Escapable as enter_escapable;
68
69/// Destructs a `v8::HandleScope`.
70pub use neon_sys::Neon_Scope_Exit_Escapable as exit_escapable;
71
72/// Gets the size of a `v8::HandleScope`.
73pub use neon_sys::Neon_Scope_Sizeof as size;
74
75/// Gets the alignment requirement of a `v8::HandleScope`.
76pub use neon_sys::Neon_Scope_Alignof as alignment;
77
78/// Gets the size of a `v8::EscapableHandleScope`.
79pub use neon_sys::Neon_Scope_SizeofEscapable as escapable_size;
80
81/// Gets the alignment requirement of a `v8::EscapableHandleScope`.
82pub use neon_sys::Neon_Scope_AlignofEscapable as escapable_alignment;
83
84/// Mutates the `out` argument provided to refer to the `v8::Local` value of the `global`
85/// object
86pub use neon_sys::Neon_Scope_GetGlobal as get_global;