Truly independent web browser
LibJS: Leave the frames of subclass constructors out of error stacks
Andreas Kling91e2fe9f5564
diff --git a/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs b/Libraries/LibJS/Rust/src/runtime/aggregate_error.rsindex 414ac5be028..3f5742cb421 100644--- a/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs+++ b/Libraries/LibJS/Rust/src/runtime/aggregate_error.rs@@ -13,6 +13,7 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::object::Object;use crate::runtime::error::Error;+use crate::runtime::function_object::FunctionObject;use crate::runtime::realm::Realm;#[repr(C)]@@ -38,9 +39,14 @@ impl AggregateError {}}- pub fn new_in_constructor(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>) -> AggregateError {+ pub fn new_in_constructor(+ vm: &Vm,+ realm: Gc<Realm>,+ prototype: Gc<Object>,+ new_target: Gc<FunctionObject>,+ ) -> AggregateError {AggregateError {- base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype),+ base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype, new_target),}}diff --git a/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rsindex 155d576c6f0..4638c6193c7 100644--- a/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/aggregate_error_constructor.rs@@ -87,7 +87,9 @@ 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_in_constructor(vm, realm, prototype));+ let aggregate_error = realm.create_object(vm, |_| {+ AggregateError::new_in_constructor(vm, realm, prototype, new_target)+ });// 3. If message is not undefined, thenif !message.is_undefined() {diff --git a/Libraries/LibJS/Rust/src/runtime/error.rs b/Libraries/LibJS/Rust/src/runtime/error.rsindex 28feff7fb54..a2b56f0b884 100644--- a/Libraries/LibJS/Rust/src/runtime/error.rs+++ b/Libraries/LibJS/Rust/src/runtime/error.rs@@ -18,6 +18,7 @@ use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::aggregate_error::AggregateError;use crate::runtime::error_data::{CompactTraceback, ErrorData};+use crate::runtime::function_object::FunctionObject;use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, ObjectMethods};use crate::runtime::primitive_string::PrimitiveString;use crate::runtime::property_attributes::{Attribute, PropertyAttributes};@@ -160,8 +161,15 @@ 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, realm: Gc<Realm>, class: &'static Class, prototype: Gc<Object>) -> Error {+ /// An error an Error constructor is creating for `new_target`: its stack leaves out the frames of the constructors,+ /// as ErrorData::new_in_constructor() says.+ pub fn new_in_constructor(+ vm: &Vm,+ realm: Gc<Realm>,+ class: &'static Class,+ prototype: Gc<Object>,+ new_target: Gc<FunctionObject>,+ ) -> Error {Error {base: Object::new_with_prototype(vm,@@ -170,7 +178,7 @@ impl Error {Some(prototype),MayInterfereWithIndexedPropertyAccess::No,),- error_data: ErrorData::new_in_constructor(vm),+ error_data: ErrorData::new_in_constructor(vm, new_target),}}@@ -257,9 +265,14 @@ macro_rules! define_native_errors {}}- pub fn new_in_constructor(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>) -> $class {+ pub fn new_in_constructor(+ vm: &Vm,+ realm: Gc<Realm>,+ prototype: Gc<Object>,+ new_target: Gc<FunctionObject>,+ ) -> $class {$class {- base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype),+ base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype, new_target),}}diff --git a/Libraries/LibJS/Rust/src/runtime/error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/error_constructor.rsindex d5ffe4cb4c7..480676d1c6b 100644--- a/Libraries/LibJS/Rust/src/runtime/error_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/error_constructor.rs@@ -117,7 +117,9 @@ 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_in_constructor(vm, realm, Error::CLASS, prototype));+ let error = realm.create_object(vm, |_| {+ Error::new_in_constructor(vm, realm, Error::CLASS, prototype, new_target)+ });// 3. If message is not undefined, thenif !message.is_undefined() {@@ -276,7 +278,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_in_constructor(vm, realm, prototype));+ let error = realm.create_object(vm, |_| $class::new_in_constructor(vm, realm, prototype, new_target));// 3. If message is not undefined, thenif !message.is_undefined() {diff --git a/Libraries/LibJS/Rust/src/runtime/error_data.rs b/Libraries/LibJS/Rust/src/runtime/error_data.rsindex 14c8b677643..f6f8653199a 100644--- a/Libraries/LibJS/Rust/src/runtime/error_data.rs+++ b/Libraries/LibJS/Rust/src/runtime/error_data.rs@@ -118,9 +118,23 @@ impl ErrorData {}/// 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::capture(vm, SkippedFrames::Count(1), stack_trace_limit(vm))+ /// code that made the error, never the constructors that made it: the frames up to and including the innermost+ /// frame of `new_target`, which are those of the constructors of the subclasses an error class extends, are left+ /// out. When `new_target` has no frame on the stack, as with Reflect.construct(), only the frame of the+ /// constructor itself is.+ pub fn new_in_constructor(vm: &Vm, new_target: Gc<FunctionObject>) -> Self {+ let mut frames_up_to_new_target = 0;+ let mut found_new_target = false;+ vm.for_each_stack_frame_top_to_bottom(|frame| {+ frames_up_to_new_target += 1;+ if frame.function == Some(new_target) {+ found_new_target = true;+ return ControlFlow::Break(());+ }+ ControlFlow::Continue(())+ });+ let skipped_frames = if found_new_target { frames_up_to_new_target } else { 1 };+ Self::capture(vm, SkippedFrames::Count(skipped_frames), stack_trace_limit(vm))}/// Error data with every frame of the call stack, whatever Error.stackTraceLimit is, for a trace of the running codediff --git a/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs b/Libraries/LibJS/Rust/src/runtime/suppressed_error.rsindex 86c77dcff37..be9b4c80607 100644--- a/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs+++ b/Libraries/LibJS/Rust/src/runtime/suppressed_error.rs@@ -13,6 +13,7 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::object::Object;use crate::runtime::error::Error;+use crate::runtime::function_object::FunctionObject;use crate::runtime::realm::Realm;#[repr(C)]@@ -38,9 +39,14 @@ impl SuppressedError {}}- pub fn new_in_constructor(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>) -> SuppressedError {+ pub fn new_in_constructor(+ vm: &Vm,+ realm: Gc<Realm>,+ prototype: Gc<Object>,+ new_target: Gc<FunctionObject>,+ ) -> SuppressedError {SuppressedError {- base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype),+ base: Error::new_in_constructor(vm, realm, Self::CLASS, prototype, new_target),}}diff --git a/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs b/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rsindex 4bef768ce95..d2cb4553256 100644--- a/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/suppressed_error_constructor.rs@@ -88,7 +88,7 @@ impl SuppressedErrorConstructor {realm,new_target,Intrinsics::suppressed_error_prototype,- |_, prototype| SuppressedError::new_in_constructor(vm, realm, prototype),+ |_, prototype| SuppressedError::new_in_constructor(vm, realm, prototype, new_target),)?;// 3. If message is not undefined, thendiff --git a/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.subclasses.js b/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.subclasses.jsnew file mode 100644index 00000000000..be85df046c6--- /dev/null+++ b/Tests/LibJS/Runtime/builtins/Error/Error.prototype.stack.subclasses.js@@ -0,0 +1,74 @@+function framesOf(error) {+ return error.stack+ .split("\n")+ .filter(line => line.startsWith(" at "))+ .map(line => line.trim().split(" ")[1]);+}++test("the constructors of error subclasses are left out", () => {+ class MyError extends Error {+ constructor(message) {+ super(message);+ }+ }+ class DerivedError extends MyError {+ constructor() {+ super("derived");+ }+ }+ class ImplicitError extends TypeError {}+ function make(ErrorClass) {+ return new ErrorClass([]);+ }+ for (const ErrorClass of [MyError, DerivedError, ImplicitError, Error, TypeError, AggregateError])+ expect(framesOf(make(ErrorClass))[0]).toBe("make");+});++test("calling an error constructor leaves out only its own frame", () => {+ function call() {+ return Error("called");+ }+ function callType() {+ return RangeError("called");+ }+ expect(framesOf(call())[0]).toBe("call");+ expect(framesOf(callType())[0]).toBe("callType");+});++test("a new.target with no frame on the stack leaves out only the frame of the constructor", () => {+ class Unrelated extends Error {}+ function reflect() {+ return Reflect.construct(TypeError, ["reflected"], Unrelated);+ }+ // NB: The frame of Reflect.construct() itself is not left out.+ expect(framesOf(reflect()).slice(0, 2)).toEqual(["<unknown>", "reflect"]);+});++test("a function constructor that makes an error for its new.target is left out", () => {+ function LegacyError(message) {+ return Reflect.construct(Error, [message], new.target);+ }+ LegacyError.prototype = Object.create(Error.prototype);+ function make() {+ return new LegacyError("legacy");+ }+ expect(framesOf(make())[0]).toBe("make");+});++test("a recursive constructor leaves out its innermost frame only", () => {+ class Nested extends Error {+ constructor(depth) {+ if (depth > 0) {+ super(String(new Nested(depth - 1).stack));+ } else {+ super("innermost");+ }+ }+ }+ const inner = new Nested(1).message;+ const frames = inner+ .split("\n")+ .filter(line => line.startsWith(" at "))+ .map(line => line.trim().split(" ")[1]);+ expect(frames[0]).toBe("Nested");+});