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: 116 118 98.3 %
Date: 2019-01-07 12:15:22 Branches: 23 36 63.9 %

Line Branch Exec Source
1
#include "node_internals.h"
2
#include "node_errors.h"
3
#include "node_watchdog.h"
4
5
namespace node {
6
namespace util {
7
8
using v8::ALL_PROPERTIES;
9
using v8::Array;
10
using v8::Boolean;
11
using v8::Context;
12
using v8::Function;
13
using v8::FunctionCallbackInfo;
14
using v8::IndexFilter;
15
using v8::Integer;
16
using v8::Isolate;
17
using v8::KeyCollectionMode;
18
using v8::Local;
19
using v8::Object;
20
using v8::ONLY_CONFIGURABLE;
21
using v8::ONLY_ENUMERABLE;
22
using v8::ONLY_WRITABLE;
23
using v8::Private;
24
using v8::Promise;
25
using v8::PropertyFilter;
26
using v8::Proxy;
27
using v8::SKIP_STRINGS;
28
using v8::SKIP_SYMBOLS;
29
using v8::String;
30
using v8::Uint32;
31
using v8::Value;
32
33
115229
static void GetOwnNonIndexProperties(
34
    const FunctionCallbackInfo<Value>& args) {
35
115229
  Environment* env = Environment::GetCurrent(args);
36
115229
  Local<Context> context = env->context();
37
38
230458
  CHECK(args[0]->IsObject());
39
230458
  CHECK(args[1]->IsUint32());
40
41
230458
  Local<Object> object = args[0].As<Object>();
42
43
  Local<Array> properties;
44
45
  PropertyFilter filter =
46
345687
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
47
48
230458
  if (!object->GetPropertyNames(
49
        context, KeyCollectionMode::kOwnOnly,
50
        filter,
51
115229
        IndexFilter::kSkipIndices)
52
345687
          .ToLocal(&properties)) {
53
115231
    return;
54
  }
55
230454
  args.GetReturnValue().Set(properties);
56
}
57
58
59
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
59
  // Return undefined if it's not a Promise.
60
118
  if (!args[0]->IsPromise())
61
59
    return;
62
63
59
  auto isolate = args.GetIsolate();
64
65
118
  Local<Promise> promise = args[0].As<Promise>();
66
67
59
  int state = promise->State();
68
177
  Local<Value> values[2] = { Integer::New(isolate, state) };
69
59
  size_t number_of_values = 1;
70
59
  if (state != Promise::PromiseState::kPending)
71
98
    values[number_of_values++] = promise->Result();
72
59
  Local<Array> ret = Array::New(isolate, values, number_of_values);
73
118
  args.GetReturnValue().Set(ret);
74
}
75
76
249
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
77
  // Return undefined if it's not a proxy.
78
498
  if (!args[0]->IsProxy())
79
456
    return;
80
81
84
  Local<Proxy> proxy = args[0].As<Proxy>();
82
83
  Local<Value> ret[] = {
84
42
    proxy->GetTarget(),
85
42
    proxy->GetHandler()
86
126
  };
87
88
  args.GetReturnValue().Set(
89
168
      Array::New(args.GetIsolate(), ret, arraysize(ret)));
90
}
91
92
119
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
93
238
  if (!args[0]->IsObject())
94
    return;
95
96
119
  Environment* env = Environment::GetCurrent(args);
97
  bool is_key_value;
98
  Local<Array> entries;
99
476
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
100
    return;
101
  // Fast path for WeakMap, WeakSet and Set iterators.
102
119
  if (args.Length() == 1)
103
122
    return args.GetReturnValue().Set(entries);
104
105
  Local<Value> ret[] = {
106
    entries,
107
    Boolean::New(env->isolate(), is_key_value)
108
174
  };
109
  return args.GetReturnValue().Set(
110
174
      Array::New(env->isolate(), ret, arraysize(ret)));
111
}
112
113
// Side effect-free stringification that will never throw exceptions.
114
6
static void SafeToString(const FunctionCallbackInfo<Value>& args) {
115
6
  auto context = args.GetIsolate()->GetCurrentContext();
116
24
  args.GetReturnValue().Set(args[0]->ToDetailString(context).ToLocalChecked());
117
6
}
118
119
134
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
120
#define V(name, _) &Environment::name,
121
  static Local<Private> (Environment::*const methods[])() const = {
122
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
123
  };
124
#undef V
125
134
  CHECK_LT(index, arraysize(methods));
126
134
  return (env->*methods[index])();
127
}
128
129
127
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
130
127
  Environment* env = Environment::GetCurrent(args);
131
132
254
  CHECK(args[0]->IsObject());
133
254
  CHECK(args[1]->IsUint32());
134
135
254
  Local<Object> obj = args[0].As<Object>();
136
508
  auto index = args[1]->Uint32Value(env->context()).FromJust();
137
127
  auto private_symbol = IndexToPrivateSymbol(env, index);
138
254
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
139
140
254
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
141
127
}
142
143
7
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
144
7
  Environment* env = Environment::GetCurrent(args);
145
146
14
  CHECK(args[0]->IsObject());
147
14
  CHECK(args[1]->IsUint32());
148
149
14
  Local<Object> obj = args[0].As<Object>();
150
28
  auto index = args[1]->Uint32Value(env->context()).FromJust();
151
7
  auto private_symbol = IndexToPrivateSymbol(env, index);
152
14
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
153
154
21
  args.GetReturnValue().Set(maybe_value.FromJust());
155
7
}
156
157
158
83
void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
159
83
  int ret = SigintWatchdogHelper::GetInstance()->Start();
160
249
  args.GetReturnValue().Set(ret == 0);
161
83
}
162
163
164
81
void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
165
81
  bool had_pending_signals = SigintWatchdogHelper::GetInstance()->Stop();
166
243
  args.GetReturnValue().Set(had_pending_signals);
167
81
}
168
169
170
3
void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
171
3
  bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal();
172
9
  args.GetReturnValue().Set(ret);
173
3
}
174
175
8
void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
176
8
  Environment* env = Environment::GetCurrent(args);
177
8
  Isolate* isolate = env->isolate();
178
179
16
  CHECK(args[0]->IsFunction());
180
181
16
  isolate->EnqueueMicrotask(args[0].As<Function>());
182
8
}
183
184
3661
void Initialize(Local<Object> target,
185
                Local<Value> unused,
186
                Local<Context> context,
187
                void* priv) {
188
3661
  Environment* env = Environment::GetCurrent(context);
189
190
#define V(name, _)                                                            \
191
  target->Set(context,                                                        \
192
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
193
              Integer::NewFromUnsigned(env->isolate(), index++)).FromJust();
194
  {
195
3661
    uint32_t index = 0;
196
91525
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
197
  }
198
#undef V
199
200
#define V(name)                                                               \
201
  target->Set(context,                                                        \
202
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
203
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
204
    .FromJust()
205
14644
  V(kPending);
206
14644
  V(kFulfilled);
207
14644
  V(kRejected);
208
#undef V
209
210
3661
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
211
3661
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
212
3661
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
213
3661
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
214
3661
  env->SetMethodNoSideEffect(target, "safeToString", SafeToString);
215
3661
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
216
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
217
3661
                                     GetOwnNonIndexProperties);
218
219
3661
  env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
220
3661
  env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
221
  env->SetMethodNoSideEffect(target, "watchdogHasPendingSigint",
222
3661
                             WatchdogHasPendingSigint);
223
224
3661
  env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
225
3661
  env->SetMethod(target, "triggerFatalException", FatalException);
226
3661
  Local<Object> constants = Object::New(env->isolate());
227
14644
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
228
14644
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
229
14644
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
230
14644
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
231
14644
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
232
14644
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
233
  target->Set(context,
234
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
235
10983
              constants).FromJust();
236
237
  Local<String> should_abort_on_uncaught_toggle =
238
3661
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
239
14644
  CHECK(target
240
            ->Set(env->context(),
241
                  should_abort_on_uncaught_toggle,
242
                  env->should_abort_on_uncaught_toggle().GetJSArray())
243
            .FromJust());
244
3661
}
245
246
}  // namespace util
247
}  // namespace node
248
249
3596
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)