Skip to content

Error has a stack: where it was made, as V8 says it - #132

Merged
ASDAlexander77 merged 1 commit into
mainfrom
error-stack-main
Nov 30, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
error-stack-main

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Error.stack, the library side. The compiler side is ASDAlexander77/TypeScriptCompiler#1081.

RangeError: too far: 3
    at inner
    at outer
    at main
  • new Error() records the return addresses it was made from (wrappers/os.cpp, tslang_stack_capture). On Windows the stack is unwound by the function tables, as the uncaught-exception report walks it: RtlCaptureStackBackTrace stops at the JIT's code, which no image holds. On Linux it uses _Unwind_Backtrace. In the DLL or shared object the JIT loads, the library's own frames are left out.
  • Reading stack gives toString()'s line, then at f (a.ts:7) lines, at most 10. The frames are named by the program's runtime (the compiler's __tslang_stack_text, found in the executable's exports on Windows, or as a weak symbol on Linux), the same way the uncaught report names frames. A runtime without it (the released compiler) leaves module+0xoffset lines. The text is made the first time stack is read, and may be replaced.
  • wasm has no frames, so its stack is the first line alone (lib.wasm.ts), with no new host import.
  • The platform layers give core.ts error_stack_capture / error_stack_text (core/core.os.d.ts).

Testing

  • New tests/error_stack.ts, which checks the shape: the first line, at lines, at most 10, and that stack can be assigned. It passes under gc, rc, none and own, JIT and exe, on Windows with the compiler of #1081, and on Linux (WSL) and Windows with the released compiler (fallback lines). It prints nothing with "Error" in it, which tests.sh treats as a failure.
  • With the compiler of #1081: the JIT names frames (at inner / at outer / at main); an exe without --di shows program.exe+0x… up to main.

🤖 Generated with Claude Code

- `new Error()` captures the return addresses it was made from (wrappers/os.cpp tslang_stack_capture: on
  Windows unwound by the function tables, which the JIT's code has too; on Linux _Unwind_Backtrace); in the
  DLL or shared object the JIT loads, the library's own frames are left out
- `stack`, read: toString()'s first line, then `    at f (a.ts:7)` lines, at most 10. Named by the program's
  runtime (the compiler's `__tslang_stack_text`: the executable's runtime, or tslang for the JIT), as the
  report of an uncaught exception names frames; a runtime without it (an older compiler's) leaves
  `module+0xoffset` lines. It may be given another text.
- wasm has no frames: its `stack` is the first line alone (lib.wasm.ts)
- the platform layers give core.ts error_stack_capture / error_stack_text (core/core.os.d.ts)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f1f557f into main Nov 30, 2026
10 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