1# Node.js C++ codebase 2 3Hi! You've found the C++ code backing Node.js. This README aims to help you 4get started working on it and document some idioms you may encounter while 5doing so. 6 7## Coding style 8 9Node.js has a document detailing its [C++ coding style][] 10that can be helpful as a reference for stylistic issues. 11 12## V8 API documentation 13 14A lot of the Node.js codebase is around what the underlying JavaScript engine, 15V8, provides through its API for embedders. Knowledge of this API can also be 16useful when working with native addons for Node.js written in C++, although for 17new projects [N-API][] is typically the better alternative. 18 19V8 does not provide much public API documentation beyond what is 20available in its C++ header files, most importantly `v8.h`, which can be 21accessed online in the following locations: 22 23* On GitHub: [`v8.h` in Node.js][] 24* On GitHub: [`v8.h` in V8][] 25* On the Chromium project's Code Search application: [`v8.h` in Code Search][] 26 27V8 also provides an [introduction for V8 embedders][], 28which can be useful for understanding some of the concepts it uses in its 29embedder API. 30 31Important concepts when using V8 are the ones of [`Isolate`][]s and 32[JavaScript value handles][]. 33 34V8 supports [fast API calls][], which can be useful for improving the 35performance in certain cases. 36 37## libuv API documentation 38 39The other major dependency of Node.js is [libuv][], providing 40the [event loop][] and other operating system abstractions to Node.js. 41 42There is a [reference documentation for the libuv API][]. 43 44## File structure 45 46The Node.js C++ files follow this structure: 47 48The `.h` header files contain declarations, and sometimes definitions that don't 49require including other headers (e.g. getters, setters, etc.). They should only 50include other `.h` header files and nothing else. 51 52The `-inl.h` header files contain definitions of inline functions from the 53corresponding `.h` header file (e.g. functions marked `inline` in the 54declaration or `template` functions). They always include the corresponding 55`.h` header file, and can include other `.h` and `-inl.h` header files as 56needed. It is not mandatory to split out the definitions from the `.h` file 57into an `-inl.h` file, but it becomes necessary when there are multiple 58definitions and contents of other `-inl.h` files start being used. Therefore, it 59is recommended to split a `-inl.h` file when inline functions become longer than 60a few lines to keep the corresponding `.h` file readable and clean. All visible 61definitions from the `-inl.h` file should be declared in the corresponding `.h` 62header file. 63 64The `.cc` files contain definitions of non-inline functions from the 65corresponding `.h` header file. They always include the corresponding `.h` 66header file, and can include other `.h` and `-inl.h` header files as needed. 67 68## Helpful concepts 69 70A number of concepts are involved in putting together Node.js on top of V8 and 71libuv. This section aims to explain some of them and how they work together. 72 73<a id="isolate"></a> 74 75### `Isolate` 76 77The `v8::Isolate` class represents a single JavaScript engine instance, in 78particular a set of JavaScript objects that can refer to each other 79(the “heap”). 80 81The `v8::Isolate` is often passed to other V8 API functions, and provides some 82APIs for managing the behaviour of the JavaScript engine or querying about its 83current state or statistics such as memory usage. 84 85V8 APIs are not thread-safe unless explicitly specified. In a typical Node.js 86application, the main thread and any `Worker` threads each have one `Isolate`, 87and JavaScript objects from one `Isolate` cannot refer to objects from 88another `Isolate`. 89 90Garbage collection, as well as other operations that affect the entire heap, 91happen on a per-`Isolate` basis. 92 93Typical ways of accessing the current `Isolate` in the Node.js code are: 94 95* Given a `FunctionCallbackInfo` for a [binding function][], 96 using `args.GetIsolate()`. 97* Given a [`Context`][], using `context->GetIsolate()`. 98* Given a [`Environment`][], using `env->isolate()`. 99* Given a [`Realm`][], using `realm->isolate()`. 100 101### V8 JavaScript values 102 103V8 provides classes that mostly correspond to JavaScript types; for example, 104`v8::Value` is a class representing any kind of JavaScript type, with 105subclasses such as `v8::Number` (which in turn has subclasses like `v8::Int32`), 106`v8::Boolean` or `v8::Object`. Most types are represented by subclasses 107of `v8::Object`, e.g. `v8::Uint8Array` or `v8::Date`. 108 109<a id="internal-fields"></a> 110 111### Internal fields 112 113V8 provides the ability to store data in so-called “internal fields” inside 114`v8::Object`s that were created as instances of C++-backed classes. The number 115of fields needs to be defined when creating that class. 116 117Both JavaScript values and `void*` pointers may be stored in such fields. 118In most native Node.js objects, the first internal field is used to store a 119pointer to a [`BaseObject`][] subclass, which then contains all relevant 120information associated with the JavaScript object. 121 122Typical ways of working with internal fields are: 123 124* `obj->InternalFieldCount()` to look up the number of internal fields for an 125 object (`0` for regular JavaScript objects). 126* `obj->GetInternalField(i)` to get a JavaScript value from an internal field. 127* `obj->SetInternalField(i, v)` to store a JavaScript value in an 128 internal field. 129* `obj->GetAlignedPointerFromInternalField(i)` to get a `void*` pointer from an 130 internal field. 131* `obj->SetAlignedPointerInInternalField(i, p)` to store a `void*` pointer in an 132 internal field. 133 134[`Context`][]s provide the same feature under the name “embedder data”. 135 136<a id="js-handles"></a> 137 138### JavaScript value handles 139 140All JavaScript values are accessed through the V8 API through so-called handles, 141of which there are two types: [`Local`][]s and [`Global`][]s. 142 143<a id="local-handles"></a> 144 145#### `Local` handles 146 147A `v8::Local` handle is a temporary pointer to a JavaScript object, where 148“temporary” usually means that is no longer needed after the current function 149is done executing. `Local` handles can only be allocated on the C++ stack. 150 151Most of the V8 API uses `Local` handles to work with JavaScript values or return 152them from functions. 153 154Whenever a `Local` handle is created, a `v8::HandleScope` or 155`v8::EscapableHandleScope` object must exist on the stack. The `Local` is then 156added to that scope and deleted along with it. 157 158When inside a [binding function][], a `HandleScope` already exists outside of 159it, so there is no need to explicitly create one. 160 161`EscapableHandleScope`s can be used to allow a single `Local` handle to be 162passed to the outer scope. This is useful when a function returns a `Local`. 163 164The following JavaScript and C++ functions are mostly equivalent: 165 166```js 167function getFoo(obj) { 168 return obj.foo; 169} 170``` 171 172```cpp 173v8::Local<v8::Value> GetFoo(v8::Local<v8::Context> context, 174 v8::Local<v8::Object> obj) { 175 v8::Isolate* isolate = context->GetIsolate(); 176 v8::EscapableHandleScope handle_scope(isolate); 177 178 // The 'foo_string' handle cannot be returned from this function because 179 // it is not “escaped” with `.Escape()`. 180 v8::Local<v8::String> foo_string = 181 v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked(); 182 183 v8::Local<v8::Value> return_value; 184 if (obj->Get(context, foo_string).ToLocal(&return_value)) { 185 return handle_scope.Escape(return_value); 186 } else { 187 // There was a JS exception! Handle it somehow. 188 return v8::Local<v8::Value>(); 189 } 190} 191``` 192 193See [exception handling][] for more information about the usage of `.To()`, 194`.ToLocalChecked()`, `v8::Maybe` and `v8::MaybeLocal` usage. 195 196##### Casting local handles 197 198If it is known that a `Local<Value>` refers to a more specific type, it can 199be cast to that type using `.As<...>()`: 200 201```cpp 202v8::Local<v8::Value> some_value; 203// CHECK() is a Node.js utilitity that works similar to assert(). 204CHECK(some_value->IsUint8Array()); 205v8::Local<v8::Uint8Array> as_uint8 = some_value.As<v8::Uint8Array>(); 206``` 207 208Generally, using `val.As<v8::X>()` is only valid if `val->IsX()` is true, and 209failing to follow that rule may lead to crashes. 210 211##### Detecting handle leaks 212 213If it is expected that no `Local` handles should be created within a given 214scope unless explicitly within a `HandleScope`, a `SealHandleScope` can be used. 215 216For example, there is a `SealHandleScope` around the event loop, forcing 217any functions that are called from the event loop and want to run or access 218JavaScript code to create `HandleScope`s. 219 220<a id="global-handles"></a> 221 222#### `Global` handles 223 224A `v8::Global` handle (sometimes also referred to by the name of its parent 225class `Persistent`, although use of that is discouraged in Node.js) is a 226reference to a JavaScript object that can remain active as long as the engine 227instance is active. 228 229Global handles can be either strong or weak. Strong global handles are so-called 230“GC roots”, meaning that they will keep the JavaScript object they refer to 231alive even if no other objects refer to them. Weak global handles do not do 232that, and instead optionally call a callback when the object they refer to 233is garbage-collected. 234 235```cpp 236v8::Global<v8::Object> reference; 237 238void StoreReference(v8::Isolate* isolate, v8::Local<v8::Object> obj) { 239 // Create a strong reference to `obj`. 240 reference.Reset(isolate, obj); 241} 242 243// Must be called with a HandleScope around it. 244v8::Local<v8::Object> LoadReference(v8::Isolate* isolate) { 245 return reference.Get(isolate); 246} 247``` 248 249##### `Eternal` handles 250 251`v8::Eternal` handles are a special kind of handles similar to `v8::Global` 252handles, with the exception that the values they point to are never 253garbage-collected while the JavaScript Engine instance is alive, even if 254the `v8::Eternal` itself is destroyed at some point. This type of handle 255is rarely used. 256 257<a id="context"></a> 258 259### `Context` 260 261JavaScript allows multiple global objects and sets of built-in JavaScript 262objects (like the `Object` or `Array` functions) to coexist inside the same 263heap. Node.js exposes this ability through the [`vm` module][]. 264 265V8 refers to each of these global objects and their associated builtins as a 266`Context`. 267 268Currently, in Node.js there is one main `Context` associated with the 269principal [`Realm`][] of an [`Environment`][] instance, and a number of 270subsidiary `Context`s that are created with `vm.Context` or associated with 271[`ShadowRealm`][]. 272 273Most Node.js features will only work inside a context associated with a 274`Realm`. The only exception at the time of writing are [`MessagePort`][] 275objects. This restriction is not inherent to the design of Node.js, and a 276sufficiently committed person could restructure Node.js to provide built-in 277modules inside of `vm.Context`s. 278 279Often, the `Context` is passed around for [exception handling][]. 280Typical ways of accessing the current `Context` in the Node.js code are: 281 282* Given an [`Isolate`][], using `isolate->GetCurrentContext()`. 283* Given an [`Environment`][], using `env->context()` to get the `Environment`'s 284 principal [`Realm`][]'s context. 285* Given a [`Realm`][], using `realm->context()` to get the `Realm`'s 286 context. 287 288<a id="event-loop"></a> 289 290### Event loop 291 292The main abstraction for an event loop inside Node.js is the `uv_loop_t` struct. 293Typically, there is one event loop per thread. This includes not only the main 294thread and Workers, but also helper threads that may occasionally be spawned 295in the course of running a Node.js program. 296 297The current event loop can be accessed using `env->event_loop()` given an 298[`Environment`][] instance. The restriction of using a single event loop 299is not inherent to the design of Node.js, and a sufficiently committed person 300could restructure Node.js to provide e.g. the ability to run parts of Node.js 301inside an event loop separate from the active thread's event loop. 302 303<a id="environment"></a> 304 305### `Environment` 306 307Node.js instances are represented by the `Environment` class. 308 309Currently, every `Environment` class is associated with: 310 311* One [event loop][] 312* One [`Isolate`][] 313* One principal [`Realm`][] 314 315The `Environment` class contains a large number of different fields for 316different built-in modules that can be shared across different `Realm` 317instances, for example, the inspector agent, async hooks info. 318 319Typical ways of accessing the current `Environment` in the Node.js code are: 320 321* Given a `FunctionCallbackInfo` for a [binding function][], 322 using `Environment::GetCurrent(args)`. 323* Given a [`BaseObject`][], using `env()` or `self->env()`. 324* Given a [`Context`][], using `Environment::GetCurrent(context)`. 325 This requires that `context` has been associated with the `Environment` 326 instance, e.g. is the main `Context` for the `Environment` or one of its 327 `vm.Context`s. 328* Given an [`Isolate`][], using `Environment::GetCurrent(isolate)`. This looks 329 up the current [`Context`][] and then uses that. 330 331<a id="realm"></a> 332 333### `Realm` 334 335The `Realm` class is a container for a set of JavaScript objects and functions 336that are associated with a particular [ECMAScript realm][]. 337 338Each ECMAScript realm comes with a global object and a set of intrinsic 339objects. An ECMAScript realm has a `[[HostDefined]]` field, which represents 340the Node.js [`Realm`][] object. 341 342Every `Realm` instance is created for a particular [`Context`][]. A `Realm` 343can be a principal realm or a synthetic realm. A principal realm is created 344for each `Environment`'s main [`Context`][]. A synthetic realm is created 345for the [`Context`][] of each [`ShadowRealm`][] constructed from the JS API. No 346`Realm` is created for the [`Context`][] of a `vm.Context`. 347 348Native bindings and built-in modules can be evaluated in either a principal 349realm or a synthetic realm. 350 351The `Realm` class contains a large number of different fields for 352different built-in modules, for example the memory for a `Uint32Array` that 353the `url` module uses for storing data returned from a 354`urlBinding.update()` call. 355 356It also provides [cleanup hooks][] and maintains a list of [`BaseObject`][] 357instances. 358 359Typical ways of accessing the current `Realm` in the Node.js code are: 360 361* Given a `FunctionCallbackInfo` for a [binding function][], 362 using `Realm::GetCurrent(args)`. 363* Given a [`BaseObject`][], using `realm()` or `self->realm()`. 364* Given a [`Context`][], using `Realm::GetCurrent(context)`. 365 This requires that `context` has been associated with the `Realm` 366 instance, e.g. is the principal `Realm` for the `Environment`. 367* Given an [`Isolate`][], using `Realm::GetCurrent(isolate)`. This looks 368 up the current [`Context`][] and then uses its `Realm`. 369 370<a id="isolate-data"></a> 371 372### `IsolateData` 373 374Every Node.js instance ([`Environment`][]) is associated with one `IsolateData` 375instance that contains information about or associated with a given 376[`Isolate`][]. 377 378#### String table 379 380`IsolateData` contains a list of strings that can be quickly accessed 381inside Node.js code, e.g. given an `Environment` instance `env` the JavaScript 382string “name” can be accessed through `env->name_string()` without actually 383creating a new JavaScript string. 384 385### Platform 386 387Every process that uses V8 has a `v8::Platform` instance that provides some 388functionalities to V8, most importantly the ability to schedule work on 389background threads. 390 391Node.js provides a `NodePlatform` class that implements the `v8::Platform` 392interface and uses libuv for providing background threading abilities. 393 394The platform can be accessed through `isolate_data->platform()` given an 395[`IsolateData`][] instance, although that only works when: 396 397* The current Node.js instance was not started by an embedder; or 398* The current Node.js instance was started by an embedder whose `v8::Platform` 399 implementation also implement's the `node::MultiIsolatePlatform` interface 400 and who passed this to Node.js. 401 402<a id="binding-functions"></a> 403 404### Binding functions 405 406C++ functions exposed to JS follow a specific signature. The following example 407is from `node_util.cc`: 408 409```cpp 410void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) { 411 CHECK(args[0]->IsArrayBufferView()); 412 args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer()); 413} 414``` 415 416(Namespaces are usually omitted through the use of `using` statements in the 417Node.js source code.) 418 419`args[n]` is a `Local<Value>` that represents the n-th argument passed to the 420function. `args.This()` is the `this` value inside this function call. 421`args.Holder()` is equivalent to `args.This()` in all use cases inside of 422Node.js. 423 424`args.GetReturnValue()` is a placeholder for the return value of the function, 425and provides a `.Set()` method that can be called with a boolean, integer, 426floating-point number or a `Local<Value>` to set the return value. 427 428Node.js provides various helpers for building JS classes in C++ and/or attaching 429C++ functions to the exports of a built-in module: 430 431```cpp 432void Initialize(Local<Object> target, 433 Local<Value> unused, 434 Local<Context> context, 435 void* priv) { 436 Environment* env = Environment::GetCurrent(context); 437 438 SetMethod(context, target, "getaddrinfo", GetAddrInfo); 439 SetMethod(context, target, "getnameinfo", GetNameInfo); 440 441 // 'SetMethodNoSideEffect' means that debuggers can safely execute this 442 // function for e.g. previews. 443 SetMethodNoSideEffect(context, target, "canonicalizeIP", CanonicalizeIP); 444 445 // ... more code ... 446 447 Isolate* isolate = env->isolate(); 448 // Building the `ChannelWrap` class for JS: 449 Local<FunctionTemplate> channel_wrap = 450 NewFunctionTemplate(isolate, ChannelWrap::New); 451 // Allow for 1 internal field, see `BaseObject` for details on this: 452 channel_wrap->InstanceTemplate()->SetInternalFieldCount(1); 453 channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env)); 454 455 // Set various methods on the class (i.e. on the prototype): 456 SetProtoMethod(isolate, channel_wrap, "queryAny", Query<QueryAnyWrap>); 457 SetProtoMethod(isolate, channel_wrap, "queryA", Query<QueryAWrap>); 458 // ... 459 SetProtoMethod(isolate, channel_wrap, "querySoa", Query<QuerySoaWrap>); 460 SetProtoMethod(isolate, channel_wrap, "getHostByAddr", Query<GetHostByAddrWrap>); 461 462 SetProtoMethodNoSideEffect(isolate, channel_wrap, "getServers", GetServers); 463 464 SetConstructorFunction(context, target, "ChannelWrap", channel_wrap); 465} 466 467// Run the `Initialize` function when loading this binding through 468// `internalBinding('cares_wrap')` in Node.js's built-in JavaScript code: 469NODE_BINDING_CONTEXT_AWARE_INTERNAL(cares_wrap, Initialize) 470``` 471 472If the C++ binding is loaded during bootstrap, it needs to be registered 473with the utilities in `node_external_reference.h`, like this: 474 475```cpp 476namespace node { 477namespace util { 478void RegisterExternalReferences(ExternalReferenceRegistry* registry) { 479 registry->Register(GetHiddenValue); 480 registry->Register(SetHiddenValue); 481 // ... register all C++ functions used to create FunctionTemplates. 482} 483} // namespace util 484} // namespace node 485 486// The first argument passed to `NODE_BINDING_EXTERNAL_REFERENCE`, 487// which is `util` here, needs to be added to the 488// `EXTERNAL_REFERENCE_BINDING_LIST_BASE` list in node_external_reference.h 489NODE_BINDING_EXTERNAL_REFERENCE(util, node::util::RegisterExternalReferences) 490``` 491 492Otherwise, you might see an error message like this when building the 493executables: 494 495```console 496FAILED: gen/node_snapshot.cc 497cd ../../; out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc 498Unknown external reference 0x107769200. 499<unresolved> 500/bin/sh: line 1: 6963 Illegal instruction: 4 out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc 501``` 502 503You can try using a debugger to symbolicate the external reference. For example, 504with lldb's `image lookup --address` command (with gdb it's `info symbol`): 505 506```console 507$ lldb -- out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc 508(lldb) run 509Process 7012 launched: '/Users/joyee/projects/node/out/Release/node_mksnapshot' (x86_64) 510Unknown external reference 0x1004c8200. 511<unresolved> 512Process 7012 stopped 513(lldb) image lookup --address 0x1004c8200 514 Address: node_mksnapshot[0x00000001004c8200] (node_mksnapshot.__TEXT.__text + 5009920) 515 Summary: node_mksnapshot`node::util::GetHiddenValue(v8::FunctionCallbackInfo<v8::Value> const&) at node_util.cc:159 516``` 517 518Which explains that the unregistered external reference is 519`node::util::GetHiddenValue` defined in `node_util.cc`. 520 521<a id="per-binding-state"></a> 522 523#### Per-binding state 524 525Some internal bindings, such as the HTTP parser, maintain internal state that 526only affects that particular binding. In that case, one common way to store 527that state is through the use of `Realm::AddBindingData`, which gives 528binding functions access to an object for storing such state. 529That object is always a [`BaseObject`][]. 530 531Its class needs to have a static `type_name` field based on a 532constant string, in order to disambiguate it from other classes of this type, 533and which could e.g. match the binding's name (in the example above, that would 534be `cares_wrap`). 535 536```cpp 537// In the HTTP parser source code file: 538class BindingData : public BaseObject { 539 public: 540 BindingData(Realm* realm, Local<Object> obj) : BaseObject(realm, obj) {} 541 542 static constexpr FastStringKey type_name { "http_parser" }; 543 544 std::vector<char> parser_buffer; 545 bool parser_buffer_in_use = false; 546 547 // ... 548}; 549 550// Available for binding functions, e.g. the HTTP Parser constructor: 551static void New(const FunctionCallbackInfo<Value>& args) { 552 BindingData* binding_data = Realm::GetBindingData<BindingData>(args); 553 new Parser(binding_data, args.This()); 554} 555 556// ... because the initialization function told the Realm to store the 557// BindingData object: 558void InitializeHttpParser(Local<Object> target, 559 Local<Value> unused, 560 Local<Context> context, 561 void* priv) { 562 Realm* realm = Realm::GetCurrent(context); 563 BindingData* const binding_data = 564 realm->AddBindingData<BindingData>(context, target); 565 if (binding_data == nullptr) return; 566 567 Local<FunctionTemplate> t = NewFunctionTemplate(realm->isolate(), Parser::New); 568 ... 569} 570``` 571 572If the binding is loaded during bootstrap, add it to the 573`SERIALIZABLE_OBJECT_TYPES` list in `src/node_snapshotable.h` and 574inherit from the `SnapshotableObject` class instead. See the comments 575of `SnapshotableObject` on how to implement its serialization and 576deserialization. 577 578<a id="exception-handling"></a> 579 580### Exception handling 581 582The V8 engine provides multiple features to work with JavaScript exceptions, 583as C++ exceptions are disabled inside of Node.js: 584 585#### Maybe types 586 587V8 provides the `v8::Maybe<T>` and `v8::MaybeLocal<T>` types, typically used 588as return values from API functions that can run JavaScript code and therefore 589can throw exceptions. 590 591Conceptually, the idea is that every `v8::Maybe<T>` is either empty (checked 592through `.IsNothing()`) or holds a value of type `T` (checked through 593`.IsJust()`). If the `Maybe` is empty, then a JavaScript exception is pending. 594A typical way of accessing the value is using the `.To()` function, which 595returns a boolean indicating success of the operation (i.e. the `Maybe` not 596being empty) and taking a pointer to a `T` to store the value if there is one. 597 598##### Checked conversion 599 600`maybe.Check()` can be used to assert that the maybe is not empty, i.e. crash 601the process otherwise. `maybe.FromJust()` (aka `maybe.ToChecked()`) can be used 602to access the value and crash the process if it is not set. 603 604This should only be performed if it is actually sure that the operation has 605not failed. A lot of the Node.js source code does **not** follow this rule, and 606can be brought to crash through this. 607 608In particular, it is often not safe to assume that an operation does not throw 609an exception, even if it seems like it would not do that. 610The most common reasons for this are: 611 612* Calls to functions like `object->Get(...)` or `object->Set(...)` may fail on 613 most objects, if the `Object.prototype` object has been modified from userland 614 code that added getters or setters. 615* Calls that invoke _any_ JavaScript code, including JavaScript code that is 616 provided from Node.js internals or V8 internals, will fail when JavaScript 617 execution is being terminated. This typically happens inside Workers when 618 `worker.terminate()` is called, but it can also affect the main thread when 619 e.g. Node.js is used as an embedded library. These exceptions can happen at 620 any point. 621 It is not always obvious whether a V8 call will enter JavaScript. In addition 622 to unexpected getters and setters, accessing some types of built-in objects 623 like `Map`s and `Set`s can also run V8-internal JavaScript code. 624 625##### MaybeLocal 626 627`v8::MaybeLocal<T>` is a variant of `v8::Maybe<T>` that is either empty or 628holds a value of type `Local<T>`. It has methods that perform the same 629operations as the methods of `v8::Maybe`, but with different names: 630 631| `Maybe` | `MaybeLocal` | 632| -------------------- | ------------------------------ | 633| `maybe.IsNothing()` | `maybe_local.IsEmpty()` | 634| `maybe.IsJust()` | `!maybe_local.IsEmpty()` | 635| `maybe.To(&value)` | `maybe_local.ToLocal(&local)` | 636| `maybe.ToChecked()` | `maybe_local.ToLocalChecked()` | 637| `maybe.FromJust()` | `maybe_local.ToLocalChecked()` | 638| `maybe.Check()` | – | 639| `v8::Nothing<T>()` | `v8::MaybeLocal<T>()` | 640| `v8::Just<T>(value)` | `v8::MaybeLocal<T>(value)` | 641 642##### Handling empty `Maybe`s 643 644Usually, the best approach to encountering an empty `Maybe` is to just return 645from the current function as soon as possible, and let execution in JavaScript 646land resume. If the empty `Maybe` is encountered inside a nested function, 647is may be a good idea to use a `Maybe` or `MaybeLocal` for the return type 648of that function and pass information about pending JavaScript exceptions along 649that way. 650 651Generally, when an empty `Maybe` is encountered, it is not valid to attempt 652to perform further calls to APIs that return `Maybe`s. 653 654A typical pattern for dealing with APIs that return `Maybe` and `MaybeLocal` is 655using `.ToLocal()` and `.To()` and returning early in case there is an error: 656 657```cpp 658// This could also return a v8::MaybeLocal<v8::Number>, for example. 659v8::Maybe<double> SumNumbers(v8::Local<v8::Context> context, 660 v8::Local<v8::Array> array_of_integers) { 661 v8::Isolate* isolate = context->GetIsolate(); 662 v8::HandleScope handle_scope(isolate); 663 664 double sum = 0; 665 666 for (uint32_t i = 0; i < array_of_integers->Length(); i++) { 667 v8::Local<v8::Value> entry; 668 if (!array_of_integers->Get(context, i).ToLocal(&entry)) { 669 // Oops, we might have hit a getter that throws an exception! 670 // It's better to not continue return an empty (“nothing”) Maybe. 671 return v8::Nothing<double>(); 672 } 673 674 if (!entry->IsNumber()) { 675 // Let's just skip any non-numbers. It would also be reasonable to throw 676 // an exception here, e.g. using the error system in src/node_errors.h, 677 // and then to return an empty Maybe again. 678 continue; 679 } 680 681 // This cast is valid, because we've made sure it's really a number. 682 v8::Local<v8::Number> entry_as_number = entry.As<v8::Number>(); 683 684 sum += entry_as_number->Value(); 685 } 686 687 return v8::Just(sum); 688} 689 690// Function that is exposed to JS: 691void SumNumbers(const v8::FunctionCallbackInfo<v8::Value>& args) { 692 // This will crash if the first argument is not an array. Let's assume we 693 // have performed type checking in a JavaScript wrapper function. 694 CHECK(args[0]->IsArray()); 695 696 double sum; 697 if (!SumNumbers(args.GetIsolate()->GetCurrentContext(), 698 args[0].As<v8::Array>()).To(&sum)) { 699 // Nothing to do, we can just return directly to JavaScript. 700 return; 701 } 702 703 args.GetReturnValue().Set(sum); 704} 705``` 706 707#### TryCatch 708 709If there is a need to catch JavaScript exceptions in C++, V8 provides the 710`v8::TryCatch` type for doing so, which we wrap into our own 711`node::errors::TryCatchScope` in Node.js. The latter has the additional feature 712of providing the ability to shut down the program in the typical Node.js way 713(printing the exception + stack trace) if an exception is caught. 714 715A `TryCatch` will catch regular JavaScript exceptions, as well as termination 716exceptions such as the ones thrown by `worker.terminate()` calls. 717In the latter case, the `try_catch.HasTerminated()` function will return `true`, 718and the exception object will not be a meaningful JavaScript value. 719`try_catch.ReThrow()` should not be used in this case. 720 721<a id="libuv-handles-and-requests"></a> 722 723### libuv handles and requests 724 725Two central concepts when working with libuv are handles and requests. 726 727Handles are subclasses of the `uv_handle_t` “class”, and generally refer to 728long-lived objects that can emit events multiple times, such as network sockets 729or file system watchers. 730 731In Node.js, handles are often managed through a [`HandleWrap`][] subclass. 732 733Requests are one-time asynchronous function calls on the event loop, such as 734file system requests or network write operations, that either succeed or fail. 735 736In Node.js, requests are often managed through a [`ReqWrap`][] subclass. 737 738### Environment cleanup 739 740When a Node.js [`Environment`][] is destroyed, it generally needs to clean up 741any resources owned by it, e.g. memory or libuv requests/handles. 742 743<a id="cleanup-hooks"></a> 744 745#### Cleanup hooks 746 747Cleanup hooks are provided that run before the [`Environment`][] or the 748[`Realm`][] is destroyed. They can be added and removed by using 749`env->AddCleanupHook(callback, hint);` and 750`env->RemoveCleanupHook(callback, hint);`, or 751`realm->AddCleanupHook(callback, hint);` and 752`realm->RemoveCleanupHook(callback, hint);` respectively, where callback takes 753a `void* hint` argument. 754 755Inside these cleanup hooks, new asynchronous operations _may_ be started on the 756event loop, although ideally that is avoided as much as possible. 757 758Every [`BaseObject`][] has its own cleanup hook that deletes it. For 759[`ReqWrap`][] and [`HandleWrap`][] instances, cleanup of the associated libuv 760objects is performed automatically, i.e. handles are closed and requests 761are cancelled if possible. 762 763#### Closing libuv handles 764 765If a libuv handle is not managed through a [`HandleWrap`][] instance, 766it needs to be closed explicitly. Do not use `uv_close()` for that, but rather 767`env->CloseHandle()`, which works the same way but keeps track of the number 768of handles that are still closing. 769 770#### Closing libuv requests 771 772There is no way to abort libuv requests in general. If a libuv request is not 773managed through a [`ReqWrap`][] instance, the 774`env->IncreaseWaitingRequestCounter()` and 775`env->DecreaseWaitingRequestCounter()` functions need to be used to keep track 776of the number of active libuv requests. 777 778#### Calling into JavaScript 779 780Calling into JavaScript is not allowed during cleanup. Worker threads explicitly 781forbid this during their shutdown sequence, but the main thread does not for 782backwards compatibility reasons. 783 784When calling into JavaScript without using [`MakeCallback()`][], check the 785`env->can_call_into_js()` flag and do not proceed if it is set to `false`. 786 787## Classes associated with JavaScript objects 788 789### `MemoryRetainer` 790 791A large number of classes in the Node.js C++ codebase refer to other objects. 792The `MemoryRetainer` class is a helper for annotating C++ classes with 793information that can be used by the heap snapshot builder in V8, so that 794memory retained by C++ can be tracked in V8 heap snapshots captured in 795Node.js applications. 796 797Inheriting from the `MemoryRetainer` class enables objects (both from JavaScript 798and C++) to refer to instances of that class, and in turn enables that class 799to point to other objects as well, including native C++ types 800such as `std::string` and track their memory usage. 801 802This can be useful for debugging memory leaks. 803 804The [`memory_tracker.h`][] header file explains how to use this class. 805 806<a id="baseobject"></a> 807 808### `BaseObject` 809 810A frequently recurring situation is that a JavaScript object and a C++ object 811need to be tied together. `BaseObject` is the main abstraction for that in 812Node.js, and most classes that are associated with JavaScript objects are 813subclasses of it. It is defined in [`base_object.h`][]. 814 815Every `BaseObject` is associated with one [`Realm`][] and one 816`v8::Object`. The `v8::Object` needs to have at least one [internal field][] 817that is used for storing the pointer to the C++ object. In order to ensure this, 818the V8 `SetInternalFieldCount()` function is usually used when setting up the 819class from C++. 820 821The JavaScript object can be accessed as a `v8::Local<v8::Object>` by using 822`self->object()`, given a `BaseObject` named `self`. 823 824Accessing a `BaseObject` from a `v8::Local<v8::Object>` (frequently that is 825`args.This()` or `args.Holder()` in a [binding function][]) can be done using 826the `Unwrap<T>(obj)` function, where `T` is a subclass of `BaseObject`. 827A helper for this is the `ASSIGN_OR_RETURN_UNWRAP` macro that returns from the 828current function if unwrapping fails (typically that means that the `BaseObject` 829has been deleted earlier). 830 831```cpp 832void Http2Session::Request(const FunctionCallbackInfo<Value>& args) { 833 Http2Session* session; 834 ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder()); 835 Environment* env = session->env(); 836 Local<Context> context = env->context(); 837 Isolate* isolate = env->isolate(); 838 839 // ... 840 // The actual function body, which can now use the `session` object. 841 // ... 842} 843``` 844 845#### Lifetime management 846 847The `BaseObject` class comes with a set of features that allow managing the 848lifetime of its instances, either associating it with the lifetime of the 849corresponding JavaScript object or untying the two. 850 851The `BaseObject::MakeWeak()` method turns the underlying [`Global`][] handle 852into a weak one, and makes it so that the `BaseObject::OnGCCollect()` virtual 853method is called when the JavaScript object is garbage collected. By default, 854that methods deletes the `BaseObject` instance. 855 856`BaseObject::ClearWeak()` undoes this effect. 857 858It generally makes sense to call `MakeWeak()` in the constructor of a 859`BaseObject` subclass, unless that subclass is referred to by e.g. the event 860loop, as is the case for the [`HandleWrap`][] and [`ReqWrap`][] classes. 861 862In addition, there are two kinds of smart pointers that can be used to refer 863to `BaseObject`s. 864 865`BaseObjectWeakPtr<T>` is similar to `std::weak_ptr<T>`, but holds on to 866an object of a `BaseObject` subclass `T` and integrates with the lifetime 867management of the former. When the `BaseObject` no longer exists, e.g. when 868it was garbage collected, accessing it through `weak_ptr.get()` will return 869`nullptr`. 870 871`BaseObjectPtr<T>` is similar to `std::shared_ptr<T>`, but also holds on to 872objects of a `BaseObject` subclass `T`. While there are `BaseObjectPtr`s 873pointing to a given object, the `BaseObject` will always maintain a strong 874reference to its associated JavaScript object. This can be useful when one 875`BaseObject` refers to another `BaseObject` and wants to make sure it stays 876alive during the lifetime of that reference. 877 878A `BaseObject` can be “detached” through the `BaseObject::Detach()` method. 879In this case, it will be deleted once the last `BaseObjectPtr` referring to 880it is destroyed. There must be at least one such pointer when `Detach()` is 881called. This can be useful when one `BaseObject` fully owns another 882`BaseObject`. 883 884<a id="asyncwrap"></a> 885 886### `AsyncWrap` 887 888`AsyncWrap` is a subclass of `BaseObject` that additionally provides tracking 889functions for asynchronous calls. It is commonly used for classes whose methods 890make calls into JavaScript without any JavaScript stack below, i.e. more or less 891directly from the event loop. It is defined in [`async_wrap.h`][]. 892 893Every `AsyncWrap` subclass has a “provider type”. A list of provider types is 894maintained in `src/async_wrap.h`. 895 896Every `AsyncWrap` instance is associated with two numbers, the “async id” 897and the “async trigger id”. The “async id” is generally unique per `AsyncWrap` 898instance, and only changes when the object is re-used in some way. 899 900See the [`async_hooks` module][] documentation for more information about how 901this information is provided to async tracking tools. 902 903<a id="makecallback"></a> 904 905#### `MakeCallback` 906 907The `AsyncWrap` class has a set of methods called `MakeCallback()`, with the 908intention of the naming being that it is used to “make calls back into 909JavaScript” from the event loop, rather than making callbacks in some way. 910(As the naming has made its way into the Node.js public API, it's not worth 911the breakage of fixing it). 912 913`MakeCallback()` generally calls a method on the JavaScript object associated 914with the current `AsyncWrap`, and informs async tracking code about these calls 915as well as takes care of running the `process.nextTick()` and `Promise` task 916queues once it returns. 917 918Before calling `MakeCallback()`, it is typically necessary to enter both a 919`HandleScope` and a `Context::Scope`. 920 921```cpp 922void StatWatcher::Callback(uv_fs_poll_t* handle, 923 int status, 924 const uv_stat_t* prev, 925 const uv_stat_t* curr) { 926 // Get the StatWatcher instance associated with this call from libuv, 927 // StatWatcher is a subclass of AsyncWrap. 928 StatWatcher* wrap = ContainerOf(&StatWatcher::watcher_, handle); 929 Environment* env = wrap->env(); 930 HandleScope handle_scope(env->isolate()); 931 Context::Scope context_scope(env->context()); 932 933 // Transform 'prev' and 'curr' into an array: 934 Local<Value> arr = ...; 935 936 Local<Value> argv[] = { Integer::New(env->isolate(), status), arr }; 937 wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); 938} 939``` 940 941See [Callback scopes][] for more information. 942 943<a id="handlewrap"></a> 944 945### `HandleWrap` 946 947`HandleWrap` is a subclass of `AsyncWrap` specifically designed to make working 948with [libuv handles][] easier. It provides the `.ref()`, `.unref()` and 949`.hasRef()` methods as well as `.close()` to enable easier lifetime management 950from JavaScript. It is defined in [`handle_wrap.h`][]. 951 952`HandleWrap` instances are [cleaned up][cleanup hooks] automatically when the 953current Node.js [`Environment`][] is destroyed, e.g. when a Worker thread stops. 954 955`HandleWrap` also provides facilities for diagnostic tooling to get an 956overview over libuv handles managed by Node.js. 957 958<a id="reqwrap"></a> 959 960### `ReqWrap` 961 962`ReqWrap` is a subclass of `AsyncWrap` specifically designed to make working 963with [libuv requests][] easier. It is defined in [`req_wrap.h`][]. 964 965In particular, its `Dispatch()` method is designed to avoid the need to keep 966track of the current count of active libuv requests. 967 968`ReqWrap` also provides facilities for diagnostic tooling to get an 969overview over libuv handles managed by Node.js. 970 971<a id="callback-scopes"></a> 972 973### Callback scopes 974 975The public `CallbackScope` and the internally used `InternalCallbackScope` 976classes provide the same facilities as [`MakeCallback()`][], namely: 977 978* Emitting the `'before'` event for async tracking when entering the scope 979* Setting the current async IDs to the ones passed to the constructor 980* Emitting the `'after'` event for async tracking when leaving the scope 981* Running the `process.nextTick()` queue 982* Running microtasks, in particular `Promise` callbacks and async/await 983 functions 984 985Usually, using `AsyncWrap::MakeCallback()` or using the constructor taking 986an `AsyncWrap*` argument (i.e. used as 987`InternalCallbackScope callback_scope(this);`) suffices inside of the Node.js 988C++ codebase. 989 990## C++ utilities 991 992Node.js uses a few custom C++ utilities, mostly defined in [`util.h`][]. 993 994### Memory allocation 995 996Node.js provides `Malloc()`, `Realloc()` and `Calloc()` functions that work 997like their C stdlib counterparts, but crash if memory cannot be allocated. 998(As V8 does not handle out-of-memory situations gracefully, it does not make 999sense for Node.js to attempt to do so in all cases.) 1000 1001The `UncheckedMalloc()`, `UncheckedRealloc()` and `UncheckedCalloc()` functions 1002return `nullptr` in these cases (or when `size == 0`). 1003 1004#### Optional stack-based memory allocation 1005 1006The `MaybeStackBuffer` class provides a way to allocate memory on the stack 1007if it is smaller than a given limit, and falls back to allocating it on the 1008heap if it is larger. This can be useful for performantly allocating temporary 1009data if it is typically expected to be small (e.g. file paths). 1010 1011The `Utf8Value`, `TwoByteValue` (i.e. UTF-16 value) and `BufferValue` 1012(`Utf8Value` but copy data from a `Buffer` if one is passed) helpers 1013inherit from this class and allow accessing the characters in a JavaScript 1014string this way. 1015 1016```cpp 1017static void Chdir(const FunctionCallbackInfo<Value>& args) { 1018 Environment* env = Environment::GetCurrent(args); 1019 // ... 1020 CHECK(args[0]->IsString()); 1021 Utf8Value path(env->isolate(), args[0]); 1022 int err = uv_chdir(*path); 1023 if (err) { 1024 // ... error handling ... 1025 } 1026} 1027``` 1028 1029### Assertions 1030 1031Node.js provides a few macros that behave similar to `assert()`: 1032 1033* `CHECK(expression)` aborts the process with a stack trace 1034 if `expression` is false. 1035* `CHECK_EQ(a, b)` checks for `a == b` 1036* `CHECK_GE(a, b)` checks for `a >= b` 1037* `CHECK_GT(a, b)` checks for `a > b` 1038* `CHECK_LE(a, b)` checks for `a <= b` 1039* `CHECK_LT(a, b)` checks for `a < b` 1040* `CHECK_NE(a, b)` checks for `a != b` 1041* `CHECK_NULL(val)` checks for `a == nullptr` 1042* `CHECK_NOT_NULL(val)` checks for `a != nullptr` 1043* `CHECK_IMPLIES(a, b)` checks that `b` is true if `a` is true. 1044* `UNREACHABLE([message])` aborts the process if it is reached. 1045 1046`CHECK`s are always enabled. For checks that should only run in debug mode, use 1047`DCHECK()`, `DCHECK_EQ()`, etc. 1048 1049### Scope-based cleanup 1050 1051The `OnScopeLeave()` function can be used to run a piece of code when leaving 1052the current C++ scope. 1053 1054```cpp 1055static void GetUserInfo(const FunctionCallbackInfo<Value>& args) { 1056 Environment* env = Environment::GetCurrent(args); 1057 uv_passwd_t pwd; 1058 // ... 1059 1060 const int err = uv_os_get_passwd(&pwd); 1061 1062 if (err) { 1063 // ... error handling, return early ... 1064 } 1065 1066 auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); }); 1067 1068 // ... 1069 // Turn `pwd` into a JavaScript object now; whenever we return from this 1070 // function, `uv_os_free_passwd()` will be called. 1071 // ... 1072} 1073``` 1074 1075[C++ coding style]: ../doc/contributing/cpp-style-guide.md 1076[Callback scopes]: #callback-scopes 1077[ECMAScript realm]: https://tc39.es/ecma262/#sec-code-realms 1078[JavaScript value handles]: #js-handles 1079[N-API]: https://nodejs.org/api/n-api.html 1080[`BaseObject`]: #baseobject 1081[`Context`]: #context 1082[`Environment`]: #environment 1083[`Global`]: #global-handles 1084[`HandleWrap`]: #handlewrap 1085[`IsolateData`]: #isolate-data 1086[`Isolate`]: #isolate 1087[`Local`]: #local-handles 1088[`MakeCallback()`]: #makecallback 1089[`MessagePort`]: https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport 1090[`Realm`]: #realm 1091[`ReqWrap`]: #reqwrap 1092[`ShadowRealm`]: https://github.com/tc39/proposal-shadowrealm 1093[`async_hooks` module]: https://nodejs.org/api/async_hooks.html 1094[`async_wrap.h`]: async_wrap.h 1095[`base_object.h`]: base_object.h 1096[`handle_wrap.h`]: handle_wrap.h 1097[`memory_tracker.h`]: memory_tracker.h 1098[`req_wrap.h`]: req_wrap.h 1099[`util.h`]: util.h 1100[`v8.h` in Code Search]: https://cs.chromium.org/chromium/src/v8/include/v8.h 1101[`v8.h` in Node.js]: https://github.com/nodejs/node/blob/HEAD/deps/v8/include/v8.h 1102[`v8.h` in V8]: https://github.com/v8/v8/blob/HEAD/include/v8.h 1103[`vm` module]: https://nodejs.org/api/vm.html 1104[binding function]: #binding-functions 1105[cleanup hooks]: #cleanup-hooks 1106[event loop]: #event-loop 1107[exception handling]: #exception-handling 1108[fast API calls]: ../doc/contributing/adding-v8-fast-api.md 1109[internal field]: #internal-fields 1110[introduction for V8 embedders]: https://v8.dev/docs/embed 1111[libuv]: https://libuv.org/ 1112[libuv handles]: #libuv-handles-and-requests 1113[libuv requests]: #libuv-handles-and-requests 1114[reference documentation for the libuv API]: http://docs.libuv.org/en/v1.x/ 1115