2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-13 04:52:59 +00:00
|
|
|
|
|
|
|
use deno_core::op2;
|
|
|
|
use deno_core::OpState;
|
2024-07-19 10:39:05 +00:00
|
|
|
use deno_terminal::colors::ColorLevel;
|
2023-11-15 12:25:55 +00:00
|
|
|
use serde::Serialize;
|
2023-11-13 04:52:59 +00:00
|
|
|
|
|
|
|
use crate::BootstrapOptions;
|
|
|
|
|
|
|
|
deno_core::extension!(
|
|
|
|
deno_bootstrap,
|
|
|
|
ops = [
|
|
|
|
op_bootstrap_args,
|
|
|
|
op_bootstrap_pid,
|
|
|
|
op_bootstrap_numcpus,
|
|
|
|
op_bootstrap_user_agent,
|
|
|
|
op_bootstrap_language,
|
|
|
|
op_bootstrap_log_level,
|
|
|
|
op_bootstrap_no_color,
|
2024-07-19 10:39:05 +00:00
|
|
|
op_bootstrap_color_depth,
|
2024-05-14 21:32:09 +00:00
|
|
|
op_bootstrap_is_stdout_tty,
|
|
|
|
op_bootstrap_is_stderr_tty,
|
2024-01-23 14:33:07 +00:00
|
|
|
op_bootstrap_unstable_args,
|
2023-11-15 12:25:55 +00:00
|
|
|
op_snapshot_options,
|
2023-11-13 04:52:59 +00:00
|
|
|
],
|
2023-11-15 12:25:55 +00:00
|
|
|
options = {
|
|
|
|
snapshot_options: Option<SnapshotOptions>,
|
|
|
|
},
|
|
|
|
state = |state, options| {
|
|
|
|
if let Some(snapshot_options) = options.snapshot_options {
|
|
|
|
state.put::<SnapshotOptions>(snapshot_options);
|
|
|
|
}
|
|
|
|
},
|
2023-11-13 04:52:59 +00:00
|
|
|
);
|
|
|
|
|
2024-03-14 03:23:37 +00:00
|
|
|
#[derive(Serialize)]
|
2023-11-15 12:25:55 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SnapshotOptions {
|
|
|
|
pub ts_version: String,
|
|
|
|
pub v8_version: &'static str,
|
|
|
|
pub target: String,
|
|
|
|
}
|
|
|
|
|
2024-03-14 03:23:37 +00:00
|
|
|
impl Default for SnapshotOptions {
|
|
|
|
fn default() -> Self {
|
2024-05-28 11:20:19 +00:00
|
|
|
let arch = std::env::consts::ARCH;
|
|
|
|
let platform = std::env::consts::OS;
|
|
|
|
let target = match platform {
|
|
|
|
"macos" => format!("{}-apple-darwin", arch),
|
|
|
|
"linux" => format!("{}-unknown-linux-gnu", arch),
|
|
|
|
"windows" => format!("{}-pc-windows-msvc", arch),
|
|
|
|
rest => format!("{}-{}", arch, rest),
|
|
|
|
};
|
|
|
|
|
2024-03-14 03:23:37 +00:00
|
|
|
Self {
|
|
|
|
ts_version: "n/a".to_owned(),
|
2024-08-19 14:51:16 +00:00
|
|
|
v8_version: deno_core::v8::VERSION_STRING,
|
2024-05-28 11:20:19 +00:00
|
|
|
target,
|
2024-03-14 03:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 12:25:55 +00:00
|
|
|
// Note: Called at snapshot time, op perf is not a concern.
|
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_snapshot_options(state: &mut OpState) -> SnapshotOptions {
|
|
|
|
state.take::<SnapshotOptions>()
|
|
|
|
}
|
|
|
|
|
2023-11-13 04:52:59 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_bootstrap_args(state: &mut OpState) -> Vec<String> {
|
|
|
|
state.borrow::<BootstrapOptions>().args.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
|
|
|
pub fn op_bootstrap_pid() -> u32 {
|
|
|
|
std::process::id()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
|
|
|
pub fn op_bootstrap_numcpus(state: &mut OpState) -> u32 {
|
|
|
|
state.borrow::<BootstrapOptions>().cpu_count as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
pub fn op_bootstrap_user_agent(state: &mut OpState) -> String {
|
|
|
|
state.borrow::<BootstrapOptions>().user_agent.clone()
|
|
|
|
}
|
|
|
|
|
2024-01-23 14:33:07 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_bootstrap_unstable_args(state: &mut OpState) -> Vec<String> {
|
|
|
|
let options = state.borrow::<BootstrapOptions>();
|
|
|
|
if options.unstable {
|
|
|
|
return vec!["--unstable".to_string()];
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flags = Vec::new();
|
2024-08-27 11:45:27 +00:00
|
|
|
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS.iter() {
|
|
|
|
if options.unstable_features.contains(&granular_flag.id) {
|
|
|
|
flags.push(format!("--unstable-{}", granular_flag.name));
|
2024-01-23 14:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flags
|
|
|
|
}
|
|
|
|
|
2023-11-13 04:52:59 +00:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
pub fn op_bootstrap_language(state: &mut OpState) -> String {
|
|
|
|
state.borrow::<BootstrapOptions>().locale.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
|
|
|
pub fn op_bootstrap_log_level(state: &mut OpState) -> i32 {
|
|
|
|
state.borrow::<BootstrapOptions>().log_level as i32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_bootstrap_no_color(state: &mut OpState) -> bool {
|
|
|
|
let options = state.borrow::<BootstrapOptions>();
|
2023-11-15 05:10:12 +00:00
|
|
|
options.no_color
|
|
|
|
}
|
|
|
|
|
2024-07-19 10:39:05 +00:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_bootstrap_color_depth(state: &mut OpState) -> i32 {
|
|
|
|
let options = state.borrow::<BootstrapOptions>();
|
|
|
|
match options.color_level {
|
|
|
|
ColorLevel::None => 1,
|
|
|
|
ColorLevel::Ansi => 4,
|
|
|
|
ColorLevel::Ansi256 => 8,
|
|
|
|
ColorLevel::TrueColor => 24,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 05:10:12 +00:00
|
|
|
#[op2(fast)]
|
2024-05-14 21:32:09 +00:00
|
|
|
pub fn op_bootstrap_is_stdout_tty(state: &mut OpState) -> bool {
|
2023-11-15 05:10:12 +00:00
|
|
|
let options = state.borrow::<BootstrapOptions>();
|
2024-05-14 21:32:09 +00:00
|
|
|
options.is_stdout_tty
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_bootstrap_is_stderr_tty(state: &mut OpState) -> bool {
|
|
|
|
let options = state.borrow::<BootstrapOptions>();
|
|
|
|
options.is_stderr_tty
|
2023-11-13 04:52:59 +00:00
|
|
|
}
|