Skip to content

Truly independent web browser

LibJS: Leave the Error constructor's own frame out of error stacks

sideshowbarker2ff6f9379c9f
diff --git a/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs b/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs
index 420e8b70711..56ca73061ba 100644
--- a/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs
+++ b/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs
@@ -38,6 +38,12 @@ impl AggregateError {
}
}
+ pub fn new_in_constructor(vm: &Vm, prototype: Gc<Object>) -> AggregateError {
+ AggregateError {
+ base: Error::new_in_constructor(vm, Self::CLASS, prototype),
+ }
+ }
+
pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<AggregateError> {
realm.create_object(
vm,
diff --git a/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs
index b375a6499e2..8c8230d13f3 100644
--- a/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs
@@ -88,7 +88,7 @@ impl AggregateErrorConstructor {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
let prototype = get_prototype_from_constructor(vm, new_target, Intrinsics::aggregate_error_prototype)?;
- let aggregate_error = realm.create_object(vm, AggregateError::new(vm, prototype));
+ let aggregate_error = realm.create_object(vm, AggregateError::new_in_constructor(vm, prototype));
// 3. If message is not undefined, then
if !message.is_undefined() {
diff --git a/Libraries/LibJS/Rust/src/runtime/error.rs b/Libraries/LibJS/Rust/src/runtime/error.rs
index e2aa4272fa8..af05599e037 100644
--- a/Libraries/LibJS/Rust/src/runtime/error.rs
+++ b/Libraries/LibJS/Rust/src/runtime/error.rs
@@ -154,6 +154,14 @@ impl Error {
}
}
+ /// An error an Error constructor is creating: its stack leaves out the constructor's own frame.
+ pub fn new_in_constructor(vm: &Vm, class: &'static Class, prototype: Gc<Object>) -> Error {
+ Error {
+ base: Object::new_with_prototype(vm, class, prototype, MayInterfereWithIndexedPropertyAccess::No),
+ error_data: ErrorData::new_in_constructor(vm),
+ }
+ }
+
fn initialize(object: &Object, vm: &Vm, _realm: Gc<Realm>) {
let error_data = error_data_of_error(object).expect("an Error has error data");
vm.heap()
@@ -229,6 +237,12 @@ macro_rules! define_native_errors {
}
}
+ pub fn new_in_constructor(vm: &Vm, prototype: Gc<Object>) -> $class {
+ $class {
+ base: Error::new_in_constructor(vm, Self::CLASS, prototype),
+ }
+ }
+
pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<$class> {
realm.create_object(vm, $class::new(vm, realm.intrinsics().$prototype(vm)))
}
diff --git a/Libraries/LibJS/Rust/src/runtime/error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/error_constructor.rs
index e7b3ef1ca39..858244ce1d4 100644
--- a/Libraries/LibJS/Rust/src/runtime/error_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/error_constructor.rs
@@ -98,7 +98,7 @@ impl ErrorConstructor {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
let prototype = get_prototype_from_constructor(vm, new_target, Intrinsics::error_prototype)?;
- let error = realm.create_object(vm, Error::new(vm, Error::CLASS, prototype));
+ let error = realm.create_object(vm, Error::new_in_constructor(vm, Error::CLASS, prototype));
// 3. If message is not undefined, then
if !message.is_undefined() {
@@ -211,7 +211,7 @@ macro_rules! define_native_error_constructors {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »).
let prototype = get_prototype_from_constructor(vm, new_target, Intrinsics::$prototype)?;
- let error = realm.create_object(vm, $class::new(vm, prototype));
+ let error = realm.create_object(vm, $class::new_in_constructor(vm, prototype));
// 3. If message is not undefined, then
if !message.is_undefined() {
diff --git a/Libraries/LibJS/Rust/src/runtime/error_data.rs b/Libraries/LibJS/Rust/src/runtime/error_data.rs
index 7cf35ba0bb8..e7491ca2a3a 100644
--- a/Libraries/LibJS/Rust/src/runtime/error_data.rs
+++ b/Libraries/LibJS/Rust/src/runtime/error_data.rs
@@ -52,7 +52,16 @@ pub struct ErrorData {
impl ErrorData {
pub fn new(vm: &Vm) -> Self {
Self {
- traceback: Self::populate_stack(vm),
+ traceback: Self::populate_stack(vm, 0),
+ cached_string: Cell::new(None),
+ }
+ }
+
+ /// Error data captured by an Error constructor, whose own frame is the innermost one. A stack string shows the
+ /// code that called the constructor, never the constructor itself, so that frame is left out.
+ pub fn new_in_constructor(vm: &Vm) -> Self {
+ Self {
+ traceback: Self::populate_stack(vm, 1),
cached_string: Cell::new(None),
}
}
@@ -70,10 +79,10 @@ impl ErrorData {
self.traceback_storage_size() + function_names_size
}
- fn populate_stack(vm: &Vm) -> Vec<TracebackFrame> {
+ fn populate_stack(vm: &Vm, skipped_innermost_frames: usize) -> Vec<TracebackFrame> {
let stack_trace = vm.stack_trace();
- let mut traceback = Vec::with_capacity(stack_trace.len());
- for element in stack_trace {
+ let mut traceback = Vec::with_capacity(stack_trace.len().saturating_sub(skipped_innermost_frames));
+ for element in stack_trace.into_iter().skip(skipped_innermost_frames) {
traceback.push(TracebackFrame {
function_name: element
.function
diff --git a/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs b/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs
index 298ac93dc76..f54f6ab5ed2 100644
--- a/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs
+++ b/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs
@@ -38,6 +38,12 @@ impl SuppressedError {
}
}
+ pub fn new_in_constructor(vm: &Vm, prototype: Gc<Object>) -> SuppressedError {
+ SuppressedError {
+ base: Error::new_in_constructor(vm, Self::CLASS, prototype),
+ }
+ }
+
pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<SuppressedError> {
realm.create_object(
vm,
diff --git a/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs
index c54c2d97939..451b2bee4d3 100644
--- a/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs
@@ -91,7 +91,7 @@ impl SuppressedErrorConstructor {
realm,
new_target,
Intrinsics::suppressed_error_prototype,
- |prototype| SuppressedError::new(vm, prototype),
+ |prototype| SuppressedError::new_in_constructor(vm, prototype),
)?;
// 3. If message is not undefined, then
diff --git a/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.js b/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.js
index adb4d9e88ae..48b69b82c81 100644
--- a/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.js
+++ b/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.js
@@ -5,7 +5,6 @@ const stackSetter = stackDescriptor.set;
describe("getter - normal behavior", () => {
test("basic functionality", () => {
const stackFrames = [
- /^ at .*Error$/,
/^ at .+[\\/]Error[\\/]Error\.prototype\.stack\.js:\d+:\d+$/,
/^ at test \(.+[\\/]test-common\.js:\d+:\d+\)$/,
/^ at .+[\\/]Error[\\/]Error\.prototype\.stack\.js:6:9$/,
diff --git a/Tests/LibWeb/Text/expected/WebIDL/interface-objects.txt b/Tests/LibWeb/Text/expected/WebIDL/interface-objects.txt
index 6f551636ad0..0e89db5dbca 100644
--- a/Tests/LibWeb/Text/expected/WebIDL/interface-objects.txt
+++ b/Tests/LibWeb/Text/expected/WebIDL/interface-objects.txt
@@ -79,6 +79,6 @@ Option.prototype: true
HTMLImageElement.prototype.constructor: true
new Image() instanceof HTMLImageElement: true
Stack frames:
-new Event(): Error <- toString <- Event <- stackFrameNames
-new Option(): Error <- toString <- <unknown> <- stackFrameNames
-new Image(): Error <- valueOf <- <unknown> <- stackFrameNames
+new Event(): toString <- Event <- stackFrameNames
+new Option(): toString <- <unknown> <- stackFrameNames
+new Image(): valueOf <- <unknown> <- stackFrameNames
diff --git a/Tests/LibWeb/Text/expected/error-stack-must-contain-full-url.txt b/Tests/LibWeb/Text/expected/error-stack-must-contain-full-url.txt
index 2c66d16513f..b5989be4902 100644
--- a/Tests/LibWeb/Text/expected/error-stack-must-contain-full-url.txt
+++ b/Tests/LibWeb/Text/expected/error-stack-must-contain-full-url.txt
@@ -1,9 +1,7 @@
classic: Error
- at Error
at blob:<origin>/[flaky-uuid-replaced]:3:36
module: Error
- at Error
at blob:<origin>/[flaky-uuid-replaced]:3:36
at <unknown>