diff --git a/lib/VM/JSError.cpp b/lib/VM/JSError.cpp index f012fc11b87..fc8c9801373 100644 --- a/lib/VM/JSError.cpp +++ b/lib/VM/JSError.cpp @@ -57,7 +57,11 @@ void JSErrorBuildMeta(const GCCell *cell, Metadata::Builder &mb) { CallResult> JSError::getErrorFromStackTarget( Runtime &runtime, Handle targetHandle) { - if (targetHandle) { + MutableHandle mutHnd = + runtime.makeMutableHandle(targetHandle.get()); + targetHandle = mutHnd; + + while (targetHandle) { NamedPropertyDescriptor desc; bool exists = JSObject::getOwnNamedDescriptor( targetHandle, @@ -71,6 +75,8 @@ CallResult> JSError::getErrorFromStackTarget( if (vmisa(*targetHandle)) { return Handle::vmcast(targetHandle); } + + mutHnd.set(targetHandle->getParent(runtime)); } return runtime.raiseTypeError( "Error.stack getter called with an invalid receiver"); diff --git a/test/hermes/error-in-proto.js b/test/hermes/error-in-proto.js new file mode 100644 index 00000000000..250acb86660 --- /dev/null +++ b/test/hermes/error-in-proto.js @@ -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{{.*}}