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-11-19 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







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


40090
    args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
51
5814
}
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
389
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
72
  // Return undefined if it's not a proxy.
73
778
  if (!args[0]->IsProxy())
74
734
    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
6676
static void SafeToString(const FunctionCallbackInfo<Value>& args) {
87
6676
  auto context = args.GetIsolate()->GetCurrentContext();
88
26704
  args.GetReturnValue().Set(args[0]->ToDetailString(context).ToLocalChecked());
89
6676
}
90
91
81
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
81
  CHECK_LT(index, arraysize(methods));
98
81
  return (env->*methods[index])();
99
}
100
101
78
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
102
78
  Environment* env = Environment::GetCurrent(args);
103
104
156
  CHECK(args[0]->IsObject());
105
156
  CHECK(args[1]->IsUint32());
106
107
156
  Local<Object> obj = args[0].As<Object>();
108
312
  auto index = args[1]->Uint32Value(env->context()).FromJust();
109
78
  auto private_symbol = IndexToPrivateSymbol(env, index);
110
156
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
111
112
156
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
113
78
}
114
115
3
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
116
3
  Environment* env = Environment::GetCurrent(args);
117
118
6
  CHECK(args[0]->IsObject());
119
6
  CHECK(args[1]->IsUint32());
120
121
6
  Local<Object> obj = args[0].As<Object>();
122
12
  auto index = args[1]->Uint32Value(env->context()).FromJust();
123
3
  auto private_symbol = IndexToPrivateSymbol(env, index);
124
6
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
125
126
9
  args.GetReturnValue().Set(maybe_value.FromJust());
127
3
}
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
73
void CreatePromise(const FunctionCallbackInfo<Value>& args) {
149
73
  Local<Context> context = args.GetIsolate()->GetCurrentContext();
150
73
  auto maybe_resolver = Promise::Resolver::New(context);
151
73
  if (!maybe_resolver.IsEmpty())
152
146
    args.GetReturnValue().Set(maybe_resolver.ToLocalChecked());
153
73
}
154
155
156
63
void PromiseResolve(const FunctionCallbackInfo<Value>& args) {
157
63
  Local<Context> context = args.GetIsolate()->GetCurrentContext();
158
63
  Local<Value> promise = args[0];
159
63
  CHECK(promise->IsPromise());
160
189
  if (promise.As<Promise>()->State() != Promise::kPending) return;
161
62
  Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();  // sic
162
62
  Maybe<bool> ret = resolver->Resolve(context, args[1]);
163
248
  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
3287
void Initialize(Local<Object> target,
179
                Local<Value> unused,
180
                Local<Context> context) {
181
3287
  Environment* env = Environment::GetCurrent(context);
182
183
#define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
184
3287
  VALUE_METHOD_MAP(V)
185
#undef V
186
187
3287
  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
3287
    uint32_t index = 0;
195
92036
    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
16435
    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
13148
  V(kPending);
211
13148
  V(kFulfilled);
212
13148
  V(kRejected);
213
#undef V
214
215
3287
  env->SetMethod(target, "getHiddenValue", GetHiddenValue);
216
3287
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
217
3287
  env->SetMethod(target, "getPromiseDetails", GetPromiseDetails);
218
3287
  env->SetMethod(target, "getProxyDetails", GetProxyDetails);
219
3287
  env->SetMethod(target, "safeToString", SafeToString);
220
221
3287
  env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
222
3287
  env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
223
3287
  env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
224
225
3287
  env->SetMethod(target, "createPromise", CreatePromise);
226
3287
  env->SetMethod(target, "promiseResolve", PromiseResolve);
227
3287
  env->SetMethod(target, "promiseReject", PromiseReject);
228
3287
}
229
230
}  // namespace util
231
}  // namespace node
232
233
3332
NODE_BUILTIN_MODULE_CONTEXT_AWARE(util, node::util::Initialize)