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: 159 165 96.4 %
Date: 2019-09-12 22:30:12 Branches: 44 67 65.7 %

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
139529
static void GetOwnNonIndexProperties(
37
    const FunctionCallbackInfo<Value>& args) {
38
139529
  Environment* env = Environment::GetCurrent(args);
39
139529
  Local<Context> context = env->context();
40
41
279058
  CHECK(args[0]->IsObject());
42
279058
  CHECK(args[1]->IsUint32());
43
44
279058
  Local<Object> object = args[0].As<Object>();
45
46
  Local<Array> properties;
47
48
  PropertyFilter filter =
49
418587
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
50
51
279058
  if (!object->GetPropertyNames(
52
        context, KeyCollectionMode::kOwnOnly,
53
        filter,
54
139529
        IndexFilter::kSkipIndices)
55
418587
          .ToLocal(&properties)) {
56
139531
    return;
57
  }
58
279054
  args.GetReturnValue().Set(properties);
59
}
60
61
947
static void GetConstructorName(
62
    const FunctionCallbackInfo<Value>& args) {
63
1894
  CHECK(args[0]->IsObject());
64
65
1894
  Local<Object> object = args[0].As<Object>();
66
947
  Local<String> name = object->GetConstructorName();
67
68
1894
  args.GetReturnValue().Set(name);
69
947
}
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
61
    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
204890
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
90
  // Return undefined if it's not a proxy.
91
409780
  if (!args[0]->IsProxy())
92
409680
    return;
93
94
200
  Local<Proxy> proxy = args[0].As<Proxy>();
95
96
  Local<Value> ret[] = {
97
100
    proxy->GetTarget(),
98
100
    proxy->GetHandler()
99
300
  };
100
101
  args.GetReturnValue().Set(
102
400
      Array::New(args.GetIsolate(), ret, arraysize(ret)));
103
}
104
105
122
static void PreviewEntries(const FunctionCallbackInfo<Value>& args) {
106
244
  if (!args[0]->IsObject())
107
    return;
108
109
122
  Environment* env = Environment::GetCurrent(args);
110
  bool is_key_value;
111
  Local<Array> entries;
112
488
  if (!args[0].As<Object>()->PreviewEntries(&is_key_value).ToLocal(&entries))
113
    return;
114
  // Fast path for WeakMap and WeakSet.
115
122
  if (args.Length() == 1)
116
16
    return args.GetReturnValue().Set(entries);
117
118
  Local<Value> ret[] = {
119
    entries,
120
    Boolean::New(env->isolate(), is_key_value)
121
342
  };
122
  return args.GetReturnValue().Set(
123
342
      Array::New(env->isolate(), ret, arraysize(ret)));
124
}
125
126
135
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
127
#define V(name, _) &Environment::name,
128
  static Local<Private> (Environment::*const methods[])() const = {
129
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
130
  };
131
#undef V
132
135
  CHECK_LT(index, arraysize(methods));
133
135
  return (env->*methods[index])();
134
}
135
136
129
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
137
129
  Environment* env = Environment::GetCurrent(args);
138
139
258
  CHECK(args[0]->IsObject());
140
258
  CHECK(args[1]->IsUint32());
141
142
258
  Local<Object> obj = args[0].As<Object>();
143
516
  auto index = args[1]->Uint32Value(env->context()).FromJust();
144
129
  auto private_symbol = IndexToPrivateSymbol(env, index);
145
258
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
146
147
258
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
148
129
}
149
150
6
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
151
6
  Environment* env = Environment::GetCurrent(args);
152
153
12
  CHECK(args[0]->IsObject());
154
12
  CHECK(args[1]->IsUint32());
155
156
12
  Local<Object> obj = args[0].As<Object>();
157
24
  auto index = args[1]->Uint32Value(env->context()).FromJust();
158
6
  auto private_symbol = IndexToPrivateSymbol(env, index);
159
12
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
160
161
18
  args.GetReturnValue().Set(maybe_value.FromJust());
162
6
}
163
164
56
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
165
112
  CHECK(args[0]->IsArrayBufferView());
166
280
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
167
56
}
168
169
894
class WeakReference : public BaseObject {
170
 public:
171
322
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
172
644
    : BaseObject(env, object) {
173
322
    MakeWeak();
174
322
    target_.Reset(env->isolate(), target);
175
322
    target_.SetWeak();
176
322
  }
177
178
322
  static void New(const FunctionCallbackInfo<Value>& args) {
179
322
    Environment* env = Environment::GetCurrent(args);
180
322
    CHECK(args.IsConstructCall());
181
644
    CHECK(args[0]->IsObject());
182
966
    new WeakReference(env, args.This(), args[0].As<Object>());
183
322
  }
184
185
2018
  static void Get(const FunctionCallbackInfo<Value>& args) {
186
2018
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
187
2018
    Isolate* isolate = args.GetIsolate();
188
4036
    if (!weak_ref->target_.IsEmpty())
189
6051
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
190
2018
  }
191
192
1009
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
193
1009
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
194
1009
    weak_ref->reference_count_++;
195
3027
    if (weak_ref->target_.IsEmpty()) return;
196
1009
    if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
197
  }
198
199
1007
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
200
1007
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
201
1007
    CHECK_GE(weak_ref->reference_count_, 1);
202
1007
    weak_ref->reference_count_--;
203
3021
    if (weak_ref->target_.IsEmpty()) return;
204
1007
    if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
205
  }
206
207
  SET_MEMORY_INFO_NAME(WeakReference)
208
  SET_SELF_SIZE(WeakReference)
209
  SET_NO_MEMORY_INFO()
210
211
 private:
212
  Global<Object> target_;
213
  uint64_t reference_count_ = 0;
214
};
215
216
5030
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
217
5030
  Environment* env = Environment::GetCurrent(args);
218
  int fd;
219
25150
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
220
5030
  CHECK_GE(fd, 0);
221
222
5030
  uv_handle_type t = uv_guess_handle(fd);
223
5030
  const char* type = nullptr;
224
225

5030
  switch (t) {
226
    case UV_TCP:
227
4
      type = "TCP";
228
4
      break;
229
    case UV_TTY:
230
31
      type = "TTY";
231
31
      break;
232
    case UV_UDP:
233
6
      type = "UDP";
234
6
      break;
235
    case UV_FILE:
236
2296
      type = "FILE";
237
2296
      break;
238
    case UV_NAMED_PIPE:
239
2688
      type = "PIPE";
240
2688
      break;
241
    case UV_UNKNOWN_HANDLE:
242
5
      type = "UNKNOWN";
243
5
      break;
244
    default:
245
      ABORT();
246
  }
247
248
15090
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
249
}
250
251
5103
void Initialize(Local<Object> target,
252
                Local<Value> unused,
253
                Local<Context> context,
254
                void* priv) {
255
5103
  Environment* env = Environment::GetCurrent(context);
256
257
#define V(name, _)                                                            \
258
  target->Set(context,                                                        \
259
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
260
              Integer::NewFromUnsigned(env->isolate(), index++)).Check();
261
  {
262
5103
    uint32_t index = 0;
263
112266
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
264
  }
265
#undef V
266
267
#define V(name)                                                               \
268
  target->Set(context,                                                        \
269
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
270
              Integer::New(env->isolate(), Promise::PromiseState::name))      \
271
    .FromJust()
272
20412
  V(kPending);
273
20412
  V(kFulfilled);
274
20412
  V(kRejected);
275
#undef V
276
277
5103
  env->SetMethodNoSideEffect(target, "getHiddenValue", GetHiddenValue);
278
5103
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
279
5103
  env->SetMethodNoSideEffect(target, "getPromiseDetails", GetPromiseDetails);
280
5103
  env->SetMethodNoSideEffect(target, "getProxyDetails", GetProxyDetails);
281
5103
  env->SetMethodNoSideEffect(target, "previewEntries", PreviewEntries);
282
  env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
283
5103
                                     GetOwnNonIndexProperties);
284
5103
  env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
285
286
5103
  env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
287
5103
  Local<Object> constants = Object::New(env->isolate());
288
20412
  NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
289
20412
  NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);
290
20412
  NODE_DEFINE_CONSTANT(constants, ONLY_ENUMERABLE);
291
20412
  NODE_DEFINE_CONSTANT(constants, ONLY_CONFIGURABLE);
292
20412
  NODE_DEFINE_CONSTANT(constants, SKIP_STRINGS);
293
20412
  NODE_DEFINE_CONSTANT(constants, SKIP_SYMBOLS);
294
  target->Set(context,
295
              FIXED_ONE_BYTE_STRING(env->isolate(), "propertyFilter"),
296
15309
              constants).Check();
297
298
  Local<String> should_abort_on_uncaught_toggle =
299
5103
      FIXED_ONE_BYTE_STRING(env->isolate(), "shouldAbortOnUncaughtToggle");
300
20412
  CHECK(target
301
            ->Set(env->context(),
302
                  should_abort_on_uncaught_toggle,
303
                  env->should_abort_on_uncaught_toggle().GetJSArray())
304
            .FromJust());
305
306
  Local<String> weak_ref_string =
307
5103
      FIXED_ONE_BYTE_STRING(env->isolate(), "WeakReference");
308
  Local<FunctionTemplate> weak_ref =
309
5103
      env->NewFunctionTemplate(WeakReference::New);
310
10206
  weak_ref->InstanceTemplate()->SetInternalFieldCount(1);
311
5103
  weak_ref->SetClassName(weak_ref_string);
312
5103
  env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
313
5103
  env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
314
5103
  env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
315
  target->Set(context, weak_ref_string,
316
15309
              weak_ref->GetFunction(context).ToLocalChecked()).Check();
317
318
5103
  env->SetMethod(target, "guessHandleType", GuessHandleType);
319
5103
}
320
321
}  // namespace util
322
}  // namespace node
323
324
4955
NODE_MODULE_CONTEXT_AWARE_INTERNAL(util, node::util::Initialize)