Skip to content

Use createError in JSError and forward it in RuntimeDecorator - #2203

Open
mrousavy wants to merge 2 commits into
facebook:static_hfrom
mrousavy:jsi-jserror-create-error
Open

mrousavy wants to merge 2 commits into
facebook:static_hfrom
mrousavy:jsi-jserror-create-error

Conversation

@mrousavy

@mrousavy mrousavy commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

JSError(rt, message) still creates its value by looking up globalThis.Error and calling it, which predates IRuntime::createError. It now uses rt.createError(...) like the JSError::createXError factories, with the same try/catch fallback. The default Runtime::createError makes the same global call, so runtimes without a native implementation behave as before. On Hermes every JSError thrown from native code skips a global lookup and a JS call, and message, stack and what() come out the same.

RuntimeDecorator and WithRuntimeDecorator didn't forward the createError family, so decorated runtimes (the thread-safe runtime, TracingRuntime) quietly fell back to the global constructor calls. They forward to plain() now, through Around for WithRuntimeDecorator, and they're public like in jsi::Runtime.

One visible difference on Hermes, same as the other createXError APIs: if user code replaced or deleted globalThis.Error, JSError(rt, message) no longer calls the replacement and creates a real Error anyway.

Test Plan

Two new tests in testlib.cpp. JSErrorMessageTest checks the value is an Error with the right message and a string stack, and that a counting globalThis.Error replacement gets called exactly as often as rt.createError calls it, so it holds for native and default implementations. DecoratorCreateErrorTest checks that all seven createXError calls, and JSError, reach the wrapped runtime once through both decorators, and that Around runs once.

JSErrorDoesNotInfinitelyRecurse and CreateJSErrorPropagatesOOMExceptionTest run through decorators that use the default Runtime::createError. This keeps the missing-constructor fallback and OOM propagation covered, since both tests depend on changing globalThis.Error.

APITests (ASan + Debug, macOS arm64, HERMESVM_EXCEPTION_ON_OOM=ON): 362/362 pass, including SynthTrace and all three OOM tests. Built with -O1 and HERMES_ENABLE_WERROR=ON; the existing GoogleTest uninitialized-const-pointer warning is demoted for AppleClang 21.

cmake --build cmake-build-asan-oom --target APITests -j 8
cmake-build-asan-oom/unittests/API/APITests

Before the OOM test adaptation, CreateJSErrorPropagatesOOMExceptionTest fails because it throws JSError instead of JSOutOfMemoryError. It passes with the decorator. The earlier checks with the jsi.cpp and decorator.h changes reverted also made the new JSI tests fail.

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Sep 25, 2026
@mrousavy
mrousavy deployed to npm-publish September 25, 2026 23:32 — with GitHub Actions Active
JSError(rt, message) built its value by looking up and calling the global
Error function, which predates IRuntime::createError. Use rt.createError
instead, as the JSError::createXError factories already do. Runtimes
without a native implementation behave as before, since the default
Runtime::createError makes the same global call. Runtimes with a native
implementation skip the global lookup and JS call.

RuntimeDecorator and WithRuntimeDecorator did not override the createError
family, so decorated runtimes (e.g. the thread-safe runtime and
TracingRuntime) fell back to the default global calls instead of the
wrapped runtime's implementation. Forward them.

JSErrorDoesNotInfinitelyRecurse now runs through a decorator that keeps
the default createError, so it still covers the fallback when creating
the Error fails.
@mrousavy
mrousavy force-pushed the jsi-jserror-create-error branch from e050793 to f3a9eab Compare September 26, 2026 13:16
@mrousavy
mrousavy deployed to npm-publish September 26, 2026 13:41 — with GitHub Actions Active
// implementation, which calls the global Error constructor, so that removing
// it below makes creating the Error fail even if the runtime has a native
// createError.
class RD : public RuntimeDecorator<Runtime, Runtime> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do the same for test CreateJSErrorPropagatesOOMExceptionTest.

lavenzg pushed a commit to lavenzg/react-native that referenced this pull request Oct 7, 2026
…eact#58936)

Summary:


`IRuntime::length(const String&)` came in with "Add `String::length` API" (590396bd4 on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime.

It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (facebook/hermes#2203) and this one didn't.

For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here.

`WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`.

`TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced.

X-link: facebook/hermes#2205

Test Plan:
Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: avp

Differential Revision: D123943189

Pulled By: lavenzg
Run CreateJSErrorPropagatesOOMExceptionTest through a decorator that calls
Runtime::createError, so its replacement global Error constructor still
triggers OOM after JSError starts using the native createError API.

Verified that the test fails before this change and passes afterward with
HERMESVM_EXCEPTION_ON_OOM=ON. All 362 API tests pass in ASan+Debug.
@mrousavy
mrousavy deployed to npm-publish October 7, 2026 22:15 — with GitHub Actions Active
meta-codesync Bot pushed a commit that referenced this pull request Oct 8, 2026
…2205)

Summary:
X-link: react/react-native#58936

`IRuntime::length(const String&)` came in with "Add `String::length` API" (590396b on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime.

It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (#2203) and this one didn't.

For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here.

`WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`.

`TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced.

Pull Request resolved: #2205

Test Plan:
Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: avp

Differential Revision: D123943189

Pulled By: lavenzg

fbshipit-source-id: 519fdde9f2ad46ce906567bfd1d94d787015915c
meta-codesync Bot pushed a commit that referenced this pull request Oct 8, 2026
…2205)

Summary:
X-link: react/react-native#58936

`IRuntime::length(const String&)` came in with "Add `String::length` API" (590396b on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime.

It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (#2203) and this one didn't.

For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here.

`WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`.

`TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced.

Pull Request resolved: #2205

Test Plan:
Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: avp

Differential Revision: D123943189

Pulled By: lavenzg

fbshipit-source-id: 519fdde9f2ad46ce906567bfd1d94d787015915c
meta-codesync Bot pushed a commit to react/react-native that referenced this pull request Oct 8, 2026
…58936)

Summary:
Pull Request resolved: #58936

`IRuntime::length(const String&)` came in with "Add `String::length` API" (590396bd4 on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime.

It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (facebook/hermes#2203) and this one didn't.

For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here.

`WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`.

`TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced.

X-link: facebook/hermes#2205

Test Plan:
Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: avp

Differential Revision: D123943189

Pulled By: lavenzg

fbshipit-source-id: 519fdde9f2ad46ce906567bfd1d94d787015915c
meta-codesync Bot pushed a commit that referenced this pull request Oct 8, 2026
…2205)

Summary:
X-link: react/react-native#58936

`IRuntime::length(const String&)` came in with "Add `String::length` API" (590396b on static_h, D96030262), and that commit didn't touch `decorator.h`. Neither `RuntimeDecorator` nor `WithRuntimeDecorator` overrides it, so on a decorated runtime `length()` falls back to the default `utf16(str).size()` and never reaches the wrapped runtime.

It looks like an oversight. The same stack added `push`, `tryGetMutableBuffer`, `detached` and the TypedArray methods, and each of those commits added decorator overrides, including one for this method's overload `length(const TypedArray&)`. The `createError*` commit (#2203) and this one didn't.

For Hermes-backed decorators the value is already right, because Hermes' `utf16()` is exact, but every call copies the whole string instead of reading the length. Runtimes with a native `length()` and the default `utf16()` get wrong answers. React Native's `JSCRuntime` is one: built against the system JavaScriptCore and wrapped in a `RuntimeDecorator`, `'a\uD800b'` has length 1 instead of 3 and `'x\uDC00yz'` has 1 instead of 4. With this change both match the undecorated runtime. That setup lives outside this repo, so it isn't a test here.

`WithRuntimeDecorator` needs the override too. Otherwise it would inherit the plain forward and `length()` would skip `Around`, which it currently goes through via `utf16()`.

`TracingRuntime` no longer records an incidental `Utf16Record` when `length()` is called. That record only came from the `utf16()` fallback. I didn't add a record for `length()`: strings are immutable, and `TracingRuntime` already leaves pure queries like `size(Array)` untraced.

Pull Request resolved: #2205

Test Plan:
Added `JSITest.DecoratorForwardsStringLength`. An inner decorator returns a sentinel from `length()` and counts `length()` and `utf16()` calls. The test checks that a `RuntimeDecorator` and a `WithRuntimeDecorator` over it both reach that `length()` without calling `utf16()`, and that `Around` runs once. It fails without the change (it gets 5 from the `utf16()` fallback) and passes with it. The full `APITests` binary passes (357 tests) in a Debug + ASan build, including the SynthTrace and TraceInterpreter tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: avp

Differential Revision: D123943189

Pulled By: lavenzg

fbshipit-source-id: 519fdde9f2ad46ce906567bfd1d94d787015915c

This branch was successfully deployed

1 active deployment
npm-publish — 336a2cba Deployed Oct 7, 2026 by mrousavy via publish #1548
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants