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: 82 91 90.1 %
Date: 2021-04-30 04:12:06 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
4879
Maybe<int> SpinEventLoop(Environment* env) {
18
4879
  CHECK_NOT_NULL(env);
19
4879
  MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env);
20
4879
  CHECK_NOT_NULL(platform);
21
22
9478
  HandleScope handle_scope(env->isolate());
23
4879
  Context::Scope context_scope(env->context());
24
9478
  SealHandleScope seal(env->isolate());
25
26
4879
  if (env->is_stopping()) return Nothing<int>();
27
28
4879
  env->set_trace_sync_io(env->options()->trace_sync_io);
29
  {
30
    bool more;
31
4879
    env->performance_state()->Mark(
32
4879
        node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
33
4517
    do {
34
4896
      if (env->is_stopping()) break;
35
4896
      uv_run(env->event_loop(), UV_RUN_DEFAULT);
36
4634
      if (env->is_stopping()) break;
37
38
4530
      platform->DrainTasks(env->isolate());
39
40
4523
      more = uv_loop_alive(env->event_loop());
41

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

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

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

14484
}  // namespace node