react-nitro-hermes 0.0.1-alpha.8 → 0.0.1-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hermes/CMakeLists.txt +874 -0
- package/hermes/android/HostObjectJniTest.cpp +178 -0
- package/hermes/android/build-logic/build.gradle.kts +31 -0
- package/hermes/android/build-logic/settings.gradle.kts +17 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
- package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
- package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
- package/hermes/android/build.gradle.kts +332 -0
- package/hermes/android/cppruntime/build.gradle +72 -0
- package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
- package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
- package/hermes/android/cross-compile/swift-libicu.patch +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
- package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
- package/hermes/android/gradle/libs.versions.toml +30 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/hermes/android/gradle.properties +3 -0
- package/hermes/android/gradlew +248 -0
- package/hermes/android/gradlew.bat +93 -0
- package/hermes/android/intltest/build.gradle +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
- package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
- package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
- package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
- package/hermes/android/settings.gradle.kts +27 -0
- package/hermes/cmake/modules/Hermes.cmake +481 -0
- package/hermes/cmake/modules/Lit.cmake +96 -0
- package/hermes/doc/Branches.md +38 -0
- package/hermes/doc/BuildingAndRunning.md +121 -0
- package/hermes/doc/CodingStandards.md +112 -0
- package/hermes/doc/CrossCompilation.md +79 -0
- package/hermes/doc/Design.md +217 -0
- package/hermes/doc/Emscripten.md +156 -0
- package/hermes/doc/Features.md +143 -0
- package/hermes/doc/GCSafeCoding.md +563 -0
- package/hermes/doc/GenGC.md +300 -0
- package/hermes/doc/Hades.md +344 -0
- package/hermes/doc/HighLevelOptimizations.md +37 -0
- package/hermes/doc/IR.md +1306 -0
- package/hermes/doc/IntlAPIs.md +196 -0
- package/hermes/doc/JIT.md +999 -0
- package/hermes/doc/LazyEvalCompilation.md +84 -0
- package/hermes/doc/MemoryProfilers.md +316 -0
- package/hermes/doc/Modules.md +144 -0
- package/hermes/doc/Optimizer.md +83 -0
- package/hermes/doc/PerfProfiling.md +29 -0
- package/hermes/doc/ProhibitInvoke.md +329 -0
- package/hermes/doc/ReactNative.md +40 -0
- package/hermes/doc/ReactNativeIntegration.md +94 -0
- package/hermes/doc/RegExp.md +24 -0
- package/hermes/doc/SpecIncompat.md +85 -0
- package/hermes/doc/Strings.md +140 -0
- package/hermes/doc/TypedLanguage.md +432 -0
- package/hermes/doc/VM.md +174 -0
- package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
- package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
- package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
- package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
- package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
- package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
- package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
- package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
- package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
- package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
- package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
- package/hermes/doc/blog/README.md +25 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
- package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
- package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
- package/hermes/doc/icu_license.txt +420 -0
- package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
- package/hermes/doc/img/logo.svg +1 -0
- package/hermes/doc/img/object_captured_by_environment.png +0 -0
- package/hermes/doc/img/root_categories.png +0 -0
- package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
- package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
- package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
- package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
- package/hermes/doc/plans/ir-type/memory.md +48 -0
- package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
- package/hermes/doc/plans/ir-type/progress.md +239 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
- package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
- package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
- package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
- package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
- package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
- package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
- package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
- package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
- package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
- package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
- package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
- package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
- package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
- package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
- package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
- package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
- package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
- package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
- package/hermes/doc/typescript-stripping.md +90 -0
- package/hermes/external/CMakeLists.txt +20 -0
- package/hermes/utils/IntlGen/IntlGen.java +531 -0
- package/hermes/utils/bench_with_stats.py +827 -0
- package/hermes/utils/build-apple-framework.sh +323 -0
- package/hermes/utils/chrome_snapshot_parser.py +210 -0
- package/hermes/utils/cmake-dev-build.sh +18 -0
- package/hermes/utils/commons.sh +55 -0
- package/hermes/utils/dump-cpp-funcs.sh +3 -0
- package/hermes/utils/es5objects.js +198 -0
- package/hermes/utils/format.sh +145 -0
- package/hermes/utils/gen-promise-internal-bc.sh +66 -0
- package/hermes/utils/genUnicodeTable.py +2503 -0
- package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
- package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
- package/hermes/utils/jit/README.md +134 -0
- package/hermes/utils/jit/jit-diff.sh +160 -0
- package/hermes/utils/jit/jit-dump.sh +193 -0
- package/hermes/utils/lit-expect-gen/generate.py +217 -0
- package/hermes/utils/promise/index.js +15 -0
- package/hermes/utils/promise/package.json +13 -0
- package/hermes/utils/promise/rollup.config.js +19 -0
- package/hermes/utils/promise/yarn.lock +247 -0
- package/hermes/utils/scripts/hermes/consts.js +26 -0
- package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
- package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
- package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
- package/hermes/utils/scripts/hermes/package.json +6 -0
- package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
- package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
- package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
- package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
- package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
- package/hermes/utils/scripts/hermes/version-utils.js +98 -0
- package/hermes/utils/scripts/hermes/yarn.lock +240 -0
- package/hermes/utils/summarize_sizes.py +184 -0
- package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
- package/hermes/utils/test_runner.py +13 -0
- package/hermes/utils/testsuite/README.facebook +3 -0
- package/hermes/utils/testsuite/README.md +69 -0
- package/hermes/utils/testsuite/cli.py +547 -0
- package/hermes/utils/testsuite/es_ast.py +337 -0
- package/hermes/utils/testsuite/external/LICENSE +28 -0
- package/hermes/utils/testsuite/external/README.facebook +8 -0
- package/hermes/utils/testsuite/external/__init__.py +0 -0
- package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
- package/hermes/utils/testsuite/external/parse_test262.py +141 -0
- package/hermes/utils/testsuite/hermes.py +522 -0
- package/hermes/utils/testsuite/preprocess.py +365 -0
- package/hermes/utils/testsuite/progress.py +377 -0
- package/hermes/utils/testsuite/skiplist.json +4356 -0
- package/hermes/utils/testsuite/skiplist.py +270 -0
- package/hermes/utils/testsuite/test_run_defs.py +435 -0
- package/hermes/utils/testsuite/typing_defs.py +52 -0
- package/hermes/utils/testsuite/utils.py +214 -0
- package/hermes/utils/tidy.sh +22 -0
- package/hermes/utils/update-fbcode-hermes.sh +53 -0
- package/hermes/utils/updateErrorTest.py +193 -0
- package/hermes/utils/wasm-compile.sh +65 -0
- package/package.json +1 -1
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
# C++ Coding With GC
|
|
2
|
+
|
|
3
|
+
Hermes is a JavaScript engine with a garbage collector (GC). Various entities like
|
|
4
|
+
JS objects, hidden classes, property maps, and so on are allocated in the GC heap
|
|
5
|
+
and their lifetime is managed by the garbage collector — they can be freed when the
|
|
6
|
+
GC discovers that they are no longer referenced. Additionally, the GC can
|
|
7
|
+
automatically move GC-managed entities for heap compaction or other reasons.
|
|
8
|
+
|
|
9
|
+
Writing C++ code that uses GC-managed objects can be difficult because of this —
|
|
10
|
+
objects that the C++ code is accessing can be freed or moved. This document explains
|
|
11
|
+
the rules for writing correct and performant GC-safe code.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## The fundamental rule of GC-safe native code
|
|
15
|
+
|
|
16
|
+
The GC can only determine that objects are unreachable or move objects at
|
|
17
|
+
**GC safepoints**. Without that guarantee, it would be impossible to write GC-safe
|
|
18
|
+
C++ code. So, the fundamental requirement for GC-safe code is that all "live"
|
|
19
|
+
pointers to GC-managed entities used by the C++ code **must** be stored in locations
|
|
20
|
+
known to the GC before a GC safepoint, and must be reloaded from those locations
|
|
21
|
+
after the GC safepoint.
|
|
22
|
+
|
|
23
|
+
Storing the pointers in a location known to the GC ensures that the GC will consider
|
|
24
|
+
those objects reachable and will not free them. Reloading the pointers after the safe
|
|
25
|
+
point guarantees that if the GC moved the objects, the native code will use the
|
|
26
|
+
updated pointer value.
|
|
27
|
+
|
|
28
|
+
This is all very abstract. In the next sections we will talk about more concrete
|
|
29
|
+
patterns.
|
|
30
|
+
|
|
31
|
+
## GC safepoints
|
|
32
|
+
|
|
33
|
+
In practice, a GC safepoint is either an allocation, or a function call that might
|
|
34
|
+
transitively reach a GC safepoint. Not all C++ calls can reach a GC safepoint.
|
|
35
|
+
|
|
36
|
+
In the Hermes code base, we use naming conventions, parameter types, and explicit
|
|
37
|
+
doc-comments to communicate this. The rules are:
|
|
38
|
+
|
|
39
|
+
- A function that takes a `Runtime &` or `PointerBase &` parameter (both of which
|
|
40
|
+
provide access to the GC) is assumed to be able to reach GC safepoints, unless
|
|
41
|
+
explicitly documented otherwise, or named with a suffix like `_noalloc` or
|
|
42
|
+
`_nogc`.
|
|
43
|
+
- A function with an **`_RJS`** suffix may invoke JavaScript code recursively
|
|
44
|
+
("Recursive JavaScript"), so it definitely reaches GC safepoints.
|
|
45
|
+
For example: `JSObject::getNamed_RJS()`, `toString_RJS()`, `toNumber_RJS()`.
|
|
46
|
+
- A function that does **not** take `Runtime &` or `PointerBase &`, and is not
|
|
47
|
+
`_RJS`, is generally safe to call without protecting pointers.
|
|
48
|
+
|
|
49
|
+
These rules must be followed absolutely by C++ code — all pointers to heap values
|
|
50
|
+
must be stored before and reloaded after. (In practice, the values simply "live" in
|
|
51
|
+
locations known to the GC and the C++ code loads them every time. The C++ compiler is
|
|
52
|
+
smart enough to optimize out consecutive loads if the underlying value couldn't have
|
|
53
|
+
changed.)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## GC roots vs heap values
|
|
57
|
+
|
|
58
|
+
The GC needs a starting point to discover which objects are alive. That starting
|
|
59
|
+
point is the set of **roots** — pointers to GC-managed objects that live *outside*
|
|
60
|
+
the GC heap, in locations the GC is explicitly told about. Roots include:
|
|
61
|
+
|
|
62
|
+
- The **register stack** — the VM's operand stack used by the bytecode interpreter.
|
|
63
|
+
- **Locals** — stack-allocated `PinnedValue<>` fields registered via `LocalsRAII`.
|
|
64
|
+
- **GCScope chains** — dynamically allocated `PinnedHermesValue` slots managed by
|
|
65
|
+
`GCScope` (legacy).
|
|
66
|
+
- **Runtime fields** — `PinnedHermesValue` fields inside the `Runtime` object
|
|
67
|
+
itself, such as well-known prototypes (`arrayPrototype`, `objectPrototype`, etc.).
|
|
68
|
+
|
|
69
|
+
None of these live in the GC heap. They live on the C++ stack or in fixed locations
|
|
70
|
+
in the `Runtime`. The GC knows about each category and walks them all during
|
|
71
|
+
collection (see `Runtime::markRoots()` in `Runtime.cpp`).
|
|
72
|
+
|
|
73
|
+
Starting from these roots, the GC traces through every pointer it finds. An object
|
|
74
|
+
in the GC heap is alive if and only if it is **transitively reachable** from at least
|
|
75
|
+
one root. Any object not reachable from any root can be freed. If the GC moves an
|
|
76
|
+
object (for compaction), it updates every pointer it knows about — both roots and
|
|
77
|
+
pointers stored inside other GC heap objects.
|
|
78
|
+
|
|
79
|
+
The key insight for C++ code: **if you hold a raw pointer to a GC object and that
|
|
80
|
+
pointer is not stored in a root, the GC does not know about it.** The pointed-to
|
|
81
|
+
object could be freed (use-after-free) or moved (dangling pointer). This is why all
|
|
82
|
+
live pointers must be stored in roots before any GC safepoint.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
## HermesValue
|
|
86
|
+
|
|
87
|
+
`HermesValue` is the fundamental value type exposed to the GC — it is a NaN-boxed
|
|
88
|
+
64-bit value that can contain JS primitives (numbers, booleans, undefined, null,
|
|
89
|
+
symbols) and, importantly, pointers to GC-managed objects.
|
|
90
|
+
|
|
91
|
+
The GC works on `HermesValue`s, using the NaN-boxing tag bits to determine whether
|
|
92
|
+
each value is a pointer or a primitive. When a value is a pointer, the GC traces
|
|
93
|
+
through it, and updates it if the pointed-to object moves.
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## PinnedHermesValue
|
|
97
|
+
|
|
98
|
+
`PinnedHermesValue` is a subclass of `HermesValue`. The only purpose of the subclass
|
|
99
|
+
is to indicate that this `HermesValue` instance is a **GC root** — it lives in
|
|
100
|
+
memory that is not in the GC heap (and is not movable by the GC), and is known to
|
|
101
|
+
the GC as a root.
|
|
102
|
+
|
|
103
|
+
Note that declaring something as `PinnedHermesValue` does not *magically* register
|
|
104
|
+
it as a root. The declaration is a semantic marker: if a value is stored outside
|
|
105
|
+
the GC heap and is registered with the GC as a root (through `Locals`, `GCScope`, or
|
|
106
|
+
`Runtime` fields), it should be typed as `PinnedHermesValue` to indicate its
|
|
107
|
+
"root-ness".
|
|
108
|
+
|
|
109
|
+
`PinnedHermesValue` is essential for writing GC-safe code. The "locations known to
|
|
110
|
+
the GC" mentioned in the first section are all `PinnedHermesValue` roots.
|
|
111
|
+
Conceptually, C++ code stores all pointers to GC objects it needs into
|
|
112
|
+
`PinnedHermesValue` instances before a safepoint and reloads them after. In
|
|
113
|
+
practice, there are convenient C++ abstractions built on top of
|
|
114
|
+
`PinnedHermesValue`.
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
## Handle and MutableHandle
|
|
118
|
+
|
|
119
|
+
A `Handle<T>` is a wrapper around a pointer to an immutable `PinnedHermesValue`
|
|
120
|
+
(`const PinnedHermesValue *`), where the `PinnedHermesValue` is known to contain a
|
|
121
|
+
value of type `T` (a number, bool, `JSObject`, etc.). Most internal APIs accept
|
|
122
|
+
`Handle`s instead of direct pointers.
|
|
123
|
+
|
|
124
|
+
A `Handle<>` (i.e. `Handle<HermesValue>`) is an untyped handle that can hold any
|
|
125
|
+
`HermesValue`.
|
|
126
|
+
|
|
127
|
+
A `MutableHandle<T>` is similar to a `Handle<T>`, but it points to a *mutable*
|
|
128
|
+
`PinnedHermesValue` and allows updating the stored value.
|
|
129
|
+
|
|
130
|
+
`Handle<T>` is trivially copyable (it is just a pointer) and is designed to be
|
|
131
|
+
passed by value.
|
|
132
|
+
|
|
133
|
+
An important property: a `PinnedValue<T>` implicitly converts to `Handle<T>`. This
|
|
134
|
+
means that when a function accepts `Handle<T>`, you can pass a `PinnedValue<T>`
|
|
135
|
+
directly.
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
## PseudoHandle
|
|
139
|
+
|
|
140
|
+
`PseudoHandle<T>` holds a GC-managed value *without* protecting it via a root. It
|
|
141
|
+
is a move-only type — once moved from, the original is invalidated (in debug mode,
|
|
142
|
+
accessing an invalidated `PseudoHandle` asserts).
|
|
143
|
+
|
|
144
|
+
`PseudoHandle` exists for performance: in many cases a function produces a value and
|
|
145
|
+
the caller immediately stores it in a root. Wrapping the value in a `PseudoHandle`
|
|
146
|
+
encodes in the type system that the value is *unrooted* and must be stored somewhere
|
|
147
|
+
safe before any GC safepoint. Many internal APIs return
|
|
148
|
+
`CallResult<PseudoHandle<T>>` for this reason.
|
|
149
|
+
|
|
150
|
+
Common patterns with `PseudoHandle`:
|
|
151
|
+
```cpp
|
|
152
|
+
// Storing into a PinnedValue (moves and invalidates the PseudoHandle):
|
|
153
|
+
lv.obj = std::move(*result);
|
|
154
|
+
|
|
155
|
+
// Converting to a Handle (allocates in GCScope — legacy):
|
|
156
|
+
auto handle = runtime.makeHandle(std::move(pseudoHandle));
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
## CallResult and error handling
|
|
161
|
+
|
|
162
|
+
`CallResult<T>` is the standard return type for operations that can throw a JS
|
|
163
|
+
exception. It is either a value of type `T` or `ExecutionStatus::EXCEPTION`. The
|
|
164
|
+
typical usage pattern is:
|
|
165
|
+
|
|
166
|
+
```cpp
|
|
167
|
+
auto result = someOperation_RJS(runtime, args);
|
|
168
|
+
if (LLVM_UNLIKELY(result == ExecutionStatus::EXCEPTION)) {
|
|
169
|
+
return ExecutionStatus::EXCEPTION;
|
|
170
|
+
}
|
|
171
|
+
// Use *result or result.getValue() to get the value.
|
|
172
|
+
lv.obj = std::move(*result);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Most functions that can trigger GC return `CallResult<PseudoHandle<T>>` or
|
|
176
|
+
`CallResult<HermesValue>`. The caller must check for exceptions before using the
|
|
177
|
+
value, and must store the value into a root before the next GC safepoint.
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
## GCScope (legacy)
|
|
181
|
+
|
|
182
|
+
`GCScope` is a RAII-based variable-sized container of `PinnedHermesValue`. GCScopes
|
|
183
|
+
are always instantiated in a stack-like manner. They keep themselves in a singly
|
|
184
|
+
linked list — the root of this list is known to the GC. The GC crawls all GCScopes
|
|
185
|
+
during garbage collection, making all `PinnedHermesValue` stored there roots.
|
|
186
|
+
|
|
187
|
+
A GCScope is an efficient dynamic container — it dynamically allocates new
|
|
188
|
+
`PinnedHermesValue` slots internally and returns instances of `Handle<>` or
|
|
189
|
+
`MutableHandle<>`. However, to prevent unlimited growth, it is configured with a
|
|
190
|
+
fixed limit (48 by default in debug builds). This limit can be changed per scope.
|
|
191
|
+
|
|
192
|
+
The typical usage pattern is to declare a GCScope at function entry, allocate values
|
|
193
|
+
inside it as needed (keeping the handles). The GCScope is destroyed automatically
|
|
194
|
+
when the function exits.
|
|
195
|
+
|
|
196
|
+
**IMPORTANT:** Values allocated in a GCScope must not be used after the scope is
|
|
197
|
+
destroyed. This mistake is rare but it happens — typically by attempting to return a
|
|
198
|
+
`Handle<>` that was allocated in a `GCScope` that is about to be destroyed.
|
|
199
|
+
|
|
200
|
+
### Implicit GCScope allocation
|
|
201
|
+
|
|
202
|
+
`runtime.makeHandle()` and `runtime.makeMutableHandle()`, as well as the
|
|
203
|
+
`Handle<>` and `MutableHandle<>` constructors, implicitly allocate a slot in the
|
|
204
|
+
topmost (i.e. most recently created) `GCScope`. This means:
|
|
205
|
+
|
|
206
|
+
- If a function creates `Handle<>` or `MutableHandle<>` values (and does not
|
|
207
|
+
return them to the caller), it likely needs its own `GCScope`, or at the very
|
|
208
|
+
least a `GCScopeMarkerRAII` to free the slots when they are no longer needed.
|
|
209
|
+
A `GCScopeMarkerRAII` is preferred when only one or two handles are allocated.
|
|
210
|
+
- If a function calls APIs that *return* a `Handle<>`, those handles are also
|
|
211
|
+
allocated in the topmost `GCScope`, so the same logic applies. Note that this
|
|
212
|
+
only applies to functions that take `Runtime &` or `PointerBase &` — those are
|
|
213
|
+
the ones that can access the GCScope and allocate new slots. Functions like
|
|
214
|
+
`vmcast<>` that do not take these parameters simply cast an existing handle
|
|
215
|
+
without allocating a new one.
|
|
216
|
+
|
|
217
|
+
Without a local `GCScope` or `GCScopeMarkerRAII`, handles accumulate in the
|
|
218
|
+
caller's `GCScope`, which can cause it to exceed its slot limit.
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
## The problem with loops and GCScopeMarkerRAII (legacy)
|
|
222
|
+
|
|
223
|
+
A common problem when using GCScope is allocating handles in a loop, causing
|
|
224
|
+
potentially unbounded growth of rooted `PinnedHermesValue` slots. This is why
|
|
225
|
+
GCScope has a limit on the maximum number of handles.
|
|
226
|
+
|
|
227
|
+
One solution is to create a GCScope in the loop body. This works, but is
|
|
228
|
+
heavyweight — a GCScope preallocates space for 16 handles and must register and
|
|
229
|
+
deregister itself from the linked list every iteration.
|
|
230
|
+
|
|
231
|
+
`GCScopeMarkerRAII` is a lightweight alternative. It saves the state of a GCScope on
|
|
232
|
+
creation and frees all subsequently allocated handles on destruction. A typical
|
|
233
|
+
pattern is to place it at the top of the loop body:
|
|
234
|
+
|
|
235
|
+
```cpp
|
|
236
|
+
GCScope gcScope(runtime);
|
|
237
|
+
// ...
|
|
238
|
+
for (...) {
|
|
239
|
+
GCScopeMarkerRAII marker(gcScope);
|
|
240
|
+
// Handles allocated here are freed at end of each iteration.
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Or, more efficiently, create the marker outside the loop and call `flush()`
|
|
245
|
+
explicitly:
|
|
246
|
+
|
|
247
|
+
```cpp
|
|
248
|
+
GCScope gcScope(runtime);
|
|
249
|
+
// ...
|
|
250
|
+
auto marker = gcScope.createMarker();
|
|
251
|
+
for (...) {
|
|
252
|
+
gcScope.flushToMarker(marker);
|
|
253
|
+
// ...
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
## Locals and PinnedValue\<T\> (preferred API)
|
|
259
|
+
|
|
260
|
+
**All new code must use `Locals` + `PinnedValue<T>` instead of `GCScope` +
|
|
261
|
+
`makeHandle()` for rooting local GC values.** Do not introduce new `GCScope`
|
|
262
|
+
instances or `makeHandle()` calls. Existing code is being migrated incrementally.
|
|
263
|
+
|
|
264
|
+
`Locals` with `PinnedValue<T>` is the preferred way to create rooted storage for
|
|
265
|
+
local GC values. `Handle<T>` remains the standard type for passing GC values between
|
|
266
|
+
functions — `PinnedValue<T>` implicitly converts to `Handle<T>`, so the two work
|
|
267
|
+
together naturally.
|
|
268
|
+
|
|
269
|
+
### How it works
|
|
270
|
+
|
|
271
|
+
You declare an anonymous struct inheriting from `Locals`, with `PinnedValue<T>`
|
|
272
|
+
fields for each GC value you need to keep alive. Then create a `LocalsRAII` to
|
|
273
|
+
register the struct with the runtime. `LocalsRAII` pushes the struct onto a linked
|
|
274
|
+
list (`runtime.vmLocals`). During GC, `Runtime::markRoots()` walks this list and
|
|
275
|
+
marks every `PinnedHermesValue` in every `Locals` struct as a root.
|
|
276
|
+
|
|
277
|
+
```cpp
|
|
278
|
+
// Locals struct lives on the C++ stack. The PinnedValue fields are
|
|
279
|
+
// PinnedHermesValues that the GC will mark as roots.
|
|
280
|
+
struct : public Locals {
|
|
281
|
+
PinnedValue<JSObject> obj;
|
|
282
|
+
PinnedValue<StringPrimitive> str;
|
|
283
|
+
PinnedValue<> genericValue; // untyped — can hold any HermesValue
|
|
284
|
+
} lv;
|
|
285
|
+
LocalsRAII lraii(runtime, &lv);
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Why Locals is better than GCScope
|
|
289
|
+
|
|
290
|
+
| GCScope + Handle | Locals + PinnedValue |
|
|
291
|
+
|---|---|
|
|
292
|
+
| Dynamically allocates `PinnedHermesValue` slots | Fields are part of the struct — no dynamic allocation |
|
|
293
|
+
| `Handle<T>` is a pointer to a `PinnedHermesValue` — one level of indirection | `PinnedValue<T>` *is* the `PinnedHermesValue` — direct access |
|
|
294
|
+
| Easy to accidentally allocate handles in a loop, requiring `GCScopeMarkerRAII` | Fixed set of fields — loops cannot cause unbounded growth |
|
|
295
|
+
| 48-handle limit per scope (debug assertion) | No limit — number of fields is determined at compile time |
|
|
296
|
+
|
|
297
|
+
### Assignment patterns
|
|
298
|
+
|
|
299
|
+
```cpp
|
|
300
|
+
// From a PseudoHandle (the typical case after a CallResult):
|
|
301
|
+
lv.obj = std::move(*callResult);
|
|
302
|
+
|
|
303
|
+
// From a CallResult with a HermesValue that you know is a specific type:
|
|
304
|
+
lv.obj.castAndSetHermesValue<JSObject>(callResult.getValue());
|
|
305
|
+
|
|
306
|
+
// From a raw pointer:
|
|
307
|
+
lv.obj = someJSObjectPtr;
|
|
308
|
+
|
|
309
|
+
// From a GCPointer:
|
|
310
|
+
lv.obj = someGCPointer.get(runtime);
|
|
311
|
+
|
|
312
|
+
// Clearing (so it doesn't keep an object alive unnecessarily):
|
|
313
|
+
lv.obj = nullptr;
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### Passing PinnedValue where Handle is expected
|
|
317
|
+
|
|
318
|
+
`PinnedValue<T>` implicitly converts to `Handle<T>`, so you can pass it directly
|
|
319
|
+
to functions that accept handles:
|
|
320
|
+
|
|
321
|
+
```cpp
|
|
322
|
+
struct : public Locals {
|
|
323
|
+
PinnedValue<JSObject> O;
|
|
324
|
+
} lv;
|
|
325
|
+
LocalsRAII lraii(runtime, &lv);
|
|
326
|
+
lv.O.castAndSetHermesValue<JSObject>(*objRes);
|
|
327
|
+
|
|
328
|
+
// getPrototypeOf takes Handle<JSObject>, but PinnedValue<JSObject> converts:
|
|
329
|
+
return getPrototypeOf(runtime, lv.O);
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Reading from PinnedValue
|
|
333
|
+
|
|
334
|
+
```cpp
|
|
335
|
+
// Get the underlying typed value:
|
|
336
|
+
JSObject *rawPtr = lv.obj.get(); // or *lv.obj
|
|
337
|
+
JSObject *rawPtr = *lv.obj;
|
|
338
|
+
|
|
339
|
+
// Dereference to access members:
|
|
340
|
+
lv.obj->someMethod();
|
|
341
|
+
|
|
342
|
+
// Get as HermesValue:
|
|
343
|
+
HermesValue hv = lv.obj.getHermesValue();
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
### Template functions
|
|
347
|
+
|
|
348
|
+
When calling `PinnedValue` member function templates like `castAndSetHermesValue<T>`
|
|
349
|
+
from a template context where the `PinnedValue` type is dependent, C++ requires the
|
|
350
|
+
`template` keyword before the member name:
|
|
351
|
+
|
|
352
|
+
```cpp
|
|
353
|
+
template <typename T>
|
|
354
|
+
void doSomething(Runtime &runtime, HermesValue value) {
|
|
355
|
+
struct : public Locals {
|
|
356
|
+
PinnedValue<T> obj;
|
|
357
|
+
} lv;
|
|
358
|
+
LocalsRAII lraii(runtime, &lv);
|
|
359
|
+
// "template" is required because PinnedValue<T> is a dependent type:
|
|
360
|
+
lv.obj.template castAndSetHermesValue<T>(value);
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Without the `template` keyword, the compiler parses the `<` as a less-than operator
|
|
365
|
+
instead of the start of a template argument list, causing a compile error. This
|
|
366
|
+
applies to any template member function called on a `PinnedValue` with a dependent
|
|
367
|
+
type parameter.
|
|
368
|
+
|
|
369
|
+
### Real-world example
|
|
370
|
+
|
|
371
|
+
From `arrayConstructor` in `Array.cpp`:
|
|
372
|
+
|
|
373
|
+
```cpp
|
|
374
|
+
CallResult<HermesValue> arrayConstructor(void *, Runtime &runtime) {
|
|
375
|
+
NativeArgs args = runtime.getCurrentFrame().getNativeArgs();
|
|
376
|
+
struct : public Locals {
|
|
377
|
+
PinnedValue<JSObject> selfParent;
|
|
378
|
+
PinnedValue<JSArray> self;
|
|
379
|
+
} lv;
|
|
380
|
+
LocalsRAII lraii(runtime, &lv);
|
|
381
|
+
|
|
382
|
+
if (LLVM_LIKELY(!args.isConstructorCall() || ...)) {
|
|
383
|
+
CallResult<PseudoHandle<JSArray>> selfRes =
|
|
384
|
+
JSArray::create(runtime, runtime.arrayPrototype);
|
|
385
|
+
if (LLVM_UNLIKELY(selfRes == ExecutionStatus::EXCEPTION))
|
|
386
|
+
return ExecutionStatus::EXCEPTION;
|
|
387
|
+
lv.self = std::move(*selfRes);
|
|
388
|
+
} else {
|
|
389
|
+
CallResult<PseudoHandle<JSObject>> thisParentRes =
|
|
390
|
+
NativeConstructor::parentForNewThis_RJS(runtime, ...);
|
|
391
|
+
if (LLVM_UNLIKELY(thisParentRes == ExecutionStatus::EXCEPTION))
|
|
392
|
+
return ExecutionStatus::EXCEPTION;
|
|
393
|
+
lv.selfParent = std::move(*thisParentRes);
|
|
394
|
+
auto arrRes = JSArray::create(runtime, lv.selfParent);
|
|
395
|
+
if (LLVM_UNLIKELY(arrRes == ExecutionStatus::EXCEPTION))
|
|
396
|
+
return ExecutionStatus::EXCEPTION;
|
|
397
|
+
lv.self = std::move(*arrRes);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ...use lv.self throughout the rest of the function...
|
|
401
|
+
return lv.self.getHermesValue();
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Locals and loops
|
|
406
|
+
|
|
407
|
+
Locals solves the loop problem inherently — the `PinnedValue` fields are a fixed
|
|
408
|
+
set, so you simply reuse them each iteration without needing `GCScopeMarkerRAII`.
|
|
409
|
+
|
|
410
|
+
However, some existing code uses a `GCScope` + `GCScopeMarkerRAII` alongside
|
|
411
|
+
`Locals` for functions that call APIs returning `Handle<>` (which require a
|
|
412
|
+
`GCScope`). In such cases, the `Locals` hold the long-lived values, while the
|
|
413
|
+
`GCScope` manages short-lived temporaries created by those API calls:
|
|
414
|
+
|
|
415
|
+
```cpp
|
|
416
|
+
struct : Locals {
|
|
417
|
+
PinnedValue<JSObject> O;
|
|
418
|
+
PinnedValue<> elem;
|
|
419
|
+
PinnedValue<StringPrimitive> sep;
|
|
420
|
+
} lv;
|
|
421
|
+
LocalsRAII lraii(runtime, &lv);
|
|
422
|
+
|
|
423
|
+
GCScope gcScope(runtime);
|
|
424
|
+
// ...
|
|
425
|
+
auto marker = gcScope.createMarker();
|
|
426
|
+
for (uint32_t i = 0; i < len; gcScope.flushToMarker(marker), ++i) {
|
|
427
|
+
// Temporary handles created inside the loop are flushed each iteration.
|
|
428
|
+
// Long-lived values stored in lv.* persist across iterations.
|
|
429
|
+
auto strRes = toString_RJS(runtime, lv.elem);
|
|
430
|
+
if (LLVM_UNLIKELY(strRes == ExecutionStatus::EXCEPTION))
|
|
431
|
+
return ExecutionStatus::EXCEPTION;
|
|
432
|
+
lv.sep = std::move(*strRes);
|
|
433
|
+
}
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
## Common mistakes
|
|
438
|
+
|
|
439
|
+
### Holding raw pointers across GC safepoints
|
|
440
|
+
|
|
441
|
+
```cpp
|
|
442
|
+
// WRONG — rawPtr may dangle after the allocation:
|
|
443
|
+
JSObject *rawPtr = someHandle->getObject();
|
|
444
|
+
auto newObj = JSObject::create(runtime); // GC safepoint!
|
|
445
|
+
rawPtr->doSomething(); // rawPtr may be invalid!
|
|
446
|
+
|
|
447
|
+
// CORRECT — store in a PinnedValue, reload after:
|
|
448
|
+
struct : public Locals {
|
|
449
|
+
PinnedValue<JSObject> obj;
|
|
450
|
+
} lv;
|
|
451
|
+
LocalsRAII lraii(runtime, &lv);
|
|
452
|
+
lv.obj = someHandle->getObject();
|
|
453
|
+
auto newObj = JSObject::create(runtime); // GC safepoint
|
|
454
|
+
lv.obj->doSomething(); // Safe: PinnedValue is a root
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Holding PseudoHandles across GC safepoints
|
|
458
|
+
|
|
459
|
+
`PseudoHandle<T>` is **not** a GC root — it holds an unrooted value just like a
|
|
460
|
+
raw pointer. It is equally dangerous to use a `PseudoHandle` after a GC
|
|
461
|
+
safepoint. This is easy to miss because `PseudoHandle` looks like a "safe" smart
|
|
462
|
+
pointer type, but it is not rooted.
|
|
463
|
+
|
|
464
|
+
Watch especially for **multi-step creation patterns** where two `::create()`
|
|
465
|
+
calls are made in sequence:
|
|
466
|
+
|
|
467
|
+
```cpp
|
|
468
|
+
// WRONG — ctor PseudoHandle is stale after the second create():
|
|
469
|
+
auto ctor = NativeConstructor::create(runtime, ...);
|
|
470
|
+
auto proto = JSObject::create(runtime); // GC safepoint — ctor is stale!
|
|
471
|
+
lv.ctor = std::move(ctor); // Too late — ctor is already stale!
|
|
472
|
+
|
|
473
|
+
// CORRECT — root the PseudoHandle before the next allocation:
|
|
474
|
+
lv.ctor = NativeConstructor::create(runtime, ...); // Root immediately.
|
|
475
|
+
lv.proto = JSObject::create(runtime); // GC safepoint — lv.ctor is safe.
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
This bug often goes undetected because the stale value is still valid most of
|
|
479
|
+
the time — the GC only moves objects during compaction, which is rare. Enable
|
|
480
|
+
`HERMESVM_SANITIZE_HANDLES` (enabled by default in ASAN builds) to catch these
|
|
481
|
+
bugs deterministically by moving the heap after every allocation.
|
|
482
|
+
|
|
483
|
+
### Returning a Handle from a destroyed GCScope or Locals
|
|
484
|
+
|
|
485
|
+
```cpp
|
|
486
|
+
// WRONG — the Handle points into the destroyed GCScope:
|
|
487
|
+
Handle<JSObject> createThing(Runtime &runtime) {
|
|
488
|
+
GCScope gcScope(runtime);
|
|
489
|
+
auto handle = runtime.makeHandle(JSObject::create(runtime));
|
|
490
|
+
return handle; // gcScope destroyed here — handle dangles!
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// WRONG — returning a Handle from a PinnedValue that is about to be destroyed:
|
|
494
|
+
Handle<JSObject> createThing(Runtime &runtime) {
|
|
495
|
+
struct : public Locals {
|
|
496
|
+
PinnedValue<JSObject> obj;
|
|
497
|
+
} lv;
|
|
498
|
+
LocalsRAII lraii(runtime, &lv);
|
|
499
|
+
lv.obj = ...;
|
|
500
|
+
return lv.obj; // lraii destroyed here — Handle dangles!
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
Instead, return a `PseudoHandle` (which copies the value out, not a pointer to the
|
|
505
|
+
root):
|
|
506
|
+
|
|
507
|
+
```cpp
|
|
508
|
+
// CORRECT — return a PseudoHandle:
|
|
509
|
+
CallResult<PseudoHandle<JSObject>> createThing(Runtime &runtime) {
|
|
510
|
+
struct : public Locals {
|
|
511
|
+
PinnedValue<JSObject> obj;
|
|
512
|
+
} lv;
|
|
513
|
+
LocalsRAII lraii(runtime, &lv);
|
|
514
|
+
lv.obj = ...;
|
|
515
|
+
return PseudoHandle<JSObject>::create(*lv.obj);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// CORRECT — write into a caller-provided MutableHandle:
|
|
519
|
+
void createThing(Runtime &runtime, MutableHandle<JSObject> result) {
|
|
520
|
+
// No local rooting needed — we write directly into the caller's root.
|
|
521
|
+
auto res = JSObject::create(runtime);
|
|
522
|
+
result = res.get();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// CORRECT — return a HermesValue (caller roots it, so risky):
|
|
526
|
+
CallResult<HermesValue> createThing(Runtime &runtime) {
|
|
527
|
+
struct : public Locals {
|
|
528
|
+
PinnedValue<JSObject> obj;
|
|
529
|
+
} lv;
|
|
530
|
+
LocalsRAII lraii(runtime, &lv);
|
|
531
|
+
lv.obj = ...;
|
|
532
|
+
return lv.obj.getHermesValue();
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
### Forgetting to check for exceptions
|
|
537
|
+
|
|
538
|
+
```cpp
|
|
539
|
+
// WRONG — using the value without checking for exception:
|
|
540
|
+
auto result = someOperation_RJS(runtime, args);
|
|
541
|
+
lv.obj = std::move(*result); // May dereference an exception!
|
|
542
|
+
|
|
543
|
+
// CORRECT:
|
|
544
|
+
auto result = someOperation_RJS(runtime, args);
|
|
545
|
+
if (LLVM_UNLIKELY(result == ExecutionStatus::EXCEPTION))
|
|
546
|
+
return ExecutionStatus::EXCEPTION;
|
|
547
|
+
lv.obj = std::move(*result);
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### Null prototype handling
|
|
551
|
+
|
|
552
|
+
When traversing prototype chains, always check for null:
|
|
553
|
+
|
|
554
|
+
```cpp
|
|
555
|
+
auto protoRes = JSObject::getPrototypeOf(obj, runtime);
|
|
556
|
+
if (LLVM_UNLIKELY(protoRes == ExecutionStatus::EXCEPTION))
|
|
557
|
+
return ExecutionStatus::EXCEPTION;
|
|
558
|
+
if (!*protoRes) {
|
|
559
|
+
lv.O = nullptr; // End of prototype chain
|
|
560
|
+
} else {
|
|
561
|
+
lv.O.castAndSetHermesValue<JSObject>(protoRes->getHermesValue());
|
|
562
|
+
}
|
|
563
|
+
```
|