GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_internals.h Lines: 16 17 94.1 %
Date: 2017-01-02 Branches: 7 12 58.3 %

Line Branch Exec Source
1
#ifndef SRC_NODE_INTERNALS_H_
2
#define SRC_NODE_INTERNALS_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "node.h"
7
#include "util.h"
8
#include "util-inl.h"
9
#include "uv.h"
10
#include "v8.h"
11
12
#include <stdint.h>
13
#include <stdlib.h>
14
15
struct sockaddr;
16
17
// Variation on NODE_DEFINE_CONSTANT that sets a String value.
18
#define NODE_DEFINE_STRING_CONSTANT(target, name, constant)                   \
19
  do {                                                                        \
20
    v8::Isolate* isolate = target->GetIsolate();                              \
21
    v8::Local<v8::String> constant_name =                                     \
22
        v8::String::NewFromUtf8(isolate, name);                               \
23
    v8::Local<v8::String> constant_value =                                    \
24
        v8::String::NewFromUtf8(isolate, constant);                           \
25
    v8::PropertyAttribute constant_attributes =                               \
26
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
27
    target->DefineOwnProperty(isolate->GetCurrentContext(),                   \
28
                              constant_name,                                  \
29
                              constant_value,                                 \
30
                              constant_attributes).FromJust();                \
31
  } while (0)
32
33
namespace node {
34
35
// Set in node.cc by ParseArgs with the value of --openssl-config.
36
// Used in node_crypto.cc when initializing OpenSSL.
37
extern const char* openssl_config;
38
39
// Set in node.cc by ParseArgs when --preserve-symlinks is used.
40
// Used in node_config.cc to set a constant on process.binding('config')
41
// that is used by lib/module.js
42
extern bool config_preserve_symlinks;
43
44
// Tells whether it is safe to call v8::Isolate::GetCurrent().
45
extern bool v8_initialized;
46
47
// Forward declaration
48
class Environment;
49
50
// If persistent.IsWeak() == false, then do not call persistent.Reset()
51
// while the returned Local<T> is still in scope, it will destroy the
52
// reference to the object.
53
template <class TypeName>
54
inline v8::Local<TypeName> PersistentToLocal(
55
    v8::Isolate* isolate,
56
    const v8::Persistent<TypeName>& persistent);
57
58
// Call with valid HandleScope and while inside Context scope.
59
v8::Local<v8::Value> MakeCallback(Environment* env,
60
                                   v8::Local<v8::Object> recv,
61
                                   const char* method,
62
                                   int argc = 0,
63
                                   v8::Local<v8::Value>* argv = nullptr);
64
65
// Call with valid HandleScope and while inside Context scope.
66
v8::Local<v8::Value> MakeCallback(Environment* env,
67
                                   v8::Local<v8::Object> recv,
68
                                   v8::Local<v8::String> symbol,
69
                                   int argc = 0,
70
                                   v8::Local<v8::Value>* argv = nullptr);
71
72
// Call with valid HandleScope and while inside Context scope.
73
v8::Local<v8::Value> MakeCallback(Environment* env,
74
                                   v8::Local<v8::Value> recv,
75
                                   v8::Local<v8::Function> callback,
76
                                   int argc = 0,
77
                                   v8::Local<v8::Value>* argv = nullptr);
78
79
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
80
// Sets address and port properties on the info object and returns it.
81
// If |info| is omitted, a new object is returned.
82
v8::Local<v8::Object> AddressToJS(
83
    Environment* env,
84
    const sockaddr* addr,
85
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
86
87
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
88
1162
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
89
1162
  T* const wrap = Unwrap<T>(args.Holder());
90

1162
  if (wrap == nullptr)
91
    return args.GetReturnValue().Set(UV_EBADF);
92

1162
  CHECK(args[0]->IsObject());
93
  sockaddr_storage storage;
94
1162
  int addrlen = sizeof(storage);
95
1162
  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
96
1162
  const int err = F(&wrap->handle_, addr, &addrlen);
97

1162
  if (err == 0)
98
2318
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
99
2324
  args.GetReturnValue().Set(err);
100
}
101
102
void SignalExit(int signo);
103
#ifdef __POSIX__
104
void RegisterSignalHandler(int signal,
105
                           void (*handler)(int signal),
106
                           bool reset_handler = false);
107
#endif
108
109
template <typename T, size_t N>
110
constexpr size_t arraysize(const T(&)[N]) { return N; }
111
112
#ifndef ROUND_UP
113
# define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a))
114
#endif
115
116
#ifdef __GNUC__
117
# define MUST_USE_RESULT __attribute__((warn_unused_result))
118
#else
119
# define MUST_USE_RESULT
120
#endif
121
122
bool IsExceptionDecorated(Environment* env, v8::Local<v8::Value> er);
123
124
enum ErrorHandlingMode { FATAL_ERROR, CONTEXTIFY_ERROR };
125
void AppendExceptionLine(Environment* env,
126
                         v8::Local<v8::Value> er,
127
                         v8::Local<v8::Message> message,
128
                         enum ErrorHandlingMode mode);
129
130
NO_RETURN void FatalError(const char* location, const char* message);
131
132
void ProcessEmitWarning(Environment* env, const char* fmt, ...);
133
134
v8::Local<v8::Value> BuildStatsObject(Environment* env, const uv_stat_t* s);
135
136
void SetupProcessObject(Environment* env,
137
                        int argc,
138
                        const char* const* argv,
139
                        int exec_argc,
140
                        const char* const* exec_argv);
141
142
enum Endianness {
143
  kLittleEndian,  // _Not_ LITTLE_ENDIAN, clashes with endian.h.
144
  kBigEndian
145
};
146
147
inline enum Endianness GetEndianness() {
148
  // Constant-folded by the compiler.
149
  const union {
150
    uint8_t u8[2];
151
    uint16_t u16;
152
  } u = {
153
    { 1, 0 }
154
7347
  };
155
7347
  return u.u16 == 1 ? kLittleEndian : kBigEndian;
156
}
157
158
inline bool IsLittleEndian() {
159
  return GetEndianness() == kLittleEndian;
160
}
161
162
inline bool IsBigEndian() {
163
  return GetEndianness() == kBigEndian;
164
}
165
166
3088
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
167
 public:
168
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
169
170
  virtual void* Allocate(size_t size);  // Defined in src/node.cc
171
347
  virtual void* AllocateUninitialized(size_t size)
172
347
    { return node::UncheckedMalloc(size); }
173
35411
  virtual void Free(void* data, size_t) { free(data); }
174
175
 private:
176
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
177
};
178
179
// Clear any domain and/or uncaughtException handlers to force the error's
180
// propagation and shutdown the process. Use this to force the process to exit
181
// by clearing all callbacks that could handle the error.
182
void ClearFatalExceptionHandlers(Environment* env);
183
184
namespace Buffer {
185
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
186
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
187
// Takes ownership of |data|.
188
v8::MaybeLocal<v8::Object> New(Environment* env,
189
                               char* data,
190
                               size_t length,
191
                               void (*callback)(char* data, void* hint),
192
                               void* hint);
193
// Takes ownership of |data|.  Must allocate |data| with malloc() or realloc()
194
// because ArrayBufferAllocator::Free() deallocates it again with free().
195
// Mixing operator new and free() is undefined behavior so don't do that.
196
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);
197
}  // namespace Buffer
198
199
}  // namespace node
200
201
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
202
203
#endif  // SRC_NODE_INTERNALS_H_