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-08-17 22:35:23 Branches: 42 63 66.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
120053
static void GetOwnNonIndexProperties(
37
    const FunctionCallbackInfo<Value>& args) {
38
120053
  Environment* env = Environment::GetCurrent(args);
39
120053
  Local<Context> context = env->context();
40
41
240106
  CHECK(args[0]->IsObject());
42
240106
  CHECK(args[1]->IsUint32());
43
44
240106
  Local<Object> object = args[0].As<Object>();
45
46
  Local<Array> properties;
47
48
  PropertyFilter filter =
49
360159
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
50
51
240106
  if (!object->GetPropertyNames(
52
        context, KeyCollectionMode::kOwnOnly,
53
        filter,
54
120053
        IndexFilter::kSkipIndices)
55
360159
          .ToLocal(&properties)) {
56
120055
    return;
57
  }
58
240102
  args.GetReturnValue().Set(properties);
59
}
60
61
1009
static void GetConstructorName(
62
    const FunctionCallbackInfo<Value>& args) {
63
2018
  CHECK(args[0]->IsObject());
64
65
2018
  Local<Object> object = args[0].As<Object>();
66
1009
  Local<String> name = object->GetConstructorName();
67
68
2018
  args.GetReturnValue().Set(name);
69
1009
}
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
60
    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
100
    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
206127
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
90
  // Return undefined if it's not a proxy.
91
412254
  if (!args[0]->IsProxy())
92
412154
    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
139
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
139
  CHECK_LT(index, arraysize(methods));
133
139
  return (env->*methods[index])();
134
}
135
136
133
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
137
133
  Environment* env = Environment::GetCurrent(args);
138
139
266
  CHECK(args[0]->IsObject());
140
266
  CHECK(args[1]->IsUint32());
141
142
266
  Local<Object> obj = args[0].As<Object>();
143
532
  auto index = args[1]->Uint32Value(env->context()).FromJust();
144
133
  auto private_symbol = IndexToPrivateSymbol(env, index);
145
266
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
146
147
266
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
148
133
}
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
321
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
172
642
    : BaseObject(env, object) {
173
321
    MakeWeak();
174
321
    target_.Reset(env->isolate(), target);
175
321
    target_.SetWeak();
176
321
  }
177
178
321
  static void New(const FunctionCallbackInfo<Value>& args) {
179
321
    Environment* env = Environment::GetCurrent(args);
180
321
    CHECK(args.IsConstructCall());
181
642
    CHECK(args[0]->IsObject());
182
963
    new WeakReference(env, args.This(), args[0].As<Object>());
183
321
  }
184
185
1993
  static void Get(const FunctionCallbackInfo<Value>& args) {
186
1993
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
187
1993
    Isolate* isolate = args.GetIsolate();
188
3986
    if (!weak_ref->target_.IsEmpty())
189
5976
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
190
1993
  }
191
192
996
  static void IncRef(const FunctionCallbackInfo<Value>& args) {
193
996
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
194
996
    if (weak_ref->reference_count_ == 0) weak_ref->target_.ClearWeak();
195
996
    weak_ref->reference_count_++;
196
996
  }
197
198
995
  static void DecRef(const FunctionCallbackInfo<Value>& args) {
199
995
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
200
995
    CHECK_GE(weak_ref->reference_count_, 1);
201
995
    weak_ref->reference_count_--;
202
995
    if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
203
995
  }
204
205
  SET_MEMORY_INFO_NAME(WeakReference)
206
  SET_SELF_SIZE(WeakReference)
207
  SET_NO_MEMORY_INFO()
208
209
 private:
210
  Global<Object> target_;
211
  uint64_t reference_count_ = 0;
212
};
213
214
5287
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
215
5287
  Environment* env = Environment::GetCurrent(args);
216
  int fd;
217
26435
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
218
5287
  CHECK_GE(fd, 0);
219
220
5287
  uv_handle_type t = uv_guess_handle(fd);
221
5287
  const char* type = nullptr;
222
223

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