package com.oracle.truffle.js.nodes.control;
import java.util.Set;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.nodes.ControlFlowException;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.GetMethodNode;
import com.oracle.truffle.js.nodes.access.JSReadFrameSlotNode;
import com.oracle.truffle.js.nodes.function.JSFunctionCallNode;
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.objects.Completion;
import com.oracle.truffle.js.runtime.objects.IteratorRecord;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.Undefined;
public class AsyncIteratorCloseWrapperNode extends AwaitNode {
@Child private JavaScriptNode loopNode;
@Child private GetMethodNode getReturnNode;
@Child private JSFunctionCallNode returnMethodCallNode;
@Child private JavaScriptNode iteratorNode;
private final BranchProfile errorBranch = BranchProfile.create();
private final BranchProfile throwBranch = BranchProfile.create();
private final BranchProfile exitBranch = BranchProfile.create();
private final BranchProfile notDoneBranch = BranchProfile.create();
@Child private InteropLibrary exceptions;
protected AsyncIteratorCloseWrapperNode(JSContext context, JavaScriptNode loopNode, JavaScriptNode iteratorNode, JSReadFrameSlotNode asyncContextNode, JSReadFrameSlotNode asyncResultNode) {
super(context, null, asyncContextNode, asyncResultNode);
this.loopNode = loopNode;
this.iteratorNode = iteratorNode;
this.getReturnNode = GetMethodNode.create(context, null, "return");
}
public static JavaScriptNode create(JSContext context, JavaScriptNode loopNode, JavaScriptNode iterator, JSReadFrameSlotNode asyncContextNode, JSReadFrameSlotNode asyncResultNode) {
return new AsyncIteratorCloseWrapperNode(context, loopNode, iterator, asyncContextNode, asyncResultNode);
}
@Override
public Object execute(VirtualFrame frame) {
Object result;
Object innerResult;
Completion completion;
await: {
try {
result = loopNode.execute(frame);
} catch (YieldException e) {
throw e;
} catch (ControlFlowException e) {
exitBranch.enter();
IteratorRecord iteratorRecord = getIteratorRecord(frame);
DynamicObject iterator = iteratorRecord.getIterator();
Object returnMethod = getReturnNode.executeWithTarget(frame, iterator);
if (returnMethod != Undefined.instance) {
innerResult = getReturnMethodCallNode().executeCall(JSArguments.createZeroArg(iterator, returnMethod));
completion = Completion.forReturn(e);
break await;
} else {
throw e;
}
} catch (Throwable e) {
if (TryCatchNode.shouldCatch(e, exceptions())) {
throwBranch.enter();
IteratorRecord iteratorRecord = getIteratorRecord(frame);
DynamicObject iterator = iteratorRecord.getIterator();
try {
Object returnMethod = getReturnNode.executeWithTarget(frame, iterator);
if (returnMethod != Undefined.instance) {
innerResult = getReturnMethodCallNode().executeCall(JSArguments.createZeroArg(iterator, returnMethod));
completion = Completion.forThrow(e);
break await;
}
} catch (Exception ex) {
}
}
throw e;
}
IteratorRecord iteratorRecord = getIteratorRecord(frame);
if (iteratorRecord.isDone()) {
return result;
} else {
notDoneBranch.enter();
DynamicObject iterator = iteratorRecord.getIterator();
Object returnMethod = getReturnNode.executeWithTarget(frame, iterator);
if (returnMethod != Undefined.instance) {
innerResult = getReturnMethodCallNode().executeCall(JSArguments.createZeroArg(iterator, returnMethod));
completion = Completion.forNormal(result);
break await;
} else {
return result;
}
}
}
setState(frame, completion);
return suspendAwait(frame, innerResult);
}
@Override
public Object resume(VirtualFrame frame) {
Object state = getState(frame);
if (state == Undefined.instance) {
return execute(frame);
} else {
resetState(frame);
Completion completion = (Completion) state;
if (completion.isThrow()) {
throw JSRuntime.rethrow((Throwable) completion.getValue());
}
Object innerResult = resumeAwait(frame);
if (!JSDynamicObject.isJSDynamicObject(innerResult)) {
errorBranch.enter();
throw Errors.createTypeErrorIterResultNotAnObject(innerResult, this);
}
if (completion.isAbrupt()) {
throw JSRuntime.rethrow((Throwable) completion.getValue());
}
return completion.getValue();
}
}
private IteratorRecord getIteratorRecord(VirtualFrame frame) {
return (IteratorRecord) iteratorNode.execute(frame);
}
private JSFunctionCallNode getReturnMethodCallNode() {
if (returnMethodCallNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
returnMethodCallNode = insert(JSFunctionCallNode.createCall());
}
return returnMethodCallNode;
}
private InteropLibrary exceptions() {
InteropLibrary e = exceptions;
if (e == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
exceptions = e = insert(InteropLibrary.getFactory().createDispatched(JSConfig.InteropLibraryLimit));
}
return e;
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return new AsyncIteratorCloseWrapperNode(context, cloneUninitialized(loopNode, materializedTags), cloneUninitialized(iteratorNode, materializedTags),
cloneUninitialized(readAsyncContextNode, materializedTags),
cloneUninitialized(readAsyncResultNode, materializedTags));
}
}