GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node.h Lines: 9 10 90.0 %
Date: 2016-11-30 Branches: 0 0 0.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_H_
2
#define SRC_NODE_H_
3
4
#ifdef _WIN32
5
# ifndef BUILDING_NODE_EXTENSION
6
#   define NODE_EXTERN __declspec(dllexport)
7
# else
8
#   define NODE_EXTERN __declspec(dllimport)
9
# endif
10
#else
11
# define NODE_EXTERN /* nothing */
12
#endif
13
14
#ifdef BUILDING_NODE_EXTENSION
15
# undef BUILDING_V8_SHARED
16
# undef BUILDING_UV_SHARED
17
# define USING_V8_SHARED 1
18
# define USING_UV_SHARED 1
19
#endif
20
21
// This should be defined in make system.
22
// See issue https://github.com/joyent/node/issues/1236
23
#if defined(__MINGW32__) || defined(_MSC_VER)
24
#ifndef _WIN32_WINNT
25
# define _WIN32_WINNT   0x0501
26
#endif
27
28
#ifndef NOMINMAX
29
# define NOMINMAX
30
#endif
31
32
#endif
33
34
#if defined(_MSC_VER)
35
#define PATH_MAX MAX_PATH
36
#endif
37
38
#ifdef _WIN32
39
# define SIGKILL         9
40
#endif
41
42
#include "v8.h"  // NOLINT(build/include_order)
43
#include "node_version.h"  // NODE_MODULE_VERSION
44
45
#define NODE_MAKE_VERSION(major, minor, patch)                                \
46
  ((major) * 0x1000 + (minor) * 0x100 + (patch))
47
48
#ifdef __clang__
49
# define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
50
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
51
      NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
52
#else
53
# define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
54
#endif
55
56
#ifdef __GNUC__
57
# define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
58
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
59
      NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
60
#else
61
# define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
62
#endif
63
64
#if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
65
# define NODE_DEPRECATED(message, declarator)                                 \
66
    __attribute__((deprecated(message))) declarator
67
#elif defined(_MSC_VER)
68
# define NODE_DEPRECATED(message, declarator)                                 \
69
    __declspec(deprecated) declarator
70
#else
71
# define NODE_DEPRECATED(message, declarator)                                 \
72
    declarator
73
#endif
74
75
// Forward-declare libuv loop
76
struct uv_loop_s;
77
78
// Forward-declare these functions now to stop MSVS from becoming
79
// terminally confused when it's done in node_internals.h
80
namespace node {
81
82
NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
83
                                                int errorno,
84
                                                const char* syscall = NULL,
85
                                                const char* message = NULL,
86
                                                const char* path = NULL);
87
NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
88
                                             int errorno,
89
                                             const char* syscall = NULL,
90
                                             const char* message = NULL,
91
                                             const char* path = NULL);
92
NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
93
                                             int errorno,
94
                                             const char* syscall,
95
                                             const char* message,
96
                                             const char* path,
97
                                             const char* dest);
98
99
NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
100
                inline v8::Local<v8::Value> ErrnoException(
101
      int errorno,
102
      const char* syscall = NULL,
103
      const char* message = NULL,
104
      const char* path = NULL) {
105
  return ErrnoException(v8::Isolate::GetCurrent(),
106
                        errorno,
107
                        syscall,
108
                        message,
109
                        path);
110
})
111
112
inline v8::Local<v8::Value> UVException(int errorno,
113
                                        const char* syscall = NULL,
114
                                        const char* message = NULL,
115
                                        const char* path = NULL) {
116
  return UVException(v8::Isolate::GetCurrent(),
117
                     errorno,
118
                     syscall,
119
                     message,
120
                     path);
121
}
122
123
/*
124
 * MakeCallback doesn't have a HandleScope. That means the callers scope
125
 * will retain ownership of created handles from MakeCallback and related.
126
 * There is by default a wrapping HandleScope before uv_run, if the caller
127
 * doesn't have a HandleScope on the stack the global will take ownership
128
 * which won't be reaped until the uv loop exits.
129
 *
130
 * If a uv callback is fired, and there is no enclosing HandleScope in the
131
 * cb, you will appear to leak 4-bytes for every invocation. Take heed.
132
 */
133
134
NODE_EXTERN v8::Local<v8::Value> MakeCallback(
135
    v8::Isolate* isolate,
136
    v8::Local<v8::Object> recv,
137
    const char* method,
138
    int argc,
139
    v8::Local<v8::Value>* argv);
140
NODE_EXTERN v8::Local<v8::Value> MakeCallback(
141
    v8::Isolate* isolate,
142
    v8::Local<v8::Object> recv,
143
    v8::Local<v8::String> symbol,
144
    int argc,
145
    v8::Local<v8::Value>* argv);
146
NODE_EXTERN v8::Local<v8::Value> MakeCallback(
147
    v8::Isolate* isolate,
148
    v8::Local<v8::Object> recv,
149
    v8::Local<v8::Function> callback,
150
    int argc,
151
    v8::Local<v8::Value>* argv);
152
153
}  // namespace node
154
155
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
156
#include "node_internals.h"
157
#endif
158
159
#include <assert.h>
160
#include <stdint.h>
161
162
#ifndef NODE_STRINGIFY
163
#define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
164
#define NODE_STRINGIFY_HELPER(n) #n
165
#endif
166
167
#ifdef _WIN32
168
// TODO(tjfontaine) consider changing the usage of ssize_t to ptrdiff_t
169
#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
170
typedef intptr_t ssize_t;
171
# define _SSIZE_T_
172
# define _SSIZE_T_DEFINED
173
#endif
174
#else  // !_WIN32
175
# include <sys/types.h>  // size_t, ssize_t
176
#endif  // _WIN32
177
178
179
namespace node {
180
181
NODE_EXTERN extern bool no_deprecation;
182
#if HAVE_OPENSSL && NODE_FIPS_MODE
183
NODE_EXTERN extern bool enable_fips_crypto;
184
NODE_EXTERN extern bool force_fips_crypto;
185
#endif
186
187
NODE_EXTERN int Start(int argc, char *argv[]);
188
NODE_EXTERN void Init(int* argc,
189
                      const char** argv,
190
                      int* exec_argc,
191
                      const char*** exec_argv);
192
193
class IsolateData;
194
class Environment;
195
196
NODE_EXTERN IsolateData* CreateIsolateData(v8::Isolate* isolate,
197
                                           struct uv_loop_s* loop);
198
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
199
200
NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
201
                                           v8::Local<v8::Context> context,
202
                                           int argc,
203
                                           const char* const* argv,
204
                                           int exec_argc,
205
                                           const char* const* exec_argv);
206
207
NODE_EXTERN void LoadEnvironment(Environment* env);
208
NODE_EXTERN void FreeEnvironment(Environment* env);
209
210
NODE_EXTERN void EmitBeforeExit(Environment* env);
211
NODE_EXTERN int EmitExit(Environment* env);
212
NODE_EXTERN void RunAtExit(Environment* env);
213
214
/* Converts a unixtime to V8 Date */
215
#define NODE_UNIXTIME_V8(t) v8::Date::New(v8::Isolate::GetCurrent(),          \
216
    1000 * static_cast<double>(t))
217
#define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->NumberValue())/1000.0);
218
219
// Used to be a macro, hence the uppercase name.
220
#define NODE_DEFINE_CONSTANT(target, constant)                                \
221
  do {                                                                        \
222
    v8::Isolate* isolate = target->GetIsolate();                              \
223
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
224
    v8::Local<v8::String> constant_name =                                     \
225
        v8::String::NewFromUtf8(isolate, #constant);                          \
226
    v8::Local<v8::Number> constant_value =                                    \
227
        v8::Number::New(isolate, static_cast<double>(constant));              \
228
    v8::PropertyAttribute constant_attributes =                               \
229
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
230
    (target)->DefineOwnProperty(context,                                      \
231
                                constant_name,                                \
232
                                constant_value,                               \
233
                                constant_attributes).FromJust();              \
234
  }                                                                           \
235
  while (0)
236
237
// Used to be a macro, hence the uppercase name.
238
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
239
                            const char* name,
240
                            v8::FunctionCallback callback) {
241
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
242
  v8::HandleScope handle_scope(isolate);
243
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
244
                                                                callback);
245
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
246
  t->SetClassName(fn_name);
247
  recv->Set(fn_name, t);
248
}
249
250
// Used to be a macro, hence the uppercase name.
251
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
252
                            const char* name,
253
                            v8::FunctionCallback callback) {
254
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
255
  v8::HandleScope handle_scope(isolate);
256
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
257
                                                                callback);
258
  v8::Local<v8::Function> fn = t->GetFunction();
259
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
260
  fn->SetName(fn_name);
261
  recv->Set(fn_name, fn);
262
}
263
#define NODE_SET_METHOD node::NODE_SET_METHOD
264
265
// Used to be a macro, hence the uppercase name.
266
// Not a template because it only makes sense for FunctionTemplates.
267
18
inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
268
                                      const char* name,
269
                                      v8::FunctionCallback callback) {
270
18
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
271
36
  v8::HandleScope handle_scope(isolate);
272
18
  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
273
  v8::Local<v8::FunctionTemplate> t =
274
18
      v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
275
18
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name);
276
18
  t->SetClassName(fn_name);
277
72
  recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), t);
278
18
}
279
#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
280
281
// BINARY is a deprecated alias of LATIN1.
282
enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER, LATIN1 = BINARY};
283
284
NODE_EXTERN enum encoding ParseEncoding(
285
    v8::Isolate* isolate,
286
    v8::Local<v8::Value> encoding_v,
287
    enum encoding default_encoding = LATIN1);
288
NODE_DEPRECATED("Use ParseEncoding(isolate, ...)",
289
                inline enum encoding ParseEncoding(
290
      v8::Local<v8::Value> encoding_v,
291
      enum encoding default_encoding = LATIN1) {
292
  return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);
293
})
294
295
NODE_EXTERN void FatalException(v8::Isolate* isolate,
296
                                const v8::TryCatch& try_catch);
297
298
NODE_DEPRECATED("Use FatalException(isolate, ...)",
299
                inline void FatalException(const v8::TryCatch& try_catch) {
300
  return FatalException(v8::Isolate::GetCurrent(), try_catch);
301
})
302
303
// Don't call with encoding=UCS2.
304
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
305
                                        const char* buf,
306
                                        size_t len,
307
                                        enum encoding encoding = LATIN1);
308
309
// The input buffer should be in host endianness.
310
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
311
                                        const uint16_t* buf,
312
                                        size_t len);
313
314
NODE_DEPRECATED("Use Encode(isolate, ...)",
315
                inline v8::Local<v8::Value> Encode(
316
    const void* buf,
317
    size_t len,
318
    enum encoding encoding = LATIN1) {
319
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
320
  if (encoding == UCS2) {
321
    assert(reinterpret_cast<uintptr_t>(buf) % sizeof(uint16_t) == 0 &&
322
           "UCS2 buffer must be aligned on two-byte boundary.");
323
    const uint16_t* that = static_cast<const uint16_t*>(buf);
324
    return Encode(isolate, that, len / sizeof(*that));
325
  }
326
  return Encode(isolate, static_cast<const char*>(buf), len, encoding);
327
})
328
329
// Returns -1 if the handle was not valid for decoding
330
NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
331
                                v8::Local<v8::Value>,
332
                                enum encoding encoding = LATIN1);
333
NODE_DEPRECATED("Use DecodeBytes(isolate, ...)",
334
                inline ssize_t DecodeBytes(
335
    v8::Local<v8::Value> val,
336
    enum encoding encoding = LATIN1) {
337
  return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding);
338
})
339
340
// returns bytes written.
341
NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
342
                                char* buf,
343
                                size_t buflen,
344
                                v8::Local<v8::Value>,
345
                                enum encoding encoding = LATIN1);
346
NODE_DEPRECATED("Use DecodeWrite(isolate, ...)",
347
                inline ssize_t DecodeWrite(char* buf,
348
                                           size_t buflen,
349
                                           v8::Local<v8::Value> val,
350
                                           enum encoding encoding = LATIN1) {
351
  return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding);
352
})
353
354
#ifdef _WIN32
355
NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
356
    v8::Isolate* isolate,
357
    int errorno,
358
    const char *syscall = NULL,
359
    const char *msg = "",
360
    const char *path = NULL);
361
362
NODE_DEPRECATED("Use WinapiErrnoException(isolate, ...)",
363
                inline v8::Local<v8::Value> WinapiErrnoException(int errorno,
364
    const char *syscall = NULL,  const char *msg = "",
365
    const char *path = NULL) {
366
  return WinapiErrnoException(v8::Isolate::GetCurrent(),
367
                              errorno,
368
                              syscall,
369
                              msg,
370
                              path);
371
})
372
#endif
373
374
const char *signo_string(int errorno);
375
376
377
typedef void (*addon_register_func)(
378
    v8::Local<v8::Object> exports,
379
    v8::Local<v8::Value> module,
380
    void* priv);
381
382
typedef void (*addon_context_register_func)(
383
    v8::Local<v8::Object> exports,
384
    v8::Local<v8::Value> module,
385
    v8::Local<v8::Context> context,
386
    void* priv);
387
388
#define NM_F_BUILTIN 0x01
389
#define NM_F_LINKED  0x02
390
391
struct node_module {
392
  int nm_version;
393
  unsigned int nm_flags;
394
  void* nm_dso_handle;
395
  const char* nm_filename;
396
  node::addon_register_func nm_register_func;
397
  node::addon_context_register_func nm_context_register_func;
398
  const char* nm_modname;
399
  void* nm_priv;
400
  struct node_module* nm_link;
401
};
402
403
node_module* get_builtin_module(const char *name);
404
node_module* get_linked_module(const char *name);
405
406
extern "C" NODE_EXTERN void node_module_register(void* mod);
407
408
#ifdef _WIN32
409
# define NODE_MODULE_EXPORT __declspec(dllexport)
410
#else
411
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
412
#endif
413
414
#ifdef NODE_SHARED_MODE
415
# define NODE_CTOR_PREFIX
416
#else
417
# define NODE_CTOR_PREFIX static
418
#endif
419
420
#if defined(_MSC_VER)
421
#pragma section(".CRT$XCU", read)
422
#define NODE_C_CTOR(fn)                                               \
423
  NODE_CTOR_PREFIX void __cdecl fn(void);                             \
424
  __declspec(dllexport, allocate(".CRT$XCU"))                         \
425
      void (__cdecl*fn ## _)(void) = fn;                              \
426
  NODE_CTOR_PREFIX void __cdecl fn(void)
427
#else
428
#define NODE_C_CTOR(fn)                                               \
429
  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
430
  NODE_CTOR_PREFIX void fn(void)
431
#endif
432
433
#define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
434
  extern "C" {                                                        \
435
    static node::node_module _module =                                \
436
    {                                                                 \
437
      NODE_MODULE_VERSION,                                            \
438
      flags,                                                          \
439
      NULL,                                                           \
440
      __FILE__,                                                       \
441
      (node::addon_register_func) (regfunc),                          \
442
      NULL,                                                           \
443
      NODE_STRINGIFY(modname),                                        \
444
      priv,                                                           \
445
      NULL                                                            \
446
    };                                                                \
447
    NODE_C_CTOR(_register_ ## modname) {                              \
448
      node_module_register(&_module);                                 \
449
    }                                                                 \
450
  }
451
452
#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
453
  extern "C" {                                                        \
454
    static node::node_module _module =                                \
455
    {                                                                 \
456
      NODE_MODULE_VERSION,                                            \
457
      flags,                                                          \
458
      NULL,                                                           \
459
      __FILE__,                                                       \
460
      NULL,                                                           \
461
      (node::addon_context_register_func) (regfunc),                  \
462
      NODE_STRINGIFY(modname),                                        \
463
      priv,                                                           \
464
      NULL                                                            \
465
    };                                                                \
466
    NODE_C_CTOR(_register_ ## modname) {                              \
467
      node_module_register(&_module);                                 \
468
    }                                                                 \
469
  }
470
471
#define NODE_MODULE(modname, regfunc)                                 \
472
  NODE_MODULE_X(modname, regfunc, NULL, 0)
473
474
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
475
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
476
477
#define NODE_MODULE_CONTEXT_AWARE_BUILTIN(modname, regfunc)           \
478
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, NM_F_BUILTIN)   \
479
480
/*
481
 * For backward compatibility in add-on modules.
482
 */
483
#define NODE_MODULE_DECL /* nothing */
484
485
/* Called after the event loop exits but before the VM is disposed.
486
 * Callbacks are run in reverse order of registration, i.e. newest first.
487
 */
488
NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = 0);
489
490
}  // namespace node
491
492
#endif  // SRC_NODE_H_