GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_util.cc Lines: 170 177 96.0 %
Date: 2020-02-19 22:14:06 Branches: 50 73 68.5 %

Line Branch Exec Source
1
#include "node_errors.h"
2
#include "util-inl.h"
3
#include "base_object-inl.h"
4
5
namespace node {
6
namespace util {
7
8
using v8::ALL_PROPERTIES;
9
using v8::Array;
10
using v8::ArrayBufferView;
11
using v8::Boolean;
12
using v8::Context;
13
using v8::Function;
14
using v8::FunctionCallbackInfo;
15
using v8::FunctionTemplate;
16
using v8::Global;
17
using v8::IndexFilter;
18
using v8::Integer;
19
using v8::Isolate;
20
using v8::KeyCollectionMode;
21
using v8::Local;
22
using v8::Object;
23
using v8::ONLY_CONFIGURABLE;
24
using v8::ONLY_ENUMERABLE;
25
using v8::ONLY_WRITABLE;
26
using v8::Private;
27
using v8::Promise;
28
using v8::PropertyFilter;
29
using v8::Proxy;
30
using v8::SKIP_STRINGS;
31
using v8::SKIP_SYMBOLS;
32
using v8::String;
33
using v8::Uint32;
34
using v8::Value;
35
36
136529
static void GetOwnNonIndexProperties(
37
    const FunctionCallbackInfo<Value>& args) {
38
136529
  Environment* env = Environment::GetCurrent(args);
39
136529
  Local<Context> context = env->context();
40
41
273058
  CHECK(args[0]->IsObject());
42
273058
  CHECK(args[1]->IsUint32());
43
44
273058
  Local<Object> object = args[0].As<Object>();
45
46
  Local<Array> properties;
47
48
  PropertyFilter filter =
49
409587
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
50
51
273058
  if (!object->GetPropertyNames(
52
        context, KeyCollectionMode::kOwnOnly,
53
        filter,
54
136529
        IndexFilter::kSkipIndices)
55
136529
          .ToLocal(&properties)) {
56
2
    return;
57
  }
58
273054
  args.GetReturnValue().Set(properties);
59
}
60
61
1033
static void GetConstructorName(
62
    const FunctionCallbackInfo<Value>& args) {
63
2066
  CHECK(args[0]->IsObject());
64
65
2066
  Local<Object> object = args[0].As<Object>();
66
1033
  Local<String> name = object->GetConstructorName();
67
68
2066
  args.GetReturnValue().Set(name);
69
1033
}
70
71
61
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
72
  // Return undefined if it's not a Promise.
73
122
  if (!args[0]->IsPromise())
74
    return;
75
76
61
  auto isolate = args.GetIsolate();
77
78
122
  Local<Promise> promise = args[0].As<Promise>();
79
80
61
  int state = promise->State();
81
183
  Local<Value> values[2] = { Integer::New(isolate, state) };
82
61
  size_t number_of_values = 1;
83
61
  if (state != Promise::PromiseState::kPending)
84
100
    values[number_of_values++] = promise->Result();
85
61
  Local<Array> ret = Array::New(isolate, values, number_of_values);
86
122
  args.GetReturnValue().Set(ret);
87
}
88
89
257990
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
90
  // Return undefined if it's not a proxy.
91
515980
  if (!args[0]->IsProxy())
92
257888
    return;
93
94
204
  Local<Proxy> proxy = args[0].As<Proxy>();
95
96
  // TODO(BridgeAR): Remove the length check as soon as we prohibit access to
97
  // the util binding layer. It's accessed in the wild and `esm` would break in
98
  // case the check is removed.
99

304
  if (args.Length() == 1 || args[1]->IsTrue()) {
100
    Local<Value> ret[] = {
101
43
      proxy->GetTarget(),
102
43
      proxy->GetHandler()
103
129
    };
104
105
172
    args.GetReturnValue().Set(
106
        Array::New(args.GetIsolate(), ret, arraysize(ret)));
107
  } else {
108
59
    Local<Value> ret = proxy->GetTarget();
109
110
118
    args.GetReturnValue().Set(ret);
111
  }
112
}
113
114
122
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
115
244
  if (!args[0]->IsObject())
116
    return;
117
118
122
  Environment* env = Environment::GetCurrent(args);
119
  bool is_key_value;
120
  Local<Array> entries;
121
488
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
122
    return;
123
  // Fast path for WeakMap and WeakSet.
124
122
  if (args.Length() == 1)
125
16
    return args.GetReturnValue().Set(entries);
126
127
  Local<Value> ret[] = {
128
    entries,
129
    Boolean::New(env->isolate(), is_key_value)
130
342
  };
131
342
  return args.GetReturnValue().Set(
132
114
      Array::New(env->isolate(), ret, arraysize(ret)));
133
}
134
135
133
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
136
#define V(name, _) &Environment::name,
137
  static Local<Private> (Environment::*const methods[])() const = {
138
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
139
  };
140
#undef V
141
133
  CHECK_LT(index, arraysize(methods));
142
133
  return (env->*methods[index])();
143
}
144
145
127
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
146
127
  Environment* env = Environment::GetCurrent(args);
147
148
254
  CHECK(args[0]->IsObject());
149
254
  CHECK(args[1]->IsUint32());
150
151
254
  Local<Object> obj = args[0].As<Object>();
152
508
  auto index = args[1]->Uint32Value(env->context()).FromJust();
153
127
  auto private_symbol = IndexToPrivateSymbol(env, index);
154
254
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
155
156
254
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
157
127
}
158
159
6
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
160
6
  Environment* env = Environment::GetCurrent(args);
161
162
12
  CHECK(args[0]->IsObject());
163
12
  CHECK(args[1]->IsUint32());
164
165
12
  Local<Object> obj = args[0].As<Object>();
166
24
  auto index = args[1]->Uint32Value(env->context()).FromJust();
167
6
  auto private_symbol = IndexToPrivateSymbol(env, index);
168
12
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
169
170
18
  args.GetReturnValue().Set(maybe_value.FromJust());
171
6
}
172
173
12
static void Sleep(const FunctionCallbackInfo<Value>& args) {
174
24
  CHECK(args[0]->IsUint32());
175
36
  uint32_t msec = args[0].As<Uint32>()->Value();
176
12
  uv_sleep(msec);
177
12
}
178
179
56
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
180
112
  CHECK(args[0]->IsArrayBufferView());
181
280
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
182
56
}
183
184
795
class WeakReference : public BaseObject {
185
 public:
186
300
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
187
600
    : BaseObject(env, object) {
188
300
    MakeWeak();
189
300
    target_.Reset(env->isolate(), target);
190
300
    target_.SetWeak();
191
300
  }
192
193
300
  static void New(const FunctionCallbackInfo<Value>& args) {
194
300
    Environment* env = Environment::GetCurrent(args);
195
300
    CHECK(args.IsConstructCall());
196
600
    CHECK(args[0]->IsObject());
197
900
    new WeakReference(env, args.This(), args[0].As<Object>());
198
300
  }
199
200
1313
  static void Get(const FunctionCallbackInfo<Value>& args) {
201
1313
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
202
1313
    Isolate* isolate = args.GetIsolate();
203
2626
    if (!weak_ref->target_.IsEmpty())
204
3936
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
205
1313
  }
206
207
657
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
208
657
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
209
657
    weak_ref->reference_count_++;
210
1314
    if (weak_ref->target_.IsEmpty()) return;
211
657
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
212
  }
213
214
654
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
215
654
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
216
654
    CHECK_GE(weak_ref->reference_count_, 1);
217
654
    weak_ref->reference_count_--;
218
1308
    if (weak_ref->target_.IsEmpty()) return;
219
654
    if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
220
  }
221
222
  SET_MEMORY_INFO_NAME(WeakReference)
223
  SET_SELF_SIZE(WeakReference)
224
  SET_NO_MEMORY_INFO()
225
226
 private:
227
  Global<Object> target_;
228
  uint64_t reference_count_ = 0;
229
};
230
231
4234
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
232
4234
  Environment* env = Environment::GetCurrent(args);
233
  int fd;
234
16936
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
235
4234
  CHECK_GE(fd, 0);
236
237
4234
  uv_handle_type t = uv_guess_handle(fd);
238
4234
  const char* type = nullptr;
239
240

4234
  switch (t) {
241
    case UV_TCP:
242
4
      type = "TCP";
243
4
      break;
244
    case UV_TTY:
245
36
      type = "TTY";
246
36
      break;
247
    case UV_UDP:
248
6
      type = "UDP";
249
6
      break;
250
    case UV_FILE:
251
2165
      type = "FILE";
252
2165
      break;
253
    case UV_NAMED_PIPE:
254
2018
      type = "PIPE";
255
2018
      break;
256
    case UV_UNKNOWN_HANDLE:
257
5
      type = "UNKNOWN";
258
5
      break;
259
    default:
260
      ABORT();
261
  }
262
263
12702
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
264
}
265
266
4377
void Initialize(Local<Object> target,
267
                Local<Value> unused,
268
                Local<Context> context,
269
                void* priv) {
270
4377
  Environment* env = Environment::GetCurrent(context);
271
272
#define V(name, _)                                                            \
273
  target->Set(context,                                                        \
274
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
275
              Integer::NewFromUnsigned(env->isolate(), index++)).Check();
276
  {
277
4377
    uint32_t index = 0;
278
96294
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
279
  }
280
#undef V
281
282
#define V(name)                                                               \
283
  target->Set(context,                                                        \
284
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
285
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
286
    .FromJust()
287
17508
  V(kPending);
288
17508
  V(kFulfilled);
289
17508
  V(kRejected);
290
#undef V
291
292
4377
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
293
4377
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
294
4377
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
295
4377
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
296
4377
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
297
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
298
4377
                                     GetOwnNonIndexProperties);
299
4377
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
300
4377
  env->SetMethod(target, "sleep", Sleep);
301
302
4377
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
303
4377
  Local<Object> constants = Object::New(env->isolate());
304
17508
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
305
17508
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
306
17508
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
307
17508
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
308
17508
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
309
17508
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
310
8754
  target->Set(context,
311
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
312
13131
              constants).Check();
313
314
  Local<String> should_abort_on_uncaught_toggle =
315
4377
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
316
17508
  CHECK(target
317
            ->Set(env->context(),
318
                  should_abort_on_uncaught_toggle,
319
                  env->should_abort_on_uncaught_toggle().GetJSArray())
320
            .FromJust());
321
322
  Local<String> weak_ref_string =
323
4377
      FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference");
324
  Local<FunctionTemplate> weak_ref =
325
4377
      env->NewFunctionTemplate(WeakReference::New);
326
8754
  weak_ref->InstanceTemplate()->SetInternalFieldCount(1);
327
4377
  weak_ref->SetClassName(weak_ref_string);
328
4377
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
329
4377
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
330
4377
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
331
8754
  target->Set(context, weak_ref_string,
332
13131
              weak_ref->GetFunction(context).ToLocalChecked()).Check();
333
334
4377
  env->SetMethod(target, "guessHandleType", GuessHandleType);
335
4377
}
336
337
}  // namespace util
338
}  // namespace node
339
340
4185
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)