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: 151 157 96.2 %
Date: 2019-05-05 22:32:45 Branches: 37 57 64.9 %

Line Branch Exec Source
1
#include "node_errors.h"
2
#include "util.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
97881
static void GetOwnNonIndexProperties(
37
    const FunctionCallbackInfo<Value>& args) {
38
97881
  Environment* env = Environment::GetCurrent(args);
39
97881
  Local<Context> context = env->context();
40
41
195762
  CHECK(args[0]->IsObject());
42
195762
  CHECK(args[1]->IsUint32());
43
44
195762
  Local<Object> object = args[0].As<Object>();
45
46
  Local<Array> properties;
47
48
  PropertyFilter filter =
49
293643
    static_cast<PropertyFilter>(args[1].As<Uint32>()->Value());
50
51
195762
  if (!object->GetPropertyNames(
52
        context, KeyCollectionMode::kOwnOnly,
53
        filter,
54
97881
        IndexFilter::kSkipIndices)
55
293643
          .ToLocal(&properties)) {
56
97883
    return;
57
  }
58
195758
  args.GetReturnValue().Set(properties);
59
}
60
61
5
static void GetConstructorName(
62
    const FunctionCallbackInfo<Value>& args) {
63
10
  CHECK(args[0]->IsObject());
64
65
10
  Local<Object> object = args[0].As<Object>();
66
5
  Local<String> name = object->GetConstructorName();
67
68
10
  args.GetReturnValue().Set(name);
69
5
}
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
173504
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
90
  // Return undefined if it's not a proxy.
91
347008
  if (!args[0]->IsProxy())
92
346908
    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
// Side effect-free stringification that will never throw exceptions.
127
9
static void SafeToString(const FunctionCallbackInfo<Value>& args) {
128
9
  auto context = args.GetIsolate()->GetCurrentContext();
129
36
  args.GetReturnValue().Set(args[0]->ToDetailString(context).ToLocalChecked());
130
9
}
131
132
116
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
133
#define V(name, _) &Environment::name,
134
  static Local<Private> (Environment::*const methods[])() const = {
135
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
136
  };
137
#undef V
138
116
  CHECK_LT(index, arraysize(methods));
139
116
  return (env->*methods[index])();
140
}
141
142
111
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
143
111
  Environment* env = Environment::GetCurrent(args);
144
145
222
  CHECK(args[0]->IsObject());
146
222
  CHECK(args[1]->IsUint32());
147
148
222
  Local<Object> obj = args[0].As<Object>();
149
444
  auto index = args[1]->Uint32Value(env->context()).FromJust();
150
111
  auto private_symbol = IndexToPrivateSymbol(env, index);
151
222
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
152
153
222
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
154
111
}
155
156
5
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
157
5
  Environment* env = Environment::GetCurrent(args);
158
159
10
  CHECK(args[0]->IsObject());
160
10
  CHECK(args[1]->IsUint32());
161
162
10
  Local<Object> obj = args[0].As<Object>();
163
20
  auto index = args[1]->Uint32Value(env->context()).FromJust();
164
5
  auto private_symbol = IndexToPrivateSymbol(env, index);
165
10
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
166
167
15
  args.GetReturnValue().Set(maybe_value.FromJust());
168
5
}
169
170
56
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
171
112
  CHECK(args[0]->IsArrayBufferView());
172
280
  args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
173
56
}
174
175
855
class WeakReference : public BaseObject {
176
 public:
177
305
  WeakReference(Environment* env, Local<Object> object, Local<Object> target)
178
305
    : BaseObject(env, object) {
179
305
    MakeWeak();
180
305
    target_.Reset(env->isolate(), target);
181
305
    target_.SetWeak();
182
305
  }
183
184
305
  static void New(const FunctionCallbackInfo<Value>& args) {
185
305
    Environment* env = Environment::GetCurrent(args);
186
305
    CHECK(args.IsConstructCall());
187
610
    CHECK(args[0]->IsObject());
188
915
    new WeakReference(env, args.This(), args[0].As<Object>());
189
305
  }
190
191
2013
  static void Get(const FunctionCallbackInfo<Value>& args) {
192
2013
    WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
193
2013
    Isolate* isolate = args.GetIsolate();
194
4026
    if (!weak_ref->target_.IsEmpty())
195
6036
      args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
196
2013
  }
197
198
  SET_MEMORY_INFO_NAME(WeakReference)
199
  SET_SELF_SIZE(WeakReference)
200
  SET_NO_MEMORY_INFO()
201
202
 private:
203
  Global<Object> target_;
204
};
205
206
4136
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
207
4136
  Environment* env = Environment::GetCurrent(args);
208
  int fd;
209
20680
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
210
4136
  CHECK_GE(fd, 0);
211
212
4136
  uv_handle_type t = uv_guess_handle(fd);
213
4136
  const char* type = nullptr;
214
215

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