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