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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use libc::c_char;
use libc::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::sync::Mutex;
use std::vec::Vec;
use crate::platform::Platform;
use crate::support::SharedRef;
use crate::support::UnitType;
extern "C" {
fn v8__V8__SetFlagsFromCommandLine(
argc: *mut c_int,
argv: *mut *mut c_char,
usage: *const c_char,
);
fn v8__V8__SetFlagsFromString(flags: *const u8, length: usize);
fn v8__V8__SetEntropySource(callback: EntropySource);
fn v8__V8__GetVersion() -> *const c_char;
fn v8__V8__InitializePlatform(platform: *mut Platform);
fn v8__V8__Initialize();
fn v8__V8__Dispose() -> bool;
fn v8__V8__DisposePlatform();
}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct EntropySource(RawEntropySource);
pub trait IntoEntropySource:
UnitType + Into<EntropySource> + FnOnce(&mut [u8]) -> bool
{
}
impl<F> IntoEntropySource for F where
F: UnitType + Into<EntropySource> + FnOnce(&mut [u8]) -> bool
{
}
type RawEntropySource = extern "C" fn(*mut u8, usize) -> bool;
impl<F> From<F> for EntropySource
where
F: UnitType + FnOnce(&mut [u8]) -> bool,
{
fn from(_: F) -> Self {
#[inline(always)]
extern "C" fn adapter<F: IntoEntropySource>(
buffer: *mut u8,
length: usize,
) -> bool {
let buffer = unsafe { std::slice::from_raw_parts_mut(buffer, length) };
(F::get())(buffer)
}
Self(adapter::<F>)
}
}
#[derive(Debug)]
enum GlobalState {
Uninitialized,
PlatformInitialized(SharedRef<Platform>),
Initialized(SharedRef<Platform>),
Disposed(SharedRef<Platform>),
PlatformShutdown,
}
use GlobalState::*;
lazy_static! {
static ref GLOBAL_STATE: Mutex<GlobalState> = Mutex::new(Uninitialized);
}
pub fn assert_initialized() {
let global_state_guard = GLOBAL_STATE.lock().unwrap();
match *global_state_guard {
Initialized(_) => {}
_ => panic!("Invalid global state"),
};
}
pub fn set_flags_from_command_line(args: Vec<String>) -> Vec<String> {
set_flags_from_command_line_with_usage(args, None)
}
pub fn set_flags_from_command_line_with_usage(
args: Vec<String>,
usage: Option<&str>,
) -> Vec<String> {
let mut raw_argv = args
.iter()
.map(|arg| CString::new(arg.as_str()).unwrap().into_bytes_with_nul())
.collect::<Vec<_>>();
let mut c_argv = raw_argv
.iter_mut()
.map(|arg| arg.as_mut_ptr() as *mut c_char)
.collect::<Vec<_>>();
let mut c_argv_len = c_argv.len() as c_int;
let c_usage = match usage {
Some(str) => CString::new(str).unwrap().into_raw() as *const c_char,
None => std::ptr::null(),
};
unsafe {
v8__V8__SetFlagsFromCommandLine(
&mut c_argv_len,
c_argv.as_mut_ptr(),
c_usage,
);
};
c_argv.truncate(c_argv_len as usize);
c_argv
.iter()
.map(|ptr| unsafe {
let cstr = CStr::from_ptr(*ptr as *const c_char);
let slice = cstr.to_str().unwrap();
slice.to_string()
})
.collect()
}
pub fn set_flags_from_string(flags: &str) {
unsafe {
v8__V8__SetFlagsFromString(flags.as_ptr(), flags.len());
}
}
pub fn set_entropy_source(
callback: impl UnitType + Into<EntropySource> + FnOnce(&mut [u8]) -> bool,
) {
unsafe { v8__V8__SetEntropySource(callback.into()) };
}
pub fn get_version() -> &'static str {
let version = unsafe { v8__V8__GetVersion() };
let c_str = unsafe { CStr::from_ptr(version) };
c_str.to_str().unwrap()
}
pub fn initialize_platform(platform: SharedRef<Platform>) {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
*global_state_guard = match *global_state_guard {
Uninitialized => PlatformInitialized(platform.clone()),
_ => panic!("Invalid global state"),
};
{
unsafe {
v8__V8__InitializePlatform(&*platform as *const Platform as *mut _)
};
}
}
pub fn initialize() {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
*global_state_guard = match *global_state_guard {
PlatformInitialized(ref platform) => Initialized(platform.clone()),
_ => panic!("Invalid global state"),
};
unsafe { v8__V8__Initialize() }
}
pub fn get_current_platform() -> SharedRef<Platform> {
let global_state_guard = GLOBAL_STATE.lock().unwrap();
match *global_state_guard {
Initialized(ref platform) => platform.clone(),
_ => panic!("Invalid global state"),
}
}
pub unsafe fn dispose() -> bool {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
*global_state_guard = match *global_state_guard {
Initialized(ref platform) => Disposed(platform.clone()),
_ => panic!("Invalid global state"),
};
assert!(v8__V8__Dispose());
true
}
pub fn dispose_platform() {
let mut global_state_guard = GLOBAL_STATE.lock().unwrap();
*global_state_guard = match *global_state_guard {
Disposed(_) => {
unsafe { v8__V8__DisposePlatform() };
PlatformShutdown
}
_ => panic!("Invalid global state"),
};
}