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