Skip to content

Commit

Permalink
NativeError: Add ES2022 cause
Browse files Browse the repository at this point in the history
This fixes a couple other test cases involving toString() as well.
  • Loading branch information
camnwalter authored and gbrail committed Aug 23, 2024
1 parent 86a2d60 commit 7e4fa18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
34 changes: 25 additions & 9 deletions rhino/src/main/java/org/mozilla/javascript/NativeError.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void init(Scriptable scope, boolean sealed) {
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);
}
Expand All @@ -51,13 +51,22 @@ static NativeError make(Context cx, Scriptable scope, IdFunctionObject ctorObj,
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]));
}
}
}
}
Expand Down Expand Up @@ -106,6 +115,10 @@ public String toString() {
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;
Expand Down Expand Up @@ -145,7 +158,10 @@ public Object execIdCall(
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);
Expand Down Expand Up @@ -311,7 +327,7 @@ private static void js_captureStackTrace(
// 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
Expand Down
9 changes: 2 additions & 7 deletions tests/testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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]}

Expand Down Expand Up @@ -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
Expand All @@ -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]}
Expand Down

0 comments on commit 7e4fa18

Please sign in to comment.