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
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Once;
use v8;
pub struct JsRuntime {
v8_isolate: Option<v8::OwnedIsolate>,
}
pub fn v8_init() {
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
}
impl Default for JsRuntime {
fn default() -> Self {
Self::new()
}
}
impl JsRuntime {
pub fn new() -> Self {
static DENO_INIT: Once = Once::new();
DENO_INIT.call_once(|| {
v8_init();
});
let isolate = {
let params = v8::Isolate::create_params();
let isolate = v8::Isolate::new(params);
let isolate = JsRuntime::setup_isolate(isolate);
{
}
isolate
};
Self {
v8_isolate: Some(isolate),
}
}
fn setup_isolate(mut isolate: v8::OwnedIsolate) -> v8::OwnedIsolate {
isolate.set_capture_stack_trace_for_uncaught_exceptions(true, 10);
isolate
}
pub fn global_context(&mut self) -> v8::Global<v8::Context> {
let state = Self::state(self.v8_isolate());
let state = state.borrow();
state.global_context.clone().unwrap()
}
pub fn v8_isolate(&mut self) -> &mut v8::OwnedIsolate {
self.v8_isolate.as_mut().unwrap()
}
pub(crate) fn state(isolate: &v8::Isolate) -> Rc<RefCell<JsRuntimeState>> {
let s = isolate.get_slot::<Rc<RefCell<JsRuntimeState>>>().unwrap();
s.clone()
}
}
pub(crate) struct JsRuntimeState {
pub global_context: Option<v8::Global<v8::Context>>,
}