Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSError: look for stack data in the prototype chain #1497

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{{.*}}