summaryrefslogtreecommitdiff
path: root/runtime/ops/bootstrap.rs
blob: eb9dbc6e84a655e15f4f7fee56dfdfc9ff859519 (plain)
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_core::op2;
use deno_core::OpState;
use serde::Serialize;

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,
    op_bootstrap_is_stdout_tty,
    op_bootstrap_is_stderr_tty,
    op_bootstrap_unstable_args,
    op_snapshot_options,
  ],
  options = {
    snapshot_options: Option<SnapshotOptions>,
  },
  state = |state, options| {
    if let Some(snapshot_options) = options.snapshot_options {
      state.put::<SnapshotOptions>(snapshot_options);
    }
  },
);

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotOptions {
  pub deno_version: String,
  pub ts_version: String,
  pub v8_version: &'static str,
  pub target: String,
}

impl Default for SnapshotOptions {
  fn default() -> Self {
    Self {
      deno_version: "dev".to_owned(),
      ts_version: "n/a".to_owned(),
      v8_version: deno_core::v8_version(),
      target: std::env::consts::ARCH.to_owned(),
    }
  }
}

// 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>()
}

#[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()
}

#[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();
  for (name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS.iter() {
    if options.unstable_features.contains(id) {
      flags.push(format!("--unstable-{}", name));
    }
  }
  flags
}

#[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>();
  options.no_color
}

#[op2(fast)]
pub fn op_bootstrap_is_stdout_tty(state: &mut OpState) -> bool {
  let options = state.borrow::<BootstrapOptions>();
  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
}