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.
Files changed (181) hide show
  1. package/hermes/CMakeLists.txt +874 -0
  2. package/hermes/android/HostObjectJniTest.cpp +178 -0
  3. package/hermes/android/build-logic/build.gradle.kts +31 -0
  4. package/hermes/android/build-logic/settings.gradle.kts +17 -0
  5. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/helpers/internal/SerializeHermesCompilerPackageJson.kt +33 -0
  6. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/plugins/internal/TestUtilsPlugin.kt +80 -0
  7. package/hermes/android/build-logic/src/main/kotlin/com/facebook/hermes/tasks/internal/CustomExecTask.kt +46 -0
  8. package/hermes/android/build-logic/src/main/kotlin/publish.gradle.kts +82 -0
  9. package/hermes/android/build.gradle.kts +332 -0
  10. package/hermes/android/cppruntime/build.gradle +72 -0
  11. package/hermes/android/cppruntime/src/main/AndroidManifest.xml +2 -0
  12. package/hermes/android/cppruntime/src/main/cpp/CMakeLists.txt +8 -0
  13. package/hermes/android/cppruntime/src/main/cpp/stub.cpp +12 -0
  14. package/hermes/android/cross-compile/swift-libicu.patch +13 -0
  15. package/hermes/android/cross-compile/toolchain-android-arm-api13-gnustl.cmake +13 -0
  16. package/hermes/android/cross-compile/toolchain-android-arm-api15-gnustl.cmake +14 -0
  17. package/hermes/android/gradle/libs.versions.toml +30 -0
  18. package/hermes/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  19. package/hermes/android/gradle/wrapper/gradle-wrapper.properties +7 -0
  20. package/hermes/android/gradle.properties +3 -0
  21. package/hermes/android/gradlew +248 -0
  22. package/hermes/android/gradlew.bat +93 -0
  23. package/hermes/android/intltest/build.gradle +147 -0
  24. package/hermes/android/intltest/java/com/facebook/hermes/test/CMakeLists.txt +45 -0
  25. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.cpp +68 -0
  26. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.h +44 -0
  27. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogue.java +45 -0
  28. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesEpilogueTestData.java +32 -0
  29. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +150 -0
  30. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +147 -0
  31. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +470 -0
  32. package/hermes/android/intltest/java/com/facebook/hermes/test/HermesRuntimeFactory.cpp +73 -0
  33. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.cpp +90 -0
  34. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.h +62 -0
  35. package/hermes/android/intltest/java/com/facebook/hermes/test/JSRuntime.java +155 -0
  36. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.cpp +48 -0
  37. package/hermes/android/intltest/java/com/facebook/hermes/test/MappedFileBuffer.h +36 -0
  38. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoad.cpp +13 -0
  39. package/hermes/android/intltest/java/com/facebook/hermes/test/OnLoadEpilogue.cpp +13 -0
  40. package/hermes/android/intltest/src/main/AndroidManifest.xml +2 -0
  41. package/hermes/android/ios-artifacts/build.gradle.kts +65 -0
  42. package/hermes/android/settings.gradle.kts +27 -0
  43. package/hermes/cmake/modules/Hermes.cmake +481 -0
  44. package/hermes/cmake/modules/Lit.cmake +96 -0
  45. package/hermes/doc/Branches.md +38 -0
  46. package/hermes/doc/BuildingAndRunning.md +121 -0
  47. package/hermes/doc/CodingStandards.md +112 -0
  48. package/hermes/doc/CrossCompilation.md +79 -0
  49. package/hermes/doc/Design.md +217 -0
  50. package/hermes/doc/Emscripten.md +156 -0
  51. package/hermes/doc/Features.md +143 -0
  52. package/hermes/doc/GCSafeCoding.md +563 -0
  53. package/hermes/doc/GenGC.md +300 -0
  54. package/hermes/doc/Hades.md +344 -0
  55. package/hermes/doc/HighLevelOptimizations.md +37 -0
  56. package/hermes/doc/IR.md +1306 -0
  57. package/hermes/doc/IntlAPIs.md +196 -0
  58. package/hermes/doc/JIT.md +999 -0
  59. package/hermes/doc/LazyEvalCompilation.md +84 -0
  60. package/hermes/doc/MemoryProfilers.md +316 -0
  61. package/hermes/doc/Modules.md +144 -0
  62. package/hermes/doc/Optimizer.md +83 -0
  63. package/hermes/doc/PerfProfiling.md +29 -0
  64. package/hermes/doc/ProhibitInvoke.md +329 -0
  65. package/hermes/doc/ReactNative.md +40 -0
  66. package/hermes/doc/ReactNativeIntegration.md +94 -0
  67. package/hermes/doc/RegExp.md +24 -0
  68. package/hermes/doc/SpecIncompat.md +85 -0
  69. package/hermes/doc/Strings.md +140 -0
  70. package/hermes/doc/TypedLanguage.md +432 -0
  71. package/hermes/doc/VM.md +174 -0
  72. package/hermes/doc/blog/2024-11-09-octane-benchmark-jit-progress.md +17 -0
  73. package/hermes/doc/blog/2024-12-19-static-hermes-update-dec-2024.md +47 -0
  74. package/hermes/doc/blog/2024-12-23-compiling-javascript-to-wasm.md +267 -0
  75. package/hermes/doc/blog/2025-06-09-jsi-runtime-data-apis.md +23 -0
  76. package/hermes/doc/blog/2025-07-15-static-h-performance-june-2025.md +18 -0
  77. package/hermes/doc/blog/2025-11-02-hermes-compilation-runtime-modes.md +23 -0
  78. package/hermes/doc/blog/2025-11-06-hermes-memory-modes.md +35 -0
  79. package/hermes/doc/blog/2025-11-24-json-parsing-performance.md +17 -0
  80. package/hermes/doc/blog/2025-12-02-iserialization.md +11 -0
  81. package/hermes/doc/blog/2025-12-12-new-way-to-contribute.md +68 -0
  82. package/hermes/doc/blog/2026-06-05-new-hermes-stable-release.md +51 -0
  83. package/hermes/doc/blog/README.md +25 -0
  84. package/hermes/doc/blog/images/2024-11-09-octane-pi3.jpeg +0 -0
  85. package/hermes/doc/blog/images/2024-11-09-octane-pi5.jpeg +0 -0
  86. package/hermes/doc/blog/images/2025-07-15-perf-hv32-interpreter.jpeg +0 -0
  87. package/hermes/doc/blog/images/2025-07-15-perf-hv32-jit.jpeg +0 -0
  88. package/hermes/doc/icu_license.txt +420 -0
  89. package/hermes/doc/img/heap_snapshot_retainers.png +0 -0
  90. package/hermes/doc/img/logo.svg +1 -0
  91. package/hermes/doc/img/object_captured_by_environment.png +0 -0
  92. package/hermes/doc/img/root_categories.png +0 -0
  93. package/hermes/doc/plans/ir-type/flow-type-system.md +324 -0
  94. package/hermes/doc/plans/ir-type/ir-type-system-v2-design.md +652 -0
  95. package/hermes/doc/plans/ir-type/ir-type-system.md +220 -0
  96. package/hermes/doc/plans/ir-type/ir-type-v2-implementation.md +1187 -0
  97. package/hermes/doc/plans/ir-type/memory.md +48 -0
  98. package/hermes/doc/plans/ir-type/phase1-steps.md +512 -0
  99. package/hermes/doc/plans/ir-type/progress.md +239 -0
  100. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-design.md +203 -0
  101. package/hermes/doc/plans/ir-type/typeinference-primitive-fastpath-results.md +71 -0
  102. package/hermes/doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md +597 -0
  103. package/hermes/doc/superpowers/plans/2026-08-18-jitemitter-split.md +1149 -0
  104. package/hermes/doc/superpowers/plans/2026-08-20-jit-type-asserts.md +969 -0
  105. package/hermes/doc/superpowers/plans/2026-08-23-jit-shared-compiler-driver.md +478 -0
  106. package/hermes/doc/superpowers/plans/2026-08-23-x86-64-jit-skeleton.md +797 -0
  107. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-arith.md +444 -0
  108. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-calls.md +261 -0
  109. package/hermes/doc/superpowers/plans/2026-08-24-x86-64-jit-props.md +240 -0
  110. package/hermes/doc/superpowers/plans/2026-10-26-regexp-bytecode-sharing.md +2197 -0
  111. package/hermes/doc/superpowers/plans/2026-10-28-regexp-start-filter-and-exec-fast-paths.md +3981 -0
  112. package/hermes/doc/superpowers/specs/2026-07-28-worker-arraybuffer-input-design.md +206 -0
  113. package/hermes/doc/superpowers/specs/2026-08-18-jitemitter-split-design.md +530 -0
  114. package/hermes/doc/superpowers/specs/2026-08-20-jit-type-asserts-design.md +360 -0
  115. package/hermes/doc/superpowers/specs/2026-08-23-x86-64-jit-design.md +246 -0
  116. package/hermes/doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md +355 -0
  117. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-convert-to-case-design.md +305 -0
  118. package/hermes/doc/superpowers/specs/2026-08-27-icu-free-locale-compare-design.md +429 -0
  119. package/hermes/doc/superpowers/specs/2026-08-29-icu-free-date-format-design.md +297 -0
  120. package/hermes/doc/superpowers/specs/2026-10-26-regexp-bytecode-sharing-design.md +605 -0
  121. package/hermes/doc/superpowers/specs/2026-10-28-regexp-start-filter-and-exec-fast-paths-design.md +551 -0
  122. package/hermes/doc/typescript-stripping.md +90 -0
  123. package/hermes/external/CMakeLists.txt +20 -0
  124. package/hermes/utils/IntlGen/IntlGen.java +531 -0
  125. package/hermes/utils/bench_with_stats.py +827 -0
  126. package/hermes/utils/build-apple-framework.sh +323 -0
  127. package/hermes/utils/chrome_snapshot_parser.py +210 -0
  128. package/hermes/utils/cmake-dev-build.sh +18 -0
  129. package/hermes/utils/commons.sh +55 -0
  130. package/hermes/utils/dump-cpp-funcs.sh +3 -0
  131. package/hermes/utils/es5objects.js +198 -0
  132. package/hermes/utils/format.sh +145 -0
  133. package/hermes/utils/gen-promise-internal-bc.sh +66 -0
  134. package/hermes/utils/genUnicodeTable.py +2503 -0
  135. package/hermes/utils/hermes-lit/CMakeLists.txt +22 -0
  136. package/hermes/utils/hermes-lit/hermes-lit.in +15 -0
  137. package/hermes/utils/jit/README.md +134 -0
  138. package/hermes/utils/jit/jit-diff.sh +160 -0
  139. package/hermes/utils/jit/jit-dump.sh +193 -0
  140. package/hermes/utils/lit-expect-gen/generate.py +217 -0
  141. package/hermes/utils/promise/index.js +15 -0
  142. package/hermes/utils/promise/package.json +13 -0
  143. package/hermes/utils/promise/rollup.config.js +19 -0
  144. package/hermes/utils/promise/yarn.lock +247 -0
  145. package/hermes/utils/scripts/hermes/consts.js +26 -0
  146. package/hermes/utils/scripts/hermes/create-tarball.js +75 -0
  147. package/hermes/utils/scripts/hermes/get-hermes-version.js +57 -0
  148. package/hermes/utils/scripts/hermes/hermes-utils.js +122 -0
  149. package/hermes/utils/scripts/hermes/package.json +6 -0
  150. package/hermes/utils/scripts/hermes/prepare-artifacts.js +113 -0
  151. package/hermes/utils/scripts/hermes/publish-artifacts.js +109 -0
  152. package/hermes/utils/scripts/hermes/publish-npm.js +96 -0
  153. package/hermes/utils/scripts/hermes/scm-utils.js +41 -0
  154. package/hermes/utils/scripts/hermes/set-artifacts-version.js +64 -0
  155. package/hermes/utils/scripts/hermes/version-utils.js +98 -0
  156. package/hermes/utils/scripts/hermes/yarn.lock +240 -0
  157. package/hermes/utils/summarize_sizes.py +184 -0
  158. package/hermes/utils/symbolicate-nativecall-stats.py +112 -0
  159. package/hermes/utils/test_runner.py +13 -0
  160. package/hermes/utils/testsuite/README.facebook +3 -0
  161. package/hermes/utils/testsuite/README.md +69 -0
  162. package/hermes/utils/testsuite/cli.py +547 -0
  163. package/hermes/utils/testsuite/es_ast.py +337 -0
  164. package/hermes/utils/testsuite/external/LICENSE +28 -0
  165. package/hermes/utils/testsuite/external/README.facebook +8 -0
  166. package/hermes/utils/testsuite/external/__init__.py +0 -0
  167. package/hermes/utils/testsuite/external/_monkeyYaml.py +164 -0
  168. package/hermes/utils/testsuite/external/parse_test262.py +141 -0
  169. package/hermes/utils/testsuite/hermes.py +522 -0
  170. package/hermes/utils/testsuite/preprocess.py +365 -0
  171. package/hermes/utils/testsuite/progress.py +377 -0
  172. package/hermes/utils/testsuite/skiplist.json +4356 -0
  173. package/hermes/utils/testsuite/skiplist.py +270 -0
  174. package/hermes/utils/testsuite/test_run_defs.py +435 -0
  175. package/hermes/utils/testsuite/typing_defs.py +52 -0
  176. package/hermes/utils/testsuite/utils.py +214 -0
  177. package/hermes/utils/tidy.sh +22 -0
  178. package/hermes/utils/update-fbcode-hermes.sh +53 -0
  179. package/hermes/utils/updateErrorTest.py +193 -0
  180. package/hermes/utils/wasm-compile.sh +65 -0
  181. package/package.json +1 -1
@@ -0,0 +1,429 @@
1
+ # ICU-free `localeCompare`
2
+
3
+ Date: 2026-08-27
4
+ Status: approved, ready for implementation planning
5
+
6
+ Sub-project 3 of 4 in the ICU4C removal. See
7
+ `doc/superpowers/specs/2026-08-26-icu-free-normalize-design.md` for the overall
8
+ decomposition and the shared conventions this follows.
9
+
10
+ ## Context
11
+
12
+ `localeCompare` backs `String.prototype.localeCompare` and is the third of the
13
+ four functions Hermes still needs ICU for on Linux and Windows. There is
14
+ exactly one caller, `String.cpp:1323`, and when `HERMES_ENABLE_INTL` is on it
15
+ never runs at all: `Intl.Collator` takes over at `Intl.cpp:1625`. Intl is
16
+ Android-only, so this spec concerns the non-Intl path.
17
+
18
+ ### What the current implementation does
19
+
20
+ `PlatformUnicodeICU.cpp:37-58` opens one `UCollator` for `uloc_getDefault()`,
21
+ falling back to the root locale, and sets `UCOL_NORMALIZATION_MODE` to
22
+ `UCOL_ON` so that canonically equivalent strings compare equal.
23
+ `ucol_strcoll` then takes explicit lengths, which is why embedded U+0000 is
24
+ handled correctly today.
25
+
26
+ The buck build passes ICU the **full** `icudt` rather than the reduced
27
+ English-only data file (`BUCK:1923`, `//third-party/icu:icudt`), so the CLDR
28
+ locale tailorings really are present at runtime: under `LANG=sv_SE` a Linux
29
+ user currently gets Swedish collation, in which `ä` sorts after `z`.
30
+
31
+ ### Why not libc
32
+
33
+ Recorded in the normalize spec and not relitigated here. `strcoll_l` is
34
+ NUL-terminated, so embedded U+0000 truncates the comparison; unpaired
35
+ surrogates have no valid UTF-8 encoding; glibc does no normalization
36
+ preprocessing, silently dropping the `UCOL_NORMALIZATION_MODE` behavior; and
37
+ results vary with the host's installed locale archive, degrading to byte order
38
+ on musl and in minimal containers.
39
+
40
+ ### Existing coverage
41
+
42
+ `test/hermes/string-functions.js:596-613` is the live test. It runs under
43
+ `LANG=en_US.UTF-8` and covers six cases, written here in escape form because
44
+ the interesting pairs are visually identical: `'abc'` against itself and
45
+ against `'def'`; `'a'` against `'Z'`; precomposed `'\u00F6'` against
46
+ decomposed `'o\u0308'`; `'a\u0323\u0308'` against
47
+ `'a\u0308\u0323'`; the Angstrom sign `'\u212B'` against
48
+ `'A\u030A'`; and diacritic reordering, `'S\u0323\u0307'`
49
+ against `'S\u0307\u0323'`.
50
+ `test/BCGen/HBC/deltamode/string-functions-update.js:454-471` is a near-copy for
51
+ delta-bytecode mode. Both must keep passing.
52
+
53
+ `test/hermes/string-locale.js` covers only casing; it does **not** test
54
+ `localeCompare`, so the locale-tailoring behavior described above is untested.
55
+ `utils/testsuite/skiplist.json` has no `localeCompare` entries, so whatever
56
+ test262 covers today passes today. There are no unit tests.
57
+
58
+ ## Scope
59
+
60
+ Replace `localeCompare` with a self-contained UTS #10 root collation on the
61
+ Hermes and LITE backends.
62
+
63
+ ### Non-goals
64
+
65
+ - Changing the Android, Apple or Emscripten backends. Emscripten defers to the
66
+ host JavaScript engine's collator, which is better than anything shipped
67
+ here.
68
+ - Implementing Intl or ECMA-402 `Intl.Collator`.
69
+ - Honoring the `locales` argument. ECMA-262 directs a non-ECMA-402
70
+ implementation to the host environment's current locale, and this
71
+ implementation has no locale at all; see below.
72
+ - CLDR locale tailorings.
73
+ - Regenerating `UnicodeData.inc`.
74
+ - Removing the ICU dependency, which happens in sub-project 4.
75
+
76
+ ### Deliberate behavior changes
77
+
78
+ - **Root collation replaces host-locale tailoring on Linux and Windows.** A
79
+ user under `LANG=sv_SE` stops getting Swedish ordering. Tailorings are
80
+ roughly a hundred locales of CLDR rule data with their own rule parser and no
81
+ conformance suite, which is a larger project than the other three
82
+ sub-projects combined. ECMA-262 without Intl makes the result
83
+ implementation-defined, and no test covers the tailored behavior.
84
+ - **LITE gains real collation.** It compares UTF-16 code units today
85
+ (`PlatformUnicodeLite.cpp:15`), so this is a substantial improvement. LITE's
86
+ contract is *no dependencies*, not small size, and it is expected to become
87
+ unnecessary now that the Hermes backend exists; table size is therefore not
88
+ an argument against wiring it in.
89
+ - **Ordering diverges from ICU on the CLDR root deltas.** ICU uses CLDR root,
90
+ which adjusts DUCET in a handful of documented ways, mostly the placement of
91
+ variable characters and a few script reorderings. Letters and digits are
92
+ unaffected.
93
+
94
+ ## Approach
95
+
96
+ DUCET root collation per UTS #10, three levels, non-ignorable variable
97
+ weighting.
98
+
99
+ Non-ignorable is ICU's default and is also the simpler algorithm: variable
100
+ characters keep their primary weights, so there is no fourth level and no
101
+ shifting logic.
102
+
103
+ Rejected alternatives:
104
+
105
+ - **NFD followed by code point comparison.** Costs no new data at all and
106
+ satisfies the ECMA-262 recommendation that canonically equivalent strings
107
+ compare equal, but makes `'a'.localeCompare('B')` positive. Sorting
108
+ user-visible strings is the overwhelmingly common use of this function, and
109
+ this would leave Linux as the only platform without real collation.
110
+ - **Primary weights only, tie-breaking by code point.** Roughly a third the
111
+ table size and gets base-letter ordering right, but accent and case ordering
112
+ fall back to code point order, and it fails the UCA conformance suite by
113
+ construction, leaving this project's "show the suite fail" discipline with
114
+ nothing to hang on.
115
+ - **`FractionalUCA.txt` from `third-party/tp2/icu/78.1rc`.** Already in the
116
+ repo at exactly UCA 17.0.0, and reproduces ICU's ordering rather than raw
117
+ DUCET. Rejected because the format is ICU-internal — variable-length
118
+ fractional primaries, script reorder groups, prefix rules — so parsing it
119
+ means reimplementing part of ICU's data model instead of the published
120
+ algorithm, and its matching conformance suite is CLDR's, which still has to
121
+ be fetched.
122
+ - **`allkeys.txt` from `third-party/tp2/nunicode`.** Also already in the repo
123
+ and needs no download, but it is DUCET 13.0.0. It would pair 13.0.0
124
+ collation weights with 17.0.0 normalization and casing tables in one binary,
125
+ and validating it would need a 13.0.0-era conformance file, which is itself a
126
+ fetch. It trades the blocker for a permanent inconsistency.
127
+
128
+ ### Input data
129
+
130
+ `allkeys.txt` and the `CollationTest_*.txt` conformance files from
131
+ `https://www.unicode.org/Public/UCA/17.0.0/`. Neither is in the repo and
132
+ unicode.org is blocked by the agent proxy, so both are fetched externally and
133
+ unpacked into `/tmp/ucd` alongside the UCD files, exactly as the UCD data was.
134
+ Their SHA1s are recorded in the generated file's provenance header:
135
+
136
+ ```
137
+ 7843ad89a16c33ba1ff5140eb3e2cee4cc6886e5 allkeys.txt
138
+ 07b4ea4038f8621cd6e6d503ebd78ea64ce921a8 CollationTest_NON_IGNORABLE_SHORT.txt
139
+ 9f95424a421d150aca5d8187936d8aff835f3de6 CollationTest_NON_IGNORABLE.txt
140
+ f8854c3264f54301ebb1160784162fb51b92b3be CollationTest_SHIFTED_SHORT.txt
141
+ 65aae53557ea9403ac9d692180e66abeb55e9097 CollationTest_SHIFTED.txt
142
+ ```
143
+
144
+ UCA 17.0.0 matches the UCD 17.0.0 the other tables are pinned to. The
145
+ checked-in ICU 77.1 implements Unicode 16.0, so the same version skew already
146
+ documented for normalize applies here; our data being newer is the correct
147
+ direction.
148
+
149
+ ### Shape of the data, measured
150
+
151
+ From `allkeys.txt` 17.0.0, before any compression:
152
+
153
+ | | Count |
154
+ | --- | --- |
155
+ | Mapping lines | 39,749 |
156
+ | Single code point, one collation element | 34,787 |
157
+ | Single code point, expansion to 2-18 elements | 3,998 |
158
+ | Contractions, all 2 or 3 code points | 964 |
159
+ | `@implicitweights` ranges (Tangut, Nushu, Khitan) | 6 |
160
+ | Distinct (variable, secondary, tertiary) triples | 307 |
161
+
162
+ Han is **not** listed and must come from the implicit weight formula; there is
163
+ no large block of algorithmically-derivable entries to drop, which is what the
164
+ earlier 60 KB end of the estimate assumed.
165
+
166
+ Two properties of the data drive the encoding. Secondary and tertiary weights
167
+ are drawn from a tiny alphabet — 307 distinct combinations, with
168
+ `(non-variable, 0x0020, 0x0002)` alone covering 21,229 entries — so they
169
+ compress to a one-byte index into a palette. And primary weights advance in
170
+ step with the code point within a script block, so the single-element mappings
171
+ collapse from 34,787 entries to 6,655 runs.
172
+
173
+ ### Measured table size
174
+
175
+ **123 KB** for a first-cut encoding, against the roughly 150 KB threshold at
176
+ which the fidelity decision was to be revisited:
177
+
178
+ | Component | Encoding | Size |
179
+ | --- | --- | --- |
180
+ | Single-element runs | 6,655 x 10 B | 65.0 KB |
181
+ | Style palette | 307 x 4 B | 1.2 KB |
182
+ | Expansions | 2,008 headers + 4,933 pooled elements | 35.0 KB |
183
+ | Contractions | 953 headers + 1,836 pooled elements | 22.1 KB |
184
+ | **Total** | | **123.2 KB** |
185
+
186
+ This is a floor on what the generator must beat, not a projection: the field
187
+ widths above are deliberately naive, storing each run's start code point in a
188
+ full four bytes when runs are sorted and the deltas would fit in two, and
189
+ giving each contraction a 16-byte header when the keys are at most three code
190
+ points. Both prior sub-projects found comparable slack.
191
+
192
+ ### Only NFD-reachable entries are generated
193
+
194
+ The measurement above already assumes an optimization worth stating
195
+ explicitly. Because step 1 normalizes to NFD, no canonically decomposable code
196
+ point can ever reach the table. Dropping those entries costs nothing and
197
+ removes 1,990 of the 3,998 expansions, halving that table from 67 KB to 35 KB.
198
+ It removes only 91 single-element entries and 11 contractions, because
199
+ precomposed characters are overwhelmingly the ones with expansions.
200
+
201
+ The filter is *canonical* decomposition only. Compatibility decompositions
202
+ survive NFD, so the ligatures and compatibility forms stay in the table. Hangul
203
+ syllables decompose arithmetically and are not listed in `UnicodeData.txt`, so
204
+ the generator must exclude `AC00..D7A3` explicitly.
205
+
206
+ This makes "the input has been normalized to NFD" a hard precondition of the
207
+ lookup rather than an implementation detail. Any fast path that skips
208
+ normalization must be restricted to code points that are NFD-stable; ASCII
209
+ qualifies, which is the only fast path contemplated here.
210
+
211
+ ## Architecture
212
+
213
+ ### Shared layer
214
+
215
+ `include/hermes/Platform/Unicode/UnicodeCollation.h`:
216
+
217
+ ```cpp
218
+ namespace hermes {
219
+ namespace unicode {
220
+
221
+ /// Compare \p left and \p right under the DUCET root collation of UTS #10,
222
+ /// using non-ignorable variable weighting and comparing through the tertiary
223
+ /// level. \return -1, 0, or 1 corresponding to whether \p left compares less
224
+ /// than, equal to, or greater than \p right.
225
+ ///
226
+ /// Both arguments are treated as WTF-16: unpaired surrogates are compared by
227
+ /// their implicit weights, and U+0000 is an ordinary character rather than a
228
+ /// terminator.
229
+ int compareUTF16(
230
+ llvh::ArrayRef<char16_t> left,
231
+ llvh::ArrayRef<char16_t> right);
232
+
233
+ } // namespace unicode
234
+ } // namespace hermes
235
+ ```
236
+
237
+ Named `compareUTF16` for consistency with `normalizeUTF16` and
238
+ `convertCaseUTF16`. The ADL hazard that forced those names does not arise here,
239
+ since no argument type lives in `hermes::unicode`, but diverging from the
240
+ established convention for that reason alone would be worse.
241
+
242
+ The function is pure and takes no locale, which is what makes the Hermes and
243
+ LITE backends able to share one call.
244
+
245
+ ### Algorithm
246
+
247
+ 1. **Normalize to NFD.** UTS #10 step 1, via the existing
248
+ `normalizeUTF16(buf, NormalizationForm::D)`. This simultaneously reproduces
249
+ the `UCOL_NORMALIZATION_MODE` behavior the ICU code opts into, which is why
250
+ the normalizer was built first.
251
+ 2. **Build the collation element array.** Walk the NFD code points, taking the
252
+ longest match against the contraction table and applying the discontiguous
253
+ contraction rule of UTS #10 S2.1.1-S2.1.3. Expansions come from a shared
254
+ collation element pool. Han, Tangut, Nushu and unassigned code points use
255
+ the implicit weight formula of UTS #10 section 10.1.3 rather than table
256
+ entries.
257
+ 3. **Compare by level.** Level 1 across all non-zero primaries, then level 2,
258
+ then level 3. Sort keys are never materialized; each level is a single pass
259
+ over the two collation element arrays.
260
+ 4. **Return -1, 0, or 1.**
261
+
262
+ Strings equal through the tertiary level return 0. This matches ICU with the
263
+ identical level off, so `"a\u0000b"` and `"ab"` compare equal there as they do
264
+ today. The result remains a consistent comparison function, which is all
265
+ ECMA-262 requires.
266
+
267
+ ### Backends
268
+
269
+ - `PlatformUnicodeHermes.cpp` calls `unicode::compareUTF16`, dropping the
270
+ `icu_impl::localeCompare` forward and its `TODO(icu-removal)`.
271
+ - `PlatformUnicodeLite.cpp` calls the same function, replacing its code-unit
272
+ comparison.
273
+ - Emscripten, Android and Apple are untouched.
274
+
275
+ `UnicodeCollation.cpp` joins `UnicodeNormalization.cpp` and
276
+ `UnicodeCaseConversion.cpp` in the conditional source list in
277
+ `lib/Platform/Unicode/CMakeLists.txt` and the corresponding BUCK target, so
278
+ Android and Apple do not compile the table.
279
+
280
+ ### Shared UTF-16 decode helper
281
+
282
+ `nextCodePoint` is currently duplicated at `UnicodeNormalization.cpp:139` and
283
+ `UnicodeCaseConversion.cpp:319`. This is the third consumer, which is the point
284
+ at which the extraction was deferred to.
285
+
286
+ It moves to `include/hermes/Platform/Unicode/CharacterProperties.h`, in
287
+ namespace `hermes`, next to the `isHighSurrogate`, `isLowSurrogate` and
288
+ `utf16SurrogatePairToCodePoint` helpers it is built from. No new header is
289
+ needed, and the two existing copies are deleted.
290
+
291
+ ## Generator changes
292
+
293
+ `utils/genUnicodeTable.py` gains two `--table` choices, following the pattern
294
+ `normalization`/`normtest` and `casing`/`casetest` already establish:
295
+
296
+ - `--table collation`, emitting `lib/Platform/Unicode/CollationData.inc`: the
297
+ run-encoded single-element mappings, the style palette, an expansion pool of
298
+ collation elements indexed by offset and length in the manner of
299
+ `FULL_UPPER`, and the contraction table. Canonically decomposable code points
300
+ are omitted per the NFD precondition above, and the six `@implicitweights`
301
+ ranges are emitted as ranges rather than expanded.
302
+ - `--table collationtest`, emitting the conformance data described below.
303
+
304
+ Two generator-side assertions matter, because both encode assumptions that
305
+ would fail silently. The run encoder must verify that every run it emits
306
+ reproduces the input weights exactly, rather than trusting the monotonicity it
307
+ exploits. And the `@implicitweights` ranges must be checked against the
308
+ formula in UTS #10 section 10.1.3, not assumed to match it.
309
+
310
+ Contraction keys are at most three code points in this version of the data,
311
+ which the generator should assert rather than hardcode, since a longer key
312
+ would silently truncate.
313
+
314
+ `allkeys.txt` joins the `URLS` dict, and a `COLLATION_FILES` list records the
315
+ provenance of the new tables.
316
+
317
+ Note the existing defect recorded in the handoff: `normtest` is dispatched from
318
+ a bare `else:` while the other choices use explicit `elif`. Adding a sixth and
319
+ seventh choice makes that latent bug reachable, so it is fixed as part of this
320
+ work rather than left as a follow-up.
321
+
322
+ ## Testing
323
+
324
+ - **UCA conformance suite.** The file asserts that each row sorts at or after
325
+ the row before it. Rows are emitted as string literals in the upstream
326
+ format and parsed by the test, as `NormalizationConformanceTest.cpp` does.
327
+
328
+ Read `CollationTest_NON_IGNORABLE_SHORT.txt`, but note that despite the name
329
+ it is **not** a subset: it carries the same 208,070 rows as the full file and
330
+ is smaller only because it omits the per-row sort key comments, 2.2 MB
331
+ against 17.7 MB. Rows average 2.07 code points, so all 208,070 emit as
332
+ roughly 3.7 MB of `.inc` source. That is 2.6 times the normalizer's 1.4 MB
333
+ data file and is the recommended starting point; if it costs too much build
334
+ time, the fallback is a stride-2 sample at 1.8 MB, which matches the
335
+ normalizer's precedent. Sampling is the fallback rather than the default
336
+ because adjacent rows are the most similar pairs in the file, so dropping
337
+ every other row discards exactly the comparisons that discriminate hardest.
338
+
339
+ The file's ordering assumes an appended **identical level** — a final code
340
+ point tie-break that this implementation deliberately does not have. The
341
+ harness must apply that tie-break itself when `compareUTF16` returns 0, or
342
+ the suite fails on rows that are correctly equal at the tertiary level.
343
+
344
+ `CollationTest_SHIFTED.txt` is not used: it tests shifted variable
345
+ weighting, which this implementation does not offer.
346
+
347
+ - **Generated weight lookup check.** For a sample of code points spanning the
348
+ compressed encoding's boundaries, assert the table lookup agrees with an
349
+ expectation derived independently from `allkeys.txt`. The compression is
350
+ where bugs hide, and this is the only test that isolates it from the
351
+ algorithm.
352
+
353
+ - **Targeted gtests**, one area each: contractions including the discontiguous
354
+ case, expansions, implicit weights for Han and for unassigned code points,
355
+ variable characters under non-ignorable weighting, canonical equivalence,
356
+ unpaired surrogates, embedded U+0000, and supplementary-plane code points.
357
+
358
+ - **A populated-data guard**, as the normalizer and case converter have, so
359
+ empty tables cannot make the rest pass vacuously.
360
+
361
+ - **Existing suites.** `test/hermes/string-functions.js` and its delta-mode
362
+ copy, plus whatever test262 covers, all of which pass today.
363
+
364
+ - **Mutation check.** Every suite must be shown to fail when the
365
+ implementation is broken. One test in an earlier plan was caught asserting
366
+ nothing at all before this discipline was applied.
367
+
368
+ Gates for the sub-project: `buck2 test //xplat/static_h:HermesUnitTests`,
369
+ `:lit` and `:testsuite_tests`, plus `arc lint`.
370
+
371
+ ## Diff breakdown
372
+
373
+ 1. Generator `--table collation` and the checked-in `CollationData.inc`, with
374
+ the `else:`/`elif` fix. No behavior change. The 123 KB above is the floor
375
+ this diff must come in at or under; coming in materially over it is the
376
+ signal to stop and revisit fidelity before writing any implementation.
377
+ 2. `UnicodeCollation.{h,cpp}` and its unit tests, plus the `nextCodePoint`
378
+ extraction into `CharacterProperties.h`. Not yet wired to any backend, so
379
+ still no behavior change.
380
+ 3. Generator `--table collationtest`, the conformance data, and
381
+ `CollationConformanceTest.cpp`, shown to fail under mutation.
382
+ 4. Wire `PlatformUnicodeHermes.cpp` and `PlatformUnicodeLite.cpp` to
383
+ `compareUTF16`, dropping `icu_impl::localeCompare`. The `lit` and
384
+ `testsuite_tests` runs are the gate.
385
+
386
+ ## Risks
387
+
388
+ - **Table size exceeds the measurement.** Largely retired: 123 KB is measured
389
+ from the real data rather than projected, and the naive field widths leave
390
+ visible slack. What remains is the risk that the generated encoding is
391
+ *worse* than the hand-measured one. Mitigation: diff 1 still stands alone and
392
+ reports its number.
393
+ - **The NFD-reachability filter is wrong.** Dropping 1,990 expansion entries is
394
+ only safe if every lookup path normalizes first, and a mistake here produces
395
+ wrong weights for real characters rather than a crash. Mitigation: the
396
+ precondition is stated in the header, the filter uses canonical
397
+ decompositions only, and the conformance suite exercises precomposed input
398
+ directly.
399
+ - **Contraction matching is subtly wrong.** The discontiguous contraction rule
400
+ is the most intricate part of UTS #10 and the easiest place to be
401
+ plausibly-but-quietly incorrect. Mitigation: the conformance suite is the
402
+ real defense here; targeted tests alone would not catch it.
403
+ - **The conformance harness masks a defect.** If the identical-level tie-break
404
+ is applied too eagerly it can turn genuine ordering failures into passes.
405
+ Mitigation: the mutation check must specifically break primary, secondary
406
+ and tertiary comparison in turn and confirm the suite catches each.
407
+ - **Divergence from ICU that matters more than expected.** Root collation is
408
+ not CLDR root, and tailorings are gone. Mitigation: the affected orderings
409
+ are documented above; the difference is confined to variable characters,
410
+ script order, and locales with tailorings, none of which any test covers.
411
+ - **The `.inc` files are large enough to slow the build.** The conformance data
412
+ is 3.7 MB against the normalizer's 1.4 MB, and it is checked in. Mitigation:
413
+ emit rows as string literals parsed at runtime rather than pre-expanded
414
+ arrays, which was measured on the normalizer at a third of the source size;
415
+ measure compile time in diff 3 and fall back to a stride-2 sample if it is
416
+ material.
417
+
418
+ ## Success criteria
419
+
420
+ - The UCA conformance suite passes, and is shown to fail when primary,
421
+ secondary and tertiary comparison are each broken in turn.
422
+ - The targeted gtests and the weight lookup check pass.
423
+ - `buck2 test //xplat/static_h:HermesUnitTests`, `:lit` and
424
+ `:testsuite_tests` all pass, including the six existing `localeCompare`
425
+ cases in `string-functions.js`.
426
+ - `localeCompare` calls no ICU symbol on Linux or Windows.
427
+ - LITE performs real collation.
428
+ - `nextCodePoint` exists once.
429
+ - The generated table comes in at or under the measured 123 KB floor.