GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node.h Lines: 6 6 100.0 %
Date: 2020-02-27 22:14:15 Branches: 0 0 - %

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_H_
23
#define SRC_NODE_H_
24
25
#ifdef _WIN32
26
# ifndef BUILDING_NODE_EXTENSION
27
#  define NODE_EXTERN __declspec(dllexport)
28
# else
29
#  define NODE_EXTERN __declspec(dllimport)
30
# endif
31
#else
32
# define NODE_EXTERN __attribute__((visibility("default")))
33
#endif
34
35
#ifdef BUILDING_NODE_EXTENSION
36
# undef BUILDING_V8_SHARED
37
# undef BUILDING_UV_SHARED
38
# define USING_V8_SHARED 1
39
# define USING_UV_SHARED 1
40
#endif
41
42
// This should be defined in make system.
43
// See issue https://github.com/nodejs/node-v0.x-archive/issues/1236
44
#if defined(__MINGW32__) || defined(_MSC_VER)
45
#ifndef _WIN32_WINNT
46
# define _WIN32_WINNT 0x0600  // Windows Server 2008
47
#endif
48
49
#ifndef NOMINMAX
50
# define NOMINMAX
51
#endif
52
53
#endif
54
55
#if defined(_MSC_VER)
56
#define PATH_MAX MAX_PATH
57
#endif
58
59
#ifdef _WIN32
60
# define SIGKILL 9
61
#endif
62
63
#if (__GNUC__ >= 8) && !defined(__clang__)
64
#pragma GCC diagnostic push
65
#pragma GCC diagnostic ignored "-Wcast-function-type"
66
#endif
67
#include "v8.h"  // NOLINT(build/include_order)
68
#if (__GNUC__ >= 8) && !defined(__clang__)
69
#pragma GCC diagnostic pop
70
#endif
71
72
#include "v8-platform.h"  // NOLINT(build/include_order)
73
#include "node_version.h"  // NODE_MODULE_VERSION
74
75
#include <memory>
76
77
// We cannot use __POSIX__ in this header because that's only defined when
78
// building Node.js.
79
#ifndef _WIN32
80
#include <signal.h>
81
#endif  // _WIN32
82
83
#define NODE_MAKE_VERSION(major, minor, patch)                                \
84
  ((major) * 0x1000 + (minor) * 0x100 + (patch))
85
86
#ifdef __clang__
87
# define NODE_CLANG_AT_LEAST(major, minor, patch)                             \
88
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
89
      NODE_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__))
90
#else
91
# define NODE_CLANG_AT_LEAST(major, minor, patch) (0)
92
#endif
93
94
#ifdef __GNUC__
95
# define NODE_GNUC_AT_LEAST(major, minor, patch)                              \
96
  (NODE_MAKE_VERSION(major, minor, patch) <=                                  \
97
      NODE_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__))
98
#else
99
# define NODE_GNUC_AT_LEAST(major, minor, patch) (0)
100
#endif
101
102
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
103
# define NODE_DEPRECATED(message, declarator) declarator
104
#else  // NODE_WANT_INTERNALS
105
# if NODE_CLANG_AT_LEAST(2, 9, 0) || NODE_GNUC_AT_LEAST(4, 5, 0)
106
#  define NODE_DEPRECATED(message, declarator)                                 \
107
    __attribute__((deprecated(message))) declarator
108
# elif defined(_MSC_VER)
109
#  define NODE_DEPRECATED(message, declarator)                                 \
110
    __declspec(deprecated) declarator
111
# else
112
#  define NODE_DEPRECATED(message, declarator) declarator
113
# endif
114
#endif
115
116
// Forward-declare libuv loop
117
struct uv_loop_s;
118
119
// Forward-declare these functions now to stop MSVS from becoming
120
// terminally confused when it's done in node_internals.h
121
namespace node {
122
123
namespace tracing {
124
125
class TracingController;
126
127
}
128
129
NODE_EXTERN v8::Local<v8::Value> ErrnoException(v8::Isolate* isolate,
130
                                                int errorno,
131
                                                const char* syscall = nullptr,
132
                                                const char* message = nullptr,
133
                                                const char* path = nullptr);
134
NODE_EXTERN v8::Local<v8::Value> UVException(v8::Isolate* isolate,
135
                                             int errorno,
136
                                             const char* syscall = nullptr,
137
                                             const char* message = nullptr,
138
                                             const char* path = nullptr,
139
                                             const char* dest = nullptr);
140
141
NODE_DEPRECATED("Use ErrnoException(isolate, ...)",
142
                inline v8::Local<v8::Value> ErrnoException(
143
      int errorno,
144
      const char* syscall = nullptr,
145
      const char* message = nullptr,
146
      const char* path = nullptr) {
147
  return ErrnoException(v8::Isolate::GetCurrent(),
148
                        errorno,
149
                        syscall,
150
                        message,
151
                        path);
152
})
153
154
NODE_DEPRECATED("Use UVException(isolate, ...)",
155
                inline v8::Local<v8::Value> UVException(int errorno,
156
                                        const char* syscall = nullptr,
157
                                        const char* message = nullptr,
158
                                        const char* path = nullptr) {
159
  return UVException(v8::Isolate::GetCurrent(),
160
                     errorno,
161
                     syscall,
162
                     message,
163
                     path);
164
})
165
166
/*
167
 * These methods need to be called in a HandleScope.
168
 *
169
 * It is preferred that you use the `MakeCallback` overloads taking
170
 * `async_context` arguments.
171
 */
172
173
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
174
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
175
                    v8::Isolate* isolate,
176
                    v8::Local<v8::Object> recv,
177
                    const char* method,
178
                    int argc,
179
                    v8::Local<v8::Value>* argv));
180
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
181
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
182
                    v8::Isolate* isolate,
183
                    v8::Local<v8::Object> recv,
184
                    v8::Local<v8::String> symbol,
185
                    int argc,
186
                    v8::Local<v8::Value>* argv));
187
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
188
                NODE_EXTERN v8::Local<v8::Value> MakeCallback(
189
                    v8::Isolate* isolate,
190
                    v8::Local<v8::Object> recv,
191
                    v8::Local<v8::Function> callback,
192
                    int argc,
193
                    v8::Local<v8::Value>* argv));
194
195
}  // namespace node
196
197
#include <cassert>
198
#include <cstdint>
199
200
#ifndef NODE_STRINGIFY
201
# define NODE_STRINGIFY(n) NODE_STRINGIFY_HELPER(n)
202
# define NODE_STRINGIFY_HELPER(n) #n
203
#endif
204
205
#ifdef _WIN32
206
#if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
207
typedef intptr_t ssize_t;
208
# define _SSIZE_T_
209
# define _SSIZE_T_DEFINED
210
#endif
211
#else  // !_WIN32
212
# include <sys/types.h>  // size_t, ssize_t
213
#endif  // _WIN32
214
215
216
namespace node {
217
218
class IsolateData;
219
class Environment;
220
221
// TODO(addaleax): Officially deprecate this and replace it with something
222
// better suited for a public embedder API.
223
NODE_EXTERN int Start(int argc, char* argv[]);
224
225
// Tear down Node.js while it is running (there are active handles
226
// in the loop and / or actively executing JavaScript code).
227
NODE_EXTERN int Stop(Environment* env);
228
229
// TODO(addaleax): Officially deprecate this and replace it with something
230
// better suited for a public embedder API.
231
NODE_EXTERN void Init(int* argc,
232
                      const char** argv,
233
                      int* exec_argc,
234
                      const char*** exec_argv);
235
236
enum OptionEnvvarSettings {
237
  kAllowedInEnvironment,
238
  kDisallowedInEnvironment
239
};
240
241
NODE_EXTERN int ProcessGlobalArgs(std::vector<std::string>* args,
242
                      std::vector<std::string>* exec_args,
243
                      std::vector<std::string>* errors,
244
                      OptionEnvvarSettings settings);
245
246
class NodeArrayBufferAllocator;
247
248
// An ArrayBuffer::Allocator class with some Node.js-specific tweaks. If you do
249
// not have to use another allocator, using this class is recommended:
250
// - It supports Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() with
251
//   uninitialized memory.
252
// - It supports transferring, rather than copying, ArrayBuffers when using
253
//   MessagePorts.
254
8386
class NODE_EXTERN ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
255
 public:
256
  // If `always_debug` is true, create an ArrayBuffer::Allocator instance
257
  // that performs additional integrity checks (e.g. make sure that only memory
258
  // that was allocated by the it is also freed by it).
259
  // This can also be set using the --debug-arraybuffer-allocations flag.
260
  static std::unique_ptr<ArrayBufferAllocator> Create(
261
      bool always_debug = false);
262
263
 private:
264
  virtual NodeArrayBufferAllocator* GetImpl() = 0;
265
266
  friend class IsolateData;
267
};
268
269
// Legacy equivalents for ArrayBufferAllocator::Create().
270
NODE_EXTERN ArrayBufferAllocator* CreateArrayBufferAllocator();
271
NODE_EXTERN void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator);
272
273
4411
class NODE_EXTERN IsolatePlatformDelegate {
274
 public:
275
  virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner() = 0;
276
  virtual bool IdleTasksEnabled() = 0;
277
};
278
279
4148
class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
280
 public:
281
4143
  ~MultiIsolatePlatform() override = default;
282
  // Returns true if work was dispatched or executed. New tasks that are
283
  // posted during flushing of the queue are postponed until the next
284
  // flushing.
285
  virtual bool FlushForegroundTasks(v8::Isolate* isolate) = 0;
286
  virtual void DrainTasks(v8::Isolate* isolate) = 0;
287
288
  // TODO(addaleax): Remove this, it is unnecessary.
289
  // This would currently be called before `UnregisterIsolate()` but will be
290
  // folded into it in the future.
291
  virtual void CancelPendingDelayedTasks(v8::Isolate* isolate);
292
293
  // This needs to be called between the calls to `Isolate::Allocate()` and
294
  // `Isolate::Initialize()`, so that initialization can already start
295
  // using the platform.
296
  // When using `NewIsolate()`, this is taken care of by that function.
297
  // This function may only be called once per `Isolate`.
298
  virtual void RegisterIsolate(v8::Isolate* isolate,
299
                               struct uv_loop_s* loop) = 0;
300
  // This method can be used when an application handles task scheduling on its
301
  // own through `IsolatePlatformDelegate`. Upon registering an isolate with
302
  // this overload any other method in this class with the exception of
303
  // `UnregisterIsolate` *must not* be used on that isolate.
304
  virtual void RegisterIsolate(v8::Isolate* isolate,
305
                               IsolatePlatformDelegate* delegate) = 0;
306
307
  // This function may only be called once per `Isolate`, and discard any
308
  // pending delayed tasks scheduled for that isolate.
309
  // This needs to be called right before calling `Isolate::Dispose()`.
310
  virtual void UnregisterIsolate(v8::Isolate* isolate) = 0;
311
312
  // The platform should call the passed function once all state associated
313
  // with the given isolate has been cleaned up. This can, but does not have to,
314
  // happen asynchronously.
315
  virtual void AddIsolateFinishedCallback(v8::Isolate* isolate,
316
                                          void (*callback)(void*),
317
                                          void* data) = 0;
318
};
319
320
enum IsolateSettingsFlags {
321
  MESSAGE_LISTENER_WITH_ERROR_LEVEL = 1 << 0,
322
  DETAILED_SOURCE_POSITIONS_FOR_PROFILING = 1 << 1
323
};
324
325
struct IsolateSettings {
326
  uint64_t flags = MESSAGE_LISTENER_WITH_ERROR_LEVEL |
327
      DETAILED_SOURCE_POSITIONS_FOR_PROFILING;
328
  v8::MicrotasksPolicy policy = v8::MicrotasksPolicy::kExplicit;
329
330
  // Error handling callbacks
331
  v8::Isolate::AbortOnUncaughtExceptionCallback
332
      should_abort_on_uncaught_exception_callback = nullptr;
333
  v8::FatalErrorCallback fatal_error_callback = nullptr;
334
  v8::PrepareStackTraceCallback prepare_stack_trace_callback = nullptr;
335
336
  // Miscellaneous callbacks
337
  v8::PromiseRejectCallback promise_reject_callback = nullptr;
338
  v8::AllowWasmCodeGenerationCallback
339
      allow_wasm_code_generation_callback = nullptr;
340
  v8::HostCleanupFinalizationGroupCallback
341
      host_cleanup_finalization_group_callback = nullptr;
342
};
343
344
// Overriding IsolateSettings may produce unexpected behavior
345
// in Node.js core functionality, so proceed at your own risk.
346
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate,
347
                                     const IsolateSettings& settings);
348
349
// Set a number of callbacks for the `isolate`, in particular the Node.js
350
// uncaught exception listener.
351
NODE_EXTERN void SetIsolateUpForNode(v8::Isolate* isolate);
352
353
// Creates a new isolate with Node.js-specific settings.
354
// This is a convenience method equivalent to using SetIsolateCreateParams(),
355
// Isolate::Allocate(), MultiIsolatePlatform::RegisterIsolate(),
356
// Isolate::Initialize(), and SetIsolateUpForNode().
357
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
358
                                    struct uv_loop_s* event_loop);
359
NODE_EXTERN v8::Isolate* NewIsolate(ArrayBufferAllocator* allocator,
360
                                    struct uv_loop_s* event_loop,
361
                                    MultiIsolatePlatform* platform);
362
NODE_EXTERN v8::Isolate* NewIsolate(
363
    std::shared_ptr<ArrayBufferAllocator> allocator,
364
    struct uv_loop_s* event_loop,
365
    MultiIsolatePlatform* platform);
366
367
// Creates a new context with Node.js-specific tweaks.
368
NODE_EXTERN v8::Local<v8::Context> NewContext(
369
    v8::Isolate* isolate,
370
    v8::Local<v8::ObjectTemplate> object_template =
371
        v8::Local<v8::ObjectTemplate>());
372
373
// Runs Node.js-specific tweaks on an already constructed context
374
// Return value indicates success of operation
375
NODE_EXTERN bool InitializeContext(v8::Local<v8::Context> context);
376
377
// If `platform` is passed, it will be used to register new Worker instances.
378
// It can be `nullptr`, in which case creating new Workers inside of
379
// Environments that use this `IsolateData` will not work.
380
NODE_EXTERN IsolateData* CreateIsolateData(
381
    v8::Isolate* isolate,
382
    struct uv_loop_s* loop,
383
    MultiIsolatePlatform* platform = nullptr,
384
    ArrayBufferAllocator* allocator = nullptr);
385
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);
386
387
// TODO(addaleax): Add an official variant using STL containers, and move
388
// per-Environment options parsing here.
389
// Returns nullptr when the Environment cannot be created e.g. there are
390
// pending JavaScript exceptions.
391
NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
392
                                           v8::Local<v8::Context> context,
393
                                           int argc,
394
                                           const char* const* argv,
395
                                           int exec_argc,
396
                                           const char* const* exec_argv);
397
398
NODE_EXTERN void LoadEnvironment(Environment* env);
399
NODE_EXTERN void FreeEnvironment(Environment* env);
400
401
// This may return nullptr if context is not associated with a Node instance.
402
NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local<v8::Context> context);
403
404
// This returns the MultiIsolatePlatform used in the main thread of Node.js.
405
// If NODE_USE_V8_PLATFORM haven't been defined when Node.js was built,
406
// it returns nullptr.
407
NODE_EXTERN MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform();
408
409
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
410
    int thread_pool_size,
411
    node::tracing::TracingController* tracing_controller);
412
NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform);
413
414
NODE_EXTERN void EmitBeforeExit(Environment* env);
415
NODE_EXTERN int EmitExit(Environment* env);
416
NODE_EXTERN void RunAtExit(Environment* env);
417
418
// This may return nullptr if the current v8::Context is not associated
419
// with a Node instance.
420
NODE_EXTERN struct uv_loop_s* GetCurrentEventLoop(v8::Isolate* isolate);
421
422
/* Converts a unixtime to V8 Date */
423
NODE_DEPRECATED("Use v8::Date::New() directly",
424
                inline v8::Local<v8::Value> NODE_UNIXTIME_V8(double time) {
425
                  return v8::Date::New(
426
                             v8::Isolate::GetCurrent()->GetCurrentContext(),
427
                             1000 * time)
428
                      .ToLocalChecked();
429
                })
430
#define NODE_UNIXTIME_V8 node::NODE_UNIXTIME_V8
431
NODE_DEPRECATED("Use v8::Date::ValueOf() directly",
432
                inline double NODE_V8_UNIXTIME(v8::Local<v8::Date> date) {
433
  return date->ValueOf() / 1000;
434
})
435
#define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME
436
437
#define NODE_DEFINE_CONSTANT(target, constant)                                \
438
  do {                                                                        \
439
    v8::Isolate* isolate = target->GetIsolate();                              \
440
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
441
    v8::Local<v8::String> constant_name =                                     \
442
        v8::String::NewFromUtf8(isolate, #constant,                           \
443
            v8::NewStringType::kInternalized).ToLocalChecked();               \
444
    v8::Local<v8::Number> constant_value =                                    \
445
        v8::Number::New(isolate, static_cast<double>(constant));              \
446
    v8::PropertyAttribute constant_attributes =                               \
447
        static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);    \
448
    (target)->DefineOwnProperty(context,                                      \
449
                                constant_name,                                \
450
                                constant_value,                               \
451
                                constant_attributes).Check();                 \
452
  }                                                                           \
453
  while (0)
454
455
#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant)                         \
456
  do {                                                                        \
457
    v8::Isolate* isolate = target->GetIsolate();                              \
458
    v8::Local<v8::Context> context = isolate->GetCurrentContext();            \
459
    v8::Local<v8::String> constant_name =                                     \
460
        v8::String::NewFromUtf8(isolate, #constant,                           \
461
                                v8::NewStringType::kInternalized)             \
462
                                  .ToLocalChecked();                          \
463
    v8::Local<v8::Number> constant_value =                                    \
464
        v8::Number::New(isolate, static_cast<double>(constant));              \
465
    v8::PropertyAttribute constant_attributes =                               \
466
        static_cast<v8::PropertyAttribute>(v8::ReadOnly |                     \
467
                                           v8::DontDelete |                   \
468
                                           v8::DontEnum);                     \
469
    (target)->DefineOwnProperty(context,                                      \
470
                                constant_name,                                \
471
                                constant_value,                               \
472
                                constant_attributes).Check();                 \
473
  }                                                                           \
474
  while (0)
475
476
// Used to be a macro, hence the uppercase name.
477
inline void NODE_SET_METHOD(v8::Local<v8::Template> recv,
478
                            const char* name,
479
                            v8::FunctionCallback callback) {
480
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
481
  v8::HandleScope handle_scope(isolate);
482
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
483
                                                                callback);
484
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
485
      v8::NewStringType::kInternalized).ToLocalChecked();
486
  t->SetClassName(fn_name);
487
  recv->Set(fn_name, t);
488
}
489
490
// Used to be a macro, hence the uppercase name.
491
inline void NODE_SET_METHOD(v8::Local<v8::Object> recv,
492
                            const char* name,
493
                            v8::FunctionCallback callback) {
494
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
495
  v8::HandleScope handle_scope(isolate);
496
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
497
  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate,
498
                                                                callback);
499
  v8::Local<v8::Function> fn = t->GetFunction(context).ToLocalChecked();
500
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
501
      v8::NewStringType::kInternalized).ToLocalChecked();
502
  fn->SetName(fn_name);
503
  recv->Set(context, fn_name, fn).Check();
504
}
505
#define NODE_SET_METHOD node::NODE_SET_METHOD
506
507
// Used to be a macro, hence the uppercase name.
508
// Not a template because it only makes sense for FunctionTemplates.
509
inline void NODE_SET_PROTOTYPE_METHOD(v8::Local<v8::FunctionTemplate> recv,
510
                                      const char* name,
511
                                      v8::FunctionCallback callback) {
512
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
513
  v8::HandleScope handle_scope(isolate);
514
  v8::Local<v8::Signature> s = v8::Signature::New(isolate, recv);
515
  v8::Local<v8::FunctionTemplate> t =
516
      v8::FunctionTemplate::New(isolate, callback, v8::Local<v8::Value>(), s);
517
  v8::Local<v8::String> fn_name = v8::String::NewFromUtf8(isolate, name,
518
      v8::NewStringType::kInternalized).ToLocalChecked();
519
  t->SetClassName(fn_name);
520
  recv->PrototypeTemplate()->Set(fn_name, t);
521
}
522
#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD
523
524
// BINARY is a deprecated alias of LATIN1.
525
enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER, LATIN1 = BINARY};
526
527
NODE_EXTERN enum encoding ParseEncoding(
528
    v8::Isolate* isolate,
529
    v8::Local<v8::Value> encoding_v,
530
    enum encoding default_encoding = LATIN1);
531
532
NODE_EXTERN void FatalException(v8::Isolate* isolate,
533
                                const v8::TryCatch& try_catch);
534
535
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
536
                                        const char* buf,
537
                                        size_t len,
538
                                        enum encoding encoding = LATIN1);
539
540
// Warning: This reverses endianness on Big Endian platforms, even though the
541
// signature using uint16_t implies that it should not.
542
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
543
                                        const uint16_t* buf,
544
                                        size_t len);
545
546
// Returns -1 if the handle was not valid for decoding
547
NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate,
548
                                v8::Local<v8::Value>,
549
                                enum encoding encoding = LATIN1);
550
// returns bytes written.
551
NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate,
552
                                char* buf,
553
                                size_t buflen,
554
                                v8::Local<v8::Value>,
555
                                enum encoding encoding = LATIN1);
556
#ifdef _WIN32
557
NODE_EXTERN v8::Local<v8::Value> WinapiErrnoException(
558
    v8::Isolate* isolate,
559
    int errorno,
560
    const char* syscall = nullptr,
561
    const char* msg = "",
562
    const char* path = nullptr);
563
#endif
564
565
const char* signo_string(int errorno);
566
567
568
typedef void (*addon_register_func)(
569
    v8::Local<v8::Object> exports,
570
    v8::Local<v8::Value> module,
571
    void* priv);
572
573
typedef void (*addon_context_register_func)(
574
    v8::Local<v8::Object> exports,
575
    v8::Local<v8::Value> module,
576
    v8::Local<v8::Context> context,
577
    void* priv);
578
579
enum ModuleFlags {
580
  kLinked = 0x02
581
};
582
583
struct node_module {
584
  int nm_version;
585
  unsigned int nm_flags;
586
  void* nm_dso_handle;
587
  const char* nm_filename;
588
  node::addon_register_func nm_register_func;
589
  node::addon_context_register_func nm_context_register_func;
590
  const char* nm_modname;
591
  void* nm_priv;
592
  struct node_module* nm_link;
593
};
594
595
extern "C" NODE_EXTERN void node_module_register(void* mod);
596
597
#ifdef _WIN32
598
# define NODE_MODULE_EXPORT __declspec(dllexport)
599
#else
600
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
601
#endif
602
603
#ifdef NODE_SHARED_MODE
604
# define NODE_CTOR_PREFIX
605
#else
606
# define NODE_CTOR_PREFIX static
607
#endif
608
609
#if defined(_MSC_VER)
610
#pragma section(".CRT$XCU", read)
611
#define NODE_C_CTOR(fn)                                               \
612
  NODE_CTOR_PREFIX void __cdecl fn(void);                             \
613
  __declspec(dllexport, allocate(".CRT$XCU"))                         \
614
      void (__cdecl*fn ## _)(void) = fn;                              \
615
  NODE_CTOR_PREFIX void __cdecl fn(void)
616
#else
617
#define NODE_C_CTOR(fn)                                               \
618
  NODE_CTOR_PREFIX void fn(void) __attribute__((constructor));        \
619
  NODE_CTOR_PREFIX void fn(void)
620
#endif
621
622
#define NODE_MODULE_X(modname, regfunc, priv, flags)                  \
623
  extern "C" {                                                        \
624
    static node::node_module _module =                                \
625
    {                                                                 \
626
      NODE_MODULE_VERSION,                                            \
627
      flags,                                                          \
628
      NULL,  /* NOLINT (readability/null_usage) */                    \
629
      __FILE__,                                                       \
630
      (node::addon_register_func) (regfunc),                          \
631
      NULL,  /* NOLINT (readability/null_usage) */                    \
632
      NODE_STRINGIFY(modname),                                        \
633
      priv,                                                           \
634
      NULL   /* NOLINT (readability/null_usage) */                    \
635
    };                                                                \
636
    NODE_C_CTOR(_register_ ## modname) {                              \
637
      node_module_register(&_module);                                 \
638
    }                                                                 \
639
  }
640
641
#define NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, priv, flags)    \
642
  extern "C" {                                                        \
643
    static node::node_module _module =                                \
644
    {                                                                 \
645
      NODE_MODULE_VERSION,                                            \
646
      flags,                                                          \
647
      NULL,  /* NOLINT (readability/null_usage) */                    \
648
      __FILE__,                                                       \
649
      NULL,  /* NOLINT (readability/null_usage) */                    \
650
      (node::addon_context_register_func) (regfunc),                  \
651
      NODE_STRINGIFY(modname),                                        \
652
      priv,                                                           \
653
      NULL  /* NOLINT (readability/null_usage) */                     \
654
    };                                                                \
655
    NODE_C_CTOR(_register_ ## modname) {                              \
656
      node_module_register(&_module);                                 \
657
    }                                                                 \
658
  }
659
660
// Usage: `NODE_MODULE(NODE_GYP_MODULE_NAME, InitializerFunction)`
661
// If no NODE_MODULE is declared, Node.js looks for the well-known
662
// symbol `node_register_module_v${NODE_MODULE_VERSION}`.
663
#define NODE_MODULE(modname, regfunc)                                 \
664
  NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
665
666
#define NODE_MODULE_CONTEXT_AWARE(modname, regfunc)                   \
667
  /* NOLINTNEXTLINE (readability/null_usage) */                       \
668
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL, 0)
669
670
// Embedders can use this type of binding for statically linked native bindings.
671
// It is used the same way addon bindings are used, except that linked bindings
672
// can be accessed through `process._linkedBinding(modname)`.
673
#define NODE_MODULE_LINKED(modname, regfunc)                               \
674
  /* NOLINTNEXTLINE (readability/null_usage) */                            \
675
  NODE_MODULE_CONTEXT_AWARE_X(modname, regfunc, NULL,                      \
676
                              node::ModuleFlags::kLinked)
677
678
/*
679
 * For backward compatibility in add-on modules.
680
 */
681
#define NODE_MODULE_DECL /* nothing */
682
683
#define NODE_MODULE_INITIALIZER_BASE node_register_module_v
684
685
#define NODE_MODULE_INITIALIZER_X(base, version)                      \
686
    NODE_MODULE_INITIALIZER_X_HELPER(base, version)
687
688
#define NODE_MODULE_INITIALIZER_X_HELPER(base, version) base##version
689
690
#define NODE_MODULE_INITIALIZER                                       \
691
  NODE_MODULE_INITIALIZER_X(NODE_MODULE_INITIALIZER_BASE,             \
692
      NODE_MODULE_VERSION)
693
694
#define NODE_MODULE_INIT()                                            \
695
  extern "C" NODE_MODULE_EXPORT void                                  \
696
  NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,              \
697
                          v8::Local<v8::Value> module,                \
698
                          v8::Local<v8::Context> context);            \
699
  NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME,                     \
700
                            NODE_MODULE_INITIALIZER)                  \
701
  void NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,         \
702
                               v8::Local<v8::Value> module,           \
703
                               v8::Local<v8::Context> context)
704
705
// Allows embedders to add a binding to the current Environment* that can be
706
// accessed through process._linkedBinding() in the target Environment and all
707
// Worker threads that it creates.
708
// In each variant, the registration function needs to be usable at least for
709
// the time during which the Environment exists.
710
NODE_EXTERN void AddLinkedBinding(Environment* env, const node_module& mod);
711
NODE_EXTERN void AddLinkedBinding(Environment* env,
712
                                  const char* name,
713
                                  addon_context_register_func fn,
714
                                  void* priv);
715
716
/* Called after the event loop exits but before the VM is disposed.
717
 * Callbacks are run in reverse order of registration, i.e. newest first.
718
 *
719
 * You should always use the three-argument variant (or, for addons,
720
 * AddEnvironmentCleanupHook) in order to avoid relying on global state.
721
 */
722
NODE_DEPRECATED(
723
    "Use the three-argument variant of AtExit() or AddEnvironmentCleanupHook()",
724
    NODE_EXTERN void AtExit(void (*cb)(void* arg), void* arg = nullptr));
725
726
/* Registers a callback with the passed-in Environment instance. The callback
727
 * is called after the event loop exits, but before the VM is disposed.
728
 * Callbacks are run in reverse order of registration, i.e. newest first.
729
 */
730
NODE_EXTERN void AtExit(Environment* env,
731
                        void (*cb)(void* arg),
732
                        void* arg);
733
5
NODE_DEPRECATED(
734
    "Use the three-argument variant of AtExit() or AddEnvironmentCleanupHook()",
735
    inline void AtExit(Environment* env,
736
                       void (*cb)(void* arg)) {
737
      AtExit(env, cb, nullptr);
738
    })
739
740
typedef double async_id;
741
struct async_context {
742
  ::node::async_id async_id;
743
  ::node::async_id trigger_async_id;
744
};
745
746
/* This is a lot like node::AtExit, except that the hooks added via this
747
 * function are run before the AtExit ones and will always be registered
748
 * for the current Environment instance.
749
 * These functions are safe to use in an addon supporting multiple
750
 * threads/isolates. */
751
NODE_EXTERN void AddEnvironmentCleanupHook(v8::Isolate* isolate,
752
                                           void (*fun)(void* arg),
753
                                           void* arg);
754
755
NODE_EXTERN void RemoveEnvironmentCleanupHook(v8::Isolate* isolate,
756
                                              void (*fun)(void* arg),
757
                                              void* arg);
758
759
/* Returns the id of the current execution context. If the return value is
760
 * zero then no execution has been set. This will happen if the user handles
761
 * I/O from native code. */
762
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
763
764
/* Return same value as async_hooks.triggerAsyncId(); */
765
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
766
767
/* If the native API doesn't inherit from the helper class then the callbacks
768
 * must be triggered manually. This triggers the init() callback. The return
769
 * value is the async id assigned to the resource.
770
 *
771
 * The `trigger_async_id` parameter should correspond to the resource which is
772
 * creating the new resource, which will usually be the return value of
773
 * `AsyncHooksGetTriggerAsyncId()`. */
774
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
775
                                        v8::Local<v8::Object> resource,
776
                                        const char* name,
777
                                        async_id trigger_async_id = -1);
778
779
NODE_EXTERN async_context EmitAsyncInit(v8::Isolate* isolate,
780
                                        v8::Local<v8::Object> resource,
781
                                        v8::Local<v8::String> name,
782
                                        async_id trigger_async_id = -1);
783
784
/* Emit the destroy() callback. The overload taking an `Environment*` argument
785
 * should be used when the Isolate’s current Context is not associated with
786
 * a Node.js Environment, or when there is no current Context, for example
787
 * when calling this function during garbage collection. In that case, the
788
 * `Environment*` value should have been acquired previously, e.g. through
789
 * `GetCurrentEnvironment()`. */
790
NODE_EXTERN void EmitAsyncDestroy(v8::Isolate* isolate,
791
                                  async_context asyncContext);
792
NODE_EXTERN void EmitAsyncDestroy(Environment* env,
793
                                  async_context asyncContext);
794
795
class InternalCallbackScope;
796
797
/* This class works like `MakeCallback()` in that it sets up a specific
798
 * asyncContext as the current one and informs the async_hooks and domains
799
 * modules that this context is currently active.
800
 *
801
 * `MakeCallback()` is a wrapper around this class as well as
802
 * `Function::Call()`. Either one of these mechanisms needs to be used for
803
 * top-level calls into JavaScript (i.e. without any existing JS stack).
804
 *
805
 * This object should be stack-allocated to ensure that it is contained in a
806
 * valid HandleScope.
807
 *
808
 * Exceptions happening within this scope will be treated like uncaught
809
 * exceptions. If this behaviour is undesirable, a new `v8::TryCatch` scope
810
 * needs to be created inside of this scope.
811
 */
812
class NODE_EXTERN CallbackScope {
813
 public:
814
  CallbackScope(v8::Isolate* isolate,
815
                v8::Local<v8::Object> resource,
816
                async_context asyncContext);
817
  ~CallbackScope();
818
819
  void operator=(const CallbackScope&) = delete;
820
  void operator=(CallbackScope&&) = delete;
821
  CallbackScope(const CallbackScope&) = delete;
822
  CallbackScope(CallbackScope&&) = delete;
823
824
 private:
825
  InternalCallbackScope* private_;
826
  v8::TryCatch try_catch_;
827
};
828
829
/* An API specific to emit before/after callbacks is unnecessary because
830
 * MakeCallback will automatically call them for you.
831
 *
832
 * These methods may create handles on their own, so run them inside a
833
 * HandleScope.
834
 *
835
 * `asyncId` and `triggerAsyncId` should correspond to the values returned by
836
 * `EmitAsyncInit()` and `AsyncHooksGetTriggerAsyncId()`, respectively, when the
837
 * invoking resource was created. If these values are unknown, 0 can be passed.
838
 * */
839
NODE_EXTERN
840
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
841
                                       v8::Local<v8::Object> recv,
842
                                       v8::Local<v8::Function> callback,
843
                                       int argc,
844
                                       v8::Local<v8::Value>* argv,
845
                                       async_context asyncContext);
846
NODE_EXTERN
847
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
848
                                       v8::Local<v8::Object> recv,
849
                                       const char* method,
850
                                       int argc,
851
                                       v8::Local<v8::Value>* argv,
852
                                       async_context asyncContext);
853
NODE_EXTERN
854
v8::MaybeLocal<v8::Value> MakeCallback(v8::Isolate* isolate,
855
                                       v8::Local<v8::Object> recv,
856
                                       v8::Local<v8::String> symbol,
857
                                       int argc,
858
                                       v8::Local<v8::Value>* argv,
859
                                       async_context asyncContext);
860
861
/* Helper class users can optionally inherit from. If
862
 * `AsyncResource::MakeCallback()` is used, then all four callbacks will be
863
 * called automatically. */
864
class NODE_EXTERN AsyncResource {
865
 public:
866
  AsyncResource(v8::Isolate* isolate,
867
                v8::Local<v8::Object> resource,
868
                const char* name,
869
                async_id trigger_async_id = -1);
870
871
  virtual ~AsyncResource();
872
873
  AsyncResource(const AsyncResource&) = delete;
874
  void operator=(const AsyncResource&) = delete;
875
876
  v8::MaybeLocal<v8::Value> MakeCallback(
877
      v8::Local<v8::Function> callback,
878
      int argc,
879
      v8::Local<v8::Value>* argv);
880
881
  v8::MaybeLocal<v8::Value> MakeCallback(
882
      const char* method,
883
      int argc,
884
      v8::Local<v8::Value>* argv);
885
886
  v8::MaybeLocal<v8::Value> MakeCallback(
887
      v8::Local<v8::String> symbol,
888
      int argc,
889
      v8::Local<v8::Value>* argv);
890
891
  v8::Local<v8::Object> get_resource();
892
  async_id get_async_id() const;
893
  async_id get_trigger_async_id() const;
894
895
 protected:
896
638
  class NODE_EXTERN CallbackScope : public node::CallbackScope {
897
   public:
898
    explicit CallbackScope(AsyncResource* res);
899
  };
900
901
 private:
902
  Environment* env_;
903
  v8::Global<v8::Object> resource_;
904
  async_context async_context_;
905
};
906
907
#ifndef _WIN32
908
// Register a signal handler without interrupting any handlers that node
909
// itself needs. This does override handlers registered through
910
// process.on('SIG...', function() { ... }). The `reset_handler` flag indicates
911
// whether the signal handler for the given signal should be reset to its
912
// default value before executing the handler (i.e. it works like SA_RESETHAND).
913
// The `reset_handler` flag is invalid when `signal` is SIGSEGV.
914
NODE_EXTERN
915
void RegisterSignalHandler(int signal,
916
                           void (*handler)(int signal,
917
                                           siginfo_t* info,
918
                                           void* ucontext),
919
                           bool reset_handler = false);
920
#endif  // _WIN32
921
922
}  // namespace node
923
924
#endif  // SRC_NODE_H_