Repository navigation
[1.9] Fix ClassCastException when reading "stack" of an error with a changed prototype - #2544
Merged
gbrail merged 1 commit intoOct 31, 2026
Conversation
…d prototype NativeError.getStackDelegated() cast the error's prototype to NativeError to look up the stack trace settings. If the prototype was replaced, for example by Object.setPrototypeOf() as transpiled "class MyError extends Error" does, this threw a Java ClassCastException, or a NullPointerException for a null prototype. Fall back to the default settings instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
Here, are you sure that this is a 1.9 bug or one in the master branch? |
Author
|
Partly both. The ClassCastException from the description is 1.9 only: on master, #2295 changed the cast to ScriptableObject. But with a null prototype, master still throws a Java NullPointerException that a script's try/catch can't catch: var e = new Error("m");
Object.setPrototypeOf(e, null);
e.stack; // java.lang.NullPointerException in NativeError.getStackDelegated()I'll open a PR for master with the same fix and tests first, and then update this PR to be a backport of it. |
Author
|
I opened #2550 with the fix for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On 1.9.2, reading
stackof an error whose prototype was replaced throws aJava
ClassCastException, which scripts cannot catch:This is exactly what transpiled
class MyError extends Error {}does (Babel'swrapNativeSuper), so reading.stackof any transpiled custom error fails.Cause and fix
NativeError.getStackDelegated()casts the error's prototype toNativeErrorto look up the stack trace settings (
stackTraceLimit,prepareStackTrace).On
masterthis cast was changed toScriptableObjectin #2295 ("ConvertNativeErrorto descriptor"), but that PR can't be backported on its own.This PR only checks whether the prototype is a
ScriptableObjectand otherwiseuses the default settings. This also covers a
nullprototype and aProxyasprototype. A
nullprototype still throws aNullPointerExceptiononmaster; I can open a separate PR for that.Tests
New test
ErrorPropertiesTest.stackWithChangedPrototypefor a plain object,nulland aProxyas prototype, in all modes. It fails without the fix. Thefull test suite passes, except
NativeDateTest.toTimeStringEST, which alsofails on an unmodified
rhino_1_9_backportson my machine (time zone data).test262.propertiesdoesn't change.A note on how this was developed
I developed this PR with the help of an AI coding agent (Claude Code), and I
reviewed, tested and take responsibility for all of the changes. If the project
has concerns about or rules for AI-assisted contributions, please let me know.
I'm dyslexic, so I usually let AI help me write or proofread text like this
description. If anything reads unclear, please ask and I'll clarify.
🤖 Generated with Claude Code