GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_util.cc Lines: 56 58 96.6 %
Date: 2016-11-30 Branches: 24 38 63.2 %

Line Branch 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(isSharedArrayBuffer, IsSharedArrayBuffer)                                 \
32
  V(isTypedArray, IsTypedArray)
33
34
35
#define V(_, ucname) \
36
  static void ucname(const FunctionCallbackInfo<Value>& args) {               \
37
    CHECK_EQ(1, args.Length());                                               \
38
    args.GetReturnValue().Set(args[0]->ucname());                             \
39
  }
40
41





420170
  VALUE_METHOD_MAP(V)
42
#undef V
43
44
53
static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
45
  // Return undefined if it's not a proxy.
46
53
  if (!args[0]->IsProxy())
47
    return;
48
49
52
  Local<Proxy> proxy = args[0].As<Proxy>();
50
51
26
  Local<Array> ret = Array::New(args.GetIsolate(), 2);
52
52
  ret->Set(0, proxy->GetTarget());
53
26
  ret->Set(1, proxy->GetHandler());
54
55
26
  args.GetReturnValue().Set(ret);
56
}
57
58
53
inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
59
#define V(name, _) &Environment::name,
60
  static Local<Private> (Environment::*const methods[])() const = {
61
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
62
  };
63
#undef V
64
53
  CHECK_LT(index, arraysize(methods));
65
53
  return (env->*methods[index])();
66
}
67
68
60
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
69
60
  Environment* env = Environment::GetCurrent(args);
70
71
60
  if (!args[0]->IsObject())
72
8
    return env->ThrowTypeError("obj must be an object");
73
74
55
  if (!args[1]->IsUint32())
75
    return env->ThrowTypeError("index must be an uint32");
76
77
104
  Local<Object> obj = args[0].As<Object>();
78
156
  auto index = args[1]->Uint32Value(env->context()).FromJust();
79
52
  auto private_symbol = IndexToPrivateSymbol(env, index);
80
52
  auto maybe_value = obj->GetPrivate(env->context(), private_symbol);
81
82
156
  args.GetReturnValue().Set(maybe_value.ToLocalChecked());
83
}
84
85
9
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
86
9
  Environment* env = Environment::GetCurrent(args);
87
88
9
  if (!args[0]->IsObject())
89
8
    return env->ThrowTypeError("obj must be an object");
90
91
4
  if (!args[1]->IsUint32())
92
    return env->ThrowTypeError("index must be an uint32");
93
94
2
  Local<Object> obj = args[0].As<Object>();
95
3
  auto index = args[1]->Uint32Value(env->context()).FromJust();
96
1
  auto private_symbol = IndexToPrivateSymbol(env, index);
97
2
  auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);
98
99
3
  args.GetReturnValue().Set(maybe_value.FromJust());
100
}
101
102
103
33
void StartSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
104
33
  int ret = SigintWatchdogHelper::GetInstance()->Start();
105
33
  if (ret != 0) {
106
    Environment* env = Environment::GetCurrent(args);
107
    env->ThrowErrnoException(ret, "StartSigintWatchdog");
108
  }
109
33
}
110
111
112
31
void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
113
31
  bool had_pending_signals = SigintWatchdogHelper::GetInstance()->Stop();
114
93
  args.GetReturnValue().Set(had_pending_signals);
115
31
}
116
117
118
3
void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
119
3
  bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal();
120
9
  args.GetReturnValue().Set(ret);
121
3
}
122
123
124
1698
void Initialize(Local<Object> target,
125
                Local<Value> unused,
126
                Local<Context> context) {
127
1698
  Environment* env = Environment::GetCurrent(context);
128
129
#define V(lcname, ucname) env->SetMethod(target, #lcname, ucname);
130
1698
  VALUE_METHOD_MAP(V)
131
#undef V
132
133
#define V(name, _)                                                            \
134
  target->Set(context,                                                        \
135
              FIXED_ONE_BYTE_STRING(env->isolate(), #name),                   \
136
              Integer::NewFromUnsigned(env->isolate(), index++)).FromJust();
137
  {
138
1698
    uint32_t index = 0;
139
42450
    PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
140
  }
141
#undef V
142
143
1698
  env->SetMethod(target, "getHiddenValue", GetHiddenValue);
144
1698
  env->SetMethod(target, "setHiddenValue", SetHiddenValue);
145
1698
  env->SetMethod(target, "getProxyDetails", GetProxyDetails);
146
147
1698
  env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
148
1698
  env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
149
1698
  env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
150
1698
}
151
152
}  // namespace util
153
}  // namespace node
154
155
1705
NODE_MODULE_CONTEXT_AWARE_BUILTIN(util, node::util::Initialize)