From 7e4fa18e09402a5a23c517351a4b832f9b3697a8 Mon Sep 17 00:00:00 2001 From: Cam Walter Date: Mon, 19 Aug 2024 18:25:57 -0600 Subject: [PATCH] NativeError: Add ES2022 `cause` This fixes a couple other test cases involving toString() as well. --- .../org/mozilla/javascript/NativeError.java | 34 ++++++++++++++----- tests/testsrc/test262.properties | 9 ++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeError.java b/rhino/src/main/java/org/mozilla/javascript/NativeError.java index f766ebf45..be88a7498 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeError.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeError.java @@ -34,8 +34,8 @@ final class NativeError extends IdScriptableObject { ScriptableObject.putProperty(obj, "message", ""); ScriptableObject.putProperty(obj, "fileName", ""); ScriptableObject.putProperty(obj, "lineNumber", 0); - obj.setAttributes("name", ScriptableObject.DONTENUM); - obj.setAttributes("message", ScriptableObject.DONTENUM); + obj.setAttributes("name", DONTENUM); + obj.setAttributes("message", DONTENUM); obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); NativeCallSite.init(obj, sealed); } @@ -51,13 +51,22 @@ final class NativeError extends IdScriptableObject { if (arglen >= 1) { if (!Undefined.isUndefined(args[0])) { ScriptableObject.putProperty(obj, "message", ScriptRuntime.toString(args[0])); - obj.setAttributes("message", ScriptableObject.DONTENUM); + obj.setAttributes("message", DONTENUM); } if (arglen >= 2) { - ScriptableObject.putProperty(obj, "fileName", args[1]); - if (arglen >= 3) { - int line = ScriptRuntime.toInt32(args[2]); - ScriptableObject.putProperty(obj, "lineNumber", line); + if (args[1] instanceof NativeObject) { + NativeObject options = (NativeObject) args[1]; + Object cause = ScriptableObject.getProperty(options, "cause"); + if (cause != NOT_FOUND) { + ScriptableObject.putProperty(obj, "cause", cause); + obj.setAttributes("cause", DONTENUM); + } + } else { + ScriptableObject.putProperty(obj, "fileName", ScriptRuntime.toString(args[1])); + if (arglen >= 3) { + ScriptableObject.putProperty( + obj, "lineNumber", ScriptRuntime.toInt32(args[2])); + } } } } @@ -106,6 +115,10 @@ final class NativeError extends IdScriptableObject { return toString instanceof String ? (String) toString : super.toString(); } + private static NativeError realThis(Scriptable thisObj, IdFunctionObject f) { + return ensureType(thisObj, NativeError.class, f); + } + @Override protected void initPrototypeId(int id) { String s; @@ -145,7 +158,10 @@ final class NativeError extends IdScriptableObject { return err; case Id_toString: - return js_toString(thisObj); + if (thisObj != scope && thisObj instanceof NativeObject) { + return js_toString(thisObj); + } + return js_toString(realThis(thisObj, f)); case Id_toSource: return js_toSource(cx, scope, thisObj); @@ -311,7 +327,7 @@ final class NativeError extends IdScriptableObject { // at the time captureStackTrace was called. Stack traces collected through // Error.captureStackTrace are immediately collected, formatted, // and attached to the given error object. - obj.defineProperty(STACK_TAG, err.get(STACK_TAG), ScriptableObject.DONTENUM); + obj.defineProperty(STACK_TAG, err.get(STACK_TAG), DONTENUM); } @Override diff --git a/tests/testsrc/test262.properties b/tests/testsrc/test262.properties index 63058c34d..9a66b5584 100644 --- a/tests/testsrc/test262.properties +++ b/tests/testsrc/test262.properties @@ -737,15 +737,11 @@ built-ins/Date 90/770 (11.69%) value-to-primitive-result-string.js year-zero.js -built-ins/Error 10/41 (24.39%) - prototype/toString/called-as-function.js - prototype/toString/invalid-receiver.js +built-ins/Error 6/41 (14.63%) prototype/toString/not-a-constructor.js {unsupported: [Reflect.construct]} prototype/no-error-data.js prototype/S15.11.4_A2.js cause_abrupt.js - cause_property.js - constructor.js is-a-constructor.js {unsupported: [Reflect.construct]} proto-from-ctor-realm.js {unsupported: [Reflect]} @@ -1066,7 +1062,7 @@ built-ins/Math 51/326 (15.64%) built-ins/NaN 0/6 (0.0%) -built-ins/NativeErrors 44/117 (37.61%) +built-ins/NativeErrors 43/117 (36.75%) AggregateError/prototype 6/6 (100.0%) AggregateError 19/19 (100.0%) EvalError/prototype/not-error-object.js @@ -1087,7 +1083,6 @@ built-ins/NativeErrors 44/117 (37.61%) URIError/prototype/not-error-object.js URIError/is-a-constructor.js {unsupported: [Reflect.construct]} URIError/proto-from-ctor-realm.js {unsupported: [Reflect]} - cause_property_native_error.js built-ins/Number 24/335 (7.16%) isFinite/not-a-constructor.js {unsupported: [Reflect.construct]}