summaryrefslogtreecommitdiff
path: root/test/embedding/embedtest.cc
blob: 3592ccb98132286b36349a26902aab267f321c85 (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
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
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "node.h"
#include "uv.h"
#include <assert.h>

// Note: This file is being referred to from doc/api/embedding.md, and excerpts
// from it are included in the documentation. Try to keep these in sync.
// Snapshot support is not part of the embedder API docs yet due to its
// experimental nature, although it is of course documented in node.h.

using node::CommonEnvironmentSetup;
using node::Environment;
using node::MultiIsolatePlatform;
using v8::Context;
using v8::HandleScope;
using v8::Isolate;
using v8::Locker;
using v8::MaybeLocal;
using v8::V8;
using v8::Value;

static int RunNodeInstance(MultiIsolatePlatform* platform,
                           const std::vector<std::string>& args,
                           const std::vector<std::string>& exec_args);

int main(int argc, char** argv) {
  argv = uv_setup_args(argc, argv);
  std::vector<std::string> args(argv, argv + argc);
  std::unique_ptr<node::InitializationResult> result =
      node::InitializeOncePerProcess(
          args,
          {node::ProcessInitializationFlags::kNoInitializeV8,
           node::ProcessInitializationFlags::kNoInitializeNodeV8Platform});

  for (const std::string& error : result->errors())
    fprintf(stderr, "%s: %s\n", args[0].c_str(), error.c_str());
  if (result->early_return() != 0) {
    return result->exit_code();
  }

  std::unique_ptr<MultiIsolatePlatform> platform =
      MultiIsolatePlatform::Create(4);
  V8::InitializePlatform(platform.get());
  V8::Initialize();

  int ret =
      RunNodeInstance(platform.get(), result->args(), result->exec_args());

  V8::Dispose();
  V8::DisposePlatform();

  node::TearDownOncePerProcess();
  return ret;
}

int RunNodeInstance(MultiIsolatePlatform* platform,
                    const std::vector<std::string>& args,
                    const std::vector<std::string>& exec_args) {
  int exit_code = 0;

  node::EmbedderSnapshotData::Pointer snapshot;
  auto snapshot_build_mode_it =
      std::find(args.begin(), args.end(), "--embedder-snapshot-create");
  auto snapshot_arg_it =
      std::find(args.begin(), args.end(), "--embedder-snapshot-blob");
  auto snapshot_as_file_it =
      std::find(args.begin(), args.end(), "--embedder-snapshot-as-file");
  if (snapshot_arg_it < args.end() - 1 &&
      snapshot_build_mode_it == args.end()) {
    const char* filename = (snapshot_arg_it + 1)->c_str();
    FILE* fp = fopen(filename, "r");
    assert(fp != nullptr);
    if (snapshot_as_file_it != args.end()) {
      snapshot = node::EmbedderSnapshotData::FromFile(fp);
    } else {
      uv_fs_t req = uv_fs_t();
      int statret = uv_fs_stat(nullptr, &req, filename, nullptr);
      assert(statret == 0);
      size_t filesize = req.statbuf.st_size;
      uv_fs_req_cleanup(&req);

      std::vector<char> vec(filesize);
      size_t read = fread(vec.data(), filesize, 1, fp);
      assert(read == 1);
      snapshot = node::EmbedderSnapshotData::FromBlob(vec);
    }
    assert(snapshot);
    int ret = fclose(fp);
    assert(ret == 0);
  }

  std::vector<std::string> errors;
  std::unique_ptr<CommonEnvironmentSetup> setup =
      snapshot ? CommonEnvironmentSetup::CreateFromSnapshot(
                     platform, &errors, snapshot.get(), args, exec_args)
      : snapshot_build_mode_it != args.end()
          ? CommonEnvironmentSetup::CreateForSnapshotting(
                platform, &errors, args, exec_args)
          : CommonEnvironmentSetup::Create(platform, &errors, args, exec_args);
  if (!setup) {
    for (const std::string& err : errors)
      fprintf(stderr, "%s: %s\n", args[0].c_str(), err.c_str());
    return 1;
  }

  Isolate* isolate = setup->isolate();
  Environment* env = setup->env();

  {
    Locker locker(isolate);
    Isolate::Scope isolate_scope(isolate);
    HandleScope handle_scope(isolate);
    Context::Scope context_scope(setup->context());

    MaybeLocal<Value> loadenv_ret;
    if (snapshot) {
      loadenv_ret = node::LoadEnvironment(env, node::StartExecutionCallback{});
    } else {
      loadenv_ret = node::LoadEnvironment(
          env,
          // Snapshots do not support userland require()s (yet)
          "if (!require('v8').startupSnapshot.isBuildingSnapshot()) {"
          "  const publicRequire ="
          "    require('module').createRequire(process.cwd() + '/');"
          "  globalThis.require = publicRequire;"
          "} else globalThis.require = require;"
          "globalThis.embedVars = { nön_ascıı: '🏳️‍🌈' };"
          "require('vm').runInThisContext(process.argv[1]);");
    }

    if (loadenv_ret.IsEmpty())  // There has been a JS exception.
      return 1;

    exit_code = node::SpinEventLoop(env).FromMaybe(1);
  }

  if (snapshot_arg_it < args.end() - 1 &&
      snapshot_build_mode_it != args.end()) {
    snapshot = setup->CreateSnapshot();
    assert(snapshot);

    FILE* fp = fopen((snapshot_arg_it + 1)->c_str(), "w");
    assert(fp != nullptr);
    if (snapshot_as_file_it != args.end()) {
      snapshot->ToFile(fp);
    } else {
      const std::vector<char> vec = snapshot->ToBlob();
      size_t written = fwrite(vec.data(), vec.size(), 1, fp);
      assert(written == 1);
    }
    int ret = fclose(fp);
    assert(ret == 0);
  }

  node::Stop(env);

  return exit_code;
}