From 05a48085b8fa7bcc2e059663b3b0ec0353e6d21e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 12 Sep 2026 17:41:31 +0000 Subject: [PATCH 1/2] [JSC] WebAssembly.Exception: the traceStack stack is the stack of an Error created there The constructor builds the `stack` of a `traceStack: true` exception with Interpreter::stackTraceAsString. That skips VM::onComputeErrorInfoJSValue, the hook an embedder uses to format and source-map every Error stack. In bun the exception's stack is raw `fn@url:line:col` text with positions of the transpiled file, next to V8-style, source-mapped Error stacks. Under USE(BUN_JSC_ADDITIONS), create an Error at that point and store its `stack`. The value takes the same path as `new Error().stack`, with or without the hook. The property stays a prototype getter over a private slot. With no frames (Error.stackTraceLimit is 0 or not a number) the stack is undefined, as for an Error, and no longer an empty string. --- .../exception-trace-stack-is-error-stack.js | 15 +++++++++++++++ .../wasm/js/WebAssemblyExceptionConstructor.cpp | 11 +++++++++++ 2 files changed, 26 insertions(+) create mode 100644 JSTests/wasm/stress/exception-trace-stack-is-error-stack.js diff --git a/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js b/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js new file mode 100644 index 0000000000000..00fa18452a53d --- /dev/null +++ b/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js @@ -0,0 +1,15 @@ +import * as assert from "../assert.js"; + +const tag = new WebAssembly.Tag({ parameters: ["i32"] }); + +function makeBoth() { + return [new WebAssembly.Exception(tag, [0], { traceStack: true }), new Error()]; +} + +// Both are created on one line, so only the column of the first frame differs. +const withoutFirstColumn = stack => stack.replace(/:\d+$/m, ""); + +const [exception, error] = makeBoth(); +assert.eq(typeof exception.stack, "string"); +assert.eq(withoutFirstColumn(exception.stack), withoutFirstColumn(error.stack)); +assert.eq(Object.hasOwn(exception, "stack"), false); diff --git a/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp b/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp index c7fcedf632383..1bb47dfbf1319 100644 --- a/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp +++ b/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp @@ -29,6 +29,7 @@ #if ENABLE(WEBASSEMBLY) #include "BuiltinNames.h" +#include "ErrorInstance.h" #include "Interpreter.h" #include "IteratorOperations.h" #include "JITOpaqueByproducts.h" @@ -100,10 +101,20 @@ JSC_DEFINE_HOST_FUNCTION(constructJSWebAssemblyException, (JSGlobalObject* globa auto* exception = JSWebAssemblyException::create(vm, structure, tag->tag(), WTF::move(payload)); if (traceStack) { +#if USE(BUN_JSC_ADDITIONS) + // Take the stack of an Error created here, so that it goes through VM::onComputeErrorInfoJSValue + // like every other stack. Interpreter::stackTraceAsString skips that hook. + constexpr bool useCurrentFrame = false; + auto* error = ErrorInstance::create(vm, globalObject->errorStructure(), String(), JSValue(), nullptr, TypeNothing, ErrorType::Error, useCurrentFrame); + JSValue stack = error->get(globalObject, vm.propertyNames->stack); + RETURN_IF_EXCEPTION(scope, { }); + exception->putDirect(vm, vm.propertyNames->builtinNames().stackPrivateName(), stack, static_cast(PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete)); +#else Vector stackTrace; constexpr size_t framesToSkip = 1; vm.interpreter.getStackTrace(exception, stackTrace, framesToSkip, globalObject->stackTraceLimit().value_or(0)); exception->putDirect(vm, vm.propertyNames->builtinNames().stackPrivateName(), jsString(vm, Interpreter::stackTraceAsString(vm, stackTrace)), static_cast(PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly | PropertyAttribute::DontDelete)); +#endif } return JSValue::encode(exception); From bb6c653fe6da23005dd8f53b090fa853f13a902f Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 12 Sep 2026 19:46:02 +0000 Subject: [PATCH 2/2] Include ErrorInstanceInlines.h, and assert the no-frames case in the test ErrorInstance::create(VM&, Structure*, const String&, ...) is inline in ErrorInstanceInlines.h. This file saw it only through the unified bundle. With Error.stackTraceLimit = 0 the traceStack stack is undefined, as for an Error. Interpreter::stackTraceAsString gave "". The jsc shell installs no VM::onComputeErrorInfoJSValue hook, so this is the one difference it can see, and it makes the test fail without the change. --- JSTests/wasm/stress/exception-trace-stack-is-error-stack.js | 6 ++++++ .../wasm/js/WebAssemblyExceptionConstructor.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js b/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js index 00fa18452a53d..f1ae4c6e6eeaa 100644 --- a/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js +++ b/JSTests/wasm/stress/exception-trace-stack-is-error-stack.js @@ -13,3 +13,9 @@ const [exception, error] = makeBoth(); assert.eq(typeof exception.stack, "string"); assert.eq(withoutFirstColumn(exception.stack), withoutFirstColumn(error.stack)); assert.eq(Object.hasOwn(exception, "stack"), false); + +// With no frames an Error has no stack. Interpreter::stackTraceAsString gave "" here. +Error.stackTraceLimit = 0; +const [exceptionWithoutFrames, errorWithoutFrames] = makeBoth(); +assert.eq(errorWithoutFrames.stack, undefined); +assert.eq(exceptionWithoutFrames.stack, undefined); diff --git a/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp b/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp index 1bb47dfbf1319..f9fea8175dc09 100644 --- a/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp +++ b/Source/JavaScriptCore/wasm/js/WebAssemblyExceptionConstructor.cpp @@ -29,7 +29,7 @@ #if ENABLE(WEBASSEMBLY) #include "BuiltinNames.h" -#include "ErrorInstance.h" +#include "ErrorInstanceInlines.h" #include "Interpreter.h" #include "IteratorOperations.h" #include "JITOpaqueByproducts.h"