GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_util.cc Lines: 108 108 100.0 %
Date: 2017-12-18 Branches: 39 68 57.4 %

Line Branch Exec Source
1
#include "node_internals.h"
2
#include "node_watchdog.h"
3
4
namespace node {
5
namespace util {
6
7
using v8::Array;
8
using v8::Context;
9
using v8::FunctionCallbackInfo;
10
using v8::Integer;
11
using v8::Local;
12
using v8::Maybe;
13
using v8::Object;
14
using v8::Private;
15
using v8::Promise;
16
using v8::Proxy;
17
using v8::Value;
18
19
20
#define VALUE_METHOD_MAP(V)                                                   \
21
  V(isArrayBuffer, IsArrayBuffer)                                             \
22
  V(isArrayBufferView, IsArrayBufferView)                                     \
23
  V(isAsyncFunction, IsAsyncFunction)                                         \
24
  V(isDataView, IsDataView)                                                   \
25
  V(isDate, IsDate)                                                           \
26
  V(isExternal, IsExternal)                                                   \
27
  V(isMap, IsMap)                                                             \
28
  V(isMapIterator, IsMapIterator)                                             \
29
  V(isNativeError, IsNativeError)                                             \
30
  V(isPromise, IsPromise)                                                     \
31
  V(isRegExp, IsRegExp)                                                       \
32
  V(isSet, IsSet)                                                             \
33
  V(isSetIterator, IsSetIterator)                                             \
34
  V(isTypedArray, IsTypedArray)                                               \
35
  V(isUint8Array, IsUint8Array)
36
37
38
#define V(_, ucname) \
39
  static void ucname(const FunctionCallbackInfo<Value>& args) {               \
40
    CHECK_EQ(1, args.Length());                                               \
41
    args.GetReturnValue().Set(args[0]->ucname());                             \
42
  }
43
44







1182444
  VALUE_METHOD_MAP(V)
45
#undef V
46
47
6137
static void IsAnyArrayBuffer(const FunctionCallbackInfo<Value>& args) {
48
6137
  CHECK_EQ(1, args.Length());
49
  args.GetReturnValue().Set(
50


42223
    args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
51
6137
}
52
53
11
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
54
  // Return undefined if it's not a Promise.
55
22
  if (!args[0]->IsPromise())
56
11
    return;
57
58
11
  auto isolate = args.GetIsolate();
59
60
22
  Local<Promise> promise = args[0].As<Promise>();
61
11
  Local<Array> ret = Array::New(isolate, 2);
62
63
11
  int state = promise->State();
64
22
  ret->Set(0, Integer::New(isolate, state));
65
11
  if (state != Promise::PromiseState::kPending)
66
14
    ret->Set(1, promise->Result());
67
68
22
  args.GetReturnValue().Set(ret);
69
}
70
71
257
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
72
  // Return undefined if it's not a proxy.
73
514
  if (!args[0]->IsProxy())
74
470
    return;
75
76
88
  Local<Proxy> proxy = args[0].As<Proxy>();
77
78
44
  Local<Array> ret = Array::New(args.GetIsolate(), 2);
79
88
  ret->Set(0, proxy->GetTarget());
80
88
  ret->Set(1, proxy->GetHandler());
81
82
88
  args.GetReturnValue().Set(ret);
83
}
84
85
// Side effect-free stringification that will never throw exceptions.
86
3
static void SafeToString(const FunctionCallbackInfo<Value>& args) {
87
3
  auto context = args.GetIsolate()->GetCurrentContext();
88
12
  args.GetReturnValue().Set(args[0]->ToDetailString(context).ToLocalChecked());
89
3
}
90
91
101
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
92
#define V(name, _) &Environment::name,
93
  static Local<Private> (Environment::*const methods[])() const = {
94
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
95
  };
96
#undef V
97
101
  CHECK_LT(index, arraysize(methods));
98
101
  return (env->*methods[index])();
99
}
100
101
96
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
102
96
  Environment* env = Environment::GetCurrent(args);
103
104
192
  CHECK(args[0]->IsObject());
105
192
  CHECK(args[1]->IsUint32());
106
107
192
  Local<Object> obj = args[0].As<Object>();
108
384
  auto index = args[1]->Uint32Value(env->context()).FromJust();
109
96
  auto private_symbol = IndexToPrivateSymbol(env, index);
110
192
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
111
112
192
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
113
96
}
114
115
5
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
116
5
  Environment* env = Environment::GetCurrent(args);
117
118
10
  CHECK(args[0]->IsObject());
119
10
  CHECK(args[1]->IsUint32());
120
121
10
  Local<Object> obj = args[0].As<Object>();
122
20
  auto index = args[1]->Uint32Value(env->context()).FromJust();
123
5
  auto private_symbol = IndexToPrivateSymbol(env, index);
124
10
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
125
126
15
  args.GetReturnValue().Set(maybe_value.FromJust());
127
5
}
128
129
130
79
void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
131
79
  int ret = SigintWatchdogHelper::GetInstance()->Start();
132
237
  args.GetReturnValue().Set(ret == 0);
133
79
}
134
135
136
77
void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
137
77
  bool had_pending_signals = SigintWatchdogHelper::GetInstance()->Stop();
138
231
  args.GetReturnValue().Set(had_pending_signals);
139
77
}
140
141
142
3
void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
143
3
  bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal();
144
9
  args.GetReturnValue().Set(ret);
145
3
}
146
147
148
91
void CreatePromise(const FunctionCallbackInfo<Value>& args) {
149
91
  Local<Context> context = args.GetIsolate()->GetCurrentContext();
150
91
  auto maybe_resolver = Promise::Resolver::New(context);
151
91
  if (!maybe_resolver.IsEmpty())
152
182
    args.GetReturnValue().Set(maybe_resolver.ToLocalChecked());
153
91
}
154
155
156
81
void PromiseResolve(const FunctionCallbackInfo<Value>& args) {
157
81
  Local<Context> context = args.GetIsolate()->GetCurrentContext();
158
81
  Local<Value> promise = args[0];
159
81
  CHECK(promise->IsPromise());
160
243
  if (promise.As<Promise>()->State() != Promise::kPending) return;
161
80
  Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();  // sic
162
80
  Maybe<bool> ret = resolver->Resolve(context, args[1]);
163
320
  args.GetReturnValue().Set(ret.FromMaybe(false));
164
}
165
166
167
12
void PromiseReject(const FunctionCallbackInfo<Value>& args) {
168
12
  Local<Context> context = args.GetIsolate()->GetCurrentContext();
169
12
  Local<Value> promise = args[0];
170
12
  CHECK(promise->IsPromise());
171
36
  if (promise.As<Promise>()->State() != Promise::kPending) return;
172
11
  Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();  // sic
173
11
  Maybe<bool> ret = resolver->Reject(context, args[1]);
174
44
  args.GetReturnValue().Set(ret.FromMaybe(false));
175
}
176
177
178
3348
void Initialize(Local<Object> target,
179
                Local<Value> unused,
180
                Local<Context> context) {
181
3348
  Environment* env = Environment::GetCurrent(context);
182
183
#define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
184
3348
  VALUE_METHOD_MAP(V)
185
#undef V
186
187
3348
  env->SetMethod(target, "isAnyArrayBuffer", IsAnyArrayBuffer);
188
189
#define V(name, _)                                                            \
190
  target->Set(context,                                                        \
191
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
192
              Integer::NewFromUnsigned(env->isolate(), index++)).FromJust();
193
  {
194
3348
    uint32_t index = 0;
195
83700
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
196
  }
197
#undef V
198
199
  target->DefineOwnProperty(
200
    env->context(),
201
    OneByteString(env->isolate(), "pushValToArrayMax"),
202
    Integer::NewFromUnsigned(env->isolate(), NODE_PUSH_VAL_TO_ARRAY_MAX),
203
16740
    v8::ReadOnly).FromJust();
204
205
#define V(name)                                                               \
206
  target->Set(context,                                                        \
207
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
208
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
209
    .FromJust()
210
13392
  V(kPending);
211
13392
  V(kFulfilled);
212
13392
  V(kRejected);
213
#undef V
214
215
3348
  env->SetMethod(target, "getHiddenValue", GetHiddenValue);
216
3348
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
217
3348
  env->SetMethod(target, "getPromiseDetails", GetPromiseDetails);
218
3348
  env->SetMethod(target, "getProxyDetails", GetProxyDetails);
219
3348
  env->SetMethod(target, "safeToString", SafeToString);
220
221
3348
  env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
222
3348
  env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
223
3348
  env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
224
225
3348
  env->SetMethod(target, "createPromise", CreatePromise);
226
3348
  env->SetMethod(target, "promiseResolve", PromiseResolve);
227
3348
  env->SetMethod(target, "promiseReject", PromiseReject);
228
3348
}
229
230
}  // namespace util
231
}  // namespace node
232
233
3391
NODE_BUILTIN_MODULE_CONTEXT_AWARE(util, node::util::Initialize)