package com.oracle.truffle.js.builtins;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.builtins.PromiseFunctionBuiltinsFactory.PromiseCombinatorNodeGen;
import com.oracle.truffle.js.builtins.PromiseFunctionBuiltinsFactory.RejectNodeGen;
import com.oracle.truffle.js.builtins.PromiseFunctionBuiltinsFactory.ResolveNodeGen;
import com.oracle.truffle.js.nodes.access.GetIteratorNode;
import com.oracle.truffle.js.nodes.access.IteratorCloseNode;
import com.oracle.truffle.js.nodes.access.PropertyGetNode;
import com.oracle.truffle.js.nodes.control.TryCatchNode;
import com.oracle.truffle.js.nodes.function.JSBuiltin;
import com.oracle.truffle.js.nodes.function.JSBuiltinNode;
import com.oracle.truffle.js.nodes.function.JSFunctionCallNode;
import com.oracle.truffle.js.nodes.promise.NewPromiseCapabilityNode;
import com.oracle.truffle.js.nodes.promise.PerformPromiseAllNode;
import com.oracle.truffle.js.nodes.promise.PerformPromiseAllSettledNode;
import com.oracle.truffle.js.nodes.promise.PerformPromiseAnyNode;
import com.oracle.truffle.js.nodes.promise.PerformPromiseCombinatorNode;
import com.oracle.truffle.js.nodes.promise.PerformPromiseRaceNode;
import com.oracle.truffle.js.nodes.promise.PromiseResolveNode;
import com.oracle.truffle.js.nodes.unary.IsCallableNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSArguments;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
import com.oracle.truffle.js.runtime.builtins.JSPromise;
import com.oracle.truffle.js.runtime.objects.IteratorRecord;
import com.oracle.truffle.js.runtime.objects.PromiseCapabilityRecord;
import com.oracle.truffle.js.runtime.objects.Undefined;
public final class PromiseFunctionBuiltins extends JSBuiltinsContainer.SwitchEnum<PromiseFunctionBuiltins.PromiseFunction> {
public static final JSBuiltinsContainer BUILTINS = new PromiseFunctionBuiltins();
protected PromiseFunctionBuiltins() {
super(JSPromise.CLASS_NAME, PromiseFunction.class);
}
public enum PromiseFunction implements BuiltinEnum<PromiseFunction> {
all(1),
race(1),
reject(1),
resolve(1),
allSettled(1),
any(1);
private final int length;
PromiseFunction(int length) {
this.length = length;
}
@Override
public int getLength() {
return length;
}
@Override
public int getECMAScriptVersion() {
if (this == any) {
return JSConfig.ECMAScript2021;
} else if (this == allSettled) {
return JSConfig.ECMAScript2020;
}
return 6;
}
}
@Override
protected Object createNode(JSContext context, JSBuiltin builtin, boolean construct, boolean newTarget, PromiseFunction builtinEnum) {
switch (builtinEnum) {
case all:
return PromiseCombinatorNodeGen.create(context, builtin, PerformPromiseAllNode.create(context), args().withThis().fixedArgs(1).createArgumentNodes(context));
case allSettled:
return PromiseCombinatorNodeGen.create(context, builtin, PerformPromiseAllSettledNode.create(context), args().withThis().fixedArgs(1).createArgumentNodes(context));
case any:
return PromiseCombinatorNodeGen.create(context, builtin, PerformPromiseAnyNode.create(context), args().withThis().fixedArgs(1).createArgumentNodes(context));
case race:
return PromiseCombinatorNodeGen.create(context, builtin, PerformPromiseRaceNode.create(context), args().withThis().fixedArgs(1).createArgumentNodes(context));
case reject:
return RejectNodeGen.create(context, builtin, args().withThis().fixedArgs(1).createArgumentNodes(context));
case resolve:
return ResolveNodeGen.create(context, builtin, args().withThis().fixedArgs(1).createArgumentNodes(context));
}
return null;
}
public abstract static class PromiseCombinatorNode extends JSBuiltinNode {
@Child private NewPromiseCapabilityNode newPromiseCapabilityNode;
@Child private PropertyGetNode getResolve;
@Child private IsCallableNode isCallable;
@Child private GetIteratorNode getIteratorNode;
@Child private PerformPromiseCombinatorNode performPromiseOpNode;
@Child private JSFunctionCallNode callRejectNode;
@Child private IteratorCloseNode iteratorCloseNode;
@Child private TryCatchNode.GetErrorObjectNode getErrorObjectNode;
@Child private InteropLibrary exceptions;
protected PromiseCombinatorNode(JSContext context, JSBuiltin builtin, PerformPromiseCombinatorNode performPromiseOp) {
super(context, builtin);
this.newPromiseCapabilityNode = NewPromiseCapabilityNode.create(context);
this.getResolve = PropertyGetNode.create(JSPromise.RESOLVE, context);
this.isCallable = IsCallableNode.create();
this.getIteratorNode = GetIteratorNode.create(context);
this.performPromiseOpNode = performPromiseOp;
}
@Specialization(guards = "isJSObject(thisObj)")
protected DynamicObject doObject(DynamicObject thisObj, Object iterable) {
DynamicObject constructor = thisObj;
PromiseCapabilityRecord promiseCapability = newPromiseCapabilityNode.execute(constructor);
Object promiseResolve;
IteratorRecord iteratorRecord;
try {
promiseResolve = getPromiseResolve(constructor);
iteratorRecord = getIteratorNode.execute(iterable);
} catch (Throwable ex) {
if (shouldCatch(ex)) {
return rejectPromise(getErrorObjectNode.execute(ex), promiseCapability);
} else {
throw ex;
}
}
try {
return performPromiseOpNode.execute(iteratorRecord, constructor, promiseCapability, promiseResolve);
} catch (Throwable ex) {
if (shouldCatch(ex)) {
if (!iteratorRecord.isDone()) {
iteratorClose(iteratorRecord);
}
return rejectPromise(getErrorObjectNode.execute(ex), promiseCapability);
} else {
throw ex;
}
}
}
private Object getPromiseResolve(DynamicObject constructor) {
assert JSRuntime.isConstructor(constructor);
Object promiseResolve = getResolve.getValue(constructor);
if (!isCallable.executeBoolean(promiseResolve)) {
throw Errors.createTypeErrorNotAFunction(promiseResolve);
}
return promiseResolve;
}
protected DynamicObject rejectPromise(Object value, PromiseCapabilityRecord promiseCapability) {
if (callRejectNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
callRejectNode = insert(JSFunctionCallNode.createCall());
}
callRejectNode.executeCall(JSArguments.createOneArg(Undefined.instance, promiseCapability.getReject(), value));
return promiseCapability.getPromise();
}
private void iteratorClose(IteratorRecord iteratorRecord) {
if (iteratorCloseNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
iteratorCloseNode = insert(IteratorCloseNode.create(getContext()));
}
iteratorCloseNode.executeAbrupt(iteratorRecord.getIterator());
}
private boolean shouldCatch(Throwable exception) {
if (getErrorObjectNode == null || exceptions == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
getErrorObjectNode = insert(TryCatchNode.GetErrorObjectNode.create(getContext()));
exceptions = insert(InteropLibrary.getFactory().createDispatched(JSConfig.InteropLibraryLimit));
}
return TryCatchNode.shouldCatch(exception, exceptions);
}
@SuppressWarnings("unused")
@Specialization(guards = "!isJSObject(thisObj)")
protected DynamicObject doNotObject(Object thisObj, Object iterable) {
throw Errors.createTypeError("Cannot create promise from this type");
}
}
public abstract static class RejectNode extends JSBuiltinNode {
@Child private NewPromiseCapabilityNode newPromiseCapability;
@Child private JSFunctionCallNode callReject;
protected RejectNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
this.newPromiseCapability = NewPromiseCapabilityNode.create(context);
this.callReject = JSFunctionCallNode.createCall();
}
@Specialization(guards = "isJSObject(constructor)")
protected DynamicObject doObject(DynamicObject constructor, Object reason) {
PromiseCapabilityRecord promiseCapability = newPromiseCapability.execute(constructor);
callReject.executeCall(JSArguments.createOneArg(Undefined.instance, promiseCapability.getReject(), reason));
return promiseCapability.getPromise();
}
@SuppressWarnings("unused")
@Specialization(guards = "!isJSObject(thisObj)")
protected DynamicObject doNotObject(Object thisObj, Object iterable) {
throw Errors.createTypeError("Cannot reject promise from this type");
}
}
public abstract static class ResolveNode extends JSBuiltinNode {
@Child private PromiseResolveNode promiseResolve;
protected ResolveNode(JSContext context, JSBuiltin builtin) {
super(context, builtin);
this.promiseResolve = PromiseResolveNode.create(context);
}
@Specialization(guards = "isJSObject(constructor)")
protected DynamicObject doObject(DynamicObject constructor, Object value) {
return promiseResolve.execute(constructor, value);
}
@SuppressWarnings("unused")
@Specialization(guards = "!isJSObject(thisObj)")
protected DynamicObject doNotObject(Object thisObj, Object iterable) {
throw Errors.createTypeError("Cannot resolve promise from this type");
}
}
}