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
//! Facilities for working with `v8::HandleScope`s and `v8::EscapableHandleScope`s.

use std::os::raw::c_void;
use raw::{HandleScope, EscapableHandleScope, InheritedHandleScope, Local, Isolate};

pub trait Root {
    unsafe fn allocate() -> Self;
    unsafe fn enter(&mut self, *mut Isolate);
    unsafe fn exit(&mut self);
}

impl Root for HandleScope {
    unsafe fn allocate() -> Self { HandleScope::new() }
    unsafe fn enter(&mut self, isolate: *mut Isolate) {
        enter(self, isolate)
    }
    unsafe fn exit(&mut self) {
        exit(self)
    }
}

impl Root for EscapableHandleScope {
    unsafe fn allocate() -> Self { EscapableHandleScope::new() }
    unsafe fn enter(&mut self, isolate: *mut Isolate) {
        enter_escapable(self, isolate)
    }
    unsafe fn exit(&mut self) {
        exit_escapable(self)
    }
}

impl Root for InheritedHandleScope {
    unsafe fn allocate() -> Self { InheritedHandleScope }
    unsafe fn enter(&mut self, _: *mut Isolate) { }
    unsafe fn exit(&mut self) { }
}

extern "C" {

    /// Mutates the `out` argument provided to refer to the newly escaped `v8::Local` value.
    #[link_name = "Neon_Scope_Escape"]
    pub fn escape(out: &mut Local, scope: *mut EscapableHandleScope, value: Local);

    /// Creates a `v8::EscapableHandleScope` and calls the `callback` provided with the argument
    /// signature `(out, parent_scope, &v8_scope, closure)`.
    #[link_name = "Neon_Scope_Chained"]
    pub fn chained(out: *mut c_void, closure: *mut c_void, callback: extern fn(&mut c_void, *mut c_void, *mut c_void, *mut c_void), parent_scope: *mut c_void);

    /// Creates a `v8::HandleScope` and calls the `callback` provided with the argument signature
    /// `(out, realm, closure)`.
    #[link_name = "Neon_Scope_Nested"]
    pub fn nested(out: *mut c_void, closure: *mut c_void, callback: extern fn(&mut c_void, *mut c_void, *mut c_void), realm: *mut c_void);

    /// Instantiates a new `v8::HandleScope`.
    #[link_name = "Neon_Scope_Enter"]
    pub fn enter(scope: &mut HandleScope, isolate: *mut c_void);

    /// Destructs a `v8::HandleScope`.
    #[link_name = "Neon_Scope_Exit"]
    pub fn exit(scope: &mut HandleScope);

    /// Instantiates a new `v8::HandleScope`.
    #[link_name = "Neon_Scope_Enter_Escapable"]
    pub fn enter_escapable(scope: &mut EscapableHandleScope, isolate: *mut c_void);

    /// Destructs a `v8::HandleScope`.
    #[link_name = "Neon_Scope_Exit_Escapable"]
    pub fn exit_escapable(scope: &mut EscapableHandleScope);

    /// Gets the size of a `v8::HandleScope`.
    #[link_name = "Neon_Scope_Sizeof"]
    pub fn size() -> usize;

    /// Gets the alignment requirement of a `v8::HandleScope`.
    #[link_name = "Neon_Scope_Alignof"]
    pub fn alignment() -> usize;

    /// Gets the size of a `v8::EscapableHandleScope`.
    #[link_name = "Neon_Scope_SizeofEscapable"]
    pub fn escapable_size() -> usize;

    /// Gets the alignment requirement of a `v8::EscapableHandleScope`.
    #[link_name = "Neon_Scope_AlignofEscapable"]
    pub fn escapable_alignment() -> usize;

    /// Mutates the `out` argument provided to refer to the `v8::Local` value of the `global`
    /// object
    #[link_name = "Neon_Scope_GetGlobal"]
    pub fn get_global(isolate: *mut c_void, out: &mut Local);

}