-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathsetup.rs
More file actions
146 lines (134 loc) · 4.67 KB
/
setup.rs
File metadata and controls
146 lines (134 loc) · 4.67 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2018-2026 the Deno authors. MIT license.
use std::borrow::Cow;
use std::sync::Mutex;
use std::sync::Once;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use super::bindings;
use super::snapshot;
use super::snapshot::V8Snapshot;
fn v8_init(
v8_platform: Option<v8::SharedRef<v8::Platform>>,
snapshot: bool,
expose_natives: bool,
) {
#[cfg(feature = "include_icu_data")]
{
v8::icu::set_common_data_77(deno_core_icudata::ICU_DATA).unwrap();
}
let base_flags = concat!(
" --wasm-test-streaming",
" --no-validate-asm",
" --turbo_fast_api_calls",
" --harmony-temporal",
" --js-float16array",
" --js-explicit-resource-management",
" --js-source-phase-imports",
" --js-defer-import-eval"
);
let snapshot_flags = "--predictable --random-seed=42";
let expose_natives_flags = "--expose_gc --allow_natives_syntax";
let lazy_flags = if cfg!(feature = "snapshot_flags_eager_parse") {
"--no-lazy --no-lazy-eval --no-lazy-streaming"
} else {
""
};
let flags = match (snapshot, expose_natives) {
(false, false) => base_flags.to_string(),
(true, false) => {
format!("{base_flags} {snapshot_flags} {lazy_flags}")
}
(false, true) => format!("{base_flags} {expose_natives_flags}"),
(true, true) => {
format!(
"{base_flags} {snapshot_flags} {lazy_flags} {expose_natives_flags}"
)
}
};
v8::V8::set_flags_from_string(&flags);
let v8_platform = v8_platform.unwrap_or_else(|| {
if cfg!(any(test, feature = "unsafe_use_unprotected_platform")) {
// We want to use the unprotected platform for unit tests
v8::new_unprotected_default_platform(0, false)
} else {
v8::new_default_platform(0, false)
}
.make_shared()
});
v8::V8::initialize_platform(v8_platform.clone());
v8::V8::initialize();
}
pub fn init_v8(
v8_platform: Option<v8::SharedRef<v8::Platform>>,
snapshot: bool,
expose_natives: bool,
) {
static DENO_INIT: Once = Once::new();
static DENO_SNAPSHOT: AtomicBool = AtomicBool::new(false);
static DENO_SNAPSHOT_SET: AtomicBool = AtomicBool::new(false);
if DENO_SNAPSHOT_SET.load(Ordering::SeqCst) {
let current = DENO_SNAPSHOT.load(Ordering::SeqCst);
assert_eq!(
current, snapshot,
"V8 may only be initialized once in either snapshotting or non-snapshotting mode. Either snapshotting or non-snapshotting mode may be used in a single process, not both."
);
DENO_SNAPSHOT_SET.store(true, Ordering::SeqCst);
DENO_SNAPSHOT.store(snapshot, Ordering::SeqCst);
}
DENO_INIT.call_once(move || v8_init(v8_platform, snapshot, expose_natives));
}
pub fn create_isolate(
will_snapshot: bool,
maybe_create_params: Option<v8::CreateParams>,
maybe_startup_snapshot: Option<V8Snapshot>,
external_refs: Cow<'static, [v8::ExternalReference]>,
) -> v8::OwnedIsolate {
let mut params = maybe_create_params.unwrap_or_default();
let mut isolate = if will_snapshot {
snapshot::create_snapshot_creator(
external_refs,
maybe_startup_snapshot,
params,
)
} else {
params = params.external_references(external_refs);
let has_snapshot = maybe_startup_snapshot.is_some();
if let Some(snapshot) = maybe_startup_snapshot {
params = params.snapshot_blob(v8::StartupData::from(snapshot.0));
}
static FIRST_SNAPSHOT_INIT: AtomicBool = AtomicBool::new(false);
static SNAPSHOW_INIT_MUT: Mutex<()> = Mutex::new(());
// On Windows, the snapshot deserialization code appears to be crashing and we are not
// certain of the reason. We take a mutex the first time an isolate with a snapshot to
// prevent this. https://github.com/denoland/deno/issues/15590
if cfg!(windows)
&& has_snapshot
&& FIRST_SNAPSHOT_INIT.load(Ordering::SeqCst)
{
let _g = SNAPSHOW_INIT_MUT.lock().unwrap();
let res = v8::Isolate::new(params);
FIRST_SNAPSHOT_INIT.store(true, Ordering::SeqCst);
res
} else {
v8::Isolate::new(params)
}
};
isolate.set_capture_stack_trace_for_uncaught_exceptions(true, 10);
isolate.set_promise_reject_callback(bindings::promise_reject_callback);
isolate.set_prepare_stack_trace_callback(
crate::error::prepare_stack_trace_callback,
);
isolate.set_host_initialize_import_meta_object_callback(
bindings::host_initialize_import_meta_object_callback,
);
isolate.set_host_import_module_dynamically_callback(
bindings::host_import_module_dynamically_callback,
);
isolate.set_host_import_module_with_phase_dynamically_callback(
bindings::host_import_module_with_phase_dynamically_callback,
);
isolate.set_wasm_async_resolve_promise_callback(
bindings::wasm_async_resolve_promise_callback,
);
isolate
}