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,522 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ import os
7
+ import shutil
8
+ import sys
9
+ import tempfile
10
+ import time
11
+ from asyncio import create_subprocess_exec, subprocess, TimeoutError, wait_for
12
+ from dataclasses import dataclass, field
13
+ from typing import List, Optional
14
+
15
+ from .preprocess import StrictMode
16
+ from .progress import TestCaseResult, TestResultCode
17
+ from .typing_defs import OptExpectedFailure, PathT
18
+
19
+ COMPILE_ARGS = [
20
+ "-test262",
21
+ "-fno-static-builtins",
22
+ "-Xes6-block-scoping",
23
+ "-Xenable-tdz",
24
+ "-Xasync-generators",
25
+ ]
26
+ ES6_ARGS = ["-Xes6-proxy"]
27
+ EXTRA_RUN_ARGS = ["-Xhermes-internal-test-methods"]
28
+ USE_MICROTASK_FLAG = ["-Xmicrotask-queue"]
29
+
30
+
31
+ @dataclass
32
+ class ExtraCompileVMArgs(object):
33
+ """
34
+ Extra compiler and vm args that may be necessary for some testsuites.
35
+ """
36
+
37
+ compile_args: List[str] = field(default_factory=list)
38
+ vm_args: List[str] = field(default_factory=list)
39
+
40
+
41
+ @dataclass
42
+ class CompileRunArgs(object):
43
+ test_name: str
44
+ """A succicnt test name for printing out."""
45
+ strict_mode: StrictMode
46
+ """Whether to compile/run with strict mode."""
47
+ binary_directory: PathT
48
+ """Directory path of Hermes executables."""
49
+ expected_failure: OptExpectedFailure
50
+ """Expected failure in test262, see TestCase for more details."""
51
+ disable_handle_san: bool
52
+ """Disable handle sanitizer to improve speed."""
53
+ bytecode_compat_check: bool
54
+ """Whether to run bytecode compatibility check."""
55
+ lazy: bool
56
+ """Force lazy evaluation."""
57
+ shermes: bool
58
+ """Run with shermes."""
59
+ opt: bool
60
+ """Enable optimizer, i.e., use -O flag instead of -O0."""
61
+ timeout: int
62
+ """Timeout (in seconds) for compiling/running each test."""
63
+ is_async: bool
64
+ """Whether the test is an async test262 test and needs AsyncTestFailure checked."""
65
+ extra_compile_vm_args: Optional[ExtraCompileVMArgs] = None
66
+ """Extra compile/run arguments given by specific testsuites."""
67
+
68
+
69
+ async def run(
70
+ base_file_name: str,
71
+ file_to_run: PathT,
72
+ expected_failure_phase: str,
73
+ compile_run_args: CompileRunArgs,
74
+ ) -> TestCaseResult:
75
+ # Run the generated bytecode/native code or source code
76
+ if compile_run_args.bytecode_compat_check and not compile_run_args.lazy:
77
+ hermes_exe = shutil.which(
78
+ os.path.join(compile_run_args.binary_directory, "b_hermes")
79
+ )
80
+ else:
81
+ hermes_exe = shutil.which(
82
+ os.path.join(compile_run_args.binary_directory, "hermes")
83
+ )
84
+ cmd_args = []
85
+ lazy = compile_run_args.lazy
86
+ if not compile_run_args.shermes:
87
+ cmd_args.append(hermes_exe)
88
+ cmd_args += [file_to_run] + ES6_ARGS + EXTRA_RUN_ARGS + USE_MICROTASK_FLAG
89
+ if lazy:
90
+ cmd_args.append("-lazy")
91
+ # In lazy mode, we should apply the same compile flags as compile_and_run_single().
92
+ cmd_args += COMPILE_ARGS
93
+ if StrictMode.STRICT in compile_run_args.strict_mode:
94
+ cmd_args.append("-strict")
95
+ else:
96
+ if not compile_run_args.shermes:
97
+ # Be able to recognize the output file as bytecode.
98
+ cmd_args.append("-b")
99
+ if compile_run_args.extra_compile_vm_args:
100
+ cmd_args += compile_run_args.extra_compile_vm_args.vm_args
101
+ if lazy:
102
+ cmd_args += compile_run_args.extra_compile_vm_args.compile_args
103
+ if compile_run_args.disable_handle_san:
104
+ cmd_args += ["-gc-sanitize-handles=0"]
105
+ # Also pass all current environment variables before running the test. This is
106
+ # useful when setting environment variables for shermes or ASAN options, etc.
107
+ env = os.environ | {"LC_ALL": "en_US.UTF-8"}
108
+ if sys.platform == "linux":
109
+ env["ICU_DATA"] = compile_run_args.binary_directory
110
+ proc = await create_subprocess_exec(
111
+ *cmd_args, env=env, stderr=subprocess.PIPE, stdout=subprocess.PIPE
112
+ )
113
+ stdout, stderr = (None, None)
114
+ t_start = time.monotonic()
115
+ try:
116
+ (stdout, stderr) = await wait_for(
117
+ proc.communicate(), timeout=compile_run_args.timeout
118
+ )
119
+ except TimeoutError:
120
+ duration = time.monotonic() - t_start
121
+ msg = f"FAIL: Execution of binary timed out for {file_to_run}"
122
+ output = f"Run command: {' '.join(cmd_args)}\n"
123
+ # Kill the subprocess and all its child processes
124
+ proc.kill()
125
+ return TestCaseResult(
126
+ compile_run_args.test_name,
127
+ TestResultCode.EXECUTE_TIMEOUT,
128
+ msg,
129
+ output,
130
+ duration,
131
+ )
132
+ duration = time.monotonic() - t_start
133
+
134
+ output = f"Run command: {' '.join(cmd_args)}\n"
135
+ if stdout:
136
+ output += f"stdout:\n {stdout.decode('utf-8')}"
137
+ if stderr:
138
+ output += f"stderr:\n {stderr.decode('utf-8')}"
139
+
140
+ stdout_str = stdout.decode("utf-8") if stdout else ""
141
+
142
+ # Check async test result regardless of exit code. The $DONE harness
143
+ # prints "Test262:AsyncTestComplete" on success and
144
+ # "Test262:AsyncTestFailure:" on failure, both potentially with exit
145
+ # code 0.
146
+ if compile_run_args.is_async and (
147
+ "Test262:AsyncTestFailure" in stdout_str
148
+ or "Test262:AsyncTestComplete" not in stdout_str
149
+ ):
150
+ msg = f"FAIL: Execution of async test failed: {stdout_str}"
151
+ return TestCaseResult(
152
+ compile_run_args.test_name,
153
+ TestResultCode.EXECUTE_FAILED,
154
+ msg,
155
+ output,
156
+ duration,
157
+ )
158
+
159
+ # Check if the run succeeded
160
+ if proc.returncode:
161
+ # Negative return code means that the subprocess is terminated with a
162
+ # system signal. See more details in compile_and_run().
163
+ if proc.returncode < 0:
164
+ msg = f"FAIL: Execution terminated with {proc.returncode}"
165
+ return TestCaseResult(
166
+ compile_run_args.test_name,
167
+ TestResultCode.EXECUTE_FAILED,
168
+ msg,
169
+ output,
170
+ duration,
171
+ )
172
+ elif (lazy and expected_failure_phase == "") or (
173
+ not lazy and expected_failure_phase != "runtime"
174
+ ):
175
+ # In lazy mode, we can't check the exact error phase, since some could
176
+ # be parse phase (e.g., SyntaxError). But we can check that if the
177
+ # test is not expecting any error, the invocation of Hermes shouldn't
178
+ # fail, even in lazy mode. This check is conservative, but may still
179
+ # capture some issues in lazy mode.
180
+ msg = f"FAIL: Execution of {base_file_name} threw unexpected error"
181
+ return TestCaseResult(
182
+ compile_run_args.test_name,
183
+ TestResultCode.EXECUTE_FAILED,
184
+ msg,
185
+ output,
186
+ duration,
187
+ )
188
+ else:
189
+ msg = f"PASS: Execution of {base_file_name} threw an error as expected"
190
+ return TestCaseResult(
191
+ compile_run_args.test_name,
192
+ TestResultCode.TEST_PASSED,
193
+ msg,
194
+ duration=duration,
195
+ )
196
+ else:
197
+ if (lazy and expected_failure_phase != "") or (
198
+ not lazy and expected_failure_phase == "runtime"
199
+ ):
200
+ # Similar to above, in lazy mode, we can't check the exact error
201
+ # phase. But we should check that the given test at least failed,
202
+ # if it's expected for a reason.
203
+ msg = f"FAIL: Expected execution of {base_file_name} to throw"
204
+ return TestCaseResult(
205
+ compile_run_args.test_name,
206
+ TestResultCode.EXECUTE_FAILED,
207
+ msg,
208
+ output,
209
+ duration,
210
+ )
211
+ return TestCaseResult(
212
+ compile_run_args.test_name,
213
+ TestResultCode.TEST_PASSED,
214
+ "Run passed",
215
+ output,
216
+ duration,
217
+ )
218
+
219
+
220
+ async def compile_with_args(
221
+ test_name: str,
222
+ expect_compile_failure: bool,
223
+ args: List[str],
224
+ timeout: int,
225
+ ) -> TestCaseResult:
226
+ """
227
+ Run hermesc with given arguments. Returns the TestCaseResult.
228
+ """
229
+
230
+ proc = await create_subprocess_exec(
231
+ *args,
232
+ stderr=subprocess.PIPE,
233
+ stdout=subprocess.PIPE,
234
+ )
235
+ stdout, stderr = (None, None)
236
+ t_start = time.monotonic()
237
+ try:
238
+ (stdout, stderr) = await wait_for(proc.communicate(), timeout=timeout)
239
+ except TimeoutError:
240
+ duration = time.monotonic() - t_start
241
+ msg = f"FAIL: Compilation timed out, args: {args}"
242
+ output = f"Run command: {' '.join(args)}"
243
+ proc.kill()
244
+ return TestCaseResult(
245
+ test_name, TestResultCode.COMPILE_TIMEOUT, msg, output, duration
246
+ )
247
+ duration = time.monotonic() - t_start
248
+
249
+ output = f"Run command: {' '.join(args)}\n"
250
+ if stdout:
251
+ output += f"stdout:\n {stdout.decode('utf-8')}"
252
+ if stderr:
253
+ output += f"stderr:\n {stderr.decode('utf-8')}"
254
+
255
+ # Check if the compilation succeeded.
256
+ if proc.returncode:
257
+ # Negative return code means that the subprocess is terminated
258
+ # by a system signal (e.g., -11 for SIGSEGV).
259
+ # Hermes may exit with return code:
260
+ # - 0 for success
261
+ # - 1-8 for compiler failure (represented in the CompileStatus
262
+ # enum)
263
+ # - -1 (converted to 255) in HVM when failed to open file
264
+ # - 1 for all other failures (e.g., used in hermes_fatal())
265
+ # We want to treat terminated exeuction differently to capture
266
+ # bugs like memory corruption.
267
+ if proc.returncode < 0:
268
+ msg = f"FAIL: Execution terminated with {proc.returncode}"
269
+ return TestCaseResult(
270
+ test_name, TestResultCode.COMPILE_FAILED, msg, output, duration
271
+ )
272
+
273
+ # If compilation failed and it's not expected, consider it a failure.
274
+ if not expect_compile_failure:
275
+ msg = f"FAIL: Compilation failed with command: {args}"
276
+ return TestCaseResult(
277
+ test_name, TestResultCode.COMPILE_FAILED, msg, output, duration
278
+ )
279
+ else:
280
+ # If the compliation succeeded but a compilation failure is expected,
281
+ # it's also considered a failure.
282
+ if expect_compile_failure:
283
+ msg = "FAIL: Compilation failure expected on with Hermes"
284
+ return TestCaseResult(
285
+ test_name, TestResultCode.COMPILE_FAILED, msg, output, duration
286
+ )
287
+
288
+ return TestCaseResult(
289
+ test_name, TestResultCode.TEST_PASSED, "Compile passed", output, duration
290
+ )
291
+
292
+
293
+ async def compile_and_run_single(
294
+ js_source_file: PathT,
295
+ compile_run_args: CompileRunArgs,
296
+ ) -> TestCaseResult:
297
+ """
298
+ Compile and run the given source file, return the corresponding
299
+ TestCaseResult.
300
+ """
301
+
302
+ expected_failure_phase = (
303
+ compile_run_args.expected_failure["phase"]
304
+ if compile_run_args.expected_failure
305
+ else ""
306
+ )
307
+ if compile_run_args.lazy:
308
+ # In lazy mode, run the source file directly.
309
+ return await run(
310
+ os.path.basename(js_source_file),
311
+ js_source_file,
312
+ expected_failure_phase,
313
+ compile_run_args,
314
+ )
315
+
316
+ file_to_run = f"{js_source_file}.out"
317
+ if compile_run_args.shermes:
318
+ hermes_exe = shutil.which(
319
+ os.path.join(compile_run_args.binary_directory, "shermes")
320
+ )
321
+ else:
322
+ hermes_exe = shutil.which(
323
+ os.path.join(compile_run_args.binary_directory, "hermes")
324
+ )
325
+ cmd_args = [
326
+ hermes_exe,
327
+ str(js_source_file),
328
+ "-o" if compile_run_args.shermes else "-out",
329
+ file_to_run,
330
+ ] + COMPILE_ARGS
331
+ if not compile_run_args.shermes:
332
+ cmd_args.append("-emit-binary")
333
+
334
+ if compile_run_args.extra_compile_vm_args:
335
+ cmd_args += compile_run_args.extra_compile_vm_args.compile_args
336
+
337
+ if StrictMode.STRICT in compile_run_args.strict_mode:
338
+ cmd_args.append("-strict")
339
+
340
+ # Whether compilation is expected to fail.
341
+ expect_compile_failure = expected_failure_phase == "parse"
342
+ if compile_run_args.opt:
343
+ cmd_args.append("-O")
344
+ else:
345
+ cmd_args.append("-O0")
346
+ # If compiling failed (not as expected), return the result immediately.
347
+ compile_result = await compile_with_args(
348
+ compile_run_args.test_name,
349
+ expect_compile_failure,
350
+ cmd_args,
351
+ compile_run_args.timeout,
352
+ )
353
+ if compile_result.code.is_failure:
354
+ return compile_result
355
+ output = compile_result.output
356
+ total_duration = compile_result.duration
357
+
358
+ # If compilation failure is not expected, we should have the bytecode
359
+ # ready to run.
360
+ if not expect_compile_failure:
361
+ # If run failed (not as expected), return it immediately
362
+ run_result = await run(
363
+ os.path.basename(js_source_file),
364
+ file_to_run,
365
+ expected_failure_phase,
366
+ compile_run_args,
367
+ )
368
+ total_duration += run_result.duration
369
+ if run_result.code.is_failure:
370
+ run_result.duration = total_duration
371
+ return run_result
372
+ output += run_result.output
373
+ return TestCaseResult(
374
+ compile_run_args.test_name,
375
+ TestResultCode.TEST_PASSED,
376
+ "Compile and Run passed",
377
+ output,
378
+ total_duration,
379
+ )
380
+
381
+
382
+ async def compile_and_run(
383
+ js_source_files: List[PathT],
384
+ compile_run_args: CompileRunArgs,
385
+ ) -> TestCaseResult:
386
+ """
387
+ Run the generated source files with async subprocess and return the
388
+ result.
389
+ """
390
+
391
+ output = ""
392
+ total_duration = 0.0
393
+ for js_source_file in js_source_files:
394
+ # If any file is not passed, return the result immediately.
395
+ result = await compile_and_run_single(
396
+ js_source_file,
397
+ compile_run_args,
398
+ )
399
+ total_duration += result.duration
400
+ if result.code.is_failure:
401
+ result.duration = total_duration
402
+ return result
403
+ output = f"Compile and Run {js_source_file}:\n{result.output}"
404
+
405
+ # All files in js_source_files passed
406
+ return TestCaseResult(
407
+ compile_run_args.test_name,
408
+ TestResultCode.TEST_PASSED,
409
+ "PASS",
410
+ output,
411
+ total_duration,
412
+ )
413
+
414
+
415
+ async def run_hermes_simple(
416
+ hermes_exe: PathT,
417
+ test_name: str,
418
+ args: List[str],
419
+ timeout: int,
420
+ ) -> TestCaseResult:
421
+ """
422
+ Simply invoke hermes on a JS file with given arguments and return the
423
+ output. This is unlike the above `compile_and_run()` function, which does
424
+ various checking such as compilation failure and runtim failure.
425
+ """
426
+ proc = await create_subprocess_exec(
427
+ hermes_exe,
428
+ *args,
429
+ stdout=subprocess.PIPE,
430
+ stderr=subprocess.PIPE,
431
+ )
432
+ stdout, stderr = (None, None)
433
+ t_start = time.monotonic()
434
+ try:
435
+ (stdout, stderr) = await wait_for(proc.communicate(), timeout=timeout)
436
+ except TimeoutError:
437
+ duration = time.monotonic() - t_start
438
+ proc.kill()
439
+ msg = "FAIL: Hermes timeout"
440
+ details = f"Run command: {' '.join(args)}"
441
+ return TestCaseResult(
442
+ test_name, TestResultCode.COMPILE_TIMEOUT, msg, details, duration
443
+ )
444
+ duration = time.monotonic() - t_start
445
+
446
+ if proc.returncode:
447
+ msg = "FAIL: Hermes failed to run"
448
+ details = f"Run command: {' '.join(args)}\n"
449
+ details += f"Return code: {proc.returncode}\n"
450
+ details += f"stdout:\n {stdout.decode('utf-8')}"
451
+ details += f"stderr:\n {stderr.decode('utf-8')}"
452
+ return TestCaseResult(
453
+ test_name, TestResultCode.TEST_FAILED, msg, details, duration
454
+ )
455
+
456
+ # Return the evaluated output (could be JS execution result or dumped AST).
457
+ return TestCaseResult(
458
+ test_name,
459
+ TestResultCode.TEST_PASSED,
460
+ "PASS: ",
461
+ stdout.decode("utf-8").strip(),
462
+ duration,
463
+ )
464
+
465
+
466
+ async def generate_ast(
467
+ test_name: str,
468
+ test_file: PathT,
469
+ binary_path: PathT,
470
+ is_flow: bool,
471
+ transformed: bool,
472
+ timeout: int,
473
+ ) -> TestCaseResult:
474
+ """
475
+ Generate the AST for given source file.
476
+
477
+ Returns:
478
+ (TestResultCode.TEST_PASSED, JSON) if success.
479
+ (TestResultCode.TEST_FAILED, message) if Hermes failed.
480
+ (TestResultCode.COMPILE_TIMEOUT, message) if Hermes timeout.
481
+ """
482
+ args = []
483
+ if is_flow:
484
+ args.append("-parse-flow")
485
+ args.append("-Xparse-component-syntax")
486
+ args.append("-parse-jsx")
487
+ args.append("-Xinclude-empty-ast-nodes")
488
+ args.append("-Xparse-flow-match")
489
+ elif "JSX" in test_file:
490
+ args.append("--parse-jsx")
491
+ args.append("-dump-transformed-ast" if transformed else "-dump-ast")
492
+ hermes_exe = shutil.which(os.path.join(binary_path, "hermes"))
493
+
494
+ # ".source.js" files has the format of "var source = \"...\";", and
495
+ # the value of the 'source' variable should be the input to the parser.
496
+ # So we evaluate the source with Hermes first and then parse the output.
497
+ if test_file.endswith(".source.js"):
498
+ with open(test_file, "rb") as f, tempfile.NamedTemporaryFile() as to_evaluate:
499
+ # append to the original source to print the 'source' variable.
500
+ for line in f:
501
+ to_evaluate.write(line)
502
+ to_evaluate.write(b"print(source);")
503
+ to_evaluate.flush()
504
+ with tempfile.NamedTemporaryFile() as evaluated:
505
+ # evaluate the source to get the actual test input.
506
+ eval_args = [to_evaluate.name]
507
+ result = await run_hermes_simple(
508
+ hermes_exe, test_name, eval_args, timeout
509
+ )
510
+ if result.code != TestResultCode.TEST_PASSED:
511
+ return result
512
+ eval_duration = result.duration
513
+ # get rid of the newline added by print().
514
+ evaluated.write(result.output.encode("utf-8"))
515
+ evaluated.flush()
516
+ # run the test through Hermes parser.
517
+ ast_result = await run_hermes_simple(
518
+ hermes_exe, test_name, args + [evaluated.name], timeout
519
+ )
520
+ ast_result.duration += eval_duration
521
+ return ast_result
522
+ return await run_hermes_simple(hermes_exe, test_name, args + [test_file], timeout)