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