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,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: building-and-running
|
|
3
|
+
title: Building and Running
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This document describes how to build and run Hermes as a standalone compiler and VM. To use Hermes in the context of a React Native app, see the [React Native](https://reactnative.dev/docs/getting-started) documentation.
|
|
7
|
+
|
|
8
|
+
## Dependencies
|
|
9
|
+
|
|
10
|
+
Hermes is a C++17 project. clang, gcc, and Visual C++ are supported, but clang is strongly recommended: gcc produces noticeably worse code. Hermes also requires cmake, git, Python. It builds with [CMake](https://cmake.org) and [ninja](https://ninja-build.org).
|
|
11
|
+
|
|
12
|
+
The Hermes REPL will also use libreadline, if available.
|
|
13
|
+
|
|
14
|
+
ICU is not required: Unicode case conversion, normalization, collation and date formatting are all self-contained. `-DHERMES_ENABLE_INTL=ON` (off by default) is supported only on Apple and Android, which use their platform's own Intl implementation; configuring it on any other platform fails.
|
|
15
|
+
|
|
16
|
+
To install dependencies on Ubuntu:
|
|
17
|
+
|
|
18
|
+
apt install clang build-essential cmake git ninja-build python3 tzdata libreadline-dev
|
|
19
|
+
|
|
20
|
+
On Arch Linux:
|
|
21
|
+
|
|
22
|
+
pacman -S clang cmake git ninja python zip readline
|
|
23
|
+
|
|
24
|
+
On Mac via Homebrew:
|
|
25
|
+
|
|
26
|
+
brew install cmake git ninja
|
|
27
|
+
|
|
28
|
+
## Building on Linux and macOS
|
|
29
|
+
|
|
30
|
+
Hermes will place its build files in the current directory by default.
|
|
31
|
+
You can also give explicit source and build directories, use `--help` on the build scripts to see how.
|
|
32
|
+
|
|
33
|
+
Create a base directory to work in, e.g. `~/workspace`, and cd into it.
|
|
34
|
+
(Tip: avoid naming it `hermes`, as `hermes` will be one of several subdirectories in the workspace).
|
|
35
|
+
After `cd`ing, follow the steps below to generate the Hermes build system:
|
|
36
|
+
|
|
37
|
+
git clone https://github.com/facebook/hermes.git
|
|
38
|
+
cmake -S hermes -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug \
|
|
39
|
+
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
|
40
|
+
|
|
41
|
+
The build system has now been generated in the `build` directory. To perform the build:
|
|
42
|
+
|
|
43
|
+
cmake --build ./build
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Release Build
|
|
47
|
+
|
|
48
|
+
The above instructions create an unoptimized debug build. The `-DCMAKE_BUILD_TYPE=Release` flag will create a release build:
|
|
49
|
+
|
|
50
|
+
cmake -S hermes -B build_release -G Ninja -DCMAKE_BUILD_TYPE=Release \
|
|
51
|
+
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
|
|
52
|
+
cmake --build ./build_release
|
|
53
|
+
|
|
54
|
+
## Building on Windows
|
|
55
|
+
|
|
56
|
+
To build on Windows using Visual Studio with a checkout in the `hermes` directory:
|
|
57
|
+
|
|
58
|
+
cmake -S hermes -B build -G 'Visual Studio 16 2019'
|
|
59
|
+
cmake --build ./build
|
|
60
|
+
|
|
61
|
+
## Running Hermes
|
|
62
|
+
|
|
63
|
+
The primary binary is the `hermes` tool, which will be found at `build/bin/hermes`. This tool compiles JavaScript to Hermes bytecode. It can also execute JavaScript, from source or bytecode or be used as a REPL.
|
|
64
|
+
|
|
65
|
+
### Executing JavaScript with Hermes
|
|
66
|
+
|
|
67
|
+
hermes test.js
|
|
68
|
+
|
|
69
|
+
## Compiling and Executing JavaScript with Bytecode
|
|
70
|
+
|
|
71
|
+
hermes -emit-binary -out test.hbc test.js
|
|
72
|
+
hermes test.hbc
|
|
73
|
+
|
|
74
|
+
## Running Tests
|
|
75
|
+
|
|
76
|
+
To run the Hermes test suite:
|
|
77
|
+
|
|
78
|
+
cmake --build ./build --target check-hermes
|
|
79
|
+
|
|
80
|
+
To run Hermes against the test262 suite, you need to have a Hermes binary built
|
|
81
|
+
already and a clone of the [test262 repo](https://github.com/tc39/test262/):
|
|
82
|
+
|
|
83
|
+
hermes/utils/testsuite/run_testsuite.py -b <hermes_build> <test262>
|
|
84
|
+
|
|
85
|
+
E.g. if we configured at `~/hermes_build` (i.e. `~/hermes_build/bin/hermes` is
|
|
86
|
+
an executable) and cloned test262 at `~/test262`, then perform:
|
|
87
|
+
|
|
88
|
+
hermes/utils/testsuite/run_testsuite.py -b ~/hermes_build ~/test262/test
|
|
89
|
+
|
|
90
|
+
Note that you can also only test against part of a test suite, e.g. to run the
|
|
91
|
+
Intl402 subset of the test262, you can specifiy a subdir:
|
|
92
|
+
|
|
93
|
+
hermes/utils/testsuite/run_testsuite.py -b ~/hermes_build ~/test262/test/intl402
|
|
94
|
+
|
|
95
|
+
## Formatting Code
|
|
96
|
+
|
|
97
|
+
To automatically format all your changes, you will need `clang-format`, then
|
|
98
|
+
simply run:
|
|
99
|
+
|
|
100
|
+
hermes/utils/format.sh
|
|
101
|
+
|
|
102
|
+
## AddressSanitizer (ASan) Build
|
|
103
|
+
|
|
104
|
+
The `-HERMES_ENABLE_ADDRESS_SANITIZER=ON` flag will create a ASan build:
|
|
105
|
+
|
|
106
|
+
git clone https://github.com/facebook/hermes.git
|
|
107
|
+
cmake -S hermes -B asan_build -G Ninja -D HERMES_ENABLE_ADDRESS_SANITIZER=ON -DCMAKE_BUILD_TYPE=Debug
|
|
108
|
+
cmake --build ./asan_build
|
|
109
|
+
|
|
110
|
+
You can verify the build by looking for `asan` symbols in the `hermes` binary:
|
|
111
|
+
|
|
112
|
+
nm asan_build/bin/hermes | grep asan
|
|
113
|
+
|
|
114
|
+
### Other Tools
|
|
115
|
+
|
|
116
|
+
In addition to `hermes`, the following tools will be built:
|
|
117
|
+
|
|
118
|
+
- `hdb`: JavaScript command line debugger
|
|
119
|
+
- `hbcdump`: Hermes bytecode disassembler
|
|
120
|
+
- `hermesc`: Standalone Hermes compiler. This can compile JavaScript to Hermes bytecode, but does not support executing it.
|
|
121
|
+
- `hvm`: Standalone Hermes VM. This can execute Hermes bytecode, but does not support compiling it.
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: coding-standards
|
|
3
|
+
title: Coding Standards
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Hermes Coding Standards
|
|
7
|
+
|
|
8
|
+
This document provides guidance for the kind of code that should go in to
|
|
9
|
+
the Hermes project. The rules in this document will allow us to scale the
|
|
10
|
+
project and ensure that the code base remains readable and maintainable.
|
|
11
|
+
|
|
12
|
+
### Code Quality
|
|
13
|
+
|
|
14
|
+
Code quality standards in Hermes are very strict. While we welcome and
|
|
15
|
+
support community contributions, expect scrutiny on changes that do not
|
|
16
|
+
meet the following standards:
|
|
17
|
+
|
|
18
|
+
- Contributions must be well understood and explained in PRs. We cannot accept
|
|
19
|
+
and maintain contributions that we don't fully understand.
|
|
20
|
+
- Contributions should not use shortcuts or hacks in the name of expediency.
|
|
21
|
+
Hermes is a critical piece of infrastructure for many apps and systems, and
|
|
22
|
+
such code creates ongoing work for maintainers.
|
|
23
|
+
|
|
24
|
+
### Language Features
|
|
25
|
+
|
|
26
|
+
Hermes uses C++14.
|
|
27
|
+
|
|
28
|
+
Exceptions or RTTI are prohibited, except where needed for
|
|
29
|
+
interfacing with other software that requires them.
|
|
30
|
+
|
|
31
|
+
GCC language extensions are discouraged for compatibility with MSVC, or they
|
|
32
|
+
should be conditionally enabled.
|
|
33
|
+
|
|
34
|
+
### Code Style
|
|
35
|
+
|
|
36
|
+
TODO: This should be sorted and expanded.
|
|
37
|
+
|
|
38
|
+
- Every declaration must have a doc-comment.
|
|
39
|
+
|
|
40
|
+
Naming
|
|
41
|
+
|
|
42
|
+
- Member variables use a "\_" suffix unless they are public, where it is
|
|
43
|
+
acceptable to omit the suffix.
|
|
44
|
+
- Classes use pascal case (`MyClass`).
|
|
45
|
+
- Functions and methods use camel case (`myFunc`).
|
|
46
|
+
- Variables generally use camel case (`myVar`). We also allow "abbreviated
|
|
47
|
+
case": "basic block" can be abbreviated as `BB`, "instruction" can be
|
|
48
|
+
abbreviated as `I`, etc.
|
|
49
|
+
- Constants use either capitalized snake case (`MY_CONST`) or camel case with
|
|
50
|
+
"k" prefix (`kMyConst`).
|
|
51
|
+
|
|
52
|
+
Misc
|
|
53
|
+
|
|
54
|
+
- `struct` should be used only when it is a true POD - there are no constructors
|
|
55
|
+
or destructors of any kind. For everything else use `class`.
|
|
56
|
+
- Inlining: Only completely trivial (one line) methods can be defined in the
|
|
57
|
+
class body. The rest should be declared as `inline` both at the declaration and
|
|
58
|
+
the definition.
|
|
59
|
+
|
|
60
|
+
### Small incremental changes
|
|
61
|
+
|
|
62
|
+
The project is developed using small incremental changes. These changes can be
|
|
63
|
+
small bug fixes or minor tweaks. Other times, these changes are small steps
|
|
64
|
+
along the path to reaching larger stated goals. Long-term development branches
|
|
65
|
+
suffer from many problems, including the lack of visibility, difficulty of code
|
|
66
|
+
review, lack of testing of the branch and merge difficulty.
|
|
67
|
+
|
|
68
|
+
Commits that go into the project need to be reviewable. This means that commits
|
|
69
|
+
need to be relatively small, well documented and self contained.
|
|
70
|
+
|
|
71
|
+
### Add tests
|
|
72
|
+
|
|
73
|
+
Functional changes to the compiler need to include a testcase. Unit tests and
|
|
74
|
+
regression tests are critical to the qualification of the compiler. Every bug
|
|
75
|
+
fix needs to include a testcase.
|
|
76
|
+
|
|
77
|
+
Reduce test cases as much as possible! It is unacceptable to commit big programs
|
|
78
|
+
because they do not describe the essence of the failure, they are fragile, and
|
|
79
|
+
they slow testing down. Tests need to be short and focused.
|
|
80
|
+
|
|
81
|
+
### Format your code
|
|
82
|
+
|
|
83
|
+
clang-format is required to enforce code style and formatting.
|
|
84
|
+
Commits that only change the formatting of code should go in independent of
|
|
85
|
+
functional changes.
|
|
86
|
+
|
|
87
|
+
### Commit messages
|
|
88
|
+
|
|
89
|
+
Here are some guidelines about the format of the commit message:
|
|
90
|
+
|
|
91
|
+
Separate the commit message into a single-line title and a separate body that
|
|
92
|
+
describes the change. Make the title short (80 chars) and readable. In changes
|
|
93
|
+
that are restricted to a specific part of the code, include a [tag] at the start
|
|
94
|
+
of the line in square brackets—for example, “[docs] ... ”.
|
|
95
|
+
|
|
96
|
+
If the commit fixes an issue in the bug tracking system, include a link or a
|
|
97
|
+
task number.
|
|
98
|
+
|
|
99
|
+
When reverting a change make sure to add a short note that describes why the
|
|
100
|
+
patch is being reverted.
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
### Code review
|
|
104
|
+
|
|
105
|
+
The project relies heavily on code review to maintain the software quality.
|
|
106
|
+
|
|
107
|
+
Review other people’s changes! Anybody is allowed to review code and comment
|
|
108
|
+
on patches.
|
|
109
|
+
|
|
110
|
+
All changes, by all developers, must be reviewed before they are committed to
|
|
111
|
+
the repository. Smaller changes (if the developer is the de-facto owner of the
|
|
112
|
+
code base) can be reviewed after being committed.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: cross-compilation
|
|
3
|
+
title: Cross Compilation
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This document describes how to build Hermes in a cross compilation setting, e.g.
|
|
7
|
+
building for Android, WASM (with Emscripten), or any other platforms different
|
|
8
|
+
than the host development machines.
|
|
9
|
+
|
|
10
|
+
## Android compilation
|
|
11
|
+
|
|
12
|
+
Edit paths:
|
|
13
|
+
* `CMAKE_TOOLCHAIN_FILE` should point to whatever `android_ndk` you need.
|
|
14
|
+
* `IMPORT_HOST_COMPILERS` should point to whatever SH host build you want to use for InternalBytecode compilation.
|
|
15
|
+
* `-B` path is to your build directory, or omit it to build in current directory.
|
|
16
|
+
|
|
17
|
+
Ensure that `CMAKE_CXX_FLAGS_RELEASE` is `-O3 -DNDEBUG` if you're in Release mode.
|
|
18
|
+
|
|
19
|
+
Then you can use `adb push` to copy any files into `/data/local/tmp/` on an Android device and run it there using 'adb shell'.
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
cmake ~/fbsource/xplat/static_h \
|
|
23
|
+
-B ~/static_h/build_android \
|
|
24
|
+
-DHERMES_FACEBOOK_BUILD=OFF \
|
|
25
|
+
-DCMAKE_BUILD_TYPE=Release \
|
|
26
|
+
-DCMAKE_TOOLCHAIN_FILE=/opt/android_ndk/r17fb2/build/cmake/android.toolchain.cmake \
|
|
27
|
+
-DHERMES_UNICODE_LITE=ON \
|
|
28
|
+
-DIMPORT_HOST_COMPILERS=~/static_h/build_release/ImportHostCompilers.cmake \
|
|
29
|
+
-DHERMES_ENABLE_DEBUGGER=OFF \
|
|
30
|
+
-DANDROID_ABI=arm64-v8a -G Ninja
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`HERMES_UNICODE_LITE=ON` selects the self-contained no-op Unicode backend.
|
|
34
|
+
Without it, an Android build uses the Java backend, which needs a JVM at
|
|
35
|
+
runtime and so is not usable from a binary pushed with `adb push` and run
|
|
36
|
+
under `adb shell`.
|
|
37
|
+
|
|
38
|
+
No ICU is needed here, or in any cross-compilation target. Hermes no
|
|
39
|
+
longer depends on ICU at all. The `platform_unicode` layer -- case
|
|
40
|
+
conversion, normalization, collation and date formatting -- is
|
|
41
|
+
self-contained, and the ICU-backed Intl backend has been removed.
|
|
42
|
+
`-DHERMES_ENABLE_INTL=ON` is now supported only on Apple and Android,
|
|
43
|
+
which have their own native Intl implementations.
|
|
44
|
+
|
|
45
|
+
## Emscripten
|
|
46
|
+
|
|
47
|
+
For Emscripten, you can find an example from the `test-emscripten` job from `hermes/.circleci/config.yml`. Also see more details at [Building with Emscripten](./Emscripten.md)
|
|
48
|
+
|
|
49
|
+
## Compiling and Running ARM32 Static Hermes from ARM64
|
|
50
|
+
|
|
51
|
+
On the ARM64 Linux machine you need clang-16 and lld-16.
|
|
52
|
+
|
|
53
|
+
On the ARM64 Linux machine on which you're compiling:
|
|
54
|
+
```
|
|
55
|
+
# Add architecture
|
|
56
|
+
dpkg --add-architecture armhf
|
|
57
|
+
# Update
|
|
58
|
+
sudo apt update
|
|
59
|
+
# For compiling
|
|
60
|
+
sudo apt install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf libc6:armhf libstdc++6:armhf
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
If you're executing on an ARM64 machine, set it up as well:
|
|
64
|
+
```
|
|
65
|
+
# Add architecture
|
|
66
|
+
dpkg --add-architecture armhf
|
|
67
|
+
# Update
|
|
68
|
+
sudo apt update
|
|
69
|
+
# For running
|
|
70
|
+
sudo apt install libc6:armhf libstdc++6:armhf
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
There's a CMake toolchain file in the source directory that will then set up Clang to compile for ARM32. To use it:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
CC=clang-16 CXX=clang++-16 cmake ${SRC_DIR?} -GNinja -DCMAKE_TOOLCHAIN_FILE=${SRC_DIR?}/armhf/arm-linux-gnueabihf.toolchain.cmake -DCMAKE_BUILD_TYPE=Release
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Then you can run `ninja` and build 32-bit binaries and libraries. They'll work on the machine on which you're executing.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: design
|
|
3
|
+
title: Design Overview
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This is the design document for the Hermes Engine.
|
|
7
|
+
|
|
8
|
+
## JSON estree parser
|
|
9
|
+
|
|
10
|
+
## JavaScript parser
|
|
11
|
+
|
|
12
|
+
## IRGen
|
|
13
|
+
|
|
14
|
+
## Hermes Bytecode Generator
|
|
15
|
+
|
|
16
|
+
The Hermes bytecode generator is responsible for translating the
|
|
17
|
+
Hermes high-level IR into Hermes bytecode. This section only describes the
|
|
18
|
+
generation of the opcode stream, not the whole bundle file format that is
|
|
19
|
+
described in a separate document.
|
|
20
|
+
|
|
21
|
+
The bytecode is a register-based bytecode. The number of registers is
|
|
22
|
+
infinite, but there are some restrictions on the registers. For example,
|
|
23
|
+
registers that are allocated to call instructions must be consecutive, and most
|
|
24
|
+
instructions only accept an 8-bit register index.
|
|
25
|
+
|
|
26
|
+
The first phase of bytecode generation is lowering of some instructions to
|
|
27
|
+
target-specific instructions. Next, the register allocator allocates
|
|
28
|
+
registers for each instruction in the lowered IR. Notice that the Hermes IR
|
|
29
|
+
has a single return value per instruction. We model multiple return values or
|
|
30
|
+
in-out variables using alloc-stack instructions.
|
|
31
|
+
|
|
32
|
+
One interesting design decision here is that register allocation happens on the
|
|
33
|
+
IR prior to instruction selection. Performing register allocation prior to
|
|
34
|
+
lowering is often done in JIT compilers where the lowering phase is trivial and
|
|
35
|
+
close to a 1:1 translation between high-level IR and the low-level target IR.
|
|
36
|
+
The lowered Hermes IR is very close to the bytecode format with a few
|
|
37
|
+
exceptions: the handling of variables, the representation of constants and the
|
|
38
|
+
representation of stack-allocated variables. Stack allocated registers are
|
|
39
|
+
implemented as values that are pinned into a specific registers (at least during
|
|
40
|
+
the lifetime of the stack allocation). Load/Store instructions from the stack
|
|
41
|
+
allocation values are lowered into MOV instructions.
|
|
42
|
+
|
|
43
|
+
The current register allocation implementation is a very simple linear scan. The
|
|
44
|
+
allocation has four main steps. First, we number the instructions in the
|
|
45
|
+
functions, and traverse the basic blocks in the function in reverse-post-order
|
|
46
|
+
scan. Second, we calculate the liveness graph of the result of each
|
|
47
|
+
instruction in the function. We propagate this information across the function
|
|
48
|
+
with a simple data-flow scan. Third, we calculate the liveness interval of each
|
|
49
|
+
instruction. Finally, we scan the instructions in the function one by one and
|
|
50
|
+
assign registers. We maintain a list of currently live intervals that correspond
|
|
51
|
+
to the register file. We represent the register file with a simple bit vector
|
|
52
|
+
and prefer to allocate registers from the beginning of the file. When we run out
|
|
53
|
+
of registers we simply allocate new registers. When we reach instructions that
|
|
54
|
+
require target-specific handling we call the backend to fill in the details. For
|
|
55
|
+
example, the Hermes backend handles call instructions in a special way. At the
|
|
56
|
+
moment we do not coalesce values and our PHI nodes generate two MOVs.
|
|
57
|
+
|
|
58
|
+
After register allocation we lower the Hermes IR into pseudo opcodes using a
|
|
59
|
+
one-to-one or one-to-many translation. There are a few interesting optimizations
|
|
60
|
+
during this translation. The first interesting optimization is the optimization
|
|
61
|
+
that eliminates redundant constant jumps. It is possible to eliminate many
|
|
62
|
+
branches by scheduling basic blocks in a way that maximizes the number of
|
|
63
|
+
fall-through opportunities. The current algorithm uses a greedy approach where
|
|
64
|
+
we emit basic blocks in some order (that starts with the entry block), and
|
|
65
|
+
attempt to place the fall-through destination right after terminators that
|
|
66
|
+
branch (conditional and unconditional branches) if the block has not been
|
|
67
|
+
generated already. In the future we may need to investigate if this greedy
|
|
68
|
+
algorithm is optimal.
|
|
69
|
+
|
|
70
|
+
When we generate the opcode stream we need to encode jump targets. However, when
|
|
71
|
+
we emit opcodes that refer to jump destinations that we have not yet emitted
|
|
72
|
+
then we do not know the address of the destination. We solve this problem by
|
|
73
|
+
emitting a dummy value and keeping a side-table that saves the location that we
|
|
74
|
+
need to patch. When we finish emitting all of the opcodes we scan the side table
|
|
75
|
+
and patch all of the locations that refer to previously unresolved addresses.
|
|
76
|
+
|
|
77
|
+
### Hermes Bytecode Instructions
|
|
78
|
+
|
|
79
|
+
Hermes bytecode adopts variable-length instructions. Each operand to a bytecode
|
|
80
|
+
instruction has a fixed-type and width, defined by the opcode. For instance,
|
|
81
|
+
Jmp takes a 1-byte offset as the jump target, while JmpLong takes a 4-byte
|
|
82
|
+
offset as the jump target. Fixed-type/width instructions allow us to decode
|
|
83
|
+
them efficiently in the interpreter.
|
|
84
|
+
However we are trading off with an increasing number of
|
|
85
|
+
opcodes to handle different operand widths (e.g. two Jmp opcodes instead of
|
|
86
|
+
one). We believe that we are able to avoid opcode explosion by generating the
|
|
87
|
+
code smartly. A full list of Hermes bytecode opcodes can be found in
|
|
88
|
+
BytecodeList.def. There are a few interesting design decisions worth mentioning
|
|
89
|
+
here:
|
|
90
|
+
- Registers: We discovered that in all of the Facebook mobile JS code as well as
|
|
91
|
+
majority of external benchmarks, no function ever uses more than 256 registers.
|
|
92
|
+
Hence we always use 1-byte to represent register index, which will be most
|
|
93
|
+
efficient for the normal cases. Spilling is implemented via MovLong which
|
|
94
|
+
supports 32 bit register indices.
|
|
95
|
+
- Constants: we achieve constant loading fully through instructions.
|
|
96
|
+
For fixed-value constants such as undefined, null, true and false, we introduce
|
|
97
|
+
a corresponding load opcode for each of them into a register (e.g.
|
|
98
|
+
LoadConstUndefined `dstReg`); For 32-bit integers, we introduce LoadConstInt
|
|
99
|
+
opcode, which takes a 4-bytes immediate value and load it into a register; for
|
|
100
|
+
doubles, we introduce LoadConstDouble, which takes a 8-bytes immediate value
|
|
101
|
+
and load it into a register; finally for strings, we introduce LoadConstString,
|
|
102
|
+
which takes an index to the string table from which to load into a register.
|
|
103
|
+
Doing so can significantly reduce the size of the bytecode, however it does
|
|
104
|
+
introduce a few more opcodes which could slow down the interpreter.
|
|
105
|
+
- Non-local Variable Access: Local variables are translated to registers.
|
|
106
|
+
Non-local variables are variables from different scopes/environments. Without a
|
|
107
|
+
compiler, accessing non-local variables in JavaScript usually means a scope
|
|
108
|
+
lookup (i.e. locating the closest scope in the scope chain that defines the
|
|
109
|
+
variable), followed by a symbol lookup in that scope.
|
|
110
|
+
However with a compiler, it is possible to statically determine the scope of
|
|
111
|
+
every variable, and hence there is no need for a real scope lookup. In Hermes
|
|
112
|
+
backend, for each non-local variable access, we simply calculate the delta
|
|
113
|
+
between the defining scope and the current scope, and locate such scope using
|
|
114
|
+
the delta (i.e. number of times the VM needs to follow the scope chain)
|
|
115
|
+
directly. Furthermore, since we know exactly what and how many variables are
|
|
116
|
+
defined in each scope (except the global scope), we skip the symbol lookup in
|
|
117
|
+
the scope but instead using a direct index access to retrieve such variable in
|
|
118
|
+
the located scope. We believe that skipping both scope lookups and symbol
|
|
119
|
+
lookups can significantly improve the runtime performance.
|
|
120
|
+
|
|
121
|
+
### Bytecode File Format
|
|
122
|
+
|
|
123
|
+
The bytecode file contains the bytecode as well as necessary metadata and
|
|
124
|
+
auxiliary data sections for the VM to execute properly. The file format is
|
|
125
|
+
defined in BytecodeFileFormat.h, and structures as following:
|
|
126
|
+
- FILE HEADER: The file header contains the MAGIC, the current format version,
|
|
127
|
+
and a list of global metadata, including the file size, offset of the function
|
|
128
|
+
header table, offset of the string table, index of the global code and number
|
|
129
|
+
of functions.
|
|
130
|
+
- FUNCTION HEADER TABLE: This is a list of function headers. Each function header
|
|
131
|
+
contains metadata of a function, such as the offset of the function bytecode in
|
|
132
|
+
the file, number of parameters, size of the frame/environment, size of the
|
|
133
|
+
bytecode and etc. This list also naturally assigns an index to each function in
|
|
134
|
+
the file, which makes access to each function convenient in the VM.
|
|
135
|
+
- STRING TABLE&STORAGE: All the strings used in all functions are uniqued and
|
|
136
|
+
stored in the section to avoid redundant string storage. This section contains
|
|
137
|
+
two parts: The string storage, which is a long sequence of raw characters; the
|
|
138
|
+
string table, which is a list of pairs, each pair represents a string through
|
|
139
|
+
the offset to the string storage and the length of the string. The string table
|
|
140
|
+
also naturally assigns an index to each unique string, which makes it
|
|
141
|
+
convenient to refer to the strings in the bytecode.
|
|
142
|
+
- FUNCTION BYTECODES: This section is the core section of the bytecode file,
|
|
143
|
+
containing a list of compiled function body. The function body contains its
|
|
144
|
+
executable bytecode, along with a few tables that are used by the bytecode,
|
|
145
|
+
including the exception handler table (tells where to jump to when exception
|
|
146
|
+
happens), the array buffer (used to initialize constant arrays). Likely there
|
|
147
|
+
will be a few more tables coming to support RegExp and debug information.
|
|
148
|
+
|
|
149
|
+
### Serialization / Deserialization
|
|
150
|
+
|
|
151
|
+
Because the Hermes backend is in the same codebase as the VM, there are
|
|
152
|
+
opportunities for us to share code between them. In particular, we want the
|
|
153
|
+
serialization in the backend to be able to share the same target data structure
|
|
154
|
+
as the deserialization in the VM. This introduces some interesting questions:
|
|
155
|
+
- How can we share data/code without having to link too much code on each side?
|
|
156
|
+
- How to avoid data copies during both serialization and deserialization?
|
|
157
|
+
|
|
158
|
+
We introduce two design pieces to achieve code sharing efficiently:
|
|
159
|
+
- Generator: During serialization, we often need many auxiliary data and
|
|
160
|
+
functionality to aid the process, though many of that will not be needed in the
|
|
161
|
+
end. To ensure the separation of the complexity and allow the deserialization
|
|
162
|
+
to share data/code most efficiently, we use a BytecodeModuleGenerator and a
|
|
163
|
+
list of BytecodeFunctionGenerator for the purpose of generating bytecode and
|
|
164
|
+
serialization. After all the processing, they will eventually generate a
|
|
165
|
+
BytecodeModule and a list of BytecodeFunction that contains minimum amount of
|
|
166
|
+
data/functionality required to generate the bytecode file. Hence we can share
|
|
167
|
+
this minimum data structure between the backend and the VM.
|
|
168
|
+
- StreamVector: During serialization, we need to move/copy the part of the
|
|
169
|
+
auxiliary data from the generator to the shared minimum data structure; during
|
|
170
|
+
deserialization, we need to move/copy the content of the file into the shared
|
|
171
|
+
minimum data structure. Both can be expensive if not managed properly. To
|
|
172
|
+
minimize the copying overhead, we abstracted the complexity using a class named
|
|
173
|
+
StreamVector. During serialization, StreamVector allows us to move the data out
|
|
174
|
+
of the generator without copying (through std::vector::swap); during
|
|
175
|
+
deserialization, StreamVector allows us to take in a raw data pointer to the
|
|
176
|
+
memory buffer from the file directly, without having to copy them too.
|
|
177
|
+
|
|
178
|
+
### Interaction with the VM
|
|
179
|
+
|
|
180
|
+
At runtime, the VM will deserialize the bytecode from the file and interpret
|
|
181
|
+
it. A few components are involved:
|
|
182
|
+
BytecodeModule: This is the in-memory representation of the whole bytecode
|
|
183
|
+
file, containing all the bytecode functions. During both serialization and
|
|
184
|
+
deserialization, this data structure is generated, as a static representation
|
|
185
|
+
of the whole file.
|
|
186
|
+
- BytecodeFunction: This is the in-memory representation of a function’s
|
|
187
|
+
bytecode.
|
|
188
|
+
- RuntimeModule: This is the dynamic version of the bytecode module, containing
|
|
189
|
+
necessary runtime information for interpretation.
|
|
190
|
+
- CodeBlock: This is the dynamic version of the bytecode function, containing
|
|
191
|
+
necessary runtime information to execute a function.
|
|
192
|
+
- Domain: A GC-managed proxy which references a set of RuntimeModules, acting as
|
|
193
|
+
a bridge between the GC heap and the C++ heap.
|
|
194
|
+
- JSFunction: This is the Function object in JavaScript.
|
|
195
|
+
|
|
196
|
+
It is important (and tricky) to efficiently manage the memory and ownership of
|
|
197
|
+
these objects properly at runtime. The following figure demonstrates it. Own
|
|
198
|
+
means one manages the memory of the other through unique_ptr; Pointer means one
|
|
199
|
+
has a raw pointer to the other, without any ownership. Indirectly Own is a
|
|
200
|
+
special kind of ownership, which will be explained below. To summarize the
|
|
201
|
+
ownership:
|
|
202
|
+
- JSFunction is a JavaScript object, and hence managed by the heap/garbage
|
|
203
|
+
collector directly.
|
|
204
|
+
- JSFunction owns a GC-visible reference to a Domain, and a pointer to the
|
|
205
|
+
corresponding CodeBlock to execute.
|
|
206
|
+
- CodeBlock contains a pointer to both the RuntimeModule to access runtime
|
|
207
|
+
information, as well as a pointer to the corresponding BytecodeFunction which
|
|
208
|
+
contains the static function bytecode to execute.
|
|
209
|
+
- BytecodeModule owns a list of BytecodeFunction.
|
|
210
|
+
- RuntimeModule owns a list of CodeBlock, as well as the corresponding
|
|
211
|
+
BytecodeModule.
|
|
212
|
+
- Domain owns one or more RuntimeModule. This is the mechanism by which an extant
|
|
213
|
+
JSFunction keeps the backing bytecode alive.
|
|
214
|
+
|
|
215
|
+
## Interpreter
|
|
216
|
+
|
|
217
|
+
## Garbage Collector
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: emscripten
|
|
3
|
+
title: Building with Emscripten
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Compiling to Wasm
|
|
7
|
+
|
|
8
|
+
## Prerequisites
|
|
9
|
+
|
|
10
|
+
This guide assumes you are familiar with building Hermes locally and have the necessary
|
|
11
|
+
tools installed (e.g., CMake, Ninja, and a compatible toolchain). The instructions are
|
|
12
|
+
written for macOS but should work on Linux with minimal adjustments.
|
|
13
|
+
|
|
14
|
+
## Install Emscripten
|
|
15
|
+
|
|
16
|
+
Install `emsdk` by following the directions on the [Emscripten website for `emsdk`](https://emscripten.org/docs/getting_started/downloads.html).
|
|
17
|
+
|
|
18
|
+
Then, inside the installed directory, we need to make sure we have the latest toolchain:
|
|
19
|
+
|
|
20
|
+
```shell
|
|
21
|
+
./emsdk install latest
|
|
22
|
+
./emsdk activate latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Create a Parent Builds Directory
|
|
26
|
+
|
|
27
|
+
We need a directory to contain the different Hermes builds:
|
|
28
|
+
|
|
29
|
+
```shell
|
|
30
|
+
mkdir ~/hermes-builds
|
|
31
|
+
cd ~/hermes-builds
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Setup Environment Variables
|
|
35
|
+
|
|
36
|
+
For convenience, we rely on two environment variables throughout this document:
|
|
37
|
+
```shell
|
|
38
|
+
export Emsdk=<path-to-emsdk-directory>
|
|
39
|
+
export HermesSourcePath=<path-to-hermes-source-directory>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Build Hermes for the Host
|
|
43
|
+
|
|
44
|
+
Hermes uses a two stage build process because parts of Hermes need to be
|
|
45
|
+
built using Hermes itself. For the first stage, we need to build Hermes for
|
|
46
|
+
the host. We will also use the host build later to compile .js to .c.
|
|
47
|
+
|
|
48
|
+
```shell
|
|
49
|
+
# From within the hermes-builds directory
|
|
50
|
+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S ${HermesSourcePath?} -B build-host
|
|
51
|
+
# Build hermes and shermes for the host
|
|
52
|
+
cmake --build build-host --target hermesc --target hermes --target shermes --parallel
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Compile the Hermes VM to Wasm
|
|
56
|
+
|
|
57
|
+
```shell
|
|
58
|
+
# From within the hermes-builds directory
|
|
59
|
+
cmake -G Ninja \
|
|
60
|
+
-S ${HermesSourcePath?} -B build-wasm \
|
|
61
|
+
-DCMAKE_TOOLCHAIN_FILE=${Emsdk?}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake \
|
|
62
|
+
-DCMAKE_BUILD_TYPE=Release \
|
|
63
|
+
-DIMPORT_HOST_COMPILERS=build-host/ImportHostCompilers.cmake \
|
|
64
|
+
-DHAVE_SYS_IOCTL_H=0 \
|
|
65
|
+
-DCMAKE_EXE_LINKER_FLAGS="-sNODERAWFS=1 -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=256KB"
|
|
66
|
+
# Build the VM and libraries
|
|
67
|
+
cmake --build build-wasm --target hermes --target shermes --target shermes-dep --parallel
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Important options:
|
|
71
|
+
* `-G Ninja` is optional but recommended. It instructs CMake to use Ninja as a build tool.
|
|
72
|
+
* `CMAKE_BUILD_TYPE`: set it to one of CMake's build modes: `Debug`, `Release`,
|
|
73
|
+
`MinSizeRel`, etc.
|
|
74
|
+
* `-DHAVE_SYS_IOCTL_H=0` is needed for compatibility with the Emscripten runtime environment.
|
|
75
|
+
* `NODERAWFS`: set to `1` if you will be running Hermes directly with Node. It
|
|
76
|
+
enables direct access to the filesystem.
|
|
77
|
+
* `ALLOW_MEMORY_GROWTH`: whether to pre-allocate all memory, or let it grow over time
|
|
78
|
+
|
|
79
|
+
Under Emscripten, Hermes relies on a [small amount of JavaScript](../lib/Platform/Unicode/PlatformUnicodeEmscripten.cpp)
|
|
80
|
+
to be executed by the Wasm host (like Node.js or a browser). If you intend to run it under a "pure" Wasm host, consider
|
|
81
|
+
using this flag:
|
|
82
|
+
|
|
83
|
+
* `-DHERMES_UNICODE_LITE=` if set to ON, provides a minimal mostly stubbed-out Unicode implementation.
|
|
84
|
+
|
|
85
|
+
> Note that running under a "pure" Wasm host is not described here and will likely require more tweaks.
|
|
86
|
+
|
|
87
|
+
Now that the VM is compiled to Wasm, we can examine it:
|
|
88
|
+
```shell
|
|
89
|
+
ls -l build-wasm/bin/hermes.*
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Execute Some JavaScript with the Wasm Hermes VM
|
|
93
|
+
|
|
94
|
+
Let's create a small .js file:
|
|
95
|
+
```shell
|
|
96
|
+
echo 'var x = "hello"; console.log(`${x} world`);' > hello.js
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Let's run the example with the Wasm VM:
|
|
100
|
+
```shell
|
|
101
|
+
node ./build-wasm/bin/hermes.js hello.js
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Let's compile the example to bytecode and then run the bytecode:
|
|
105
|
+
```shell
|
|
106
|
+
node ./build-wasm/bin/hermes.js hello.js --emit-binary -out hello.hbc
|
|
107
|
+
node ./build-wasm/bin/hermes.js hello.hbc
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Finally, let's compare the performance of the Wasm VM with the native one
|
|
111
|
+
by running one of the micro-benchmarks that come with Hermes.
|
|
112
|
+
```shell
|
|
113
|
+
# Run the micro-benchmark with the host VM.
|
|
114
|
+
./build-host/bin/hermes -w ${HermesSourcePath?}/benchmarks/bench-runner/resource/test-suites/micros/interp-dispatch.js
|
|
115
|
+
# Run it with the Wasm VM
|
|
116
|
+
node ./build-wasm/bin/hermes.js -w ${HermesSourcePath?}/benchmarks/bench-runner/resource/test-suites/micros/interp-dispatch.js
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Compile JavaScript to Wasm
|
|
120
|
+
|
|
121
|
+
Now, let's compile JavaScript to Wasm! This is still not directly supported by
|
|
122
|
+
the Hermes CLI tools, so we will need a manual step. But no worries, it is easy.
|
|
123
|
+
|
|
124
|
+
First, we must make sure we have activated the Emscripten SDK in the current shell.
|
|
125
|
+
```shell
|
|
126
|
+
emcc --help
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
If that runs fine, the SDK is already active. Otherwise, we need to activate it:
|
|
130
|
+
```shell
|
|
131
|
+
source ${Emsdk?}/emsdk_env.sh
|
|
132
|
+
```
|
|
133
|
+
> Note that we didn't need to activate it when we were building with CMake, because
|
|
134
|
+
> we used a CMake toolchain file.
|
|
135
|
+
|
|
136
|
+
Now we are ready to compile the example js file we created earlier directly to Wasm.
|
|
137
|
+
`wasm-compile.sh` is a helper script in the Hermes `utils/` directory.
|
|
138
|
+
```shell
|
|
139
|
+
${HermesSourcePath?}/utils/wasm-compile.sh build-host build-wasm hello.js
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The compilation generates two files:
|
|
143
|
+
- `hello-wasm.wasm`: the compiled Wasm module. Note that this also contains
|
|
144
|
+
the entire JS library.
|
|
145
|
+
- `hello-wasm.js`: this is the Node.js wrapper to load the Wasm module.
|
|
146
|
+
|
|
147
|
+
We can run it:
|
|
148
|
+
```shell
|
|
149
|
+
node ./hello-wasm.js
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Now let's try compiling the micro-benchmark to Wasm:
|
|
153
|
+
```shell
|
|
154
|
+
${HermesSourcePath?}/utils/wasm-compile.sh build-host build-wasm \
|
|
155
|
+
${HermesSourcePath?}/benchmarks/bench-runner/resource/test-suites/micros/interp-dispatch.js
|
|
156
|
+
```
|