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
use crate::isolate::Isolate;
use crate::Context;
use crate::Local;
use crate::Object;
use crate::ObjectTemplate;
use crate::ToLocal;
use crate::Value;
use std::ptr::null;
extern "C" {
fn v8__Context__New(
isolate: *mut Isolate,
templ: *const ObjectTemplate,
global_object: *const Value,
) -> *const Context;
fn v8__Context__Enter(this: *const Context);
fn v8__Context__Exit(this: *const Context);
fn v8__Context__Global(this: *const Context) -> *const Object;
}
impl Context {
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Context> {
let ptr = unsafe { v8__Context__New(scope.isolate(), null(), null()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn new_from_template<'sc>(
scope: &mut impl ToLocal<'sc>,
templ: Local<ObjectTemplate>,
) -> Local<'sc, Context> {
let ptr = unsafe { v8__Context__New(scope.isolate(), &*templ, null()) };
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn global<'sc>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Object> {
unsafe { scope.to_local(v8__Context__Global(self)) }.unwrap()
}
pub(crate) fn enter(&self) {
unsafe { v8__Context__Enter(self) };
}
pub(crate) fn exit(&self) {
unsafe { v8__Context__Exit(self) };
}
}