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-02-27 22:14:15 Branches: 8 14 57.1 %

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
4411
void RunAtExit(Environment* env) {
19
4411
  env->RunAtExitCallbacks();
20
4412
}
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
8737
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) {
28
8737
  CHECK_NOT_NULL(env);
29
8737
  env->AtExit(cb, arg);
30
8737
}
31
32
3922
void EmitBeforeExit(Environment* env) {
33
3922
  env->RunBeforeExitCallbacks();
34
35
7846
  HandleScope handle_scope(env->isolate());
36
3924
  Context::Scope context_scope(env->context());
37
7846
  Local<Value> exit_code = env->process_object()
38
19616
                               ->Get(env->context(), env->exit_code_string())
39
7846
                               .ToLocalChecked()
40
11770
                               ->ToInteger(env->context())
41
                               .ToLocalChecked();
42
7845
  ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked();
43
3922
}
44
45
3908
int EmitExit(Environment* env) {
46
  // process.emit('exit')
47
7811
  HandleScope handle_scope(env->isolate());
48
3909
  Context::Scope context_scope(env->context());
49
3909
  Local<Object> process_object = env->process_object();
50
  process_object
51
7818
      ->Set(env->context(),
52
            FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"),
53
19544
            True(env->isolate()))
54
      .Check();
55
56
3909
  Local<String> exit_code = env->exit_code_string();
57
11726
  int code = process_object->Get(env->context(), exit_code)
58
7818
                 .ToLocalChecked()
59
11726
                 ->Int32Value(env->context())
60
3909
                 .ToChecked();
61
7818
  ProcessEmit(env, "exit", Integer::New(env->isolate(), code));
62
63
  // Reload exit code, it may be changed by `emit('exit')`
64
11709
  return process_object->Get(env->context(), exit_code)
65
7804
      .ToLocalChecked()
66
11707
      ->Int32Value(env->context())
67
7806
      .ToChecked();
68
}
69
70
38
void AddEnvironmentCleanupHook(Isolate* isolate,
71
                               void (*fun)(void* arg),
72
                               void* arg) {
73
38
  Environment* env = Environment::GetCurrent(isolate);
74
38
  CHECK_NOT_NULL(env);
75
38
  env->AddCleanupHook(fun, arg);
76
38
}
77
78
24
void RemoveEnvironmentCleanupHook(Isolate* isolate,
79
                                  void (*fun)(void* arg),
80
                                  void* arg) {
81
24
  Environment* env = Environment::GetCurrent(isolate);
82
24
  CHECK_NOT_NULL(env);
83
24
  env->RemoveCleanupHook(fun, arg);
84
24
}
85
86
2
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
87
2
  Environment* env = Environment::GetCurrent(isolate);
88
2
  if (env == nullptr) return -1;
89
2
  return env->execution_async_id();
90
}
91
92
2
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
93
2
  Environment* env = Environment::GetCurrent(isolate);
94
2
  if (env == nullptr) return -1;
95
2
  return env->trigger_async_id();
96
}
97
98
99
539
async_context EmitAsyncInit(Isolate* isolate,
100
                            Local<Object> resource,
101
                            const char* name,
102
                            async_id trigger_async_id) {
103
1078
  HandleScope handle_scope(isolate);
104
  Local<String> type =
105
1078
      String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
106
539
          .ToLocalChecked();
107
1078
  return EmitAsyncInit(isolate, resource, type, trigger_async_id);
108
}
109
110
547
async_context EmitAsyncInit(Isolate* isolate,
111
                            Local<Object> resource,
112
                            Local<String> name,
113
                            async_id trigger_async_id) {
114
547
  DebugSealHandleScope handle_scope(isolate);
115
547
  Environment* env = Environment::GetCurrent(isolate);
116
547
  CHECK_NOT_NULL(env);
117
118
  // Initialize async context struct
119
547
  if (trigger_async_id == -1)
120
544
    trigger_async_id = env->get_default_trigger_async_id();
121
122
  async_context context = {
123
547
    env->new_async_id(),  // async_id_
124
    trigger_async_id  // trigger_async_id_
125
547
  };
126
127
  // Run init hooks
128
547
  AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id,
129
547
                           context.trigger_async_id);
130
131
547
  return context;
132
}
133
134
3
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
135
3
  EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext);
136
3
}
137
138
542
void EmitAsyncDestroy(Environment* env, async_context asyncContext) {
139
542
  AsyncWrap::EmitDestroy(env, asyncContext.async_id);
140
542
}
141
142
}  // namespace node