GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/hooks.cc Lines: 77 77 100.0 %
Date: 2020-06-24 22:13:30 Branches: 17 24 70.8 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_internals.h"
3
#include "node_process.h"
4
#include "async_wrap.h"
5
6
namespace node {
7
8
using v8::Context;
9
using v8::HandleScope;
10
using v8::Integer;
11
using v8::Isolate;
12
using v8::Local;
13
using v8::Object;
14
using v8::String;
15
using v8::Value;
16
using v8::NewStringType;
17
18
4762
void RunAtExit(Environment* env) {
19
4762
  env->RunAtExitCallbacks();
20
4761
}
21
22
1
void AtExit(void (*cb)(void* arg), void* arg) {
23
1
  auto env = Environment::GetThreadLocalEnv();
24
1
  AtExit(env, cb, arg);
25
1
}
26
27
9409
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) {
28
9409
  CHECK_NOT_NULL(env);
29
9409
  env->AtExit(cb, arg);
30
9409
}
31
32
4148
void EmitBeforeExit(Environment* env) {
33
  TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),
34
8294
                              "BeforeExit", env);
35
4148
  if (!env->destroy_async_id_list()->empty())
36
153
    AsyncWrap::DestroyAsyncIdsCallback(env);
37
38
8296
  HandleScope handle_scope(env->isolate());
39
8296
  Context::Scope context_scope(env->context());
40
41
  Local<Value> exit_code_v;
42
16597
  if (!env->process_object()->Get(env->context(), env->exit_code_string())
43
4152
      .ToLocal(&exit_code_v)) return;
44
45
  Local<Integer> exit_code;
46
12444
  if (!exit_code_v->ToInteger(env->context()).ToLocal(&exit_code)) return;
47
48
8294
  ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked();
49
}
50
51
4131
int EmitExit(Environment* env) {
52
  // process.emit('exit')
53
8254
  HandleScope handle_scope(env->isolate());
54
4132
  Context::Scope context_scope(env->context());
55
4131
  Local<Object> process_object = env->process_object();
56
  process_object
57
8263
      ->Set(env->context(),
58
            FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"),
59
20657
            True(env->isolate()))
60
      .Check();
61
62
4132
  Local<String> exit_code = env->exit_code_string();
63
12395
  int code = process_object->Get(env->context(), exit_code)
64
8263
                 .ToLocalChecked()
65
12395
                 ->Int32Value(env->context())
66
4131
                 .ToChecked();
67
8263
  ProcessEmit(env, "exit", Integer::New(env->isolate(), code));
68
69
  // Reload exit code, it may be changed by `emit('exit')`
70
12369
  return process_object->Get(env->context(), exit_code)
71
8246
      .ToLocalChecked()
72
12368
      ->Int32Value(env->context())
73
8246
      .ToChecked();
74
}
75
76
38
void AddEnvironmentCleanupHook(Isolate* isolate,
77
                               void (*fun)(void* arg),
78
                               void* arg) {
79
38
  Environment* env = Environment::GetCurrent(isolate);
80
38
  CHECK_NOT_NULL(env);
81
38
  env->AddCleanupHook(fun, arg);
82
38
}
83
84
24
void RemoveEnvironmentCleanupHook(Isolate* isolate,
85
                                  void (*fun)(void* arg),
86
                                  void* arg) {
87
24
  Environment* env = Environment::GetCurrent(isolate);
88
24
  CHECK_NOT_NULL(env);
89
24
  env->RemoveCleanupHook(fun, arg);
90
24
}
91
92
2
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
93
2
  Environment* env = Environment::GetCurrent(isolate);
94
2
  if (env == nullptr) return -1;
95
2
  return env->execution_async_id();
96
}
97
98
2
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
99
2
  Environment* env = Environment::GetCurrent(isolate);
100
2
  if (env == nullptr) return -1;
101
2
  return env->trigger_async_id();
102
}
103
104
105
539
async_context EmitAsyncInit(Isolate* isolate,
106
                            Local<Object> resource,
107
                            const char* name,
108
                            async_id trigger_async_id) {
109
1078
  HandleScope handle_scope(isolate);
110
  Local<String> type =
111
1078
      String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
112
539
          .ToLocalChecked();
113
1078
  return EmitAsyncInit(isolate, resource, type, trigger_async_id);
114
}
115
116
550
async_context EmitAsyncInit(Isolate* isolate,
117
                            Local<Object> resource,
118
                            Local<String> name,
119
                            async_id trigger_async_id) {
120
550
  DebugSealHandleScope handle_scope(isolate);
121
550
  Environment* env = Environment::GetCurrent(isolate);
122
550
  CHECK_NOT_NULL(env);
123
124
  // Initialize async context struct
125
550
  if (trigger_async_id == -1)
126
547
    trigger_async_id = env->get_default_trigger_async_id();
127
128
  async_context context = {
129
550
    env->new_async_id(),  // async_id_
130
    trigger_async_id  // trigger_async_id_
131
550
  };
132
133
  // Run init hooks
134
550
  AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id,
135
550
                           context.trigger_async_id);
136
137
550
  return context;
138
}
139
140
3
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
141
3
  EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext);
142
3
}
143
144
546
void EmitAsyncDestroy(Environment* env, async_context asyncContext) {
145
546
  AsyncWrap::EmitDestroy(env, asyncContext.async_id);
146
546
}
147
148
}  // namespace node