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,140 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: strings
|
|
3
|
+
title: Strings
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Hermes Bytecode String Kinds
|
|
7
|
+
|
|
8
|
+
Hermes bytecode has three kinds of string. The simple JS program `Object.foo = "bar";` contains examples of all of them.
|
|
9
|
+
|
|
10
|
+
- `Object` is **Predefined** because it is being used directly to access a property (i.e. it is an identifier) -- in this case on the global object -- and both the compiler and runtime are already aware of it. See `hermes/VM/PredefinedStrings.def` for a full list of strings that could be Predefined (if they were used for property access).
|
|
11
|
+
- `foo` is an **Identifier** because it is being used directly to access a property but it is not built-in to the VM or compiler.
|
|
12
|
+
- `bar` is a **String**, because it is referred to as a literal but neither of the previous conditions apply.
|
|
13
|
+
|
|
14
|
+
The distinction between these is important during bytecode initialisation, particularly "Identifier Table Initialisation", detailed in the section below.
|
|
15
|
+
|
|
16
|
+
## String Table Format
|
|
17
|
+
|
|
18
|
+
In the Hermes bytecode format, the string table is split into the following consecutive sections of the file:
|
|
19
|
+
|
|
20
|
+
1. String Kinds. A sequence which describes the kinds of the strings in the table (as described in "Hermes Bytecode String Kinds"). Abstractly, the i-th element in the sequence is the kind of the i-th string in the table. Represented as a run-length encoding.
|
|
21
|
+
2. Identifier Hashes. This sequence has an element corresponding to each identifier in the table. The i-th element of this sequence corresponds to the i-th identifier in the table. If the string is an Identifier, the element contains a hash of the identifier's string representation.
|
|
22
|
+
3. Small String Table. The primary index into the string table. When a bytecode instruction refers to a string by its index, it uses the offset into this data structure. Represents the string with three pieces of information:
|
|
23
|
+
- Whether it is UTF16 or not.
|
|
24
|
+
- Its offset into the character storage.
|
|
25
|
+
- Its length.
|
|
26
|
+
Each entry is packed in 32 bits: 1 bit for the UTF16 flag, 23 bits for the offset and 8 for the length. If the length or the offset do not fit in the available space, the entry spills into the overflow table (the next section). In this case the small entry's length is 255 and its offset field is interpreted as the offset into the overflow table.
|
|
27
|
+
4. Overflow String Table. Contains the offset and length information (32 bits for each) for strings that could not fit into a small entry.
|
|
28
|
+
5. ASCII String Storage. A contiguous sequence of ASCII character data. Character data for individual strings can potentially overlap.
|
|
29
|
+
6. UTF16 String Storage. As above, but for UTF16 strings.
|
|
30
|
+
|
|
31
|
+
## Identifier Table
|
|
32
|
+
|
|
33
|
+
To speed up property accesses the Runtime interns property names, assigning a number to refer to them by. Although bytecode instructions also refer to property names as numbers, the same string -> number mapping can't be used because a property name's number in a bytecode file stems from that file's string table and will not in general agree across all bytecode files (the Runtime could be running code from multiple files).
|
|
34
|
+
|
|
35
|
+
The Runtime's intern table is managed by the `IdentifierTable` which maintains a two-way mapping between strings and their numbers. It is known to the GC which will clean up entries corresponding to unused names.
|
|
36
|
+
|
|
37
|
+
### Importing String Table IDs
|
|
38
|
+
|
|
39
|
+
The Runtime representation of a bytecode file maintains a mapping between the numbers that strings are referred to by in the bytecode (in instructions) and the numbers that strings are referred to by in the `IdentifierTable`, this is initialised when the bytecode is loaded. Identifier Translations are used to speed this process up:
|
|
40
|
+
|
|
41
|
+
- Only the entries in the mapping corresponding to identifiers are assigned.
|
|
42
|
+
- If the identifier is Predefined its Identifier Translation is its IdentifierTable ID and the mapping can be initialised without querying the IdentifierTable. This is because the compiler and Runtime agree ahead of time on the IdentifierTable IDs that Predefined strings will be assigned to.
|
|
43
|
+
- If the identifier is not predefined, its Identifier Translation is its precomputed hash, this can be passed to the IdentifierTable when inserting it to avoid having to page in the contents of the string to calculate the hash during initialisation.
|
|
44
|
+
|
|
45
|
+
## Lowering Strings to Hermes Bytecode
|
|
46
|
+
|
|
47
|
+
When a string is mentioned as a literal in the source code, this is always represented by an instance of `LiteralString` in the IR (Intermediate Representation). This is a pointer to a UTF8 string that has been interned in the compiler's memory.
|
|
48
|
+
|
|
49
|
+
In HBC (Hermes bytecode) opcodes refer to literal strings by an index into a string table (part of the bytecode file). See "String Table Format", above, for a detailed description.
|
|
50
|
+
|
|
51
|
+
- Before the IR is lowered, all the mentioned strings are gathered into a table and assigned indices.
|
|
52
|
+
- Indices are not assigned until all strings are gathered, at which point the table is frozen. It will remain immutable, but can now be queried: Given the UTF8 representation of a string, it will return its ID.
|
|
53
|
+
- The frozen table is used during lowering to emit String IDs in bytecode instructions.
|
|
54
|
+
- Finally the table is serialized into the bytecode, following the format laid out above.
|
|
55
|
+
|
|
56
|
+
Gathering is handled in `hbc::generateBytecodeModule`. The `UniquingStringLiteralAccumulator` class is responsible for assigning indices to strings and functions in `include/BCGen/HBC/TraverseLiteralStrings.h` are responsible for visiting each string in the IR. Before lowering instructions, the accumulator is converted into a `StringLiteralTable` which is an immutable representation of the mapping.
|
|
57
|
+
|
|
58
|
+
**NB** Every potential occurrence of a string must be visited by a function declared in `TraverseLiteralStrings.h`, otherwise they will not appear in the string table. Similarly, every instance where a string could be used as an identifier should be enumerated in `isIdOperand` in `TraverseLiteralStrings.cpp`. The compiler will throw an assertion failure in debug builds if it encounters a string without an ID or an identifier that has not been marked as such during lowering.
|
|
59
|
+
|
|
60
|
+
## String Table Index Ordering
|
|
61
|
+
|
|
62
|
+
The order of entries in the String Table is significant for two reasons:
|
|
63
|
+
|
|
64
|
+
1. Instructions that access properties have variants. E.g. `GetByIdShort`, `GetById` and `GetByIdLong`. They differ in the number of bytes they have available to encode a string table index (1, 2 and 4 respectively) and their own size increases correspondingly. The compiler emits the narrowest instruction that can fit the ID being accessed, so by arranging for strings that are accessed more often to have smaller IDs, the space taken by instructions can be saved.
|
|
65
|
+
2. Because the String Kind section (see "String Table Format" above) is a run-length encoding, its size can be minimised by grouping together strings of the same kind.
|
|
66
|
+
|
|
67
|
+
In order to make good use of these properties, the compiler counts the occurrences of strings as it is gathering them. The top 2^8 most accessed strings are given the first 2^8 IDs and so on, to minimise instruction size with respect to the first constraint. Then within each category of ID (short, regular and long) strings are grouped by kind with all the Strings coming first, then Identifiers, and finally Predefineds.
|
|
68
|
+
|
|
69
|
+
## Delta Optimizing Mode
|
|
70
|
+
|
|
71
|
+
In delta-optimizing mode Hermes tries to minimise the effect of incremental changes to the source on the bytecode bundle. Outside of this mode, small changes to string usage can cause the output bytecode's string table to be re-ordered. This has knock-on effects throughout the instruction stream wherever strings are referred to. When delta-optimizing mode is enabled, the order of strings that existed in the bundle before the change is preserved.
|
|
72
|
+
|
|
73
|
+
## How Hermes Packs Strings
|
|
74
|
+
|
|
75
|
+
This is a description of the string packing algorithm employed by Hermes. Its design is guided by the Hermes optimization principles: it must be predictable and compile-time efficient.
|
|
76
|
+
|
|
77
|
+
A Hermes bytecode file represents strings as a pair (offset, length) in a big character buffer, which necessarily contains all strings as substrings. By being clever about arranging the strings in the character buffer, we can take advantage of shared substrings. This reduces the size of the bytecode file and the memory footprint of the app.
|
|
78
|
+
|
|
79
|
+
There are two types of relationships between strings:
|
|
80
|
+
|
|
81
|
+
1. A full containment relationship. For example, `once upon a time` contains `upon`. In this case, we would like to emit only the characters of the first string, and have the second string reference a substring of the first. We say that the first string is a *parent* of the second.
|
|
82
|
+
2. An overlapping relationship: `splitpea` and `peasoup`. Here `pea` is a suffix of the first string and a prefix of the second, and so we would like to emit `splitpeasoup` where the `pea` is shared between strings. We say that the pair (`splitpea`, `peasoup`) has *overlap* of length 3.
|
|
83
|
+
|
|
84
|
+
The challenge is to find a *superstring* of all our strings which is shorter than simply concatenating them. Finding the shortest such string is the "Shortest Common Superstring" problem, and it is NP-Hard. We employ a greedy heuristic. The output is not as small as the Set Cover approach, but the Set Cover approach appears necessarily quadratic while ours is N log N. (Input size is on the order of 35k strings, so quadratic algorithms are not tolerable.)
|
|
85
|
+
|
|
86
|
+
The basic approach is inspired by "A greedy approximation algorithm for constructing shortest common superstrings" by Tarhio and Ukkonen. Given a set of strings S:
|
|
87
|
+
|
|
88
|
+
1. Consider S to be vertices of a weighted directed graph.
|
|
89
|
+
2. Let there be an edge from s1 to s2 if s1 overlaps s2, that is, some string is both a suffix of s1 and a prefix of s2. The weight of the edge is the amount of overlap.
|
|
90
|
+
3. Construct a Hamiltonian Path through the graph by greedily choosing edges with the maximum weight, but ignoring edges that would result in a cycle.
|
|
91
|
+
4. Output the strings in the order of the path, applying the overlap.
|
|
92
|
+
|
|
93
|
+
The key bottleneck of the algorithm is step 2: identifying overlapping strings. Tarhio and Ukkonen suggest using KMP string matching on all pairs of strings, which is quadratic. We instead build a generalized suffix array on all strings, which allows both finding parent and overlap relationships via binary search.
|
|
94
|
+
|
|
95
|
+
### Computing Parents and Overlap
|
|
96
|
+
|
|
97
|
+
Recall that an overlap is a pair (left, right) where there is some string that is simultaneously a suffix of left, and a prefix of right. For example, `splitpea` and `peasoup` has overlap of 3. We wish to find all parents and all overlaps. We are armed with a suffix array: a sorted list of suffixes of all our strings, where each suffix points back to the string(s) that contain it.
|
|
98
|
+
|
|
99
|
+
We loop over the right strings, and find all overlapping left mates. For each right string, we loop over its prefixes in increasing length. For example, given `peasoup` we loop over `p`, `pe`, `pea`... Call this the test prefix. We use binary search in our suffix array to identify suffixes that are prefixed by the the test prefix. This set of suffixes is necessarily contiguous, because the suffix array is sorted. If a suffix is exactly equal to that prefix, then we have an overlap. This is necessarily the leftmost element of our range, because our range is sorted.
|
|
100
|
+
|
|
101
|
+
A key idea is that moving to the next prefix can only narrow the matching suffixes. For example, only suffixes that have `pe` as a prefix may have `pea` as a prefix. Therefore we do not need to reset the binary search range across iterations. Furthermore, for each iteration, we only need to consider one character in each suffix, since we know all previous characters necessarily match. This is key to performance.
|
|
102
|
+
|
|
103
|
+
When we find nonzero overlap, we add an `Arc` (left -> right) representing edges in our graph. The Arcs are maintained sorted by weight for our greedy algorithm.
|
|
104
|
+
|
|
105
|
+
#### Example
|
|
106
|
+
|
|
107
|
+
Say we wish to find overlaps with left string `splitpea` and right string `peasoup` (of course, there is only one overlap of length 3). Our proposed right string is `peasoup` and our suffix array is (considering only the suffixes of `splitpea`):
|
|
108
|
+
|
|
109
|
+
`[a, ea, itpea, litpea, pea, plitpea, splitpea, tpea]`
|
|
110
|
+
|
|
111
|
+
We are going to iterate the prefixes of our right string: `p`, `pe`, `pea`, `peas`, `peaso`, `peasou`, `peasoup`, but we will see that we exit early.
|
|
112
|
+
|
|
113
|
+
1. Start with `p`. Binary search at index 0 to get the contiguous range `[pea, plitpea]` We then check if the first element is exactly equal to `p` (which we can do simply by checking its length); if it were, we would add an arc of weight 1.
|
|
114
|
+
|
|
115
|
+
2. Next is `pe`. Binary search on index 2 with `e`, yielding a one-element range `[pea]` Again we would add an arc if the first element were equal to `pe`.
|
|
116
|
+
|
|
117
|
+
3. Next is `pea`. Binary search on index 3 with `a`, again yielding `[pea]` The first string of this range is indeed equal to our prefix `pea`, so we add an Arc `peasoup -> splitpea` with overlap 3.
|
|
118
|
+
|
|
119
|
+
4. Next is `peas`. Binary search to narrow our range by the fourth character `s`. This yields an empty range, so we exit the loop.
|
|
120
|
+
|
|
121
|
+
After exhausting our prefixes, it may happen that our range of suffixes is not empty. For example, if we searched for `lit` we would end up with one suffix `litpea`. This is how we identify substring containment relationships. In this case we don't add an arc (since there's not actually any overlap) but we do set a pointer from `lit` to the parent string that contains it, and the offset within that parent.
|
|
122
|
+
|
|
123
|
+
#### Suffix Array Construction Optimizations
|
|
124
|
+
|
|
125
|
+
We construct the suffix array by uniquing suffixes via a hash set, and then sorting them. This was found to be faster than using a sorted data structure (such as `std::map`) because comparing long strings lexicographically is expensive.
|
|
126
|
+
|
|
127
|
+
There are some optimizations we take advantage of:
|
|
128
|
+
|
|
129
|
+
1. We are only interested in suffixes that share a prefix with some string in our set. For example, for the set [splitpea, peasoup] we only need to look at suffixes that start with s or p. We take this a bit further by making a hash set of three-character prefixes of our strings ("trigrams"), and only constructing suffixes prefixed by one of those trigrams. This causes us to miss overlaps of length 1 and 2, but massively reduces the number of suffixes that we have to consider.
|
|
130
|
+
2. There are many long strings with shared suffixes. When constructing the suffix array, using a three-way radix quicksort instead of `std::sort()` is a substantial win, because it avoids comparing known-matching prefixes.
|
|
131
|
+
|
|
132
|
+
#### Further Optimization
|
|
133
|
+
|
|
134
|
+
1. Computing overlap is a natural candidate to be done in parallel.
|
|
135
|
+
2. It's possible that we could employ a faster algorithm for suffix array construction such as SA-IS, but it is unclear how we would unique our suffixes with that approach.
|
|
136
|
+
|
|
137
|
+
### References
|
|
138
|
+
|
|
139
|
+
1. ["A greedy approximation algorithm for constructing shortest common superstrings" by Tarhio and Ukkonen](http://www.sciencedirect.com/science/article/pii/0304397588901673)
|
|
140
|
+
2. [3-way radix quicksort](https://en.wikipedia.org/wiki/Multi-key_quicksort)
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
# Typed Language
|
|
2
|
+
|
|
3
|
+
The typed language implementation is in progress and considered unstable. This is a usage guide that explains how to use it and what features currently work.
|
|
4
|
+
|
|
5
|
+
## Type System
|
|
6
|
+
|
|
7
|
+
The Flow type syntax is supported (use `-typed`).
|
|
8
|
+
|
|
9
|
+
The TypeScript type syntax is partially supported (use `-parse-ts` to convert TS to Flow before compilation).
|
|
10
|
+
|
|
11
|
+
### Primitive Types
|
|
12
|
+
|
|
13
|
+
* `void` (`undefined`)
|
|
14
|
+
* `null` (`null`)
|
|
15
|
+
* `boolean` (`true`, `false`)
|
|
16
|
+
* `string` (`"abc"`)
|
|
17
|
+
* `number` (`1`, `3.5`, `Infinity`, `NaN`)
|
|
18
|
+
* `bigint` (`1n`)
|
|
19
|
+
|
|
20
|
+
### Native Types
|
|
21
|
+
|
|
22
|
+
* `c_ptr` (raw C pointer)
|
|
23
|
+
|
|
24
|
+
### Any/Mixed
|
|
25
|
+
|
|
26
|
+
* `any` is a supertype of all types, casts implicitly to other types with a runtime type check.
|
|
27
|
+
* `mixed` is a supertype of all types, but does not cast implicitly.
|
|
28
|
+
|
|
29
|
+
### Empty
|
|
30
|
+
|
|
31
|
+
* `empty` has no values. It is the subtype of all other types.
|
|
32
|
+
|
|
33
|
+
### Literal Types
|
|
34
|
+
|
|
35
|
+
* String literal types: `"left"`, `"right"`
|
|
36
|
+
* Number literal types: `1`, `-3`
|
|
37
|
+
* Boolean literal types: `true`, `false`
|
|
38
|
+
|
|
39
|
+
`"abc"` is typed as `"abc"`. Literal types flows into their general (widened) types, and into another literal only when the value is identical. Operators treat a literal as the widened type. This can be used for tagged unions and string enums.
|
|
40
|
+
|
|
41
|
+
#### Widening
|
|
42
|
+
|
|
43
|
+
A fresh literal is widened to its general type (`"abc"` -> `string`) when it becomes the inferred type of a mutable binding, for example:
|
|
44
|
+
|
|
45
|
+
* `let`/`var`: `let x = 'a'` infers `string`.
|
|
46
|
+
* Array literals: `['a','b']` infers `Array<string>`.
|
|
47
|
+
* Object fields, indexer keys, and indexer values.
|
|
48
|
+
|
|
49
|
+
The literal is kept (no widening) for `const`, or any annotated context (e.g. `Array<"a">`).
|
|
50
|
+
|
|
51
|
+
#### Absorption in unions
|
|
52
|
+
|
|
53
|
+
A literal arm is dropped from a union that already contains a type the literal flows into: its general type, `any`, or `mixed`. So `string | "abc"` is just `string`, while `"a" | "b"` stays a union.
|
|
54
|
+
|
|
55
|
+
### Unions
|
|
56
|
+
|
|
57
|
+
`A | B` indicates a union between two types `A` and `B`. Values that are either `A` or `B` are valid values of `A | B`. Unions may be cyclic, but cycles must include another type. For example: `type A = A` is invalid, but `type A = [A, B] | null` is valid.
|
|
58
|
+
|
|
59
|
+
### Arrays
|
|
60
|
+
|
|
61
|
+
The typed language implements Array as a generic: `Array<T>` or `T[]` both work, and mean the same thing.
|
|
62
|
+
|
|
63
|
+
These soundly typed arrays are incompatible with untyped JS arrays and are more restrictive. They do not allow holes or empty spaces: every element of an array `a` in the range `[0, a.length)` _must_ be of type `T`. Indexed access to arrays is bounds-checked and throws on out-of-bounds access.
|
|
64
|
+
|
|
65
|
+
### Tuple
|
|
66
|
+
|
|
67
|
+
Tuples are shown with brackets: `[A, B, C]`.
|
|
68
|
+
|
|
69
|
+
When instantiating a tuple, it is typically necessary to specify the type to avoid confusion with arrays:
|
|
70
|
+
```
|
|
71
|
+
var x: [number, string] = [1, "abc"];
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Tuples are plain objects with properties `"0"`, `"1"`, ..., not arrays, so
|
|
75
|
+
untyped code that expects an iterable (e.g. `new Map(Object.entries(o))`)
|
|
76
|
+
does not accept them.
|
|
77
|
+
|
|
78
|
+
### Objects
|
|
79
|
+
|
|
80
|
+
All object types in the typed language are _exact_ objects.
|
|
81
|
+
```
|
|
82
|
+
var obj: {a: number, b: string} = {a: 1, b: "xyz"};
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
They must have exactly the set of properties listed in their type (no extra properties allowed). Properties must be in the same order as the type requires. This allows the compiler to make access extremely efficient.
|
|
86
|
+
|
|
87
|
+
#### Variance
|
|
88
|
+
|
|
89
|
+
Object properties (and indexers, below) may carry a variance sigil:
|
|
90
|
+
```
|
|
91
|
+
var obj: {+ro: number, -wo: string} = {ro: 1, wo: "x"};
|
|
92
|
+
```
|
|
93
|
+
`+` or `readonly` makes a property read-only (covariant); `-` or `writeonly` makes it write-only (contravariant). The default (no sigil) is invariant: readable and writable, and subtyping requires the property type to match exactly.
|
|
94
|
+
|
|
95
|
+
#### Indexers
|
|
96
|
+
|
|
97
|
+
An object type may declare a single _indexer_ (index signature) instead of named properties, describing a dictionary with a uniform value type:
|
|
98
|
+
```
|
|
99
|
+
var dict: {[string]: number} = {};
|
|
100
|
+
var n: number = dict["x"]; // read
|
|
101
|
+
dict["y"] = 3; // write
|
|
102
|
+
var m: number = dict.x; // dot access works too
|
|
103
|
+
dict.y = 3;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
An object type has at most one indexer, and an indexer cannot be combined with named properties. The key type is checked against the index expression.
|
|
107
|
+
|
|
108
|
+
Reads yield `T` but are checked by a checked cast at runtime (so a read of a missing key throws if `undefined` is not in `T`). Writes require the value type `T`.
|
|
109
|
+
|
|
110
|
+
Non-computed access (`dict.x`) routes through the indexer using a `string` key, so it requires a `string`-keyed indexer.
|
|
111
|
+
|
|
112
|
+
Spreading an object that has an indexer produces an object with an indexer of the same key type. If the result also has named properties, they fold into the indexer and its value type becomes the union of all the value types:
|
|
113
|
+
```
|
|
114
|
+
var d: {[string]: number} = {};
|
|
115
|
+
var a = {...d}; // {[string]: number}
|
|
116
|
+
var b = {...d, x: "s"}; // {[string]: number | string}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
A computed property (`{[k]: v}`) in an object literal also produces an indexer object: the key type is the union of all computed key types (plus `string` if named fields are present, plus any spread indexer key type), and the value type is the union of all value types. Mixing computed and named properties results in an indexer. If the literal is constrained to an indexer type, its key and value types are used.
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
var k: string;
|
|
123
|
+
var x = {[k]: 1}; // {[string]: number}
|
|
124
|
+
var n: number;
|
|
125
|
+
var y = {[n]: 1}; // {[number]: number}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Functions
|
|
129
|
+
|
|
130
|
+
Function types can be quite complex:
|
|
131
|
+
```
|
|
132
|
+
type F = (this: string, x: number, y?: boolean) => void;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The type can contain the type of `this` inside the function, as well as optional arguments.
|
|
136
|
+
|
|
137
|
+
If a function has NO type annotations, it is considered to be an "untyped" function, and calls to it _will not be typechecked_. This allows for convenient calling to untyped JS from typed JS without extra effort or modification.
|
|
138
|
+
|
|
139
|
+
### Classes
|
|
140
|
+
|
|
141
|
+
Classes are declared as in JS:
|
|
142
|
+
```
|
|
143
|
+
class C {
|
|
144
|
+
x: number;
|
|
145
|
+
y: number;
|
|
146
|
+
|
|
147
|
+
constructor(x: number, y: number) {
|
|
148
|
+
this.x = x;
|
|
149
|
+
this.y = y;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Classes in the typed language are _nominally typed_. This means that two classes that happen to have the same field names cannot be used interchangeably.
|
|
155
|
+
|
|
156
|
+
Class methods can be decorated as final, meaning they cannot be overridden by a subclass:
|
|
157
|
+
```
|
|
158
|
+
class A {
|
|
159
|
+
@Hermes.final
|
|
160
|
+
foo(): number { return 1; }
|
|
161
|
+
}
|
|
162
|
+
class B extends A {
|
|
163
|
+
foo(): number { return 2; } // ERROR
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Methods and getter-only accessors are read-only; setter-only accessors are write-only.
|
|
168
|
+
|
|
169
|
+
`C.prototype` is the object holding the class's methods. It is read-only, and only methods that live on it are reachable: `@Hermes.final`, private and accessor members are stored in variables instead and cannot be read through it.
|
|
170
|
+
```
|
|
171
|
+
class A { foo(): number { return 1; } }
|
|
172
|
+
const f = A.prototype.foo; // OK
|
|
173
|
+
A.prototype.foo = f; // ERROR
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
A method read through `.prototype` is selected statically, not dispatched on the receiver.
|
|
177
|
+
|
|
178
|
+
Static members are stored in variables and resolved at compile time, including through `this` (the class) and `super` (the superclass) inside static members. To keep that consistent with JS:
|
|
179
|
+
* A subclass cannot redeclare an inherited static field or method.
|
|
180
|
+
* A static field can only be written through the name of the class that declares it, not through `this`, `super` or a subclass name, because JS would create a separate property on the receiver.
|
|
181
|
+
* A class declaration's name cannot be reassigned, so it always refers to exactly that class.
|
|
182
|
+
* A private static can only be accessed through the name of the class that declares it, because JS would throw for a subclass receiver.
|
|
183
|
+
* Static members cannot reference the class type parameters.
|
|
184
|
+
* Inside a generic class, a member access on the class name (`G.x`, `G.#x`) refers to the current specialization, which has its own static storage.
|
|
185
|
+
* `new this()` in a static method constructs the declaring class.
|
|
186
|
+
```
|
|
187
|
+
class A {
|
|
188
|
+
static x: number = 1;
|
|
189
|
+
static set(): void { A.x = 2; } // OK
|
|
190
|
+
static bad(): void { this.x = 2; } // ERROR
|
|
191
|
+
}
|
|
192
|
+
class B extends A {
|
|
193
|
+
static x: number = 3; // ERROR
|
|
194
|
+
}
|
|
195
|
+
B.x = 4; // ERROR
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Optional Types
|
|
199
|
+
|
|
200
|
+
The `?` indicates that a type may also be either `null` or `void`:
|
|
201
|
+
```
|
|
202
|
+
type A = ?number;
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Type Aliases
|
|
206
|
+
|
|
207
|
+
Types can be aliased using the `type` keyword:
|
|
208
|
+
```
|
|
209
|
+
type A = number;
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Generics
|
|
213
|
+
|
|
214
|
+
Classes and type aliases can both be generic.
|
|
215
|
+
|
|
216
|
+
```
|
|
217
|
+
class Wrapper<T> { x: T }
|
|
218
|
+
type NumberTuple<T> = [T, number];
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### Bounds
|
|
222
|
+
|
|
223
|
+
A type parameter may have a bound (`<T extends Bound>`). Each type argument must flow into the bound, otherwise it's an error. Bounds are checked for each specialization of a generic. A bound may reference any of the type parameters, including its own (`<T extends C<T>>`). Example:
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
function g<T extends Base, U extends T>(x: T, y: U): void {}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Typechecking
|
|
230
|
+
|
|
231
|
+
### Compile-time Typechecking
|
|
232
|
+
|
|
233
|
+
The compiler will emit errors for any type errors which can be detected ahead of time.
|
|
234
|
+
|
|
235
|
+
### Runtime Typechecking
|
|
236
|
+
|
|
237
|
+
Certain operations will result in checked casts which must happen at runtime.
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
function foo(a: any) {
|
|
241
|
+
var b: number = a; // Checked cast inserted, will throw if 'a' is not a number.
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Type Inference
|
|
246
|
+
|
|
247
|
+
#### Variable Declarations
|
|
248
|
+
|
|
249
|
+
If the declarator has an annotation, it wins.
|
|
250
|
+
|
|
251
|
+
Otherwise the initializer is visited and its type becomes the declaration's type. Globals and `catch` variables are always `any`.
|
|
252
|
+
|
|
253
|
+
Reassignments to a `let`/`var` variable must obey the type inferred/declared at the variable's declaration.
|
|
254
|
+
|
|
255
|
+
#### Array and Tuple Literals
|
|
256
|
+
|
|
257
|
+
An array literal becomes a tuple or an array based on the constraint:
|
|
258
|
+
|
|
259
|
+
* Tuple constraint: each element is checked against the matching slot.
|
|
260
|
+
* Array constraint: each element is checked against `T`, with checked casts inserted as needed.
|
|
261
|
+
* No constraint specified: element types are unioned and the literal becomes `Array<union>`.
|
|
262
|
+
* Empty literal, no constraint: Warns and assumes 'any'. Annotate the declaration for a proper type.
|
|
263
|
+
|
|
264
|
+
Tuples are never inferred without context — `[1,"x"]` alone is `Array<number|string>`, not a tuple.
|
|
265
|
+
|
|
266
|
+
#### Generic Calls
|
|
267
|
+
|
|
268
|
+
When a generic call omits type arguments, each type parameter is attempted to be inferred. Arguments are processed in two passes: first those whose parameter type is a bare type variable, then those with complex types like `(T) => U` from left to right. Inference succeeds only if every placeholder is resolved.
|
|
269
|
+
|
|
270
|
+
Type arguments may also be supplied partially, in which case only the unspecified trailing type parameters are inferred:
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
function makePair<T, U>(a: T, b: U): [T, U] { return [a, b]; }
|
|
274
|
+
makePair<number>(1, 'a'); // T = number (explicit), U = string (inferred)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Matching is structural: same-kind containers (`Array`, `Tuple`, `ExactObject`, function types) recurse into their components, a union constraint tries each arm against a non-union concrete, etc.
|
|
278
|
+
|
|
279
|
+
Limitations:
|
|
280
|
+
|
|
281
|
+
* First-write wins: `[T, T]` against `[number, string]` fixes `T = number`, and the second slot then fails the regular flow check.
|
|
282
|
+
* Single forward pass: fixpoint iteration.
|
|
283
|
+
|
|
284
|
+
### Type Refinement
|
|
285
|
+
|
|
286
|
+
Typed language doesn't have full proper type refinement support yet but it supports a partial implementation that allows for certain use cases by implicitly checking `null` or `void` at usage time.
|
|
287
|
+
|
|
288
|
+
```
|
|
289
|
+
function foo(a: number | void): number {
|
|
290
|
+
var b: number = a; // Typechecks at compile time, but will throw if 'a' is not a number.
|
|
291
|
+
return b;
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Decorations on Function Declarations
|
|
296
|
+
|
|
297
|
+
JS decorator syntax (`@Hermes.foo`) does not apply to function declarations, so the typed language uses a `Hermes.decorate(...)` call to attach decorations:
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
function charAt(this: string, pos: number): string { return this[pos]; }
|
|
301
|
+
Hermes.decorate(charAt, Hermes.builtin);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
* The call must appear as a statement immediately following the `FunctionDeclaration` it decorates.
|
|
305
|
+
* The first argument must be an identifier matching the preceding function's name.
|
|
306
|
+
* The second argument must be the decorator.
|
|
307
|
+
* Exactly two arguments are required.
|
|
308
|
+
|
|
309
|
+
## JS Language Features
|
|
310
|
+
|
|
311
|
+
The typed language is a subset of JavaScript. Most common ES2015+ constructs work, with the notable exceptions listed under "Not yet supported" below.
|
|
312
|
+
|
|
313
|
+
### Function Parameters
|
|
314
|
+
|
|
315
|
+
Function parameters can be typed, optional, defaulted, or destructured.
|
|
316
|
+
|
|
317
|
+
```
|
|
318
|
+
// Typed parameter.
|
|
319
|
+
function f(x: number): void {}
|
|
320
|
+
|
|
321
|
+
// Optional parameter (must come last). Inside the body, type is `T | void`.
|
|
322
|
+
function g(x: number, y?: string): void {}
|
|
323
|
+
|
|
324
|
+
// Default parameter.
|
|
325
|
+
function h(x: number = 1): number { return x; }
|
|
326
|
+
|
|
327
|
+
// Object destructuring parameter, with optional default.
|
|
328
|
+
function i({x, y}: {x: number, y: string} = {x: 0, y: ""}): void {}
|
|
329
|
+
|
|
330
|
+
// Array (tuple) destructuring parameter, including nested patterns.
|
|
331
|
+
function j([a, b]: [number, string]): void {}
|
|
332
|
+
function k({a: {b}}: {a: {b: number}}): void {}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
### Destructuring
|
|
336
|
+
|
|
337
|
+
Destructuring is supported in variable declarations and parameters, including nested patterns and default values. Type annotations inside destructuring patterns are not allowed; annotate the whole pattern instead.
|
|
338
|
+
|
|
339
|
+
```
|
|
340
|
+
const [x, y]: [number, string] = [1, "abc"];
|
|
341
|
+
const {a, b}: {a: number, b: number} = obj;
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Destructuring names in object patterns can have defaults, but typed tuple and `Array<T>` patterns don't support defaults yet.
|
|
345
|
+
|
|
346
|
+
An unannotated `let`/`const` loop variable is typed from the loop: the element type for `for-of` over a typed array, `string` for `for-in`, and `any` otherwise. Loop targets are checked against that type. An annotated destructuring pattern must match it exactly (no implicit casts), since its leaves are read without a runtime check.
|
|
347
|
+
|
|
348
|
+
### Spread in Array Literals
|
|
349
|
+
|
|
350
|
+
Spread of an array into an array literal is supported.
|
|
351
|
+
|
|
352
|
+
```
|
|
353
|
+
const xs: number[] = [1, 2, ...other];
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Spread in Calls
|
|
357
|
+
|
|
358
|
+
Only a tuple can be spread into a call, because only a tuple has a width that
|
|
359
|
+
is known at compile time. The elements are read out individually and passed as
|
|
360
|
+
ordinary arguments, so the call is a direct call, and arity and element types
|
|
361
|
+
are checked as if the elements had been written out by hand.
|
|
362
|
+
|
|
363
|
+
```
|
|
364
|
+
function f(a: number, b: string): void {}
|
|
365
|
+
const t: [number, string] = [1, "abc"];
|
|
366
|
+
f(...t); // same as f(t[0], t[1])
|
|
367
|
+
f(...t, ...([]: []));
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
This works in calls, method calls, `new`, `super()`, `fn.call()` and
|
|
371
|
+
`$SHBuiltin.call()`. In the last two, the `this` argument is consumed
|
|
372
|
+
positionally and cannot itself be a spread.
|
|
373
|
+
|
|
374
|
+
`Array.push()` is the exception: it is special-cased rather than treated as a
|
|
375
|
+
real function, so it takes an array spread and rejects a tuple one.
|
|
376
|
+
|
|
377
|
+
An array cannot be spread into a call: its length is not known at compile time,
|
|
378
|
+
so neither is the call's arity. An untyped callee accepts an array spread, but
|
|
379
|
+
not alongside a tuple spread, because tuples are not iterable.
|
|
380
|
+
|
|
381
|
+
An element whose type needs a checked cast to reach the parameter type is
|
|
382
|
+
rejected, because there is no argument expression to attach the cast to.
|
|
383
|
+
|
|
384
|
+
### Overloading
|
|
385
|
+
|
|
386
|
+
Class methods can be overloaded using the `@Hermes.overload` decoration. Overloaded methods must be final.
|
|
387
|
+
```
|
|
388
|
+
class C {
|
|
389
|
+
@Hermes.final
|
|
390
|
+
@Hermes.overload
|
|
391
|
+
foo(x: number): string { return 'a'; }
|
|
392
|
+
@Hermes.final
|
|
393
|
+
@Hermes.overload
|
|
394
|
+
foo(x: string): void {}
|
|
395
|
+
}
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
Overloads are resolved at compile time, and are not stored on the home object.
|
|
399
|
+
|
|
400
|
+
Both public and private methods may be overloaded, and an overload set may mix non-generic and generic overloads.
|
|
401
|
+
|
|
402
|
+
Restrictions:
|
|
403
|
+
* No overloading constructors/accessors
|
|
404
|
+
* Overloaded methods must be called (`var x = obj.overloadedMethod` is an error)
|
|
405
|
+
|
|
406
|
+
### Comparisons
|
|
407
|
+
|
|
408
|
+
Strict equality (`===` and `!==`) is type-checked. An expression `a === b` (or `a !== b`) is legal only when one operand's type flows into the other's without a checked cast.
|
|
409
|
+
|
|
410
|
+
Loose equality (`==` and `!=`) is generally disallowed. To compare two values of the same type, use `===` / `!==`. It exists for the `x == null` / `x == undefined` idiom used to test nullability. Specifically, `a == b` is allowed only when either operand has type `any`, or one operand has type `null` or `void` and `null` or `void` flows into the other type.
|
|
411
|
+
|
|
412
|
+
### Other Features
|
|
413
|
+
|
|
414
|
+
* Arrow functions (without type parameters).
|
|
415
|
+
* `for-of` loops over arrays.
|
|
416
|
+
* `try`/`catch`/`throw` (the catch binding is typed `any`).
|
|
417
|
+
* Template literals.
|
|
418
|
+
* Classes with static members, private fields and methods (`#name`),
|
|
419
|
+
inheritance, `super` calls, and `super.method()` access.
|
|
420
|
+
* Getters and setters on classes (must be marked `@Hermes.final`).
|
|
421
|
+
* Rest parameters in function declarations and function types
|
|
422
|
+
(`function f(...args)`).
|
|
423
|
+
|
|
424
|
+
### Not supported (yet)
|
|
425
|
+
|
|
426
|
+
* Spreading an array into a call (`f(...args)` where `args` is an array).
|
|
427
|
+
* Rest elements in tuple destructuring (`[a, ...rest]` on a tuple).
|
|
428
|
+
* Spread elements in tuple literals.
|
|
429
|
+
* `async` methods and generator methods.
|
|
430
|
+
* Computed property names in class definitions.
|
|
431
|
+
* Optional call expressions (`f?.()`).
|
|
432
|
+
* Type parameters on function expressions and arrow functions.
|