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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use crate::array_buffer::Allocator;
use crate::external_references::ExternalReferences;
use crate::promise::PromiseRejectMessage;
use crate::support::intptr_t;
use crate::support::Delete;
use crate::support::Opaque;
use crate::support::UniqueRef;
use crate::Context;
use crate::Function;
use crate::Local;
use crate::Message;
use crate::Module;
use crate::Object;
use crate::Promise;
use crate::ScriptOrModule;
use crate::StartupData;
use crate::String;
use crate::Value;
use std::ffi::c_void;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr::NonNull;
pub type MessageCallback = extern "C" fn(Local<Message>, Local<Value>);
pub type PromiseRejectCallback = extern "C" fn(PromiseRejectMessage);
pub type HostInitializeImportMetaObjectCallback =
extern "C" fn(Local<Context>, Local<Module>, Local<Object>);
pub type HostImportModuleDynamicallyCallback = extern "C" fn(
Local<Context>,
Local<ScriptOrModule>,
Local<String>,
) -> *mut Promise;
extern "C" {
fn v8__Isolate__New(params: *mut CreateParams) -> *mut Isolate;
fn v8__Isolate__Dispose(this: *mut Isolate);
fn v8__Isolate__SetData(this: *mut Isolate, slot: u32, data: *mut c_void);
fn v8__Isolate__GetData(this: *const Isolate, slot: u32) -> *mut c_void;
fn v8__Isolate__GetNumberOfDataSlots(this: *const Isolate) -> u32;
fn v8__Isolate__Enter(this: *mut Isolate);
fn v8__Isolate__Exit(this: *mut Isolate);
fn v8__Isolate__GetCurrentContext(this: *mut Isolate) -> *mut Context;
fn v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(
this: *mut Isolate,
caputre: bool,
frame_limit: i32,
);
fn v8__Isolate__AddMessageListener(
this: &mut Isolate,
callback: MessageCallback,
) -> bool;
fn v8__Isolate__SetPromiseRejectCallback(
isolate: *mut Isolate,
callback: PromiseRejectCallback,
);
fn v8__Isolate__SetHostInitializeImportMetaObjectCallback(
isolate: *mut Isolate,
callback: HostInitializeImportMetaObjectCallback,
);
fn v8__Isolate__SetHostImportModuleDynamicallyCallback(
isolate: *mut Isolate,
callback: HostImportModuleDynamicallyCallback,
);
fn v8__Isolate__ThrowException(
isolate: &Isolate,
exception: &Value,
) -> *mut Value;
fn v8__Isolate__TerminateExecution(isolate: &Isolate);
fn v8__Isolate__IsExecutionTerminating(isolate: &Isolate) -> bool;
fn v8__Isolate__CancelTerminateExecution(isolate: &Isolate);
fn v8__Isolate__RunMicrotasks(isolate: &Isolate);
fn v8__Isolate__EnqueueMicrotask(isolate: &Isolate, microtask: *mut Function);
fn v8__Isolate__CreateParams__NEW() -> *mut CreateParams;
fn v8__Isolate__CreateParams__DELETE(this: &mut CreateParams);
fn v8__Isolate__CreateParams__SET__array_buffer_allocator(
this: &mut CreateParams,
value: *mut Allocator,
);
fn v8__Isolate__CreateParams__SET__external_references(
this: &mut CreateParams,
value: *const intptr_t,
);
fn v8__Isolate__CreateParams__SET__snapshot_blob(
this: &mut CreateParams,
snapshot_blob: *mut StartupData,
);
}
#[repr(C)]
pub struct Isolate(Opaque);
impl Isolate {
#[allow(clippy::new_ret_no_self)]
pub fn new(params: UniqueRef<CreateParams>) -> OwnedIsolate {
crate::V8::assert_initialized();
unsafe { new_owned_isolate(v8__Isolate__New(params.into_raw())) }
}
pub fn create_params() -> UniqueRef<CreateParams> {
CreateParams::new()
}
pub unsafe fn set_data(&mut self, slot: u32, ptr: *mut c_void) {
v8__Isolate__SetData(self, slot, ptr)
}
pub fn get_data(&self, slot: u32) -> *mut c_void {
unsafe { v8__Isolate__GetData(self, slot) }
}
pub fn get_number_of_data_slots(&self) -> u32 {
unsafe { v8__Isolate__GetNumberOfDataSlots(self) }
}
pub fn enter(&mut self) {
unsafe { v8__Isolate__Enter(self) }
}
pub fn exit(&mut self) {
unsafe { v8__Isolate__Exit(self) }
}
pub fn get_current_context<'sc>(&mut self) -> Local<'sc, Context> {
unsafe { Local::from_raw(v8__Isolate__GetCurrentContext(self)).unwrap() }
}
pub fn set_capture_stack_trace_for_uncaught_exceptions(
&mut self,
capture: bool,
frame_limit: i32,
) {
unsafe {
v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(
self,
capture,
frame_limit,
)
}
}
pub fn add_message_listener(&mut self, callback: MessageCallback) -> bool {
unsafe { v8__Isolate__AddMessageListener(self, callback) }
}
pub fn set_promise_reject_callback(
&mut self,
callback: PromiseRejectCallback,
) {
unsafe { v8__Isolate__SetPromiseRejectCallback(self, callback) }
}
pub fn set_host_initialize_import_meta_object_callback(
&mut self,
callback: HostInitializeImportMetaObjectCallback,
) {
unsafe {
v8__Isolate__SetHostInitializeImportMetaObjectCallback(self, callback)
}
}
pub fn set_host_import_module_dynamically_callback(
&mut self,
callback: HostImportModuleDynamicallyCallback,
) {
unsafe {
v8__Isolate__SetHostImportModuleDynamicallyCallback(self, callback)
}
}
pub fn throw_exception<'sc>(
&self,
exception: Local<'_, Value>,
) -> Local<'sc, Value> {
unsafe {
let ptr = v8__Isolate__ThrowException(self, &exception);
Local::from_raw(ptr).unwrap()
}
}
pub fn terminate_execution(&self) {
unsafe { v8__Isolate__TerminateExecution(self) }
}
pub fn is_execution_terminating(&self) -> bool {
unsafe { v8__Isolate__IsExecutionTerminating(self) }
}
pub fn cancel_terminate_execution(&self) {
unsafe { v8__Isolate__CancelTerminateExecution(self) }
}
pub fn run_microtasks(&self) {
unsafe { v8__Isolate__RunMicrotasks(self) }
}
pub fn enqueue_microtask(&self, mut microtask: Local<Function>) {
unsafe { v8__Isolate__EnqueueMicrotask(self, &mut *microtask) }
}
pub unsafe fn dispose(&mut self) {
v8__Isolate__Dispose(self)
}
}
pub unsafe fn new_owned_isolate(isolate_ptr: *mut Isolate) -> OwnedIsolate {
OwnedIsolate(NonNull::new(isolate_ptr).unwrap())
}
pub struct OwnedIsolate(NonNull<Isolate>);
impl Drop for OwnedIsolate {
fn drop(&mut self) {
unsafe { self.0.as_mut().dispose() }
}
}
impl Deref for OwnedIsolate {
type Target = Isolate;
fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref() }
}
}
impl DerefMut for OwnedIsolate {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.as_mut() }
}
}
pub trait InIsolate {
fn isolate(&mut self) -> &mut Isolate;
}
#[repr(C)]
pub struct CreateParams(Opaque);
impl CreateParams {
pub fn new() -> UniqueRef<CreateParams> {
unsafe { UniqueRef::from_raw(v8__Isolate__CreateParams__NEW()) }
}
pub fn set_array_buffer_allocator(&mut self, value: UniqueRef<Allocator>) {
unsafe {
v8__Isolate__CreateParams__SET__array_buffer_allocator(
self,
value.into_raw(),
)
};
}
pub fn set_external_references(
&mut self,
external_references: &'static ExternalReferences,
) {
unsafe {
v8__Isolate__CreateParams__SET__external_references(
self,
external_references.as_ptr(),
)
};
}
pub fn set_snapshot_blob(&mut self, snapshot_blob: &StartupData) {
unsafe {
v8__Isolate__CreateParams__SET__snapshot_blob(
self,
snapshot_blob as *const _ as *mut StartupData,
)
};
}
}
impl Delete for CreateParams {
fn delete(&'static mut self) {
unsafe { v8__Isolate__CreateParams__DELETE(self) }
}
}