Skip to content

Commit df18e59

Browse files
committed
[GR-58994] Implementation of Error.isError proposal.
PullRequest: js/3310
2 parents f68f59a + 577c031 commit df18e59

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ See [release calendar](https://www.graalvm.org/release-calendar/) for release da
77

88
## Version 24.2.0
99
* Updated Node.js to version 20.15.1.
10+
* Implemented the [`Error.isError`](https://github.com/tc39/proposal-is-error) proposal. It is available in ECMAScript staging mode (`--js.ecmascript-version=staging`).
1011
* Implemented the [`Promise.try`](https://github.com/tc39/proposal-promise-try) proposal. It is available in ECMAScript staging mode (`--js.ecmascript-version=staging`).
1112
* Implemented the [Source Phase Imports](https://github.com/tc39/proposal-source-phase-imports) proposal. It is available behind the experimental option (`--js.source-phase-imports`).
1213
* Implemented basic Worker API (resembling the API available in `d8`). It is available behind the experimental option `--js.worker`.

graal-js/src/com.oracle.truffle.js.test.external/src/com/oracle/truffle/js/test/external/test262/Test262Runnable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public class Test262Runnable extends TestRunnable {
116116
"DataView.prototype.getUint16",
117117
"DataView.prototype.getUint32",
118118
"DataView.prototype.setUint8",
119+
"Error.isError",
119120
"FinalizationRegistry",
120121
"FinalizationRegistry.prototype.cleanupSome",
121122
"Float16Array",
@@ -274,7 +275,6 @@ public class Test262Runnable extends TestRunnable {
274275
});
275276
private static final Set<String> UNSUPPORTED_FEATURES = featureSet(new String[]{
276277
"Atomics.pause",
277-
"Error.isError",
278278
"Intl.DurationFormat",
279279
"IsHTMLDDA",
280280
"Math.sumPrecise",
@@ -286,6 +286,7 @@ public class Test262Runnable extends TestRunnable {
286286
});
287287
private static final Set<String> STAGING_FEATURES = featureSet(new String[]{
288288
"Array.fromAsync",
289+
"Error.isError",
289290
"FinalizationRegistry.prototype.cleanupSome",
290291
"Float16Array",
291292
"Intl.Locale-info",

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/ErrorFunctionBuiltins.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -45,33 +45,74 @@
4545
import com.oracle.truffle.api.frame.VirtualFrame;
4646
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
4747
import com.oracle.truffle.js.builtins.ErrorFunctionBuiltinsFactory.ErrorCaptureStackTraceNodeGen;
48+
import com.oracle.truffle.js.builtins.ErrorFunctionBuiltinsFactory.ErrorIsErrorNodeGen;
4849
import com.oracle.truffle.js.nodes.access.ErrorStackTraceLimitNode;
4950
import com.oracle.truffle.js.nodes.access.InitErrorObjectNode;
5051
import com.oracle.truffle.js.nodes.function.JSBuiltin;
5152
import com.oracle.truffle.js.nodes.function.JSBuiltinNode;
5253
import com.oracle.truffle.js.runtime.Errors;
5354
import com.oracle.truffle.js.runtime.JSArguments;
55+
import com.oracle.truffle.js.runtime.JSConfig;
5456
import com.oracle.truffle.js.runtime.JSContext;
5557
import com.oracle.truffle.js.runtime.JSRuntime;
56-
import com.oracle.truffle.js.runtime.Strings;
5758
import com.oracle.truffle.js.runtime.UserScriptException;
59+
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
5860
import com.oracle.truffle.js.runtime.builtins.JSError;
61+
import com.oracle.truffle.js.runtime.builtins.JSErrorObject;
5962
import com.oracle.truffle.js.runtime.builtins.JSFunction;
6063
import com.oracle.truffle.js.runtime.builtins.JSFunctionObject;
61-
import com.oracle.truffle.js.runtime.objects.JSAttributes;
6264
import com.oracle.truffle.js.runtime.objects.JSObject;
6365
import com.oracle.truffle.js.runtime.objects.Undefined;
6466

6567
/**
6668
* Contains builtins for {@linkplain JSError} function (constructor).
6769
*/
68-
public final class ErrorFunctionBuiltins extends JSBuiltinsContainer.Lambda {
70+
public final class ErrorFunctionBuiltins extends JSBuiltinsContainer.SwitchEnum<ErrorFunctionBuiltins.ErrorFunction> {
6971
public static final JSBuiltinsContainer BUILTINS = new ErrorFunctionBuiltins();
7072

7173
protected ErrorFunctionBuiltins() {
72-
super(JSError.CLASS_NAME);
73-
defineFunction(Strings.CAPTURE_STACK_TRACE, 2, JSAttributes.getDefault(),
74-
(context, builtin) -> ErrorCaptureStackTraceNodeGen.create(context, builtin, args().fixedArgs(2).createArgumentNodes(context)));
74+
super(JSError.CLASS_NAME, ErrorFunction.class);
75+
}
76+
77+
public enum ErrorFunction implements BuiltinEnum<ErrorFunction> {
78+
captureStackTrace(2),
79+
isError(1);
80+
81+
private final int length;
82+
83+
ErrorFunction(int length) {
84+
this.length = length;
85+
}
86+
87+
@Override
88+
public int getLength() {
89+
return length;
90+
}
91+
92+
@Override
93+
public int getECMAScriptVersion() {
94+
if (this == isError) {
95+
return JSConfig.StagingECMAScriptVersion;
96+
}
97+
return BuiltinEnum.super.getECMAScriptVersion();
98+
}
99+
100+
@Override
101+
public boolean isOptional() {
102+
return (this == captureStackTrace);
103+
}
104+
105+
}
106+
107+
@Override
108+
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, ErrorFunction builtinEnum) {
109+
switch (builtinEnum) {
110+
case captureStackTrace:
111+
return ErrorCaptureStackTraceNodeGen.create(context, builtin, args().fixedArgs(2).createArgumentNodes(context));
112+
case isError:
113+
return ErrorIsErrorNodeGen.create(context, builtin, args().fixedArgs(1).createArgumentNodes(context));
114+
}
115+
return null;
75116
}
76117

77118
public abstract static class ErrorCaptureStackTraceNode extends JSBuiltinNode {
@@ -109,4 +150,15 @@ public boolean countsTowardsStackTraceLimit() {
109150
return false;
110151
}
111152
}
153+
154+
public abstract static class ErrorIsErrorNode extends JSBuiltinNode {
155+
public ErrorIsErrorNode(JSContext context, JSBuiltin builtin) {
156+
super(context, builtin);
157+
}
158+
159+
@Specialization
160+
protected boolean isError(Object argument) {
161+
return (argument instanceof JSErrorObject);
162+
}
163+
}
112164
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/Strings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,6 @@ private Strings() {
458458
public static final TruffleString FULFILLED = constant("fulfilled");
459459
public static final TruffleString REJECTED = constant("rejected");
460460

461-
public static final TruffleString CAPTURE_STACK_TRACE = constant("captureStackTrace");
462461
public static final TruffleString JAVA_CLASS_BRACKET = constant("JavaClass[");
463462
public static final TruffleString JAVA_OBJECT_BRACKET = constant("JavaObject[");
464463
public static final TruffleString RESOLVED = constant("resolved");

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/JSError.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,12 @@ public static JSConstructor createErrorConstructor(JSRealm realm, JSErrorType er
210210
JSObjectUtil.putConstructorProperty(classPrototype, errorConstructor);
211211
JSObjectUtil.putDataProperty(classPrototype, NAME, name, MESSAGE_ATTRIBUTES);
212212
JSObjectUtil.putConstructorPrototypeProperty(errorConstructor, classPrototype);
213-
if (realm.getContextOptions().isStackTraceAPI() && errorType == JSErrorType.Error) {
213+
if (errorType == JSErrorType.Error) {
214214
JSObjectUtil.putFunctionsFromContainer(realm, errorConstructor, ErrorFunctionBuiltins.BUILTINS);
215-
JSObjectUtil.putDataProperty(errorConstructor, STACK_TRACE_LIMIT_PROPERTY_NAME, JSContextOptions.STACK_TRACE_LIMIT.getValue(realm.getOptions()), JSAttributes.getDefault());
215+
if (realm.getContextOptions().isStackTraceAPI()) {
216+
JSObjectUtil.putFunctionFromContainer(realm, errorConstructor, ErrorFunctionBuiltins.BUILTINS, ErrorFunctionBuiltins.ErrorFunction.captureStackTrace.getKey());
217+
JSObjectUtil.putDataProperty(errorConstructor, STACK_TRACE_LIMIT_PROPERTY_NAME, JSContextOptions.STACK_TRACE_LIMIT.getValue(realm.getOptions()), JSAttributes.getDefault());
218+
}
216219
}
217220

218221
return new JSConstructor(errorConstructor, classPrototype);

graal-js/test/test262.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,26 @@
302302
"COMPILE_IMMEDIATELY" : "SKIP"
303303
},
304304
"comment" : "Contains a hard-coded timeout that is exceeded in CompileImmediately mode"
305+
}, {
306+
"filePath" : "built-ins/Error/isError/bigints.js",
307+
"status" : "FAIL",
308+
"comment" : "Incorrect test: uses 'new BigInt(0)' instead of 'BigInt(0)'."
309+
}, {
310+
"filePath" : "built-ins/Error/isError/error-subclass.js",
311+
"status" : "FAIL",
312+
"comment" : "Incorrect test: invokes AggregateError constructor with no arguments."
313+
}, {
314+
"filePath" : "built-ins/Error/isError/errors-other-realm.js",
315+
"status" : "FAIL",
316+
"comment" : "Incorrect test: invokes AggregateError constructor with no arguments."
317+
}, {
318+
"filePath" : "built-ins/Error/isError/errors.js",
319+
"status" : "FAIL",
320+
"comment" : "Incorrect test: invokes AggregateError constructor with no arguments."
321+
}, {
322+
"filePath" : "built-ins/Error/isError/symbols.js",
323+
"status" : "FAIL",
324+
"comment" : "Incorrect test: uses 'new Symbol()' instead of 'Symbol()'."
305325
}, {
306326
"filePath" : "built-ins/Function/prototype/toString/built-in-function-object.js",
307327
"status" : "PASS",

0 commit comments

Comments
 (0)