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 78 94.9 %
Date: 2019-02-23 22:23:05 Branches: 8 16 50.0 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node.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
4035
void RunAtExit(Environment* env) {
19
4035
  env->RunAtExitCallbacks();
20
4035
}
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
3994
void EmitBeforeExit(Environment* env) {
33
3994
  HandleScope handle_scope(env->isolate());
34
3994
  Context::Scope context_scope(env->context());
35
  Local<Value> exit_code = env->process_object()
36
15976
                               ->Get(env->context(), env->exit_code_string())
37
7988
                               .ToLocalChecked()
38
15976
                               ->ToInteger(env->context())
39
7988
                               .ToLocalChecked();
40
11980
  ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked();
41
3993
}
42
43
3980
int EmitExit(Environment* env) {
44
  // process.emit('exit')
45
3980
  HandleScope handle_scope(env->isolate());
46
3980
  Context::Scope context_scope(env->context());
47
3980
  Local<Object> process_object = env->process_object();
48
  process_object
49
      ->Set(env->context(),
50
            FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"),
51
19900
            True(env->isolate()))
52
7960
      .FromJust();
53
54
3980
  Local<String> exit_code = env->exit_code_string();
55
11940
  int code = process_object->Get(env->context(), exit_code)
56
7960
                 .ToLocalChecked()
57
15920
                 ->Int32Value(env->context())
58
7960
                 .ToChecked();
59
7960
  ProcessEmit(env, "exit", Integer::New(env->isolate(), code));
60
61
  // Reload exit code, it may be changed by `emit('exit')`
62
11925
  return process_object->Get(env->context(), exit_code)
63
7950
      .ToLocalChecked()
64
15900
      ->Int32Value(env->context())
65
11925
      .ToChecked();
66
}
67
68
void AddPromiseHook(Isolate* isolate, promise_hook_func fn, void* arg) {
69
  Environment* env = Environment::GetCurrent(isolate);
70
  CHECK_NOT_NULL(env);
71
  env->AddPromiseHook(fn, arg);
72
}
73
74
28
void AddEnvironmentCleanupHook(Isolate* isolate,
75
                               void (*fun)(void* arg),
76
                               void* arg) {
77
28
  Environment* env = Environment::GetCurrent(isolate);
78
28
  CHECK_NOT_NULL(env);
79
28
  env->AddCleanupHook(fun, arg);
80
28
}
81
82
21
void RemoveEnvironmentCleanupHook(Isolate* isolate,
83
                                  void (*fun)(void* arg),
84
                                  void* arg) {
85
21
  Environment* env = Environment::GetCurrent(isolate);
86
21
  CHECK_NOT_NULL(env);
87
21
  env->RemoveCleanupHook(fun, arg);
88
21
}
89
90
2
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
91
  // Environment::GetCurrent() allocates a Local<> handle.
92
2
  HandleScope handle_scope(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
  // Environment::GetCurrent() allocates a Local<> handle.
100
2
  HandleScope handle_scope(isolate);
101
2
  Environment* env = Environment::GetCurrent(isolate);
102
2
  if (env == nullptr) return -1;
103
2
  return env->trigger_async_id();
104
}
105
106
107
534
async_context EmitAsyncInit(Isolate* isolate,
108
                            Local<Object> resource,
109
                            const char* name,
110
                            async_id trigger_async_id) {
111
534
  HandleScope handle_scope(isolate);
112
  Local<String> type =
113
      String::NewFromUtf8(isolate, name, NewStringType::kInternalized)
114
1068
          .ToLocalChecked();
115
534
  return EmitAsyncInit(isolate, resource, type, trigger_async_id);
116
}
117
118
541
async_context EmitAsyncInit(Isolate* isolate,
119
                            Local<Object> resource,
120
                            Local<String> name,
121
                            async_id trigger_async_id) {
122
541
  HandleScope handle_scope(isolate);
123
541
  Environment* env = Environment::GetCurrent(isolate);
124
541
  CHECK_NOT_NULL(env);
125
126
  // Initialize async context struct
127
541
  if (trigger_async_id == -1)
128
538
    trigger_async_id = env->get_default_trigger_async_id();
129
130
  async_context context = {
131
541
    env->new_async_id(),  // async_id_
132
    trigger_async_id  // trigger_async_id_
133
541
  };
134
135
  // Run init hooks
136
  AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id,
137
541
                           context.trigger_async_id);
138
139
541
  return context;
140
}
141
142
538
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
143
  // Environment::GetCurrent() allocates a Local<> handle.
144
538
  HandleScope handle_scope(isolate);
145
  AsyncWrap::EmitDestroy(
146
538
      Environment::GetCurrent(isolate), asyncContext.async_id);
147
538
}
148
149
}  // namespace node