GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_internals.h Lines: 35 36 97.2 %
Date: 2017-06-14 Branches: 20 30 66.7 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_NODE_INTERNALS_H_
23
#define SRC_NODE_INTERNALS_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "node.h"
28
#include "util.h"
29
#include "util-inl.h"
30
#include "uv.h"
31
#include "v8.h"
32
#include "tracing/trace_event.h"
33
34
#include <stdint.h>
35
#include <stdlib.h>
36
37
#include <string>
38
39
// Custom constants used by both node_constants.cc and node_zlib.cc
40
#define Z_MIN_WINDOWBITS 8
41
#define Z_MAX_WINDOWBITS 15
42
#define Z_DEFAULT_WINDOWBITS 15
43
// Fewer than 64 bytes per chunk is not recommended.
44
// Technically it could work with as few as 8, but even 64 bytes
45
// is low.  Usually a MB or more is best.
46
#define Z_MIN_CHUNK 64
47
#define Z_MAX_CHUNK std::numeric_limits<double>::infinity()
48
#define Z_DEFAULT_CHUNK (16 * 1024)
49
#define Z_MIN_MEMLEVEL 1
50
#define Z_MAX_MEMLEVEL 9
51
#define Z_DEFAULT_MEMLEVEL 8
52
#define Z_MIN_LEVEL -1
53
#define Z_MAX_LEVEL 9
54
#define Z_DEFAULT_LEVEL Z_DEFAULT_COMPRESSION
55
56
struct sockaddr;
57
58
// Variation on NODE_DEFINE_CONSTANT that sets a String value.
59
#define NODE_DEFINE_STRING_CONSTANT(target, name, constant)                   \
60
  do {                                                                        \
61
    v8::Isolate* isolate = target->GetIsolate();                              \
62
    v8::Local<v8::String> constant_name =                                     \
63
        v8::String::NewFromUtf8(isolate, name);                               \
64
    v8::Local<v8::String> constant_value =                                    \
65
        v8::String::NewFromUtf8(isolate, constant);                           \
66
    v8::PropertyAttribute constant_attributes =                               \
67
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
68
    target->DefineOwnProperty(isolate->GetCurrentContext(),                   \
69
                              constant_name,                                  \
70
                              constant_value,                                 \
71
                              constant_attributes).FromJust();                \
72
  } while (0)
73
74
namespace node {
75
76
// Set in node.cc by ParseArgs with the value of --openssl-config.
77
// Used in node_crypto.cc when initializing OpenSSL.
78
extern std::string openssl_config;
79
80
// Set in node.cc by ParseArgs when --preserve-symlinks is used.
81
// Used in node_config.cc to set a constant on process.binding('config')
82
// that is used by lib/module.js
83
extern bool config_preserve_symlinks;
84
85
// Set in node.cc by ParseArgs when --expose-internals or --expose_internals is
86
// used.
87
// Used in node_config.cc to set a constant on process.binding('config')
88
// that is used by lib/internal/bootstrap_node.js
89
extern bool config_expose_internals;
90
91
// Set in node.cc by ParseArgs when --redirect-warnings= is used.
92
// Used to redirect warning output to a file rather than sending
93
// it to stderr.
94
extern std::string config_warning_file;  // NOLINT(runtime/string)
95
96
// Set in node.cc by ParseArgs when --pending-deprecation or
97
// NODE_PENDING_DEPRECATION is used
98
extern bool config_pending_deprecation;
99
100
// Tells whether it is safe to call v8::Isolate::GetCurrent().
101
extern bool v8_initialized;
102
103
// Forward declaration
104
class Environment;
105
106
// If persistent.IsWeak() == false, then do not call persistent.Reset()
107
// while the returned Local<T> is still in scope, it will destroy the
108
// reference to the object.
109
template <class TypeName>
110
inline v8::Local<TypeName> PersistentToLocal(
111
    v8::Isolate* isolate,
112
    const v8::Persistent<TypeName>& persistent);
113
114
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
115
// Sets address and port properties on the info object and returns it.
116
// If |info| is omitted, a new object is returned.
117
v8::Local<v8::Object> AddressToJS(
118
    Environment* env,
119
    const sockaddr* addr,
120
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
121
122
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
123
1257
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
124
1257
  T* const wrap = Unwrap<T>(args.Holder());
125

1257
  if (wrap == nullptr)
126
1257
    return args.GetReturnValue().Set(UV_EBADF);
127

2514
  CHECK(args[0]->IsObject());
128
  sockaddr_storage storage;
129
1257
  int addrlen = sizeof(storage);
130
1257
  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
131
1257
  const int err = F(&wrap->handle_, addr, &addrlen);
132

1257
  if (err == 0)
133
2506
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
134
2514
  args.GetReturnValue().Set(err);
135
}
136
137
void SignalExit(int signo);
138
#ifdef __POSIX__
139
void RegisterSignalHandler(int signal,
140
                           void (*handler)(int signal),
141
                           bool reset_handler = false);
142
#endif
143
144
bool SafeGetenv(const char* key, std::string* text);
145
146
template <typename T, size_t N>
147
1835217
constexpr size_t arraysize(const T(&)[N]) { return N; }
148
149
#ifndef ROUND_UP
150
# define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
151
#endif
152
153
#ifdef __GNUC__
154
# define MUST_USE_RESULT __attribute__((warn_unused_result))
155
#else
156
# define MUST_USE_RESULT
157
#endif
158
159
bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);
160
161
enum ErrorHandlingMode { FATAL_ERROR, CONTEXTIFY_ERROR };
162
void AppendExceptionLine(Environment* env,
163
                         v8::Local<v8::Value> er,
164
                         v8::Local<v8::Message> message,
165
                         enum ErrorHandlingMode mode);
166
167
NO_RETURN void FatalError(const char* location, const char* message);
168
169
void ProcessEmitWarning(Environment* env, const char* fmt, ...);
170
171
void FillStatsArray(double* fields, const uv_stat_t* s);
172
173
void SetupProcessObject(Environment* env,
174
                        int argc,
175
                        const char* const* argv,
176
                        int exec_argc,
177
                        const char* const* exec_argv);
178
179
enum Endianness {
180
  kLittleEndian,  // _Not_ LITTLE_ENDIAN, clashes with endian.h.
181
  kBigEndian
182
};
183
184
7788
inline enum Endianness GetEndianness() {
185
  // Constant-folded by the compiler.
186
  const union {
187
    uint8_t u8[2];
188
    uint16_t u16;
189
  } u = {
190
    { 1, 0 }
191
7788
  };
192
7788
  return u.u16 == 1 ? kLittleEndian : kBigEndian;
193
}
194
195
2705
inline bool IsLittleEndian() {
196
2705
  return GetEndianness() == kLittleEndian;
197
}
198
199
4965
inline bool IsBigEndian() {
200
4965
  return GetEndianness() == kBigEndian;
201
}
202
203
2097
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
204
 public:
205
2401
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
206
207
  virtual void* Allocate(size_t size);  // Defined in src/node.cc
208
5438
  virtual void* AllocateUninitialized(size_t size)
209
5438
    { return node::UncheckedMalloc(size); }
210
43940
  virtual void Free(void* data, size_t) { free(data); }
211
212
 private:
213
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
214
};
215
216
// Clear any domain and/or uncaughtException handlers to force the error's
217
// propagation and shutdown the process. Use this to force the process to exit
218
// by clearing all callbacks that could handle the error.
219
void ClearFatalExceptionHandlers(Environment* env);
220
221
namespace Buffer {
222
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
223
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
224
// Takes ownership of |data|.
225
v8::MaybeLocal<v8::Object> New(Environment* env,
226
                               char* data,
227
                               size_t length,
228
                               void (*callback)(char* data, void* hint),
229
                               void* hint);
230
// Takes ownership of |data|.  Must allocate |data| with malloc() or realloc()
231
// because ArrayBufferAllocator::Free() deallocates it again with free().
232
// Mixing operator new and free() is undefined behavior so don't do that.
233
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);
234
235
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
236
// Utf8Value and TwoByteValue).
237
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
238
// changed.
239
// If |buf| contains actual data, this method takes ownership of |buf|'s
240
// underlying buffer. However, |buf| itself can be reused even after this call,
241
// but its capacity, if increased through AllocateSufficientStorage, is not
242
// guaranteed to stay the same.
243
template <typename T>
244
10
static v8::MaybeLocal<v8::Object> New(Environment* env,
245
                                      MaybeStackBuffer<T>* buf) {
246
  v8::MaybeLocal<v8::Object> ret;
247
10
  char* src = reinterpret_cast<char*>(buf->out());
248
10
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
249
250

10
  if (buf->IsAllocated())
251
2
    ret = New(env, src, len_in_bytes);
252

8
  else if (!buf->IsInvalidated())
253
8
    ret = Copy(env, src, len_in_bytes);
254
255

10
  if (ret.IsEmpty())
256
    return ret;
257
258

10
  if (buf->IsAllocated())
259
2
    buf->Release();
260
261
10
  return ret;
262
}
263
}  // namespace Buffer
264
265
}  // namespace node
266
267
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
268
269
#endif  // SRC_NODE_INTERNALS_H_