GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/embed_helpers.cc Lines: 83 92 90.2 %
Date: 2021-06-03 04:12:28 Branches: 33 52 63.5 %

Line Branch Exec Source
1
#include "node.h"
2
#include "env-inl.h"
3
#include "debug_utils-inl.h"
4
5
using v8::Context;
6
using v8::Global;
7
using v8::HandleScope;
8
using v8::Isolate;
9
using v8::Local;
10
using v8::Locker;
11
using v8::Maybe;
12
using v8::Nothing;
13
using v8::SealHandleScope;
14
15
namespace node {
16
17
4903
Maybe<int> SpinEventLoop(Environment* env) {
18
4903
  CHECK_NOT_NULL(env);
19
4903
  MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env);
20
4903
  CHECK_NOT_NULL(platform);
21
22
4903
  Isolate* isolate = env->isolate();
23
9525
  HandleScope handle_scope(isolate);
24
4903
  Context::Scope context_scope(env->context());
25
9525
  SealHandleScope seal(isolate);
26
27
4903
  if (env->is_stopping()) return Nothing<int>();
28
29
4903
  env->set_trace_sync_io(env->options()->trace_sync_io);
30
  {
31
    bool more;
32
4903
    env->performance_state()->Mark(
33
4903
        node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
34
4543
    do {
35
4922
      if (env->is_stopping()) break;
36
4922
      uv_run(env->event_loop(), UV_RUN_DEFAULT);
37
4659
      if (env->is_stopping()) break;
38
39
4555
      platform->DrainTasks(isolate);
40
41
4548
      more = uv_loop_alive(env->event_loop());
42

4548
      if (more && !env->is_stopping()) continue;
43
44
9084
      if (EmitProcessBeforeExit(env).IsNothing())
45
3
        break;
46
47
      // Emit `beforeExit` if the loop became alive either after emitting
48
      // event, or after running some callbacks.
49
4538
      more = uv_loop_alive(env->event_loop());
50

4543
    } while (more == true && !env->is_stopping());
51
4631
    env->performance_state()->Mark(
52
4631
        node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
53
  }
54
4631
  if (env->is_stopping()) return Nothing<int>();
55
56
4523
  env->set_trace_sync_io(false);
57
4523
  env->PrintInfoForSnapshotIfDebug();
58
4523
  env->VerifyNoStrongBaseObjects();
59
4523
  return EmitProcessExit(env);
60
}
61
62
24
struct CommonEnvironmentSetup::Impl {
63
  MultiIsolatePlatform* platform = nullptr;
64
  uv_loop_t loop;
65
  std::shared_ptr<ArrayBufferAllocator> allocator;
66
  Isolate* isolate = nullptr;
67
  DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data;
68
  DeleteFnPtr<Environment, FreeEnvironment> env;
69
  Global<Context> context;
70
};
71
72
7
CommonEnvironmentSetup::CommonEnvironmentSetup(
73
    MultiIsolatePlatform* platform,
74
    std::vector<std::string>* errors,
75
7
    std::function<Environment*(const CommonEnvironmentSetup*)> make_env)
76
7
  : impl_(new Impl()) {
77
7
  CHECK_NOT_NULL(platform);
78
7
  CHECK_NOT_NULL(errors);
79
80
7
  impl_->platform = platform;
81
7
  uv_loop_t* loop = &impl_->loop;
82
  // Use `data` to tell the destructor whether the loop was initialized or not.
83
7
  loop->data = nullptr;
84
7
  int ret = uv_loop_init(loop);
85
7
  if (ret != 0) {
86
    errors->push_back(
87
        SPrintF("Failed to initialize loop: %s", uv_err_name(ret)));
88
    return;
89
  }
90
7
  loop->data = this;
91
92
7
  impl_->allocator = ArrayBufferAllocator::Create();
93
7
  impl_->isolate = NewIsolate(impl_->allocator, &impl_->loop, platform);
94
7
  Isolate* isolate = impl_->isolate;
95
96
  {
97
7
    Locker locker(isolate);
98
14
    Isolate::Scope isolate_scope(isolate);
99
7
    impl_->isolate_data.reset(CreateIsolateData(
100
14
        isolate, loop, platform, impl_->allocator.get()));
101
102
14
    HandleScope handle_scope(isolate);
103
7
    Local<Context> context = NewContext(isolate);
104
7
    impl_->context.Reset(isolate, context);
105
7
    if (context.IsEmpty()) {
106
      errors->push_back("Failed to initialize V8 Context");
107
      return;
108
    }
109
110
    Context::Scope context_scope(context);
111
7
    impl_->env.reset(make_env(this));
112
  }
113
}
114
115
10
CommonEnvironmentSetup::~CommonEnvironmentSetup() {
116
5
  if (impl_->isolate != nullptr) {
117
5
    Isolate* isolate = impl_->isolate;
118
    {
119
5
      Locker locker(isolate);
120
10
      Isolate::Scope isolate_scope(isolate);
121
122
5
      impl_->context.Reset();
123
5
      impl_->env.reset();
124
5
      impl_->isolate_data.reset();
125
    }
126
127
5
    bool platform_finished = false;
128
30
    impl_->platform->AddIsolateFinishedCallback(isolate, [](void* data) {
129
5
      *static_cast<bool*>(data) = true;
130
25
    }, &platform_finished);
131
5
    impl_->platform->UnregisterIsolate(isolate);
132
5
    isolate->Dispose();
133
134
    // Wait until the platform has cleaned up all relevant resources.
135
15
    while (!platform_finished)
136
5
      uv_run(&impl_->loop, UV_RUN_ONCE);
137
  }
138
139

5
  if (impl_->isolate || impl_->loop.data != nullptr)
140
5
    CheckedUvLoopClose(&impl_->loop);
141
142
5
  delete impl_;
143
5
}
144
145
146
uv_loop_t* CommonEnvironmentSetup::event_loop() const {
147
  return &impl_->loop;
148
}
149
150
std::shared_ptr<ArrayBufferAllocator>
151
CommonEnvironmentSetup::array_buffer_allocator() const {
152
  return impl_->allocator;
153
}
154
155
7
Isolate* CommonEnvironmentSetup::isolate() const {
156
7
  return impl_->isolate;
157
}
158
159
7
IsolateData* CommonEnvironmentSetup::isolate_data() const {
160
7
  return impl_->isolate_data.get();
161
}
162
163
7
Environment* CommonEnvironmentSetup::env() const {
164
7
  return impl_->env.get();
165
}
166
167
14
v8::Local<v8::Context> CommonEnvironmentSetup::context() const {
168
28
  return impl_->context.Get(impl_->isolate);
169
}
170
171

14532
}  // namespace node