Skip to content

refactor(compile): single-source the guest-error raise idiom (159 sites, −407 lines)#1271

Merged
nickna merged 1 commit into
mainfrom
worktree-guest-error-helper
Jul 11, 2026
Merged

refactor(compile): single-source the guest-error raise idiom (159 sites, −407 lines)#1271
nickna merged 1 commit into
mainfrom
worktree-guest-error-helper

Conversation

@nickna

@nickna nickna commented Jul 11, 2026

Copy link
Copy Markdown
Owner

The largest remaining item from the hygiene backlog: the 4-opcode guest-error raise tail (Ldstr msg; Newobj TS{Type,Range,Reference,Syntax}ErrorCtor; Call CreateException; Throw) was open-coded ~170× across the emitter files — any change to the raise idiom had to be edited everywhere, and two files had already grown local re-inventions.

Change

New GuestErrorEmitter static class with ThrowTypeError/ThrowRangeError/ThrowReferenceError/ThrowSyntaxError (constant message) and ThrowErrorFromStack (computed message already on the stack). 159 sites across 40 files converted via a same-identifier-anchored multiline rewrite — the pattern required the same ILGenerator identifier and the same runtime expression on all four lines, so only exact matches were touched. The two file-local helpers in Objects.StrictShared now delegate through it.

Deliberately not converted (genuinely different shapes): sites with error-code setters, non-throwing CreateException uses, the ILCompiler.Classes error-ctor lookup table, and TSArray's label-dispatch throw variants.

The helper emits the identical opcode stream, so compiled output is byte-for-byte unchanged.

Verification

  • Full unit suite: 15,452/15,452 pass — including the compiled-mode integration tests, which compile and run whole programs (a mis-converted site would surface as a wrong error type or crash)
  • Test262 interpreter baseline diff clean
  • Net −407 lines (204+, 611−)

…tErrorEmitter

The 4-opcode tail (Ldstr msg; Newobj TS{Type,Range,Reference,Syntax}
ErrorCtor; Call CreateException; Throw) was open-coded ~170x across the
emitter files. GuestErrorEmitter now provides ThrowTypeError/
ThrowRangeError/ThrowReferenceError/ThrowSyntaxError (constant message)
and ThrowErrorFromStack (computed message on the stack); 159 sites
across 40 files were converted mechanically (same-identifier-anchored
multiline rewrite), including the two file-local helpers in
Objects.StrictShared whose bodies now delegate.

Deliberately NOT converted: sites with code setters, non-throwing
CreateException uses, the ILCompiler.Classes ctor lookup table, and
TSArray's label-dispatch variants — those are genuinely different
shapes. Emitted opcode stream is identical; helper calls emit the same
IL.
@nickna
nickna merged commit c6a5d51 into main Jul 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant