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: 110 112 98.2 %
Date: 2017-10-21 Branches: 43 70 61.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







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


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