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,597 @@
|
|
|
1
|
+
# Worker ArrayBuffer/TypedArray/DataView Input Implementation Plan
|
|
2
|
+
|
|
3
|
+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
4
|
+
|
|
5
|
+
**Goal:** Let the Hermes `Worker` constructor accept `ArrayBuffer`, `TypedArray`, and `DataView` (carrying either precompiled bytecode or UTF-8 source) in addition to a source string.
|
|
6
|
+
|
|
7
|
+
**Architecture:** All type detection stays in native C++ (tamper-proof, reads VM internal slots for ArrayBuffer/TypedArray and install-time-captured `DataView.prototype` getters for DataView). The selected byte range is copied into a `std::string` on the parent thread and handed to the existing worker startup path; `evaluateJavaScript` already sniffs the bytecode magic number and dispatches source-vs-bytecode. The JS shim (`11-Worker.js`) is unchanged — it already forwards the raw argument.
|
|
8
|
+
|
|
9
|
+
**Tech Stack:** C++17 (no exceptions/RTTI in general VM code, but the extensions layer and `Worker.cpp` already use `try`/`catch`), JSI, Hermes extensions layer, GoogleTest (`APITests`), LLVM lit + FileCheck.
|
|
10
|
+
|
|
11
|
+
## Global Constraints
|
|
12
|
+
|
|
13
|
+
- Copyright header (Meta MIT, from `CLAUDE.md`) on every new file.
|
|
14
|
+
- C++ style: 80-column lines, 2-space indent, doc comment on every declaration.
|
|
15
|
+
- Feature is gated by `JSI_UNSTABLE` (Worker already is) and, for C++ tests, `HERMES_ENABLE_CORE_EXTENSIONS`.
|
|
16
|
+
- Run `arc f` after editing any `.cpp`/`.h` before committing.
|
|
17
|
+
- Every `sl` invocation passes `--reason "<intent> - sl help <cmd>"`. Commit messages use the `[SH]` format and hard-wrap the body at 72 columns.
|
|
18
|
+
- Detection order in the constructor: `isString` -> `isArrayBuffer` -> `isTypedArray` -> `isDataView` -> `TypeError`.
|
|
19
|
+
- Empty binary input (0 bytes) and detached buffers throw `TypeError`. Empty *string* keeps existing behavior (runs empty program).
|
|
20
|
+
- Copy at construction; never retain/transfer the parent's ArrayBuffer.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
### Task 1: Native constructor accepts ArrayBuffer / TypedArray / DataView
|
|
25
|
+
|
|
26
|
+
**Files:**
|
|
27
|
+
- Modify: `API/hermes/extensions/Intrinsics.h` (add 3 captured getters + 4 helper decls)
|
|
28
|
+
- Modify: `API/hermes/extensions/Intrinsics.cpp` (capture getters + implement helpers)
|
|
29
|
+
- Modify: `API/hermes/extensions/Worker.cpp` (extract `startWorker`, add `copyBufferBytes`, rewrite `initializeWorker` as dispatcher)
|
|
30
|
+
- Test: `unittests/API/APITest.cpp` (new `TEST_P(HermesWorkerTest, ...)` cases)
|
|
31
|
+
|
|
32
|
+
**Interfaces:**
|
|
33
|
+
- Produces (in `namespace facebook::hermes`, declared in `Intrinsics.h`):
|
|
34
|
+
- `bool isDataView(jsi::Runtime &rt, const jsi::Object &obj);`
|
|
35
|
+
- `jsi::ArrayBuffer dataViewBuffer(jsi::Runtime &rt, const jsi::Object &dv);`
|
|
36
|
+
- `size_t dataViewByteOffset(jsi::Runtime &rt, const jsi::Object &dv);`
|
|
37
|
+
- `size_t dataViewByteLength(jsi::Runtime &rt, const jsi::Object &dv);`
|
|
38
|
+
- Consumes: existing `getIntrinsics(rt)`, `throwTypeError(rt, msg)`; JSI `Object::isArrayBuffer/getArrayBuffer/isTypedArray/getTypedArray`, `ArrayBuffer::size/data/detached`, `TypedArray::buffer/byteOffset/byteLength`, `Function::callWithThis`.
|
|
39
|
+
|
|
40
|
+
- [ ] **Step 1: Write the failing test**
|
|
41
|
+
|
|
42
|
+
Add these cases inside the existing `#if HERMES_ENABLE_CORE_EXTENSIONS` block in `unittests/API/APITest.cpp` (right after the `WebWorkerBasic` test, before the `#endif`):
|
|
43
|
+
|
|
44
|
+
```cpp
|
|
45
|
+
TEST_P(HermesWorkerTest, WorkerFromArrayBuffer) {
|
|
46
|
+
// Source carried in an ArrayBuffer must construct a Worker (no throw).
|
|
47
|
+
auto code = R"(
|
|
48
|
+
var bytes = new TextEncoder().encode("var x = 1;");
|
|
49
|
+
var worker = new Worker(bytes.buffer);
|
|
50
|
+
worker;
|
|
51
|
+
)";
|
|
52
|
+
auto worker = eval(code).asObject(*rt);
|
|
53
|
+
worker.getPropertyAsFunction(*rt, "terminate").callWithThis(*rt, worker);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
TEST_P(HermesWorkerTest, WorkerFromTypedArrayWithOffset) {
|
|
57
|
+
// A Uint8Array view with a non-zero byteOffset must slice correctly.
|
|
58
|
+
auto code = R"(
|
|
59
|
+
var whole = new TextEncoder().encode("XXvar y = 2;");
|
|
60
|
+
var view = new Uint8Array(whole.buffer, 2); // skip the leading "XX"
|
|
61
|
+
var worker = new Worker(view);
|
|
62
|
+
worker;
|
|
63
|
+
)";
|
|
64
|
+
auto worker = eval(code).asObject(*rt);
|
|
65
|
+
worker.getPropertyAsFunction(*rt, "terminate").callWithThis(*rt, worker);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
TEST_P(HermesWorkerTest, WorkerFromDataView) {
|
|
69
|
+
auto code = R"(
|
|
70
|
+
var bytes = new TextEncoder().encode("var z = 3;");
|
|
71
|
+
var dv = new DataView(bytes.buffer);
|
|
72
|
+
var worker = new Worker(dv);
|
|
73
|
+
worker;
|
|
74
|
+
)";
|
|
75
|
+
auto worker = eval(code).asObject(*rt);
|
|
76
|
+
worker.getPropertyAsFunction(*rt, "terminate").callWithThis(*rt, worker);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
TEST_P(HermesWorkerTest, WorkerFromBinaryErrors) {
|
|
80
|
+
// Non-buffer, non-string argument.
|
|
81
|
+
EXPECT_THROW(eval("new Worker({});"), JSError);
|
|
82
|
+
// Empty binary input.
|
|
83
|
+
EXPECT_THROW(eval("new Worker(new ArrayBuffer(0));"), JSError);
|
|
84
|
+
EXPECT_THROW(eval("new Worker(new Uint8Array(0));"), JSError);
|
|
85
|
+
// Detached ArrayBuffer. Detach via HermesInternal, matching the existing
|
|
86
|
+
// DetachedArrayBuffer / ArrayBufferDetached tests in this file.
|
|
87
|
+
EXPECT_THROW(
|
|
88
|
+
eval(R"(
|
|
89
|
+
var ab = new ArrayBuffer(8);
|
|
90
|
+
HermesInternal.detachArrayBuffer(ab);
|
|
91
|
+
new Worker(ab);
|
|
92
|
+
)"),
|
|
93
|
+
JSError);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- [ ] **Step 2: Run the tests to verify they fail**
|
|
98
|
+
|
|
99
|
+
Run: `cmake --build cmake-build-asan --target APITests && cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest.WorkerFrom*'`
|
|
100
|
+
Expected: FAIL — the ArrayBuffer/TypedArray/DataView cases throw `TypeError` today ("Must provide the source code as a String"), so the positive tests fail.
|
|
101
|
+
|
|
102
|
+
- [ ] **Step 3: Add the DataView getters and helper declarations to `Intrinsics.h`**
|
|
103
|
+
|
|
104
|
+
In `struct ExtensionIntrinsics`, after `jsi::Function uint8Array;`, add:
|
|
105
|
+
|
|
106
|
+
```cpp
|
|
107
|
+
/// DataView.prototype accessor getters, captured before user code runs so
|
|
108
|
+
/// DataView detection cannot be subverted by replacing globals.
|
|
109
|
+
jsi::Function dataViewBufferGetter;
|
|
110
|
+
jsi::Function dataViewByteOffsetGetter;
|
|
111
|
+
jsi::Function dataViewByteLengthGetter;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
After the `throwRangeError` declaration (before the closing `} // namespace hermes`), add:
|
|
115
|
+
|
|
116
|
+
```cpp
|
|
117
|
+
/// \return true iff \p obj is a genuine DataView. Brand-checks via the
|
|
118
|
+
/// captured DataView.prototype.buffer getter (immune to shadowing and global
|
|
119
|
+
/// replacement). The buffer getter is used rather than byteLength/byteOffset
|
|
120
|
+
/// because it does not reject a genuine but detached DataView.
|
|
121
|
+
bool isDataView(jsi::Runtime &rt, const jsi::Object &obj);
|
|
122
|
+
|
|
123
|
+
/// \return the underlying ArrayBuffer of the DataView \p dv.
|
|
124
|
+
/// \pre isDataView(rt, dv) is true.
|
|
125
|
+
jsi::ArrayBuffer dataViewBuffer(jsi::Runtime &rt, const jsi::Object &dv);
|
|
126
|
+
|
|
127
|
+
/// \return the byteOffset of the DataView \p dv.
|
|
128
|
+
/// \pre isDataView(rt, dv) is true.
|
|
129
|
+
size_t dataViewByteOffset(jsi::Runtime &rt, const jsi::Object &dv);
|
|
130
|
+
|
|
131
|
+
/// \return the byteLength of the DataView \p dv.
|
|
132
|
+
/// \pre isDataView(rt, dv) is true.
|
|
133
|
+
size_t dataViewByteLength(jsi::Runtime &rt, const jsi::Object &dv);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
- [ ] **Step 4: Capture the getters and implement the helpers in `Intrinsics.cpp`**
|
|
137
|
+
|
|
138
|
+
Add a file-local helper in the anonymous namespace (after the `kIntrinsicsUUID` definition):
|
|
139
|
+
|
|
140
|
+
```cpp
|
|
141
|
+
/// Fetch the getter function of DataView.prototype.<name> using the genuine
|
|
142
|
+
/// Object.getOwnPropertyDescriptor. Called only during capture, before user
|
|
143
|
+
/// code runs.
|
|
144
|
+
jsi::Function captureDataViewGetter(jsi::Runtime &rt, const char *name) {
|
|
145
|
+
auto getOwnDesc = rt.global()
|
|
146
|
+
.getPropertyAsObject(rt, "Object")
|
|
147
|
+
.getPropertyAsFunction(rt, "getOwnPropertyDescriptor");
|
|
148
|
+
auto dvProto = rt.global()
|
|
149
|
+
.getPropertyAsObject(rt, "DataView")
|
|
150
|
+
.getPropertyAsObject(rt, "prototype");
|
|
151
|
+
return getOwnDesc
|
|
152
|
+
.call(rt, dvProto, jsi::String::createFromAscii(rt, name))
|
|
153
|
+
.asObject(rt)
|
|
154
|
+
.getPropertyAsFunction(rt, "get");
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Extend the constructor initializer list:
|
|
159
|
+
|
|
160
|
+
```cpp
|
|
161
|
+
ExtensionIntrinsics::ExtensionIntrinsics(jsi::Runtime &rt)
|
|
162
|
+
: typeError(rt.global().getPropertyAsFunction(rt, "TypeError")),
|
|
163
|
+
rangeError(rt.global().getPropertyAsFunction(rt, "RangeError")),
|
|
164
|
+
uint8Array(rt.global().getPropertyAsFunction(rt, "Uint8Array")),
|
|
165
|
+
dataViewBufferGetter(captureDataViewGetter(rt, "buffer")),
|
|
166
|
+
dataViewByteOffsetGetter(captureDataViewGetter(rt, "byteOffset")),
|
|
167
|
+
dataViewByteLengthGetter(captureDataViewGetter(rt, "byteLength")) {}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Add the helper implementations before the closing `} // namespace hermes`:
|
|
171
|
+
|
|
172
|
+
```cpp
|
|
173
|
+
bool isDataView(jsi::Runtime &rt, const jsi::Object &obj) {
|
|
174
|
+
const auto &intrinsics = getIntrinsics(rt);
|
|
175
|
+
try {
|
|
176
|
+
// The buffer getter throws a TypeError unless `obj` has a genuine
|
|
177
|
+
// [[DataView]] internal slot. Unlike byteLength/byteOffset it does not
|
|
178
|
+
// reject a genuine but detached DataView.
|
|
179
|
+
intrinsics.dataViewBufferGetter.callWithThis(rt, obj);
|
|
180
|
+
return true;
|
|
181
|
+
} catch (const jsi::JSError &) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
jsi::ArrayBuffer dataViewBuffer(jsi::Runtime &rt, const jsi::Object &dv) {
|
|
187
|
+
const auto &intrinsics = getIntrinsics(rt);
|
|
188
|
+
return intrinsics.dataViewBufferGetter.callWithThis(rt, dv)
|
|
189
|
+
.asObject(rt)
|
|
190
|
+
.getArrayBuffer(rt);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
size_t dataViewByteOffset(jsi::Runtime &rt, const jsi::Object &dv) {
|
|
194
|
+
const auto &intrinsics = getIntrinsics(rt);
|
|
195
|
+
return static_cast<size_t>(
|
|
196
|
+
intrinsics.dataViewByteOffsetGetter.callWithThis(rt, dv).asNumber());
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
size_t dataViewByteLength(jsi::Runtime &rt, const jsi::Object &dv) {
|
|
200
|
+
const auto &intrinsics = getIntrinsics(rt);
|
|
201
|
+
return static_cast<size_t>(
|
|
202
|
+
intrinsics.dataViewByteLengthGetter.callWithThis(rt, dv).asNumber());
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
- [ ] **Step 5: Extract `startWorker` and add `copyBufferBytes` in `Worker.cpp`**
|
|
207
|
+
|
|
208
|
+
Add these two functions in the anonymous namespace, immediately before `initializeWorker` (around line 526):
|
|
209
|
+
|
|
210
|
+
```cpp
|
|
211
|
+
/// Copy [\p offset, \p offset + \p length) of \p ab into a std::string.
|
|
212
|
+
/// Throws TypeError if the buffer is detached or the range is empty.
|
|
213
|
+
/// std::string carries arbitrary binary faithfully (no UTF-8 re-encoding).
|
|
214
|
+
std::string copyBufferBytes(
|
|
215
|
+
jsi::Runtime &rt,
|
|
216
|
+
jsi::ArrayBuffer ab,
|
|
217
|
+
size_t offset,
|
|
218
|
+
size_t length) {
|
|
219
|
+
if (ab.detached(rt)) {
|
|
220
|
+
throwTypeError(rt, "Cannot create Worker from a detached buffer");
|
|
221
|
+
}
|
|
222
|
+
if (length == 0) {
|
|
223
|
+
throwTypeError(rt, "Cannot create Worker from empty binary input");
|
|
224
|
+
}
|
|
225
|
+
const uint8_t *data = ab.data(rt);
|
|
226
|
+
return std::string(
|
|
227
|
+
reinterpret_cast<const char *>(data + offset), length);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/// Create the Worker runtime/thread and attach state to \p self, running the
|
|
231
|
+
/// bytes in \p script (source or bytecode, decided by evaluateJavaScript).
|
|
232
|
+
/// Shared by all constructor input types.
|
|
233
|
+
void startWorker(jsi::Runtime &rt, jsi::Object self, std::string script) {
|
|
234
|
+
auto *api = jsi::castInterface<IHermesRootAPI>(makeHermesRootAPI());
|
|
235
|
+
auto workerRuntime = api->makeHermesRuntime(::hermes::vm::RuntimeConfig());
|
|
236
|
+
auto workerState = std::make_shared<WorkerState>(rt, self);
|
|
237
|
+
|
|
238
|
+
installPostMessageFromWorker(*workerRuntime, workerState);
|
|
239
|
+
installCloseFromWorker(*workerRuntime, workerState);
|
|
240
|
+
|
|
241
|
+
auto workerNativeState = std::make_shared<WorkerNativeState>(
|
|
242
|
+
workerState, std::move(workerRuntime));
|
|
243
|
+
self.setNativeState(rt, workerNativeState);
|
|
244
|
+
|
|
245
|
+
auto *setEventLoopControlInterface =
|
|
246
|
+
jsi::castInterface<ISetEventLoopControl>(&workerState->parentRuntime);
|
|
247
|
+
assert(
|
|
248
|
+
setEventLoopControlInterface && "ISetEventLoopControl is not supported");
|
|
249
|
+
auto *eventLoopControl = setEventLoopControlInterface->getEventLoopControl();
|
|
250
|
+
if (LLVM_LIKELY(eventLoopControl)) {
|
|
251
|
+
workerState->id = eventLoopControl->registerTaskQueueSource();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
workerNativeState->startWorkerThread(std::move(script));
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
- [ ] **Step 6: Rewrite `initializeWorker` as the dispatcher**
|
|
259
|
+
|
|
260
|
+
Replace the entire body of `initializeWorker` (from `assert(count == 2);` through `return jsi::Value::undefined();`) with:
|
|
261
|
+
|
|
262
|
+
```cpp
|
|
263
|
+
// This is only called by the Worker extension script in `11-Worker.js`, so we
|
|
264
|
+
// can guarantee that the argument count is 2.
|
|
265
|
+
assert(count == 2);
|
|
266
|
+
|
|
267
|
+
// The self object is provided by `11-Worker.js`, so it is always an Object.
|
|
268
|
+
auto self = args[0].asObject(rt);
|
|
269
|
+
const jsi::Value &input = args[1];
|
|
270
|
+
|
|
271
|
+
std::string script;
|
|
272
|
+
if (input.isString()) {
|
|
273
|
+
script = input.asString(rt).utf8(rt);
|
|
274
|
+
} else if (input.isObject()) {
|
|
275
|
+
jsi::Object obj = input.asObject(rt);
|
|
276
|
+
if (obj.isArrayBuffer(rt)) {
|
|
277
|
+
jsi::ArrayBuffer ab = obj.getArrayBuffer(rt);
|
|
278
|
+
size_t size = ab.size(rt);
|
|
279
|
+
script = copyBufferBytes(rt, std::move(ab), 0, size);
|
|
280
|
+
} else if (obj.isTypedArray(rt)) {
|
|
281
|
+
jsi::TypedArray ta = obj.getTypedArray(rt);
|
|
282
|
+
size_t offset = ta.byteOffset(rt);
|
|
283
|
+
size_t length = ta.byteLength(rt);
|
|
284
|
+
jsi::ArrayBuffer ab = ta.buffer(rt);
|
|
285
|
+
script = copyBufferBytes(rt, std::move(ab), offset, length);
|
|
286
|
+
} else if (isDataView(rt, obj)) {
|
|
287
|
+
size_t offset = dataViewByteOffset(rt, obj);
|
|
288
|
+
size_t length = dataViewByteLength(rt, obj);
|
|
289
|
+
jsi::ArrayBuffer ab = dataViewBuffer(rt, obj);
|
|
290
|
+
script = copyBufferBytes(rt, std::move(ab), offset, length);
|
|
291
|
+
} else {
|
|
292
|
+
throwTypeError(
|
|
293
|
+
rt,
|
|
294
|
+
"Worker script must be a string, ArrayBuffer, TypedArray, or DataView");
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
throwTypeError(
|
|
298
|
+
rt,
|
|
299
|
+
"Worker script must be a string, ArrayBuffer, TypedArray, or DataView");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
startWorker(rt, std::move(self), std::move(script));
|
|
303
|
+
return jsi::Value::undefined();
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
If `<string>` is not already transitively included, add `#include <string>` near the top of `Worker.cpp` (after the existing standard includes).
|
|
307
|
+
|
|
308
|
+
- [ ] **Step 7: Run the tests to verify they pass**
|
|
309
|
+
|
|
310
|
+
Run: `arc f && cmake --build cmake-build-asan --target APITests && cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest*'`
|
|
311
|
+
Expected: PASS — all `WorkerFrom*` cases and the existing `WebWorkerBasic` pass.
|
|
312
|
+
|
|
313
|
+
- [ ] **Step 8: Commit**
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
sl commit --reason "worker accepts ArrayBuffer/TypedArray/DataView input - sl help commit" --message "$(cat <<'EOF'
|
|
317
|
+
[SH]: Accept ArrayBuffer/TypedArray/DataView in Worker ctor
|
|
318
|
+
|
|
319
|
+
Summary:
|
|
320
|
+
Extend the Worker constructor to accept binary input (ArrayBuffer,
|
|
321
|
+
any TypedArray, or DataView) in addition to a source string. The
|
|
322
|
+
bytes may be precompiled bytecode or UTF-8 source; evaluateJavaScript
|
|
323
|
+
already sniffs the magic number and dispatches accordingly.
|
|
324
|
+
|
|
325
|
+
All type detection happens in native C++ and is tamper-proof:
|
|
326
|
+
ArrayBuffer/TypedArray use VM internal-slot checks, and DataView uses
|
|
327
|
+
DataView.prototype getters captured in captureIntrinsics before user
|
|
328
|
+
code runs. The selected byte range is copied into a std::string on
|
|
329
|
+
the parent thread, avoiding the UTF-8 re-encoding that previously
|
|
330
|
+
corrupted binary input. Empty binary input and detached buffers throw
|
|
331
|
+
TypeError.
|
|
332
|
+
|
|
333
|
+
Test Plan: cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest*'
|
|
334
|
+
|
|
335
|
+
Reviewers: Hermes
|
|
336
|
+
|
|
337
|
+
Subscribers:
|
|
338
|
+
|
|
339
|
+
Tasks:
|
|
340
|
+
|
|
341
|
+
Tags:
|
|
342
|
+
EOF
|
|
343
|
+
)"
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
### Task 2: LIT round-trip, byte fidelity, and tamper-resistance tests
|
|
349
|
+
|
|
350
|
+
**Files:**
|
|
351
|
+
- Create: `test/hermes/worker/worker-from-buffer.js`
|
|
352
|
+
|
|
353
|
+
**Interfaces:**
|
|
354
|
+
- Consumes: the constructor behavior from Task 1; the `hermes`/`shermes` CLIs (which provide an event loop that drains worker->parent messages).
|
|
355
|
+
- Produces: nothing consumed by later tasks.
|
|
356
|
+
|
|
357
|
+
Note: deliberately avoids `TextEncoder` (it is gated on `core_extensions`, is
|
|
358
|
+
untested under `shermes -exec`, and needs a UTF-8 locale). Bytes are built with
|
|
359
|
+
`Uint8Array.from(str, c => c.charCodeAt(0))` for ASCII source, and high-byte
|
|
360
|
+
fidelity is proven with an explicit byte array whose worker echoes a charCode
|
|
361
|
+
as ASCII output — keeping both RUN lines and ASCII-only FileCheck matching.
|
|
362
|
+
|
|
363
|
+
- [ ] **Step 1: Write the LIT test**
|
|
364
|
+
|
|
365
|
+
Create `test/hermes/worker/worker-from-buffer.js`:
|
|
366
|
+
|
|
367
|
+
```javascript
|
|
368
|
+
/**
|
|
369
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
370
|
+
*
|
|
371
|
+
* This source code is licensed under the MIT license found in the
|
|
372
|
+
* LICENSE file in the root directory of this source tree.
|
|
373
|
+
*/
|
|
374
|
+
|
|
375
|
+
// RUN: %hermes %s | %FileCheck %s --match-full-lines
|
|
376
|
+
// RUN: %shermes -exec %s | %FileCheck %s --match-full-lines
|
|
377
|
+
|
|
378
|
+
// The Worker constructor accepts ArrayBuffer, TypedArray, and DataView carrying
|
|
379
|
+
// source, faithfully preserving bytes (including bytes >= 0x80).
|
|
380
|
+
|
|
381
|
+
// Build source bytes from ASCII text without TextEncoder (works under shermes).
|
|
382
|
+
function asciiBytes(str) {
|
|
383
|
+
return Uint8Array.from(str, function (c) { return c.charCodeAt(0); });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
var src = 'onmessage = function() { postMessage("ran"); };';
|
|
387
|
+
var bytes = asciiBytes(src);
|
|
388
|
+
|
|
389
|
+
// 1) ArrayBuffer
|
|
390
|
+
var w1 = new Worker(bytes.buffer);
|
|
391
|
+
w1.onmessage = function (msg) { print("ab: " + msg); w1.terminate(); };
|
|
392
|
+
w1.postMessage("go");
|
|
393
|
+
|
|
394
|
+
// 2) Uint8Array with non-zero byteOffset (prepend 2 junk bytes, then view).
|
|
395
|
+
var padded = new Uint8Array(bytes.length + 2);
|
|
396
|
+
padded.set(bytes, 2);
|
|
397
|
+
var view = new Uint8Array(padded.buffer, 2, bytes.length);
|
|
398
|
+
var w2 = new Worker(view);
|
|
399
|
+
w2.onmessage = function (msg) { print("ta: " + msg); w2.terminate(); };
|
|
400
|
+
w2.postMessage("go");
|
|
401
|
+
|
|
402
|
+
// 3) DataView
|
|
403
|
+
var w3 = new Worker(new DataView(bytes.buffer));
|
|
404
|
+
w3.onmessage = function (msg) { print("dv: " + msg); w3.terminate(); };
|
|
405
|
+
w3.postMessage("go");
|
|
406
|
+
|
|
407
|
+
// 4) Byte fidelity: the source buffer contains a byte >= 0x80 (the two-byte
|
|
408
|
+
// UTF-8 encoding of U+00E9 é). The worker echoes its charCode; if the copy
|
|
409
|
+
// corrupted high bytes the decoded char would not be 233.
|
|
410
|
+
var hi = [];
|
|
411
|
+
var prefix = 'onmessage = function() { postMessage("code=" + "';
|
|
412
|
+
for (var i = 0; i < prefix.length; i++) hi.push(prefix.charCodeAt(i));
|
|
413
|
+
hi.push(0xc3, 0xa9); // UTF-8 for U+00E9 é
|
|
414
|
+
var suffix = '".charCodeAt(0)); };';
|
|
415
|
+
for (var j = 0; j < suffix.length; j++) hi.push(suffix.charCodeAt(j));
|
|
416
|
+
var w4 = new Worker(new Uint8Array(hi).buffer);
|
|
417
|
+
w4.onmessage = function (msg) { print("fidelity: " + msg); w4.terminate(); };
|
|
418
|
+
w4.postMessage("go");
|
|
419
|
+
|
|
420
|
+
// Error cases (all synchronous throws at construction).
|
|
421
|
+
function expectThrow(fn, label) {
|
|
422
|
+
try { fn(); print(label + ": NO THROW"); }
|
|
423
|
+
catch (e) { print(label + ": " + e.constructor.name); }
|
|
424
|
+
}
|
|
425
|
+
expectThrow(function () { new Worker(123); }, "number");
|
|
426
|
+
expectThrow(function () { new Worker({}); }, "object");
|
|
427
|
+
expectThrow(function () { new Worker(new ArrayBuffer(0)); }, "empty-ab");
|
|
428
|
+
expectThrow(function () { new Worker(new Uint8Array(0)); }, "empty-ta");
|
|
429
|
+
|
|
430
|
+
// Tamper-resistance: replacing globals must not break native detection.
|
|
431
|
+
ArrayBuffer.isView = function () { return false; };
|
|
432
|
+
DataView = null;
|
|
433
|
+
var w5 = new Worker(bytes.buffer);
|
|
434
|
+
w5.onmessage = function (msg) { print("tampered: " + msg); w5.terminate(); };
|
|
435
|
+
w5.postMessage("go");
|
|
436
|
+
|
|
437
|
+
// CHECK: number: TypeError
|
|
438
|
+
// CHECK-NEXT: object: TypeError
|
|
439
|
+
// CHECK-NEXT: empty-ab: TypeError
|
|
440
|
+
// CHECK-NEXT: empty-ta: TypeError
|
|
441
|
+
// CHECK-DAG: ab: ran
|
|
442
|
+
// CHECK-DAG: ta: ran
|
|
443
|
+
// CHECK-DAG: dv: ran
|
|
444
|
+
// CHECK-DAG: fidelity: code=233
|
|
445
|
+
// CHECK-DAG: tampered: ran
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
Note: the synchronous `expectThrow` output is emitted before any worker
|
|
449
|
+
message is delivered (messages are processed by the event loop after the
|
|
450
|
+
top-level script finishes), so the four error lines are ordered with
|
|
451
|
+
`CHECK`/`CHECK-NEXT`, while the five async worker results use `CHECK-DAG`
|
|
452
|
+
(order among them is not guaranteed).
|
|
453
|
+
|
|
454
|
+
- [ ] **Step 2: Build the CLIs**
|
|
455
|
+
|
|
456
|
+
Run: `cmake --build cmake-build-asan --target hermes shermes`
|
|
457
|
+
Expected: build succeeds.
|
|
458
|
+
|
|
459
|
+
- [ ] **Step 3: Run the LIT test and verify it passes**
|
|
460
|
+
|
|
461
|
+
Run: `LIT_OPTS="-j1" LIT_FILTER="worker/worker-from-buffer" cmake --build cmake-build-asan --target check-hermes`
|
|
462
|
+
Expected: PASS (1 test, 2 RUN lines).
|
|
463
|
+
|
|
464
|
+
- [ ] **Step 4: Commit**
|
|
465
|
+
|
|
466
|
+
```bash
|
|
467
|
+
sl commit --reason "add LIT tests for Worker binary input - sl help commit" --message "$(cat <<'EOF'
|
|
468
|
+
[SH]: Add LIT tests for Worker binary input
|
|
469
|
+
|
|
470
|
+
Summary:
|
|
471
|
+
Add worker-from-buffer.js covering construction from ArrayBuffer,
|
|
472
|
+
TypedArray (non-zero byteOffset), and DataView carrying UTF-8 source,
|
|
473
|
+
verifying multi-byte fidelity via a round-tripped accented/emoji
|
|
474
|
+
string. Also covers TypeError on non-buffer/empty input and confirms
|
|
475
|
+
detection still works after replacing ArrayBuffer.isView and DataView.
|
|
476
|
+
|
|
477
|
+
Test Plan: LIT_OPTS="-j1" LIT_FILTER="worker/worker-from-buffer" cmake --build cmake-build-asan --target check-hermes
|
|
478
|
+
|
|
479
|
+
Reviewers: Hermes
|
|
480
|
+
|
|
481
|
+
Subscribers:
|
|
482
|
+
|
|
483
|
+
Tasks:
|
|
484
|
+
|
|
485
|
+
Tags:
|
|
486
|
+
EOF
|
|
487
|
+
)"
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
---
|
|
491
|
+
|
|
492
|
+
### Task 3: Bytecode-input C++ test
|
|
493
|
+
|
|
494
|
+
**Files:**
|
|
495
|
+
- Modify: `unittests/API/APITest.cpp` (one new `TEST_P(HermesWorkerTest, ...)`)
|
|
496
|
+
|
|
497
|
+
**Interfaces:**
|
|
498
|
+
- Consumes: `hermes::compileJS` (already included via `<hermes/CompileJS.h>`), the constructor from Task 1, and the existing `api->isHermesBytecode` root API.
|
|
499
|
+
- Produces: nothing consumed by later tasks.
|
|
500
|
+
|
|
501
|
+
- [ ] **Step 1: Write the failing test**
|
|
502
|
+
|
|
503
|
+
Add inside the `#if HERMES_ENABLE_CORE_EXTENSIONS` block in `unittests/API/APITest.cpp`, after the Task 1 cases:
|
|
504
|
+
|
|
505
|
+
```cpp
|
|
506
|
+
TEST_P(HermesWorkerTest, WorkerFromBytecode) {
|
|
507
|
+
// Compile a trivial script to Hermes bytecode.
|
|
508
|
+
std::string bytecode;
|
|
509
|
+
ASSERT_TRUE(hermes::compileJS("var x = 1;", bytecode));
|
|
510
|
+
|
|
511
|
+
auto *api = castInterface<IHermesRootAPI>(makeHermesRootAPI());
|
|
512
|
+
ASSERT_TRUE(api->isHermesBytecode(
|
|
513
|
+
reinterpret_cast<const uint8_t *>(bytecode.data()), bytecode.size()));
|
|
514
|
+
|
|
515
|
+
// Expose the bytecode to JS as a Uint8Array so `new Worker` receives the
|
|
516
|
+
// exact bytes (bytecode magic contains bytes >= 0x80, which the old
|
|
517
|
+
// string/utf8 path would have corrupted).
|
|
518
|
+
auto u8ctor = rt->global().getPropertyAsFunction(*rt, "Uint8Array");
|
|
519
|
+
auto arr =
|
|
520
|
+
u8ctor.callAsConstructor(*rt, (double)bytecode.size()).asObject(*rt);
|
|
521
|
+
for (size_t i = 0; i < bytecode.size(); ++i) {
|
|
522
|
+
arr.setProperty(
|
|
523
|
+
*rt,
|
|
524
|
+
PropNameID::forUtf8(*rt, std::to_string(i)),
|
|
525
|
+
(double)(uint8_t)bytecode[i]);
|
|
526
|
+
}
|
|
527
|
+
rt->global().setProperty(*rt, "__bc", arr);
|
|
528
|
+
|
|
529
|
+
// Constructing from the bytecode bytes must succeed (worker thread starts
|
|
530
|
+
// and evaluateJavaScript takes the bytecode path).
|
|
531
|
+
auto worker = eval("var w = new Worker(__bc); w;").asObject(*rt);
|
|
532
|
+
worker.getPropertyAsFunction(*rt, "terminate").callWithThis(*rt, worker);
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
- [ ] **Step 2: Run the test to verify it fails**
|
|
537
|
+
|
|
538
|
+
Run: `cmake --build cmake-build-asan --target APITests && cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest.WorkerFromBytecode'`
|
|
539
|
+
Expected: FAIL if run before Task 1 is present. If Task 1 is already committed, this test should already PASS (it exercises the same code path); in that case treat this task as adding explicit bytecode coverage and skip to Step 3.
|
|
540
|
+
|
|
541
|
+
- [ ] **Step 3: Confirm it passes**
|
|
542
|
+
|
|
543
|
+
Run: `cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest.WorkerFromBytecode'`
|
|
544
|
+
Expected: PASS.
|
|
545
|
+
|
|
546
|
+
Rationale: worker execution cannot be observed in the unit-test harness (no integrator event loop), so this test verifies that real bytecode is accepted by the constructor without corruption. End-to-end source execution/fidelity is covered by the LIT test in Task 2; bytecode *execution* itself is covered by existing `evaluateJavaScript` bytecode tests.
|
|
547
|
+
|
|
548
|
+
- [ ] **Step 4: Commit**
|
|
549
|
+
|
|
550
|
+
```bash
|
|
551
|
+
sl commit --reason "add bytecode-input Worker test - sl help commit" --message "$(cat <<'EOF'
|
|
552
|
+
[SH]: Add bytecode-input test for Worker ctor
|
|
553
|
+
|
|
554
|
+
Summary:
|
|
555
|
+
Add WorkerFromBytecode, which compiles a script to Hermes bytecode and
|
|
556
|
+
constructs a Worker from a Uint8Array of those exact bytes, asserting
|
|
557
|
+
the constructor accepts them. This guards the binary path against the
|
|
558
|
+
UTF-8 corruption that would previously have mangled bytecode's
|
|
559
|
+
high-byte magic number.
|
|
560
|
+
|
|
561
|
+
Test Plan: cmake-build-asan/unittests/API/APITests --gtest_filter='*HermesWorkerTest.WorkerFromBytecode'
|
|
562
|
+
|
|
563
|
+
Reviewers: Hermes
|
|
564
|
+
|
|
565
|
+
Subscribers:
|
|
566
|
+
|
|
567
|
+
Tasks:
|
|
568
|
+
|
|
569
|
+
Tags:
|
|
570
|
+
EOF
|
|
571
|
+
)"
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
---
|
|
575
|
+
|
|
576
|
+
## Self-Review
|
|
577
|
+
|
|
578
|
+
**Spec coverage:**
|
|
579
|
+
- Accept string / ArrayBuffer / TypedArray / DataView -> Task 1 dispatcher.
|
|
580
|
+
- Bytecode-or-source via magic sniff -> unchanged `evaluateJavaScript`; exercised in Task 3 (bytecode) and Task 2 (source).
|
|
581
|
+
- Native, tamper-proof detection; trivial JS shim -> Task 1 (`11-Worker.js` unchanged, verified by Task 2 tamper cases).
|
|
582
|
+
- DataView via captured getters in `captureIntrinsics` -> Task 1, Steps 3-4.
|
|
583
|
+
- Copy at construction -> `copyBufferBytes` in Task 1, Step 5.
|
|
584
|
+
- Honor byteOffset/byteLength -> Task 1 TypedArray/DataView branches; Task 2 offset case.
|
|
585
|
+
- Empty binary -> TypeError; empty string unchanged -> `copyBufferBytes` length check (Task 1); Task 2 empty cases.
|
|
586
|
+
- Detached buffer -> TypeError -> `copyBufferBytes` detached check (Task 1); Task 2/Task 1 detached cases.
|
|
587
|
+
- Wrong type -> TypeError -> Task 1 `else` branches; Task 1/Task 2 negative cases.
|
|
588
|
+
- No change to shared `jsi.h` -> confirmed; DataView helpers live in the extensions layer.
|
|
589
|
+
- Tests under `test/hermes/worker/` and `unittests/API` -> Tasks 2 and 1/3.
|
|
590
|
+
|
|
591
|
+
**Placeholder scan:** none — every step contains concrete code/commands.
|
|
592
|
+
|
|
593
|
+
**Type consistency:** helper names (`isDataView`, `dataViewBuffer`, `dataViewByteOffset`, `dataViewByteLength`), `copyBufferBytes(rt, ArrayBuffer, size_t, size_t)`, and `startWorker(rt, Object, std::string)` are used identically in Task 1 declarations and call sites. `startWorkerThread(std::string)` is the existing method, unchanged.
|
|
594
|
+
|
|
595
|
+
## Execution Handoff
|
|
596
|
+
|
|
597
|
+
Plan complete and saved to `doc/superpowers/plans/2026-07-28-worker-arraybuffer-input.md`.
|