GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/node_util.cc Lines: 51 53 96.2 %
Date: 2016-07-23 Branches: 23 36 63.9 %

Line Exec Source
1
#include "node.h"
2
#include "node_watchdog.h"
3
#include "v8.h"
4
#include "env.h"
5
#include "env-inl.h"
6
7
namespace node {
8
namespace util {
9
10
using v8::Array;
11
using v8::Context;
12
using v8::FunctionCallbackInfo;
13
using v8::Integer;
14
using v8::Local;
15
using v8::Object;
16
using v8::Private;
17
using v8::Proxy;
18
using v8::Value;
19
20
21
#define VALUE_METHOD_MAP(V)                                                   \
22
  V(isArrayBuffer, IsArrayBuffer)                                             \
23
  V(isDataView, IsDataView)                                                   \
24
  V(isDate, IsDate)                                                           \
25
  V(isMap, IsMap)                                                             \
26
  V(isMapIterator, IsMapIterator)                                             \
27
  V(isPromise, IsPromise)                                                     \
28
  V(isRegExp, IsRegExp)                                                       \
29
  V(isSet, IsSet)                                                             \
30
  V(isSetIterator, IsSetIterator)                                             \
31
  V(isTypedArray, IsTypedArray)
32
33
34
#define V(_, ucname) \
35
  static void ucname(const FunctionCallbackInfo<Value>& args) {               \
36
    CHECK_EQ(1, args.Length());                                               \
37
    args.GetReturnValue().Set(args[0]->ucname());                             \
38
  }
39
40
111505
  VALUE_METHOD_MAP(V)
41
#undef V
42
43
53
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
44
  // Return undefined if it's not a proxy.
45
53
  if (!args[0]->IsProxy())
46
    return;
47
48
52
  Local<Proxy> proxy = args[0].As<Proxy>();
49
50
26
  Local<Array> ret = Array::New(args.GetIsolate(), 2);
51
52
  ret->Set(0, proxy->GetTarget());
52
26
  ret->Set(1, proxy->GetHandler());
53
54
26
  args.GetReturnValue().Set(ret);
55
}
56
57
57
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
58
#define V(name, _) &Environment::name,
59
  static Local<Private> (Environment::*const methods[])() const = {
60
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
61
  };
62
#undef V
63
57
  CHECK_LT(index, arraysize(methods));
64
57
  return (env->*methods[index])();
65
}
66
67
64
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
68
64
  Environment* env = Environment::GetCurrent(args);
69
70
64
  if (!args[0]->IsObject())
71
8
    return env->ThrowTypeError("obj must be an object");
72
73
59
  if (!args[1]->IsUint32())
74
    return env->ThrowTypeError("index must be an uint32");
75
76
112
  Local<Object> obj = args[0].As<Object>();
77
168
  auto index = args[1]->Uint32Value(env->context()).FromJust();
78
56
  auto private_symbol = IndexToPrivateSymbol(env, index);
79
56
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
80
81
168
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
82
}
83
84
9
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
85
9
  Environment* env = Environment::GetCurrent(args);
86
87
9
  if (!args[0]->IsObject())
88
8
    return env->ThrowTypeError("obj must be an object");
89
90
4
  if (!args[1]->IsUint32())
91
    return env->ThrowTypeError("index must be an uint32");
92
93
2
  Local<Object> obj = args[0].As<Object>();
94
3
  auto index = args[1]->Uint32Value(env->context()).FromJust();
95
1
  auto private_symbol = IndexToPrivateSymbol(env, index);
96
2
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
97
98
3
  args.GetReturnValue().Set(maybe_value.FromJust());
99
}
100
101
102
24
void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
103
24
  int ret = SigintWatchdogHelper::GetInstance()->Start();
104
24
  if (ret != 0) {
105
    Environment* env = Environment::GetCurrent(args);
106
    env->ThrowErrnoException(ret, "StartSigintWatchdog");
107
  }
108
24
}
109
110
111
22
void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
112
22
  bool had_pending_signals = SigintWatchdogHelper::GetInstance()->Stop();
113
66
  args.GetReturnValue().Set(had_pending_signals);
114
22
}
115
116
1563
void Initialize(Local<Object> target,
117
                Local<Value> unused,
118
                Local<Context> context) {
119
1563
  Environment* env = Environment::GetCurrent(context);
120
121
#define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
122
1563
  VALUE_METHOD_MAP(V)
123
#undef V
124
125
#define V(name, _)                                                            \
126
  target->Set(context,                                                        \
127
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
128
              Integer::NewFromUnsigned(env->isolate(), index++));
129
  {
130
1563
    uint32_t index = 0;
131
26571
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
132
  }
133
#undef V
134
135
1563
  env->SetMethod(target, "getHiddenValue", GetHiddenValue);
136
1563
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
137
1563
  env->SetMethod(target, "getProxyDetails", GetProxyDetails);
138
139
1563
  env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
140
1563
  env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
141
1563
}
142
143
}  // namespace util
144
}  // namespace node
145
146
1568
NODE_MODULE_CONTEXT_AWARE_BUILTIN(util, node::util::Initialize)