1 //! Facilities for working with `v8::HandleScope`s and `v8::EscapableHandleScope`s.
2 
3 use crate::raw::{EscapableHandleScope, HandleScope, InheritedHandleScope, Isolate};
4 
5 pub trait Root {
6     /// # Safety
7     /// Allocates an uninitialized scope. See `enter` and `exit`.
allocate() -> Self8     unsafe fn allocate() -> Self;
9     /// # Safety
10     /// Must be called exactly once after creating a `Root` and before usage
enter(&mut self, _: Isolate)11     unsafe fn enter(&mut self, _: Isolate);
12     /// # Safety
13     /// Must be called exactly once, if and only if `enter` succeeds
exit(&mut self, _: Isolate)14     unsafe fn exit(&mut self, _: Isolate);
15 }
16 
17 impl Root for HandleScope {
allocate() -> Self18     unsafe fn allocate() -> Self {
19         HandleScope::new()
20     }
enter(&mut self, isolate: Isolate)21     unsafe fn enter(&mut self, isolate: Isolate) {
22         enter(self, isolate)
23     }
exit(&mut self, _: Isolate)24     unsafe fn exit(&mut self, _: Isolate) {
25         exit(self)
26     }
27 }
28 
29 impl Root for EscapableHandleScope {
allocate() -> Self30     unsafe fn allocate() -> Self {
31         EscapableHandleScope::new()
32     }
enter(&mut self, isolate: Isolate)33     unsafe fn enter(&mut self, isolate: Isolate) {
34         enter_escapable(self, isolate)
35     }
exit(&mut self, _: Isolate)36     unsafe fn exit(&mut self, _: Isolate) {
37         exit_escapable(self)
38     }
39 }
40 
41 impl Root for InheritedHandleScope {
allocate() -> Self42     unsafe fn allocate() -> Self {
43         InheritedHandleScope
44     }
enter(&mut self, _: Isolate)45     unsafe fn enter(&mut self, _: Isolate) {}
exit(&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.
50 pub 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)`.
54 pub 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)`.
58 pub use neon_sys::Neon_Scope_Nested as nested;
59 
60 /// Instantiates a new `v8::HandleScope`.
61 pub use neon_sys::Neon_Scope_Enter as enter;
62 
63 /// Destructs a `v8::HandleScope`.
64 pub use neon_sys::Neon_Scope_Exit as exit;
65 
66 /// Instantiates a new `v8::HandleScope`.
67 pub use neon_sys::Neon_Scope_Enter_Escapable as enter_escapable;
68 
69 /// Destructs a `v8::HandleScope`.
70 pub use neon_sys::Neon_Scope_Exit_Escapable as exit_escapable;
71 
72 /// Gets the size of a `v8::HandleScope`.
73 pub use neon_sys::Neon_Scope_Sizeof as size;
74 
75 /// Gets the alignment requirement of a `v8::HandleScope`.
76 pub use neon_sys::Neon_Scope_Alignof as alignment;
77 
78 /// Gets the size of a `v8::EscapableHandleScope`.
79 pub use neon_sys::Neon_Scope_SizeofEscapable as escapable_size;
80 
81 /// Gets the alignment requirement of a `v8::EscapableHandleScope`.
82 pub 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
86 pub use neon_sys::Neon_Scope_GetGlobal as get_global;
87