GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_main_instance.cc Lines: 89 90 98.9 %
Date: 2019-07-28 22:34:34 Branches: 37 50 74.0 %

Line Branch Exec Source
1
#include "node_main_instance.h"
2
#include "node_internals.h"
3
#include "node_options-inl.h"
4
#include "node_v8_platform-inl.h"
5
#include "util-inl.h"
6
7
namespace node {
8
9
using v8::Context;
10
using v8::HandleScope;
11
using v8::Isolate;
12
using v8::Local;
13
using v8::Locker;
14
using v8::SealHandleScope;
15
16
1
NodeMainInstance::NodeMainInstance(Isolate* isolate,
17
                                   uv_loop_t* event_loop,
18
                                   MultiIsolatePlatform* platform,
19
                                   const std::vector<std::string>& args,
20
                                   const std::vector<std::string>& exec_args)
21
    : args_(args),
22
      exec_args_(exec_args),
23
      array_buffer_allocator_(nullptr),
24
      isolate_(isolate),
25
      platform_(platform),
26
      isolate_data_(nullptr),
27
      owns_isolate_(false),
28
1
      deserialize_mode_(false) {
29
1
  isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
30
1
  SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
31
1
}
32
33
1
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
34
    Isolate* isolate,
35
    uv_loop_t* event_loop,
36
    MultiIsolatePlatform* platform,
37
    const std::vector<std::string>& args,
38
    const std::vector<std::string>& exec_args) {
39
  return std::unique_ptr<NodeMainInstance>(
40
1
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
41
}
42
43
4768
NodeMainInstance::NodeMainInstance(
44
    Isolate::CreateParams* params,
45
    uv_loop_t* event_loop,
46
    MultiIsolatePlatform* platform,
47
    const std::vector<std::string>& args,
48
    const std::vector<std::string>& exec_args,
49
    const std::vector<size_t>* per_isolate_data_indexes)
50
    : args_(args),
51
      exec_args_(exec_args),
52
      array_buffer_allocator_(ArrayBufferAllocator::Create()),
53
      isolate_(nullptr),
54
      platform_(platform),
55
      isolate_data_(nullptr),
56
4768
      owns_isolate_(true) {
57
4768
  params->array_buffer_allocator = array_buffer_allocator_.get();
58
4768
  isolate_ = Isolate::Allocate();
59
4768
  CHECK_NOT_NULL(isolate_);
60
  // Register the isolate on the platform before the isolate gets initialized,
61
  // so that the isolate can access the platform during initialization.
62
4768
  platform->RegisterIsolate(isolate_, event_loop);
63
4768
  SetIsolateCreateParamsForNode(params);
64
4768
  Isolate::Initialize(isolate_, *params);
65
66
4768
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
67
  // If the indexes are not nullptr, we are not deserializing
68

4768
  CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr);
69
  isolate_data_.reset(new IsolateData(isolate_,
70
                                      event_loop,
71
                                      platform,
72
4768
                                      array_buffer_allocator_.get(),
73
4768
                                      per_isolate_data_indexes));
74
4768
  SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
75
4768
  if (!deserialize_mode_) {
76
    // If in deserialize mode, delay until after the deserialization is
77
    // complete.
78
1
    SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
79
  }
80
4768
}
81
82
1
void NodeMainInstance::Dispose() {
83
1
  CHECK(!owns_isolate_);
84
1
  platform_->DrainTasks(isolate_);
85
1
}
86
87


8748
NodeMainInstance::~NodeMainInstance() {
88
4374
  if (!owns_isolate_) {
89
1
    return;
90
  }
91
4373
  isolate_->Dispose();
92
4373
  platform_->UnregisterIsolate(isolate_);
93
4373
}
94
95
4768
int NodeMainInstance::Run() {
96
4768
  Locker locker(isolate_);
97
9141
  Isolate::Scope isolate_scope(isolate_);
98
9141
  HandleScope handle_scope(isolate_);
99
100
4768
  int exit_code = 0;
101
9141
  std::unique_ptr<Environment> env = CreateMainEnvironment(&exit_code);
102
103
4768
  CHECK_NOT_NULL(env);
104
4768
  Context::Scope context_scope(env->context());
105
106
4768
  if (exit_code == 0) {
107
    {
108
4767
      AsyncCallbackScope callback_scope(env.get());
109
4767
      env->async_hooks()->push_async_ids(1, 0);
110
4767
      LoadEnvironment(env.get());
111
4524
      env->async_hooks()->pop_async_id(1);
112
    }
113
114
    {
115
4523
      SealHandleScope seal(isolate_);
116
      bool more;
117
      env->performance_state()->Mark(
118
4523
          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
119
4390
      do {
120
4536
        uv_run(env->event_loop(), UV_RUN_DEFAULT);
121
122
4391
        per_process::v8_platform.DrainVMTasks(isolate_);
123
124
4391
        more = uv_loop_alive(env->event_loop());
125

4391
        if (more && !env->is_stopping()) continue;
126
127
4391
        env->RunBeforeExitCallbacks();
128
129
4391
        if (!uv_loop_alive(env->event_loop())) {
130
4391
          EmitBeforeExit(env.get());
131
        }
132
133
        // Emit `beforeExit` if the loop became alive either after emitting
134
        // event, or after running some callbacks.
135
4390
        more = uv_loop_alive(env->event_loop());
136

4390
      } while (more == true && !env->is_stopping());
137
      env->performance_state()->Mark(
138
4377
          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
139
    }
140
141
4377
    env->set_trace_sync_io(false);
142
4377
    exit_code = EmitExit(env.get());
143
4372
    WaitForInspectorDisconnect(env.get());
144
  }
145
146
4373
  env->set_can_call_into_js(false);
147
4373
  env->stop_sub_worker_contexts();
148
4373
  ResetStdio();
149
4373
  env->RunCleanup();
150
4373
  RunAtExit(env.get());
151
152
4373
  per_process::v8_platform.DrainVMTasks(isolate_);
153
4373
  per_process::v8_platform.CancelVMTasks(isolate_);
154
155
#if defined(LEAK_SANITIZER)
156
  __lsan_do_leak_check();
157
#endif
158
159
8746
  return exit_code;
160
}
161
162
// TODO(joyeecheung): align this with the CreateEnvironment exposed in node.h
163
// and the environment creation routine in workers somehow.
164
4768
std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
165
    int* exit_code) {
166
4768
  *exit_code = 0;  // Reset the exit code to 0
167
168
4768
  HandleScope handle_scope(isolate_);
169
170
  // TODO(addaleax): This should load a real per-Isolate option, currently
171
  // this is still effectively per-process.
172
4768
  if (isolate_data_->options()->track_heap_objects) {
173
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
174
  }
175
176
  Local<Context> context;
177
4768
  if (deserialize_mode_) {
178
    context =
179
9534
        Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
180
4767
    SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
181
  } else {
182
1
    context = NewContext(isolate_);
183
  }
184
185
4768
  CHECK(!context.IsEmpty());
186
  Context::Scope context_scope(context);
187
188
  std::unique_ptr<Environment> env = std::make_unique<Environment>(
189
4768
      isolate_data_.get(),
190
      context,
191
      args_,
192
      exec_args_,
193
      static_cast<Environment::Flags>(Environment::kIsMainThread |
194
                                      Environment::kOwnsProcessState |
195
9536
                                      Environment::kOwnsInspector));
196
4768
  env->InitializeLibuv(per_process::v8_is_profiling);
197
4768
  env->InitializeDiagnostics();
198
199
  // TODO(joyeecheung): when we snapshot the bootstrapped context,
200
  // the inspector and diagnostics setup should after after deserialization.
201
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
202
4768
  *exit_code = env->InitializeInspector(nullptr);
203
#endif
204
4768
  if (*exit_code != 0) {
205
1
    return env;
206
  }
207
208
9534
  if (env->RunBootstrapping().IsEmpty()) {
209
    *exit_code = 1;
210
  }
211
212
9535
  return env;
213
}
214
215
}  // namespace node