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: 171 178 96.1 %
Date: 2020-05-27 22:15:15 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
137383
static void GetOwnNonIndexProperties(
37
    const FunctionCallbackInfo<Value>& args) {
38
137383
  Environment* env = Environment::GetCurrent(args);
39
137383
  Local<Context> context = env->context();
40
41
274766
  CHECK(args[0]->IsObject());
42
274766
  CHECK(args[1]->IsUint32());
43
44
274766
  Local<Object> object = args[0].As<Object>();
45
46
  Local<Array> properties;
47
48
  PropertyFilter filter =
49
412149
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
50
51
274766
  if (!object->GetPropertyNames(
52
        context, KeyCollectionMode::kOwnOnly,
53
        filter,
54
137383
        IndexFilter::kSkipIndices)
55
137383
          .ToLocal(&properties)) {
56
2
    return;
57
  }
58
274762
  args.GetReturnValue().Set(properties);
59
}
60
61
1041
static void GetConstructorName(
62
    const FunctionCallbackInfo<Value>& args) {
63
2082
  CHECK(args[0]->IsObject());
64
65
2082
  Local<Object> object = args[0].As<Object>();
66
1041
  Local<String> name = object->GetConstructorName();
67
68
2082
  args.GetReturnValue().Set(name);
69
1041
}
70
71
60
static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) {
72
  // Return undefined if it's not a Promise.
73
120
  if (!args[0]->IsPromise())
74
    return;
75
76
60
  auto isolate = args.GetIsolate();
77
78
120
  Local<Promise> promise = args[0].As<Promise>();
79
80
60
  int state = promise->State();
81
180
  Local<Value> values[2] = { Integer::New(isolate, state) };
82
60
  size_t number_of_values = 1;
83
60
  if (state != Promise::PromiseState::kPending)
84
98
    values[number_of_values++] = promise->Result();
85
60
  Local<Array> ret = Array::New(isolate, values, number_of_values);
86
120
  args.GetReturnValue().Set(ret);
87
}
88
89
259175
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
90
  // Return undefined if it's not a proxy.
91
518350
  if (!args[0]->IsProxy())
92
259072
    return;
93
94
206
  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

307
  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
60
    Local<Value> ret = proxy->GetTarget();
109
110
120
    args.GetReturnValue().Set(ret);
111
  }
112
}
113
114
120
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
115
240
  if (!args[0]->IsObject())
116
    return;
117
118
120
  Environment* env = Environment::GetCurrent(args);
119
  bool is_key_value;
120
  Local<Array> entries;
121
480
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
122
    return;
123
  // Fast path for WeakMap and WeakSet.
124
120
  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
336
  };
131
336
  return args.GetReturnValue().Set(
132
112
      Array::New(env->isolate(), ret, arraysize(ret)));
133
}
134
135
10232
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
10232
  CHECK_LT(index, arraysize(methods));
142
10232
  return (env->*methods[index])();
143
}
144
145
142
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
146
142
  Environment* env = Environment::GetCurrent(args);
147
148
284
  CHECK(args[0]->IsObject());
149
284
  CHECK(args[1]->IsUint32());
150
151
284
  Local<Object> obj = args[0].As<Object>();
152
568
  auto index = args[1]->Uint32Value(env->context()).FromJust();
153
142
  auto private_symbol = IndexToPrivateSymbol(env, index);
154
284
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
155
156
284
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
157
142
}
158
159
10090
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
160
10090
  Environment* env = Environment::GetCurrent(args);
161
162
20180
  CHECK(args[0]->IsObject());
163
20180
  CHECK(args[1]->IsUint32());
164
165
20180
  Local<Object> obj = args[0].As<Object>();
166
40360
  auto index = args[1]->Uint32Value(env->context()).FromJust();
167
10090
  auto private_symbol = IndexToPrivateSymbol(env, index);
168
20180
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
169
170
30270
  args.GetReturnValue().Set(maybe_value.FromJust());
171
10090
}
172
173
10
static void Sleep(const FunctionCallbackInfo<Value>& args) {
174
20
  CHECK(args[0]->IsUint32());
175
30
  uint32_t msec = args[0].As<Uint32>()->Value();
176
10
  uv_sleep(msec);
177
10
}
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
780
class WeakReference : public BaseObject {
185
 public:
186
299
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
187
598
    : BaseObject(env, object) {
188
299
    MakeWeak();
189
299
    target_.Reset(env->isolate(), target);
190
299
    target_.SetWeak();
191
299
  }
192
193
299
  static void New(const FunctionCallbackInfo<Value>& args) {
194
299
    Environment* env = Environment::GetCurrent(args);
195
299
    CHECK(args.IsConstructCall());
196
598
    CHECK(args[0]->IsObject());
197
897
    new WeakReference(env, args.This(), args[0].As<Object>());
198
299
  }
199
200
1323
  static void Get(const FunctionCallbackInfo<Value>& args) {
201
1323
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
202
1323
    Isolate* isolate = args.GetIsolate();
203
2646
    if (!weak_ref->target_.IsEmpty())
204
3966
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
205
1323
  }
206
207
662
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
208
662
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
209
662
    weak_ref->reference_count_++;
210
1324
    if (weak_ref->target_.IsEmpty()) return;
211
662
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
212
  }
213
214
659
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
215
659
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
216
659
    CHECK_GE(weak_ref->reference_count_, 1);
217
659
    weak_ref->reference_count_--;
218
1318
    if (weak_ref->target_.IsEmpty()) return;
219
659
    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
4219
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
232
4219
  Environment* env = Environment::GetCurrent(args);
233
  int fd;
234
16876
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
235
4219
  CHECK_GE(fd, 0);
236
237
4219
  uv_handle_type t = uv_guess_handle(fd);
238
4219
  const char* type = nullptr;
239
240

4219
  switch (t) {
241
    case UV_TCP:
242
4
      type = "TCP";
243
4
      break;
244
    case UV_TTY:
245
35
      type = "TTY";
246
35
      break;
247
    case UV_UDP:
248
6
      type = "UDP";
249
6
      break;
250
    case UV_FILE:
251
2181
      type = "FILE";
252
2181
      break;
253
    case UV_NAMED_PIPE:
254
1988
      type = "PIPE";
255
1988
      break;
256
    case UV_UNKNOWN_HANDLE:
257
5
      type = "UNKNOWN";
258
5
      break;
259
    default:
260
      ABORT();
261
  }
262
263
12657
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
264
}
265
266
4594
void Initialize(Local<Object> target,
267
                Local<Value> unused,
268
                Local<Context> context,
269
                void* priv) {
270
4594
  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
4594
    uint32_t index = 0;
278
101068
    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
18376
  V(kPending);
288
18376
  V(kFulfilled);
289
18376
  V(kRejected);
290
#undef V
291
292
4594
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
293
4594
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
294
4594
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
295
4594
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
296
4594
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
297
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
298
4594
                                     GetOwnNonIndexProperties);
299
4594
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
300
4594
  env->SetMethod(target, "sleep", Sleep);
301
302
4594
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
303
4594
  Local<Object> constants = Object::New(env->isolate());
304
18376
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
305
18376
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
306
18376
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
307
18376
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
308
18376
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
309
18376
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
310
9188
  target->Set(context,
311
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
312
13782
              constants).Check();
313
314
  Local<String> should_abort_on_uncaught_toggle =
315
4594
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
316
18376
  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
4594
      FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference");
324
  Local<FunctionTemplate> weak_ref =
325
4594
      env->NewFunctionTemplate(WeakReference::New);
326
13782
  weak_ref->InstanceTemplate()->SetInternalFieldCount(
327
4594
      WeakReference::kInternalFieldCount);
328
4594
  weak_ref->SetClassName(weak_ref_string);
329
4594
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
330
4594
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
331
4594
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
332
9188
  target->Set(context, weak_ref_string,
333
13782
              weak_ref->GetFunction(context).ToLocalChecked()).Check();
334
335
4594
  env->SetMethod(target, "guessHandleType", GuessHandleType);
336
4594
}
337
338
}  // namespace util
339
}  // namespace node
340
341
4325
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)