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: 200 207 96.6 %
Date: 2020-08-07 22:13:19 Branches: 55 83 66.3 %

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

307
  if (args.Length() == 1 || args[1]->IsTrue()) {
114
    Local<Value> ret[] = {
115
43
      proxy->GetTarget(),
116
43
      proxy->GetHandler()
117
129
    };
118
119
172
    args.GetReturnValue().Set(
120
        Array::New(args.GetIsolate(), ret, arraysize(ret)));
121
  } else {
122
60
    Local<Value> ret = proxy->GetTarget();
123
124
120
    args.GetReturnValue().Set(ret);
125
  }
126
}
127
128
120
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
129
240
  if (!args[0]->IsObject())
130
    return;
131
132
120
  Environment* env = Environment::GetCurrent(args);
133
  bool is_key_value;
134
  Local<Array> entries;
135
480
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
136
    return;
137
  // Fast path for WeakMap and WeakSet.
138
120
  if (args.Length() == 1)
139
16
    return args.GetReturnValue().Set(entries);
140
141
  Local<Value> ret[] = {
142
    entries,
143
    Boolean::New(env->isolate(), is_key_value)
144
336
  };
145
336
  return args.GetReturnValue().Set(
146
112
      Array::New(env->isolate(), ret, arraysize(ret)));
147
}
148
149
5149
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
150
#define V(name, _) &Environment::name,
151
  static Local<Private> (Environment::*const methods[])() const = {
152
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
153
  };
154
#undef V
155
5149
  CHECK_LT(index, arraysize(methods));
156
5149
  return (env->*methods[index])();
157
}
158
159
143
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
160
143
  Environment* env = Environment::GetCurrent(args);
161
162
286
  CHECK(args[0]->IsObject());
163
286
  CHECK(args[1]->IsUint32());
164
165
286
  Local<Object> obj = args[0].As<Object>();
166
429
  uint32_t index = args[1].As<Uint32>()->Value();
167
143
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
168
  Local<Value> ret;
169
429
  if (obj->GetPrivate(env->context(), private_symbol).ToLocal(&ret))
170
286
    args.GetReturnValue().Set(ret);
171
143
}
172
173
5006
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
174
5006
  Environment* env = Environment::GetCurrent(args);
175
176
10012
  CHECK(args[0]->IsObject());
177
10012
  CHECK(args[1]->IsUint32());
178
179
10012
  Local<Object> obj = args[0].As<Object>();
180
15018
  uint32_t index = args[1].As<Uint32>()->Value();
181
5006
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
182
  bool ret;
183
15018
  if (obj->SetPrivate(env->context(), private_symbol, args[2]).To(&ret))
184
15018
    args.GetReturnValue().Set(ret);
185
5006
}
186
187
10
static void Sleep(const FunctionCallbackInfo<Value>& args) {
188
20
  CHECK(args[0]->IsUint32());
189
30
  uint32_t msec = args[0].As<Uint32>()->Value();
190
10
  uv_sleep(msec);
191
10
}
192
193
56
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
194
112
  CHECK(args[0]->IsArrayBufferView());
195
280
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
196
56
}
197
198
780
class WeakReference : public BaseObject {
199
 public:
200
301
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
201
602
    : BaseObject(env, object) {
202
301
    MakeWeak();
203
301
    target_.Reset(env->isolate(), target);
204
301
    target_.SetWeak();
205
301
  }
206
207
301
  static void New(const FunctionCallbackInfo<Value>& args) {
208
301
    Environment* env = Environment::GetCurrent(args);
209
301
    CHECK(args.IsConstructCall());
210
602
    CHECK(args[0]->IsObject());
211
903
    new WeakReference(env, args.This(), args[0].As<Object>());
212
301
  }
213
214
1551
  static void Get(const FunctionCallbackInfo<Value>& args) {
215
1551
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
216
1551
    Isolate* isolate = args.GetIsolate();
217
3102
    if (!weak_ref->target_.IsEmpty())
218
4650
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
219
1551
  }
220
221
776
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
222
776
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
223
776
    weak_ref->reference_count_++;
224
1552
    if (weak_ref->target_.IsEmpty()) return;
225
776
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
226
  }
227
228
773
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
229
773
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
230
773
    CHECK_GE(weak_ref->reference_count_, 1);
231
773
    weak_ref->reference_count_--;
232
1546
    if (weak_ref->target_.IsEmpty()) return;
233
773
    if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
234
  }
235
236
  SET_MEMORY_INFO_NAME(WeakReference)
237
  SET_SELF_SIZE(WeakReference)
238
  SET_NO_MEMORY_INFO()
239
240
 private:
241
  Global<Object> target_;
242
  uint64_t reference_count_ = 0;
243
};
244
245
4332
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
246
4332
  Environment* env = Environment::GetCurrent(args);
247
  int fd;
248
17328
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
249
4332
  CHECK_GE(fd, 0);
250
251
4332
  uv_handle_type t = uv_guess_handle(fd);
252
4332
  const char* type = nullptr;
253
254

4332
  switch (t) {
255
    case UV_TCP:
256
4
      type = "TCP";
257
4
      break;
258
    case UV_TTY:
259
35
      type = "TTY";
260
35
      break;
261
    case UV_UDP:
262
6
      type = "UDP";
263
6
      break;
264
    case UV_FILE:
265
2276
      type = "FILE";
266
2276
      break;
267
    case UV_NAMED_PIPE:
268
2006
      type = "PIPE";
269
2006
      break;
270
    case UV_UNKNOWN_HANDLE:
271
5
      type = "UNKNOWN";
272
5
      break;
273
    default:
274
      ABORT();
275
  }
276
277
12996
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
278
}
279
280
4371
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
281
4371
  registry->Register(GetHiddenValue);
282
4371
  registry->Register(SetHiddenValue);
283
4371
  registry->Register(GetPromiseDetails);
284
4371
  registry->Register(GetProxyDetails);
285
4371
  registry->Register(PreviewEntries);
286
4371
  registry->Register(GetOwnNonIndexProperties);
287
4371
  registry->Register(GetConstructorName);
288
4371
  registry->Register(GetExternalValue);
289
4371
  registry->Register(Sleep);
290
4371
  registry->Register(ArrayBufferViewHasBuffer);
291
4371
  registry->Register(WeakReference::New);
292
4371
  registry->Register(WeakReference::Get);
293
4371
  registry->Register(WeakReference::IncRef);
294
4371
  registry->Register(WeakReference::DecRef);
295
4371
  registry->Register(GuessHandleType);
296
4371
}
297
298
389
void Initialize(Local<Object> target,
299
                Local<Value> unused,
300
                Local<Context> context,
301
                void* priv) {
302
389
  Environment* env = Environment::GetCurrent(context);
303
304
#define V(name, _)                                                            \
305
  target->Set(context,                                                        \
306
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
307
              Integer::NewFromUnsigned(env->isolate(), index++)).Check();
308
  {
309
389
    uint32_t index = 0;
310
9725
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
311
  }
312
#undef V
313
314
#define V(name)                                                               \
315
  target->Set(context,                                                        \
316
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
317
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
318
    .FromJust()
319
1556
  V(kPending);
320
1556
  V(kFulfilled);
321
1556
  V(kRejected);
322
#undef V
323
324
389
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
325
389
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
326
389
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
327
389
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
328
389
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
329
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
330
389
                                     GetOwnNonIndexProperties);
331
389
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
332
389
  env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
333
389
  env->SetMethod(target, "sleep", Sleep);
334
335
389
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
336
389
  Local<Object> constants = Object::New(env->isolate());
337
1556
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
338
1556
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
339
1556
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
340
1556
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
341
1556
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
342
1556
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
343
778
  target->Set(context,
344
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
345
1167
              constants).Check();
346
347
  Local<String> should_abort_on_uncaught_toggle =
348
389
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
349
1556
  CHECK(target
350
            ->Set(env->context(),
351
                  should_abort_on_uncaught_toggle,
352
                  env->should_abort_on_uncaught_toggle().GetJSArray())
353
            .FromJust());
354
355
  Local<String> weak_ref_string =
356
389
      FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference");
357
  Local<FunctionTemplate> weak_ref =
358
389
      env->NewFunctionTemplate(WeakReference::New);
359
1167
  weak_ref->InstanceTemplate()->SetInternalFieldCount(
360
389
      WeakReference::kInternalFieldCount);
361
389
  weak_ref->SetClassName(weak_ref_string);
362
778
  weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
363
389
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
364
389
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
365
389
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
366
778
  target->Set(context, weak_ref_string,
367
1167
              weak_ref->GetFunction(context).ToLocalChecked()).Check();
368
369
389
  env->SetMethod(target, "guessHandleType", GuessHandleType);
370
389
}
371
372
}  // namespace util
373
}  // namespace node
374
375
4440
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)
376

17694
NODE_MODULE_EXTERNAL_REFERENCE(util, node::util::RegisterExternalReferences)