Skip to content

Commit dbe2da6

Browse files
addaleaxcodebytere
authored andcommitted
doc: expand C++ README with information about exception handling
Add more information about why it is advisable not to use `.FromJust()` etc. on Maybe(Local)s, and general information about termination exceptions. PR-URL: #31720 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 236943a commit dbe2da6

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,23 @@ This should only be performed if it is actually sure that the operation has
424424
not failed. A lot of Node.js’s source code does **not** follow this rule, and
425425
can be brought to crash through this.
426426
427+
In particular, it is often not safe to assume that an operation does not throw
428+
an exception, even if it seems like it would not do that.
429+
The most common reasons for this are:
430+
431+
* Calls to functions like `object->Get(...)` or `object->Set(...)` may fail on
432+
most objects, if the `Object.prototype` object has been modified from userland
433+
code that added getters or setters.
434+
* Calls that invoke *any* JavaScript code, including JavaScript code that is
435+
provided from Node.js internals or V8 internals, will fail when JavaScript
436+
execution is being terminated. This typically happens inside Workers when
437+
`worker.terminate()` is called, but it can also affect the main thread when
438+
e.g. Node.js is used as an embedded library. These exceptions can happen at
439+
any point.
440+
It is not always obvious whether a V8 call will enter JavaScript. In addition
441+
to unexpected getters and setters, accessing some types of built-in objects
442+
like `Map`s and `Set`s can also run V8-internal JavaScript code.
443+
427444
##### MaybeLocal
428445
429446
`v8::MaybeLocal<T>` is a variant of `v8::Maybe<T>` that is either empty or
@@ -433,7 +450,7 @@ operations as the methods of `v8::Maybe`, but with different names:
433450
| `Maybe` | `MaybeLocal` |
434451
| ---------------------- | ------------------------------- |
435452
| `maybe.IsNothing()` | `maybe_local.IsEmpty()` |
436-
| `maybe.IsJust()` | |
453+
| `maybe.IsJust()` | `!maybe_local.IsEmpty()` |
437454
| `maybe.To(&value)` | `maybe_local.ToLocal(&local)` |
438455
| `maybe.ToChecked()` | `maybe_local.ToLocalChecked()` |
439456
| `maybe.FromJust()` | `maybe_local.ToLocalChecked()` |
@@ -514,6 +531,12 @@ If there is a need to catch JavaScript exceptions in C++, V8 provides the
514531
of providing the ability to shut down the program in the typical Node.js way
515532
(printing the exception + stack trace) if an exception is caught.
516533

534+
A `TryCatch` will catch regular JavaScript exceptions, as well as termination
535+
exceptions such as the ones thrown by `worker.terminate()` calls.
536+
In the latter case, the `try_catch.HasTerminated()` function will return `true`,
537+
and the exception object will not be a meaningful JavaScript value.
538+
`try_catch.ReThrow()` should not be used in this case.
539+
517540
<a id="libuv-handles-and-requests"></a>
518541
### libuv handles and requests
519542

0 commit comments

Comments
 (0)