Skip to content

Commit

Permalink
JSError: look for stack data in the prototype chain
Browse files Browse the repository at this point in the history
Summary:
Imported from static_h
Original Author: [email protected]
Original Git: 8b7a9f8
Original Reviewed By: avp
Original Revision: D61870728

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

See #1496

Reviewed By: fbmal7

Differential Revision: D62357838

fbshipit-source-id: 3873bfd8bcb5c16998dbec67f44ad4c098179758
  • Loading branch information
Tzvetan Mikov authored and facebook-github-bot committed Sep 8, 2024
1 parent 468f005 commit db6d12e
Show file tree
Hide file tree
Showing 2 changed files with 33 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 @@ -57,7 +57,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 @@ -71,6 +75,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
26 changes: 26 additions & 0 deletions test/hermes/error-in-proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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

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 db6d12e

Please sign in to comment.