forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_bootstrap.rs
191 lines (165 loc) · 4.54 KB
/
worker_bootstrap.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::v8;
use deno_core::ModuleSpecifier;
use std::thread;
use crate::colors;
use crate::ops::runtime::ppid;
/// Common bootstrap options for MainWorker & WebWorker
#[derive(Clone)]
pub struct BootstrapOptions {
/// Sets `Deno.args` in JS runtime.
pub args: Vec<String>,
pub cpu_count: usize,
pub debug_flag: bool,
pub enable_testing_features: bool,
pub locale: String,
pub location: Option<ModuleSpecifier>,
/// Sets `Deno.noColor` in JS runtime.
pub no_color: bool,
pub is_tty: bool,
/// Sets `Deno.version.deno` in JS runtime.
pub runtime_version: String,
/// Sets `Deno.version.typescript` in JS runtime.
pub ts_version: String,
pub unstable: bool,
pub user_agent: String,
pub inspect: bool,
}
impl Default for BootstrapOptions {
fn default() -> Self {
let cpu_count = thread::available_parallelism()
.map(|p| p.get())
.unwrap_or(1);
let runtime_version = env!("CARGO_PKG_VERSION").into();
let user_agent = format!("Deno/{runtime_version}");
Self {
runtime_version,
user_agent,
cpu_count,
no_color: !colors::use_color(),
is_tty: colors::is_tty(),
enable_testing_features: Default::default(),
debug_flag: Default::default(),
ts_version: Default::default(),
locale: "en".to_string(),
location: Default::default(),
unstable: Default::default(),
inspect: Default::default(),
args: Default::default(),
}
}
}
impl BootstrapOptions {
pub fn as_v8<'s>(
&self,
scope: &mut v8::HandleScope<'s>,
) -> v8::Local<'s, v8::Array> {
let array = v8::Array::new(scope, 17);
{
let args = v8::Array::new(scope, self.args.len() as i32);
for (idx, arg) in self.args.iter().enumerate() {
let arg_str = v8::String::new(scope, arg).unwrap();
args.set_index(scope, idx as u32, arg_str.into());
}
array.set_index(scope, 0, args.into());
}
{
let val = v8::Integer::new(scope, self.cpu_count as i32);
array.set_index(scope, 1, val.into());
}
{
let val = v8::Boolean::new(scope, self.debug_flag);
array.set_index(scope, 2, val.into());
}
{
let val = v8::String::new_from_one_byte(
scope,
self.runtime_version.as_bytes(),
v8::NewStringType::Internalized,
)
.unwrap();
array.set_index(scope, 3, val.into());
}
{
let val = v8::String::new_from_one_byte(
scope,
self.locale.as_bytes(),
v8::NewStringType::Normal,
)
.unwrap();
array.set_index(scope, 4, val.into());
}
{
let val: v8::Local<v8::Value> = if let Some(location) = &self.location {
v8::String::new(scope, location.as_str()).unwrap().into()
} else {
v8::undefined(scope).into()
};
array.set_index(scope, 5, val);
}
{
let val = v8::Boolean::new(scope, self.no_color);
array.set_index(scope, 6, val.into());
}
{
let val = v8::Boolean::new(scope, self.is_tty);
array.set_index(scope, 7, val.into());
}
{
let val = v8::String::new_from_one_byte(
scope,
self.ts_version.as_bytes(),
v8::NewStringType::Normal,
)
.unwrap();
array.set_index(scope, 8, val.into());
}
{
let val = v8::Boolean::new(scope, self.unstable);
array.set_index(scope, 9, val.into());
}
{
let val = v8::Integer::new(scope, std::process::id() as i32);
array.set_index(scope, 10, val.into());
}
{
let val = v8::Integer::new(scope, ppid() as i32);
array.set_index(scope, 11, val.into());
}
{
let val = v8::String::new_external_onebyte_static(
scope,
env!("TARGET").as_bytes(),
)
.unwrap();
array.set_index(scope, 12, val.into());
}
{
let val = v8::String::new_from_one_byte(
scope,
deno_core::v8_version().as_bytes(),
v8::NewStringType::Normal,
)
.unwrap();
array.set_index(scope, 13, val.into());
}
{
let val = v8::String::new_from_one_byte(
scope,
self.user_agent.as_bytes(),
v8::NewStringType::Normal,
)
.unwrap();
array.set_index(scope, 14, val.into());
}
{
let val = v8::Boolean::new(scope, self.inspect);
array.set_index(scope, 15, val.into());
}
{
let val = v8::Boolean::new(scope, self.enable_testing_features);
array.set_index(scope, 16, val.into());
}
array
}
}