Skip to content

Commit

Permalink
JSError: look for stack data in the prototype chain (facebook#1497)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#1497

Look for stack data in the entire prototype chain in order to accommodate
usage like the one in the test.

See facebook#1496

Reviewed By: avp

Differential Revision: D61870728
  • Loading branch information
Tzvetan Mikov authored and facebook-github-bot committed Aug 28, 2024
1 parent fd99ca5 commit d9f2472
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/VM/JSError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ void JSErrorBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
CallResult<Handle<JSError>> JSError::getErrorFromStackTarget(
Runtime &runtime,
Handle<JSObject> targetHandle) {
if (targetHandle) {
MutableHandle<JSObject> mutHnd =
runtime.makeMutableHandle<JSObject>(targetHandle.get());
targetHandle = mutHnd;

while (targetHandle) {
NamedPropertyDescriptor desc;
bool exists = JSObject::getOwnNamedDescriptor(
targetHandle,
Expand All @@ -68,6 +72,8 @@ CallResult<Handle<JSError>> JSError::getErrorFromStackTarget(
if (vmisa<JSError>(*targetHandle)) {
return Handle<JSError>::vmcast(targetHandle);
}

mutHnd.set(targetHandle->getParent(runtime));
}
return runtime.raiseTypeError(
"Error.stack getter called with an invalid receiver");
Expand Down
27 changes: 27 additions & 0 deletions test/hermes/error-in-proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %hermes %s | %FileCheck --match-full-lines %s
// RUN: %shermes -exec %s | %FileCheck --match-full-lines %s

const MyError = function(message) {
this.message = message;
};
MyError.prototype = new Error;
MyError.prototype.name = 'MyError';


try {
throw new MyError('1234')
} catch (e) {
if (e instanceof Error)
print("Caught Error", e.stack);
else
print("Caught non-Error");
}
// CHECK: Caught Error MyError: 1234
// CHECK-NEXT: at global{{.*}}

0 comments on commit d9f2472

Please sign in to comment.