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
use crate::InIsolate;
use crate::Isolate;
use crate::Local;
use crate::Module;
use crate::ScriptOrigin;
use crate::String;
use std::mem::MaybeUninit;
extern "C" {
fn v8__ScriptCompiler__Source__CONSTRUCT(
buf: *mut MaybeUninit<Source>,
source_string: *const String,
origin: *const ScriptOrigin,
);
fn v8__ScriptCompiler__Source__DESTRUCT(this: *mut Source);
fn v8__ScriptCompiler__CompileModule(
isolate: *mut Isolate,
source: *mut Source,
options: CompileOptions,
no_cache_reason: NoCacheReason,
) -> *const Module;
}
#[repr(C)]
pub struct Source([usize; 8]);
impl Source {
pub fn new(source_string: Local<String>, origin: &ScriptOrigin) -> Self {
let mut buf = MaybeUninit::<Self>::uninit();
unsafe {
v8__ScriptCompiler__Source__CONSTRUCT(&mut buf, &*source_string, origin);
buf.assume_init()
}
}
}
impl Drop for Source {
fn drop(&mut self) {
unsafe { v8__ScriptCompiler__Source__DESTRUCT(self) }
}
}
#[repr(C)]
pub enum CompileOptions {
NoCompileOptions = 0,
ConsumeCodeCache,
EagerCompile,
}
#[repr(C)]
pub enum NoCacheReason {
NoReason = 0,
BecauseCachingDisabled,
BecauseNoResource,
BecauseInlineScript,
BecauseModule,
BecauseStreamingSource,
BecauseInspector,
BecauseScriptTooSmall,
BecauseCacheTooCold,
BecauseV8Extension,
BecauseExtensionModule,
BecausePacScript,
BecauseInDocumentWrite,
BecauseResourceWithNoCacheHandler,
BecauseDeferredProduceCodeCache,
}
pub fn compile_module<'a>(
scope: &mut impl InIsolate,
source: Source,
) -> Option<Local<'a, Module>> {
compile_module2(
scope,
source,
CompileOptions::NoCompileOptions,
NoCacheReason::NoReason,
)
}
pub fn compile_module2<'a>(
scope: &mut impl InIsolate,
mut source: Source,
options: CompileOptions,
no_cache_reason: NoCacheReason,
) -> Option<Local<'a, Module>> {
unsafe {
Local::from_raw(v8__ScriptCompiler__CompileModule(
scope.isolate(),
&mut source,
options,
no_cache_reason,
))
}
}