Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
316 lines (281 loc) · 7.59 KB
/
Copy pathbuild.rs
File metadata and controls
316 lines (281 loc) · 7.59 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
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
// Build script: compile the jse extension bootstrap (src/js/*.js + ops) into
// a V8 startup snapshot consumed by src/snapshot.rs. This recompiles
// deno_core for the build-script target, which roughly doubles a cold build;
// cargo caches it afterwards.
use std::path::PathBuf;
// Stub modules so src/ops.rs compiles inside the build script.
mod worker {
pub fn spawn_worker_thread(_specifier: String, _host: crate::ops::WorkerHost) -> std::io::Result<()> {
Ok(())
}
}
#[allow(dead_code)]
mod logger {
include!("src/logger.rs");
}
#[allow(dead_code)]
mod permissions {
include!("src/permissions.rs");
}
#[allow(dead_code)]
mod serve {
include!("src/serve.rs");
}
#[allow(dead_code)]
mod wasm_compiler {
pub fn compile_file_to_wasm(_file_path: &std::path::Path) -> Result<Vec<u8>, deno_error::JsErrorBox> {
Ok(Vec::new())
}
pub fn write_u32_leb128(buf: &mut Vec<u8>, mut val: u32) {
loop {
let mut byte = (val & 0x7f) as u8;
val >>= 7;
if val != 0 {
byte |= 0x80;
}
buf.push(byte);
if val == 0 {
break;
}
}
}
pub fn write_i32_leb128(buf: &mut Vec<u8>, mut val: i32) {
let mut more = true;
while more {
let mut byte = (val & 0x7f) as u8;
val >>= 7;
let sign_bit = (byte & 0x40) != 0;
if (val == 0 && !sign_bit) || (val == -1 && sign_bit) {
more = false;
} else {
byte |= 0x80;
}
buf.push(byte);
}
}
pub fn write_section(buf: &mut Vec<u8>, section_id: u8, payload: &[u8]) {
buf.push(section_id);
write_u32_leb128(buf, payload.len() as u32);
buf.extend_from_slice(payload);
}
pub fn write_name(buf: &mut Vec<u8>, name: &str) {
write_u32_leb128(buf, name.len() as u32);
buf.extend_from_slice(name.as_bytes());
}
}
#[allow(dead_code)]
mod optimizer {
include!("src/optimizer.rs");
}
#[allow(dead_code)]
mod kv {
include!("src/kv.rs");
}
#[allow(dead_code)]
mod router {
include!("src/router.rs");
}
#[allow(dead_code)]
mod crypto {
include!("src/crypto.rs");
}
mod platform {
include!("src/platform.rs");
}
#[allow(dead_code)]
mod production {
include!("src/production.rs");
}
// The snapshot only needs the op's declaration.
#[allow(dead_code)]
mod loader {
#[deno_core::op2]
#[string]
pub fn op_require_resolve(
#[string] _specifier: String,
#[string] _parent: String,
) -> Result<String, deno_error::JsErrorBox> {
Err(deno_error::JsErrorBox::generic("unavailable during snapshot build"))
}
}
mod vm {
include!("src/vm.rs");
}
#[allow(dead_code)]
mod sql {
use deno_core::op2;
use deno_error::JsErrorBox;
use std::collections::HashMap;
#[op2(fast)]
pub fn op_sql_open(#[string] _path: String) -> Result<u32, JsErrorBox> {
Ok(1)
}
#[op2(fast)]
pub fn op_sql_close(_id: u32) -> bool {
true
}
#[op2]
pub fn op_sql_exec(
_id: u32,
#[string] _sql: String,
#[serde] _params: Vec<serde_json::Value>,
) -> Result<f64, JsErrorBox> {
Ok(0.0)
}
#[op2(fast)]
pub fn op_sql_last_insert_rowid(_id: u32) -> Result<f64, JsErrorBox> {
Ok(0.0)
}
#[op2]
#[serde]
pub fn op_sql_query(
_id: u32,
#[string] _sql: String,
#[serde] _params: Vec<serde_json::Value>,
) -> Result<Vec<HashMap<String, serde_json::Value>>, JsErrorBox> {
Ok(Vec::new())
}
}
#[allow(dead_code)]
mod napi {
use deno_core::OpState;
use deno_core::v8;
use deno_error::JsErrorBox;
use std::cell::RefCell;
use std::rc::Rc;
pub fn init_addon<'s>(
scope: &mut v8::PinScope<'s, '_>,
_state: Rc<RefCell<OpState>>,
_path: String,
_exports: v8::Local<'s, v8::Value>,
) -> Result<v8::Local<'s, v8::Value>, JsErrorBox> {
Ok(v8::undefined(scope).into())
}
pub async fn next_event(_state: Rc<RefCell<OpState>>) -> bool {
false
}
pub fn drain_event(_scope: &mut v8::PinScope<'_, '_>, _state: &mut OpState) {}
pub fn active_async_count(_state: &OpState) -> usize {
0
}
}
#[allow(dead_code)]
mod config {
use std::collections::HashMap;
use std::sync::{LazyLock, RwLock};
pub static CONFIG_ENV: LazyLock<RwLock<HashMap<String, String>>> = LazyLock::new(|| RwLock::new(HashMap::new()));
}
#[allow(dead_code)]
mod redis {
use deno_core::OpState;
use deno_core::op2;
use deno_error::JsErrorBox;
#[op2(fast)]
pub fn op_redis_connect(_state: &mut OpState, #[string] _url: String) -> Result<u32, JsErrorBox> {
Ok(0)
}
#[op2]
#[serde]
pub fn op_redis_command(
_state: &mut OpState,
_id: u32,
#[serde] _args: Vec<String>,
) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}
#[op2]
#[serde]
pub fn op_redis_pipeline(
_state: &mut OpState,
_id: u32,
#[serde] _commands: Vec<Vec<String>>,
) -> Result<Vec<serde_json::Value>, JsErrorBox> {
Ok(Vec::new())
}
}
#[allow(dead_code)]
mod s3 {
use deno_core::OpState;
use deno_core::op2;
use deno_error::JsErrorBox;
#[op2(fast)]
pub fn op_s3_connect(
_state: &mut OpState,
#[string] _endpoint: String,
#[string] _bucket: String,
#[string] _region: String,
#[string] _access_key: String,
#[string] _secret_key: String,
) -> Result<u32, JsErrorBox> {
Ok(0)
}
#[op2]
#[serde]
pub fn op_s3_get(_state: &mut OpState, _id: u32, #[string] _key: String) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}
#[op2]
#[serde]
pub fn op_s3_put(
_state: &mut OpState,
_id: u32,
#[string] _key: String,
#[buffer] _data: &[u8],
) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}
#[op2]
#[serde]
pub fn op_s3_delete(_state: &mut OpState, _id: u32, #[string] _key: String) -> Result<serde_json::Value, JsErrorBox> {
Ok(serde_json::Value::Null)
}
#[op2]
#[string]
pub fn op_s3_presign(
_state: &mut OpState,
_id: u32,
#[string] _key: String,
_expires_secs: u32,
) -> Result<String, JsErrorBox> {
Ok(String::new())
}
}
#[allow(dead_code)]
mod ops {
include!("src/ops.rs");
}
fn main() {
if cfg!(target_os = "macos") {
println!("cargo::rustc-link-arg=-Wl,-export_dynamic");
} else if cfg!(target_os = "linux") {
println!("cargo::rustc-link-arg=-Wl,--export-dynamic");
} else if cfg!(target_os = "windows") {
// V8 + deno_core need a larger stack than the Windows default (1 MB).
// Set 16 MB at link time so debug builds and tests don't overflow.
println!("cargo::rustc-link-arg=/STACK:16777216");
}
println!("cargo::rerun-if-changed=src/js");
println!("cargo::rerun-if-changed=src/ops.rs");
println!("cargo::rerun-if-changed=src/vm.rs");
println!("cargo::rerun-if-changed=src/ops_extra.rs");
println!("cargo::rerun-if-changed=src/serve.rs");
println!("cargo::rerun-if-changed=src/permissions.rs");
println!("cargo::rerun-if-changed=src/logger.rs");
println!("cargo::rerun-if-changed=src/panic.rs");
println!("cargo::rerun-if-changed=Cargo.lock");
// Snapshot creation runs outside any tokio runtime, but creating a
// JsRuntime schedules delayed V8 tasks on one: enter a private runtime.
let tokio_rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.expect("tokio runtime for snapshot build");
let _guard = tokio_rt.enter();
let runtime = deno_core::JsRuntimeForSnapshot::new(deno_core::RuntimeOptions {
extensions: vec![ops::jse::init(None)],
..Default::default()
});
let snapshot = runtime.snapshot();
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR"));
std::fs::write(out_dir.join("jse_snapshot.bin"), &snapshot).expect("write snapshot");
println!("cargo::warning=jse startup snapshot: {} bytes", snapshot.len());
}