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