File: node_mksnapshot.cc

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (105 lines) | stat: -rw-r--r-- 3,170 bytes parent folder | download | duplicates (2)
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
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include "libplatform/libplatform.h"
#include "node_internals.h"
#include "node_snapshot_builder.h"
#include "util-inl.h"
#include "v8.h"

int BuildSnapshot(int argc, char* argv[]);

#ifdef _WIN32
#include <windows.h>

int wmain(int argc, wchar_t* wargv[]) {
  // Windows needs conversion from wchar_t to char.

  // Convert argv to UTF8.
  char** argv = new char*[argc + 1];
  for (int i = 0; i < argc; i++) {
    // Compute the size of the required buffer
    DWORD size = WideCharToMultiByte(
        CP_UTF8, 0, wargv[i], -1, nullptr, 0, nullptr, nullptr);
    if (size == 0) {
      // This should never happen.
      fprintf(stderr, "Could not convert arguments to utf8.");
      exit(1);
    }
    // Do the actual conversion
    argv[i] = new char[size];
    DWORD result = WideCharToMultiByte(
        CP_UTF8, 0, wargv[i], -1, argv[i], size, nullptr, nullptr);
    if (result == 0) {
      // This should never happen.
      fprintf(stderr, "Could not convert arguments to utf8.");
      exit(1);
    }
  }
  argv[argc] = nullptr;
#else   // UNIX
int main(int argc, char* argv[]) {
  argv = uv_setup_args(argc, argv);

  // Disable stdio buffering, it interacts poorly with printf()
  // calls elsewhere in the program (e.g., any logging from V8.)
  setvbuf(stdout, nullptr, _IONBF, 0);
  setvbuf(stderr, nullptr, _IONBF, 0);
#endif  // _WIN32

  return BuildSnapshot(argc, argv);
}

int BuildSnapshot(int argc, char* argv[]) {
  if (argc < 2) {
    std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n";
    std::cerr << "       " << argv[0] << " --build-snapshot "
              << "<path/to/script.js> <path/to/output.cc>\n";
    return 1;
  }

  std::shared_ptr<node::InitializationResult> result =
      node::InitializeOncePerProcess(
          std::vector<std::string>(argv, argv + argc),
          node::ProcessInitializationFlags::kGeneratePredictableSnapshot);

  CHECK(!result->early_return());
  CHECK_EQ(result->exit_code(), 0);

  std::string out_path;
  std::optional<std::string_view> builder_script_path = std::nullopt;
  if (node::per_process::cli_options->per_isolate->build_snapshot) {
    builder_script_path = result->args()[1];
    out_path = result->args()[2];
  } else {
    out_path = result->args()[1];
  }

#ifdef NODE_MKSNAPSHOT_USE_ARRAY_LITERALS
  bool use_array_literals = true;
#else
  bool use_array_literals = false;
#endif

  node::SnapshotConfig snapshot_config;
  snapshot_config.builder_script_path = builder_script_path;

#ifdef NODE_USE_NODE_CODE_CACHE
  snapshot_config.flags = node::SnapshotFlags::kDefault;
#else
  snapshot_config.flags = node::SnapshotFlags::kWithoutCodeCache;
#endif

  node::ExitCode exit_code =
      node::SnapshotBuilder::GenerateAsSource(out_path.c_str(),
                                              result->args(),
                                              result->exec_args(),
                                              snapshot_config,
                                              use_array_literals);

  node::TearDownOncePerProcess();
  return static_cast<int>(exit_code);
}