Skip to content

Commit 3952a50

Browse files
committed
Mark immediately thrown promise rejections as handled.
(cherry picked from commit eb9a87c)
1 parent f00d096 commit 3952a50

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

graal-js/src/com.oracle.truffle.js.parser/src/com/oracle/truffle/js/parser/GraalJSEvaluator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,7 @@ private int innerModuleEvaluation(JSRealm realm, AbstractModuleRecord abstractMo
754754
if (!(abstractModule instanceof CyclicModuleRecord moduleRecord)) {
755755
JSPromiseObject promise = abstractModule.evaluate(realm);
756756
assert !JSPromise.isPending(promise);
757-
if (JSPromise.isRejected(promise)) {
758-
throw JSRuntime.getException(JSPromise.getPromiseResult(promise));
759-
}
757+
JSPromise.throwIfRejected(promise, realm);
760758
return index;
761759
}
762760
if (moduleRecord.getStatus() == Status.EvaluatingAsync || moduleRecord.getStatus() == Status.Evaluated) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -52,6 +52,7 @@
5252
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
5353
import com.oracle.truffle.js.runtime.objects.JSObject;
5454
import com.oracle.truffle.js.runtime.objects.JSObjectUtil;
55+
import com.oracle.truffle.js.runtime.objects.Undefined;
5556

5657
public final class JSPromise extends JSNonProxy implements JSConstructorFactory.Default.WithFunctionsAndSpecies, PrototypeSupplier {
5758
public static final TruffleString CLASS_NAME = Strings.constant("Promise");
@@ -154,6 +155,17 @@ public static Object getPromiseResult(JSPromiseObject obj) {
154155
return JSRuntime.nullToUndefined(obj.getPromiseResult());
155156
}
156157

158+
public static void throwIfRejected(JSPromiseObject promise, JSRealm realm) {
159+
if (JSPromise.isRejected(promise)) {
160+
if (!promise.isHandled()) {
161+
// Notify the promise rejection tracker that this promise has been handled.
162+
realm.getContext().notifyPromiseRejectionTracker(promise, JSPromise.REJECTION_TRACKER_OPERATION_HANDLE, Undefined.instance, realm.getAgent());
163+
promise.setIsHandled(true);
164+
}
165+
throw JSRuntime.getException(JSPromise.getPromiseResult(promise));
166+
}
167+
}
168+
157169
@Override
158170
public TruffleString getClassName() {
159171
return CLASS_NAME;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/objects/AbstractModuleRecord.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import com.oracle.truffle.js.runtime.Errors;
5656
import com.oracle.truffle.js.runtime.JSContext;
5757
import com.oracle.truffle.js.runtime.JSRealm;
58-
import com.oracle.truffle.js.runtime.JSRuntime;
5958
import com.oracle.truffle.js.runtime.builtins.JSModuleNamespace;
6059
import com.oracle.truffle.js.runtime.builtins.JSModuleNamespaceObject;
6160
import com.oracle.truffle.js.runtime.builtins.JSPromise;
@@ -88,9 +87,7 @@ protected AbstractModuleRecord(JSContext context, Source source, Object hostDefi
8887
public final void loadRequestedModulesSync(JSRealm realm, Object hostDefinedArg) {
8988
JSPromiseObject loadPromise = loadRequestedModules(realm, hostDefinedArg);
9089
assert !JSPromise.isPending(loadPromise);
91-
if (JSPromise.isRejected(loadPromise)) {
92-
throw JSRuntime.getException(JSPromise.getPromiseResult(loadPromise));
93-
}
90+
JSPromise.throwIfRejected(loadPromise, realm);
9491
}
9592

9693
/**

0 commit comments

Comments
 (0)