summaryrefslogtreecommitdiff
path: root/cli/util/v8.rs
blob: 6e690e6f30d9a3e34dee45e34605dd87a045bee6 (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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

pub mod convert;

#[inline(always)]
pub fn get_v8_flags_from_env() -> Vec<String> {
  std::env::var("DENO_V8_FLAGS")
    .ok()
    .map(|flags| flags.split(',').map(String::from).collect::<Vec<String>>())
    .unwrap_or_default()
}

#[inline(always)]
pub fn construct_v8_flags(
  default_v8_flags: &[String],
  v8_flags: &[String],
  env_v8_flags: Vec<String>,
) -> Vec<String> {
  std::iter::once("UNUSED_BUT_NECESSARY_ARG0".to_owned())
    .chain(default_v8_flags.iter().cloned())
    .chain(env_v8_flags)
    .chain(v8_flags.iter().cloned())
    .collect::<Vec<_>>()
}

pub fn init_v8_flags(
  default_v8_flags: &[String],
  v8_flags: &[String],
  env_v8_flags: Vec<String>,
) {
  if default_v8_flags.is_empty()
    && v8_flags.is_empty()
    && env_v8_flags.is_empty()
  {
    return;
  }

  let v8_flags_includes_help = env_v8_flags
    .iter()
    .chain(v8_flags)
    .any(|flag| flag == "-help" || flag == "--help");
  // Keep in sync with `standalone.rs`.
  let v8_flags = construct_v8_flags(default_v8_flags, v8_flags, env_v8_flags);
  let unrecognized_v8_flags = deno_core::v8_set_flags(v8_flags)
    .into_iter()
    .skip(1)
    .collect::<Vec<_>>();

  if !unrecognized_v8_flags.is_empty() {
    for f in unrecognized_v8_flags {
      log::error!("error: V8 did not recognize flag '{f}'");
    }
    log::error!("\nFor a list of V8 flags, use '--v8-flags=--help'");
    deno_runtime::exit(1);
  }
  if v8_flags_includes_help {
    deno_runtime::exit(0);
  }
}