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: 119 205 58.0 %
Date: 2021-02-19 04:08:54 Branches: 22 83 26.5 %

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
1003
static void GetOwnNonIndexProperties(
39
    const FunctionCallbackInfo<Value>& args) {
40
1003
  Environment* env = Environment::GetCurrent(args);
41
1003
  Local<Context> context = env->context();
42
43
2006
  CHECK(args[0]->IsObject());
44
2006
  CHECK(args[1]->IsUint32());
45
46
2006
  Local<Object> object = args[0].As<Object>();
47
48
  Local<Array> properties;
49
50
  PropertyFilter filter =
51
3009
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
52
53
2006
  if (!object->GetPropertyNames(
54
        context, KeyCollectionMode::kOwnOnly,
55
        filter,
56
1003
        IndexFilter::kSkipIndices)
57
1003
          .ToLocal(&properties)) {
58
    return;
59
  }
60
2006
  args.GetReturnValue().Set(properties);
61
}
62
63
static void GetConstructorName(
64
    const FunctionCallbackInfo<Value>& args) {
65
  CHECK(args[0]->IsObject());
66
67
  Local<Object> object = args[0].As<Object>();
68
  Local<String> name = object->GetConstructorName();
69
70
  args.GetReturnValue().Set(name);
71
}
72
73
static void GetExternalValue(
74
    const FunctionCallbackInfo<Value>& args) {
75
  CHECK(args[0]->IsExternal());
76
  Isolate* isolate = args.GetIsolate();
77
  Local<External> external = args[0].As<External>();
78
79
  void* ptr = external->Value();
80
  uint64_t value = reinterpret_cast<uint64_t>(ptr);
81
  Local<BigInt> ret = BigInt::NewFromUnsigned(isolate, value);
82
  args.GetReturnValue().Set(ret);
83
}
84
85
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
86
  // Return undefined if it's not a Promise.
87
  if (!args[0]->IsPromise())
88
    return;
89
90
  auto isolate = args.GetIsolate();
91
92
  Local<Promise> promise = args[0].As<Promise>();
93
94
  int state = promise->State();
95
  Local<Value> values[2] = { Integer::New(isolate, state) };
96
  size_t number_of_values = 1;
97
  if (state != Promise::PromiseState::kPending)
98
    values[number_of_values++] = promise->Result();
99
  Local<Array> ret = Array::New(isolate, values, number_of_values);
100
  args.GetReturnValue().Set(ret);
101
}
102
103
1280
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
104
  // Return undefined if it's not a proxy.
105
2560
  if (!args[0]->IsProxy())
106
1189
    return;
107
108
182
  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

273
  if (args.Length() == 1 || args[1]->IsTrue()) {
114
    Local<Value> ret[] = {
115
      proxy->GetTarget(),
116
      proxy->GetHandler()
117
    };
118
119
    args.GetReturnValue().Set(
120
        Array::New(args.GetIsolate(), ret, arraysize(ret)));
121
  } else {
122
91
    Local<Value> ret = proxy->GetTarget();
123
124
182
    args.GetReturnValue().Set(ret);
125
  }
126
}
127
128
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
129
  if (!args[0]->IsObject())
130
    return;
131
132
  Environment* env = Environment::GetCurrent(args);
133
  bool is_key_value;
134
  Local<Array> entries;
135
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
136
    return;
137
  // Fast path for WeakMap and WeakSet.
138
  if (args.Length() == 1)
139
    return args.GetReturnValue().Set(entries);
140
141
  Local<Value> ret[] = {
142
    entries,
143
    Boolean::New(env->isolate(), is_key_value)
144
  };
145
  return args.GetReturnValue().Set(
146
      Array::New(env->isolate(), ret, arraysize(ret)));
147
}
148
149
1650
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
1650
  CHECK_LT(index, arraysize(methods));
156
1650
  return (env->*methods[index])();
157
}
158
159
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
160
  Environment* env = Environment::GetCurrent(args);
161
162
  CHECK(args[0]->IsObject());
163
  CHECK(args[1]->IsUint32());
164
165
  Local<Object> obj = args[0].As<Object>();
166
  uint32_t index = args[1].As<Uint32>()->Value();
167
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
168
  Local<Value> ret;
169
  if (obj->GetPrivate(env->context(), private_symbol).ToLocal(&ret))
170
    args.GetReturnValue().Set(ret);
171
}
172
173
1650
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
174
1650
  Environment* env = Environment::GetCurrent(args);
175
176
3300
  CHECK(args[0]->IsObject());
177
3300
  CHECK(args[1]->IsUint32());
178
179
3300
  Local<Object> obj = args[0].As<Object>();
180
4950
  uint32_t index = args[1].As<Uint32>()->Value();
181
1650
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
182
  bool ret;
183
4950
  if (obj->SetPrivate(env->context(), private_symbol, args[2]).To(&ret))
184
4950
    args.GetReturnValue().Set(ret);
185
1650
}
186
187
static void Sleep(const FunctionCallbackInfo<Value>& args) {
188
  CHECK(args[0]->IsUint32());
189
  uint32_t msec = args[0].As<Uint32>()->Value();
190
  uv_sleep(msec);
191
}
192
193
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
194
  CHECK(args[0]->IsArrayBufferView());
195
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
196
}
197
198
class WeakReference : public BaseObject {
199
 public:
200
4
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
201
8
    : BaseObject(env, object) {
202
4
    MakeWeak();
203
4
    target_.Reset(env->isolate(), target);
204
4
    target_.SetWeak();
205
4
  }
206
207
4
  static void New(const FunctionCallbackInfo<Value>& args) {
208
4
    Environment* env = Environment::GetCurrent(args);
209
4
    CHECK(args.IsConstructCall());
210
8
    CHECK(args[0]->IsObject());
211
12
    new WeakReference(env, args.This(), args[0].As<Object>());
212
4
  }
213
214
  static void Get(const FunctionCallbackInfo<Value>& args) {
215
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
216
    Isolate* isolate = args.GetIsolate();
217
    if (!weak_ref->target_.IsEmpty())
218
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
219
  }
220
221
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
222
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
223
    weak_ref->reference_count_++;
224
    if (weak_ref->target_.IsEmpty()) return;
225
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
226
  }
227
228
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
229
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
230
    CHECK_GE(weak_ref->reference_count_, 1);
231
    weak_ref->reference_count_--;
232
    if (weak_ref->target_.IsEmpty()) return;
233
    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
405
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
246
405
  Environment* env = Environment::GetCurrent(args);
247
  int fd;
248
1620
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
249
405
  CHECK_GE(fd, 0);
250
251
405
  uv_handle_type t = uv_guess_handle(fd);
252
405
  const char* type = nullptr;
253
254

405
  switch (t) {
255
    case UV_TCP:
256
      type = "TCP";
257
      break;
258
    case UV_TTY:
259
      type = "TTY";
260
      break;
261
    case UV_UDP:
262
      type = "UDP";
263
      break;
264
    case UV_FILE:
265
2
      type = "FILE";
266
2
      break;
267
    case UV_NAMED_PIPE:
268
403
      type = "PIPE";
269
403
      break;
270
    case UV_UNKNOWN_HANDLE:
271
      type = "UNKNOWN";
272
      break;
273
    default:
274
      ABORT();
275
  }
276
277
1215
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
278
}
279
280
107
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
281
107
  registry->Register(GetHiddenValue);
282
107
  registry->Register(SetHiddenValue);
283
107
  registry->Register(GetPromiseDetails);
284
107
  registry->Register(GetProxyDetails);
285
107
  registry->Register(PreviewEntries);
286
107
  registry->Register(GetOwnNonIndexProperties);
287
107
  registry->Register(GetConstructorName);
288
107
  registry->Register(GetExternalValue);
289
107
  registry->Register(Sleep);
290
107
  registry->Register(ArrayBufferViewHasBuffer);
291
107
  registry->Register(WeakReference::New);
292
107
  registry->Register(WeakReference::Get);
293
107
  registry->Register(WeakReference::IncRef);
294
107
  registry->Register(WeakReference::DecRef);
295
107
  registry->Register(GuessHandleType);
296
107
}
297
298
52
void Initialize(Local<Object> target,
299
                Local<Value> unused,
300
                Local<Context> context,
301
                void* priv) {
302
52
  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
52
    uint32_t index = 0;
310
624
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
311
  }
312
#undef V
313
314
312
#define V(name)                                                               \
315
208
  target->Set(context,                                                        \
316
208
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
317
104
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
318
312
    .FromJust()
319
364
  V(kPending);
320
208
  V(kFulfilled);
321
208
  V(kRejected);
322
#undef V
323
324
52
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
325
52
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
326
52
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
327
52
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
328
52
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
329
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
330
52
                                     GetOwnNonIndexProperties);
331
52
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
332
52
  env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
333
52
  env->SetMethod(target, "sleep", Sleep);
334
335
52
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
336
52
  Local<Object> constants = Object::New(env->isolate());
337
104
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
338
52
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
339
260
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
340
416
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
341
520
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
342
520
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
343
520
  target->Set(context,
344
312
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
345
416
              constants).Check();
346
104
347
  Local<String> should_abort_on_uncaught_toggle =
348
52
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
349
208
  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<FunctionTemplate> weak_ref =
356
52
      env->NewFunctionTemplate(WeakReference::New);
357
156
  weak_ref->InstanceTemplate()->SetInternalFieldCount(
358
52
      WeakReference::kInternalFieldCount);
359
104
  weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
360
52
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
361
52
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
362
52
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
363
52
  env->SetConstructorFunction(target, "WeakReference", weak_ref);
364
365
52
  env->SetMethod(target, "guessHandleType", GuessHandleType);
366
52
}
367
368
}  // namespace util
369
}  // namespace node
370
371
115
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)
372

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