GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_util.cc Lines: 211 215 98.1 %
Date: 2021-09-20 04:12:42 Branches: 78 123 63.4 %

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

399
  if (args.Length() == 1 || args[1]->IsTrue()) {
114
    Local<Value> ret[] = {
115
43
      proxy->GetTarget(),
116
43
      proxy->GetHandler()
117
129
    };
118
119
129
    args.GetReturnValue().Set(
120
        Array::New(args.GetIsolate(), ret, arraysize(ret)));
121
  } else {
122
157
    Local<Value> ret = proxy->GetTarget();
123
124
314
    args.GetReturnValue().Set(ret);
125
  }
126
}
127
128
124
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
129
124
  if (!args[0]->IsObject())
130
    return;
131
132
124
  Environment* env = Environment::GetCurrent(args);
133
  bool is_key_value;
134
  Local<Array> entries;
135
372
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
136
    return;
137
  // Fast path for WeakMap and WeakSet.
138
124
  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
232
  };
145
116
  return args.GetReturnValue().Set(
146
116
      Array::New(env->isolate(), ret, arraysize(ret)));
147
}
148
149
4654
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
4654
  CHECK_LT(index, arraysize(methods));
156
4654
  return (env->*methods[index])();
157
}
158
159
177
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
160
177
  Environment* env = Environment::GetCurrent(args);
161
162
177
  CHECK(args[0]->IsObject());
163
177
  CHECK(args[1]->IsUint32());
164
165
354
  Local<Object> obj = args[0].As<Object>();
166
354
  uint32_t index = args[1].As<Uint32>()->Value();
167
177
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
168
  Local<Value> ret;
169
354
  if (obj->GetPrivate(env->context(), private_symbol).ToLocal(&ret))
170
354
    args.GetReturnValue().Set(ret);
171
177
}
172
173
4477
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
174
4477
  Environment* env = Environment::GetCurrent(args);
175
176
4477
  CHECK(args[0]->IsObject());
177
4477
  CHECK(args[1]->IsUint32());
178
179
8954
  Local<Object> obj = args[0].As<Object>();
180
8954
  uint32_t index = args[1].As<Uint32>()->Value();
181
4477
  Local<Private> private_symbol = IndexToPrivateSymbol(env, index);
182
  bool ret;
183

13431
  if (obj->SetPrivate(env->context(), private_symbol, args[2]).To(&ret))
184
8954
    args.GetReturnValue().Set(ret);
185
4477
}
186
187
9
static void Sleep(const FunctionCallbackInfo<Value>& args) {
188
9
  CHECK(args[0]->IsUint32());
189
18
  uint32_t msec = args[0].As<Uint32>()->Value();
190
9
  uv_sleep(msec);
191
9
}
192
193
56
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
194
56
  CHECK(args[0]->IsArrayBufferView());
195

168
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
196
56
}
197
198
class WeakReference : public BaseObject {
199
 public:
200
1906
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
201
1906
    : BaseObject(env, object) {
202
1906
    MakeWeak();
203
1906
    target_.Reset(env->isolate(), target);
204
1906
    target_.SetWeak();
205
1906
  }
206
207
1906
  static void New(const FunctionCallbackInfo<Value>& args) {
208
1906
    Environment* env = Environment::GetCurrent(args);
209
1906
    CHECK(args.IsConstructCall());
210
1906
    CHECK(args[0]->IsObject());
211
5718
    new WeakReference(env, args.This(), args[0].As<Object>());
212
1906
  }
213
214
8031
  static void Get(const FunctionCallbackInfo<Value>& args) {
215
8031
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
216
8031
    Isolate* isolate = args.GetIsolate();
217
8031
    if (!weak_ref->target_.IsEmpty())
218
16060
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
219
8031
  }
220
221
4014
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
222
4014
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
223
4014
    weak_ref->reference_count_++;
224
4014
    if (weak_ref->target_.IsEmpty()) return;
225
4014
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
226
  }
227
228
4011
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
229
4011
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
230
4011
    CHECK_GE(weak_ref->reference_count_, 1);
231
4011
    weak_ref->reference_count_--;
232
4011
    if (weak_ref->target_.IsEmpty()) return;
233
4011
    if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
234
  }
235
236
4
  SET_MEMORY_INFO_NAME(WeakReference)
237
4
  SET_SELF_SIZE(WeakReference)
238
4
  SET_NO_MEMORY_INFO()
239
240
 private:
241
  Global<Object> target_;
242
  uint64_t reference_count_ = 0;
243
};
244
245
4759
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
246
4759
  Environment* env = Environment::GetCurrent(args);
247
  int fd;
248
9518
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
249
4759
  CHECK_GE(fd, 0);
250
251
4759
  uv_handle_type t = uv_guess_handle(fd);
252
4759
  const char* type = nullptr;
253
254

4759
  switch (t) {
255
4
    case UV_TCP:
256
4
      type = "TCP";
257
4
      break;
258
36
    case UV_TTY:
259
36
      type = "TTY";
260
36
      break;
261
6
    case UV_UDP:
262
6
      type = "UDP";
263
6
      break;
264
2411
    case UV_FILE:
265
2411
      type = "FILE";
266
2411
      break;
267
2297
    case UV_NAMED_PIPE:
268
2297
      type = "PIPE";
269
2297
      break;
270
5
    case UV_UNKNOWN_HANDLE:
271
5
      type = "UNKNOWN";
272
5
      break;
273
    default:
274
      ABORT();
275
  }
276
277
9518
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
278
}
279
280
8
static void IsConstructor(const FunctionCallbackInfo<Value>& args) {
281
8
  CHECK(args[0]->IsFunction());
282

24
  args.GetReturnValue().Set(args[0].As<v8::Function>()->IsConstructor());
283
8
}
284
285
4853
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
286
4853
  registry->Register(GetHiddenValue);
287
4853
  registry->Register(SetHiddenValue);
288
4853
  registry->Register(GetPromiseDetails);
289
4853
  registry->Register(GetProxyDetails);
290
4853
  registry->Register(PreviewEntries);
291
4853
  registry->Register(GetOwnNonIndexProperties);
292
4853
  registry->Register(GetConstructorName);
293
4853
  registry->Register(GetExternalValue);
294
4853
  registry->Register(Sleep);
295
4853
  registry->Register(ArrayBufferViewHasBuffer);
296
4853
  registry->Register(WeakReference::New);
297
4853
  registry->Register(WeakReference::Get);
298
4853
  registry->Register(WeakReference::IncRef);
299
4853
  registry->Register(WeakReference::DecRef);
300
4853
  registry->Register(GuessHandleType);
301
4853
  registry->Register(IsConstructor);
302
4853
}
303
304
613
void Initialize(Local<Object> target,
305
                Local<Value> unused,
306
                Local<Context> context,
307
                void* priv) {
308
613
  Environment* env = Environment::GetCurrent(context);
309
310
#define V(name, _)                                                            \
311
  target->Set(context,                                                        \
312
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
313
              Integer::NewFromUnsigned(env->isolate(), index++)).Check();
314
  {
315
613
    uint32_t index = 0;
316
15325
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
317
  }
318
#undef V
319
320
#define V(name)                                                               \
321
  target->Set(context,                                                        \
322
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
323
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
324
    .FromJust()
325
2452
  V(kPending);
326
2452
  V(kFulfilled);
327
1839
  V(kRejected);
328
#undef V
329
330
613
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
331
613
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
332
613
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
333
613
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
334
613
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
335
613
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
336
                                     GetOwnNonIndexProperties);
337
613
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
338
613
  env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
339
613
  env->SetMethod(target, "sleep", Sleep);
340
613
  env->SetMethodNoSideEffect(target, "isConstructor", IsConstructor);
341
342
613
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
343
613
  Local<Object> constants = Object::New(env->isolate());
344
1839
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
345
1839
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
346
1839
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
347
1839
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
348
1839
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
349
1839
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
350
613
  target->Set(context,
351
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
352
1226
              constants).Check();
353
354
  Local<String> should_abort_on_uncaught_toggle =
355
613
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
356
1839
  CHECK(target
357
            ->Set(env->context(),
358
                  should_abort_on_uncaught_toggle,
359
                  env->should_abort_on_uncaught_toggle().GetJSArray())
360
            .FromJust());
361
362
  Local<FunctionTemplate> weak_ref =
363
613
      env->NewFunctionTemplate(WeakReference::New);
364
1226
  weak_ref->InstanceTemplate()->SetInternalFieldCount(
365
      WeakReference::kInternalFieldCount);
366
613
  weak_ref->Inherit(BaseObject::GetConstructorTemplate(env));
367
613
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
368
613
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
369
613
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
370
613
  env->SetConstructorFunction(target, "WeakReference", weak_ref);
371
372
613
  env->SetMethod(target, "guessHandleType", GuessHandleType);
373
613
}
374
375
}  // namespace util
376
}  // namespace node
377
378
4926
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)
379
4853
NODE_MODULE_EXTERNAL_REFERENCE(util, node::util::RegisterExternalReferences)