package com.oracle.truffle.js.nodes.control;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.GetIteratorNode;
import com.oracle.truffle.js.nodes.access.GetMethodNode;
import com.oracle.truffle.js.nodes.access.IteratorCompleteNode;
import com.oracle.truffle.js.nodes.access.IteratorNextNode;
import com.oracle.truffle.js.nodes.access.IteratorValueNode;
import com.oracle.truffle.js.nodes.access.JSReadFrameSlotNode;
import com.oracle.truffle.js.nodes.access.WriteNode;
import com.oracle.truffle.js.nodes.control.ReturnNode.FrameReturnNode;
import com.oracle.truffle.js.nodes.control.YieldNode.ExceptionYieldResultNode;
import com.oracle.truffle.js.nodes.control.YieldNode.YieldResultNode;
import com.oracle.truffle.js.nodes.function.JSFunctionCallNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.GraalJSException;
import com.oracle.truffle.js.runtime.JSArguments;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.UserScriptException;
import com.oracle.truffle.js.runtime.objects.Completion;
import com.oracle.truffle.js.runtime.objects.IteratorRecord;
import com.oracle.truffle.js.runtime.objects.Undefined;
import java.util.Set;
public class AsyncGeneratorYieldNode extends AwaitNode {
@Child protected ReturnNode returnNode;
@Child private YieldResultNode generatorYieldNode;
protected AsyncGeneratorYieldNode(JSContext context, JavaScriptNode expression, JSReadFrameSlotNode readAsyncContextNode, JSReadFrameSlotNode readYieldResultNode, ReturnNode returnNode) {
super(context, expression, readAsyncContextNode, readYieldResultNode);
this.returnNode = returnNode;
this.generatorYieldNode = new ExceptionYieldResultNode();
}
public static AsyncGeneratorYieldNode createYield(JSContext context, JavaScriptNode expression, JSReadFrameSlotNode readAsyncContextNode, JSReadFrameSlotNode readAsyncResultNode,
ReturnNode returnNode) {
return new AsyncGeneratorYieldNode(context, expression, readAsyncContextNode, readAsyncResultNode, returnNode);
}
public static AsyncGeneratorYieldNode createYieldStar(JSContext context, JavaScriptNode expression, JSReadFrameSlotNode readAsyncContextNode, JSReadFrameSlotNode readAsyncResultNode,
ReturnNode returnNode, JavaScriptNode readTemp, WriteNode writeTemp) {
return new AsyncGeneratorYieldStarNode(context, expression, readAsyncContextNode, readAsyncResultNode, returnNode, readTemp, writeTemp);
}
@Override
public Object resume(VirtualFrame frame) {
int state = getStateAsInt(frame);
final int awaitValue = 1;
final int suspendedYield = 2;
final int awaitResumptionValue = 3;
if (state == 0) {
Object value = expression.execute(frame);
setState(frame, awaitValue);
return suspendAwait(frame, value);
} else if (state == awaitValue) {
Object awaited = resumeAwait(frame);
setState(frame, suspendedYield);
return suspendYield(frame, awaited);
} else {
assert state >= suspendedYield;
setState(frame, 0);
if (state == suspendedYield) {
Completion completion = resumeYield(frame);
if (completion.isNormal()) {
return completion.getValue();
} else if (completion.isThrow()) {
throw UserScriptException.create(completion.getValue(), this, context.getContextOptions().getStackTraceLimit());
} else {
assert completion.isReturn();
setState(frame, awaitResumptionValue);
return suspendAwait(frame, completion.getValue());
}
} else {
assert state == awaitResumptionValue;
Object awaited = resumeAwait(frame);
return returnValue(frame, awaited);
}
}
}
protected final Object suspendYield(VirtualFrame frame, Object awaited) {
return generatorYieldNode.generatorYield(frame, awaited);
}
protected final Completion resumeYield(VirtualFrame frame) {
return (Completion) readAsyncResultNode.execute(frame);
}
protected final Object returnValue(VirtualFrame frame, Object value) {
assert getStateAsInt(frame) == 0;
if (returnNode instanceof FrameReturnNode) {
((WriteNode) returnNode.expression).executeWrite(frame, value);
}
throw new ReturnException(value);
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return createYield(context, cloneUninitialized(expression, materializedTags), cloneUninitialized(readAsyncContextNode, materializedTags),
cloneUninitialized(readAsyncResultNode, materializedTags), cloneUninitialized(returnNode, materializedTags));
}
}
class AsyncGeneratorYieldStarNode extends AsyncGeneratorYieldNode {
@Child private JavaScriptNode readIteratorTemp;
@Child private WriteNode writeIteratorTemp;
@Child private GetIteratorNode getIteratorNode;
@Child private IteratorNextNode iteratorNextNode;
@Child private IteratorCompleteNode iteratorCompleteNode;
@Child private IteratorValueNode iteratorValueNode;
@Child private GetMethodNode getThrowMethodNode;
@Child private GetMethodNode getReturnMethodNode;
@Child private JSFunctionCallNode callThrowNode;
@Child private JSFunctionCallNode callReturnNode;
protected AsyncGeneratorYieldStarNode(JSContext context, JavaScriptNode expression, JSReadFrameSlotNode readAsyncContextNode, JSReadFrameSlotNode readYieldResultNode,
ReturnNode returnNode, JavaScriptNode readTemp, WriteNode writeTemp) {
super(context, expression, readAsyncContextNode, readYieldResultNode, returnNode);
this.readIteratorTemp = readTemp;
this.writeIteratorTemp = writeTemp;
this.getIteratorNode = GetIteratorNode.createAsync(context, null);
this.iteratorNextNode = IteratorNextNode.create();
this.iteratorCompleteNode = IteratorCompleteNode.create(context);
this.iteratorValueNode = IteratorValueNode.create(context, null);
this.getThrowMethodNode = GetMethodNode.create(context, null, "throw");
this.getReturnMethodNode = GetMethodNode.create(context, null, "return");
this.callThrowNode = JSFunctionCallNode.createCall();
this.callReturnNode = JSFunctionCallNode.createCall();
}
@Override
public Object resume(VirtualFrame frame) {
int state = getStateAsInt(frame);
final int loopBegin = 1;
final int normalOrThrowAwaitInnerResult = 2;
final int returnAwaitInnerReturnResult = 3;
final int asyncGeneratorYieldInnerResult = 4;
final int asyncGeneratorYieldInnerResultSuspendedYield = 5;
final int asyncGeneratorYieldInnerResultReturn = 6;
final int returnAwaitReceivedValue = 7;
final int throwAwaitReturnResult = 8;
IteratorRecord iteratorRecord;
if (state == 0) {
iteratorRecord = getIteratorNode.execute(expression.execute(frame));
writeIteratorTemp.executeWrite(frame, iteratorRecord);
state = loopBegin;
} else {
iteratorRecord = (IteratorRecord) readIteratorTemp.execute(frame);
}
DynamicObject iterator = iteratorRecord.getIterator();
Completion received = Completion.forNormal(Undefined.instance);
for (;;) {
switch (state) {
case loopBegin: {
if (received.isNormal()) {
DynamicObject innerResult = iteratorNextNode.execute(iteratorRecord, received.getValue());
awaitWithNext(frame, innerResult, normalOrThrowAwaitInnerResult);
} else if (received.isThrow()) {
Object throwMethod = getThrowMethodNode.executeWithTarget(iterator);
if (throwMethod != Undefined.instance) {
Object innerResult = callThrowMethod(throwMethod, iterator, received.getValue());
awaitWithNext(frame, innerResult, normalOrThrowAwaitInnerResult);
} else {
Object returnMethod = getReturnMethodNode.executeWithTarget(iterator);
error: if (returnMethod != Undefined.instance) {
Object returnResult;
try {
returnResult = callReturnNode.executeCall(JSArguments.createZeroArg(iterator, returnMethod));
} catch (GraalJSException e) {
break error;
}
awaitWithNext(frame, returnResult, throwAwaitReturnResult);
}
throw Errors.createTypeErrorYieldStarThrowMethodMissing(this);
}
} else {
assert received.isReturn();
Object returnMethod = getReturnMethodNode.executeWithTarget(iterator);
if (returnMethod != Undefined.instance) {
Object innerReturnResult = callReturnMethod(returnMethod, iterator, received.getValue());
awaitWithNext(frame, innerReturnResult, returnAwaitInnerReturnResult);
} else {
awaitWithNext(frame, received.getValue(), returnAwaitReceivedValue);
}
}
break;
}
case normalOrThrowAwaitInnerResult: {
Object awaited = resumeAwait(frame);
DynamicObject innerResult = checkcastIterResult(awaited);
boolean done = iteratorCompleteNode.execute(innerResult);
if (done) {
reset(frame);
return iteratorValueNode.execute(innerResult);
}
Object iteratorValue = iteratorValueNode.execute(innerResult);
awaitWithNext(frame, iteratorValue, asyncGeneratorYieldInnerResult);
break;
}
case returnAwaitInnerReturnResult: {
Object awaited = resumeAwait(frame);
DynamicObject innerReturnResult = checkcastIterResult(awaited);
boolean done = iteratorCompleteNode.execute(innerReturnResult);
if (done) {
reset(frame);
return returnValue(frame, iteratorValueNode.execute(innerReturnResult));
}
Object iteratorValue = iteratorValueNode.execute(innerReturnResult);
awaitWithNext(frame, iteratorValue, asyncGeneratorYieldInnerResult);
break;
}
case asyncGeneratorYieldInnerResult: {
Object awaited = resumeAwait(frame);
yieldWithNext(frame, awaited, asyncGeneratorYieldInnerResultSuspendedYield);
break;
}
case asyncGeneratorYieldInnerResultSuspendedYield: {
Completion resumptionValue = resumeYield(frame);
if (!resumptionValue.isReturn()) {
received = resumptionValue;
state = loopBegin;
break;
} else {
assert resumptionValue.isReturn();
awaitWithNext(frame, resumptionValue.getValue(), asyncGeneratorYieldInnerResultReturn);
break;
}
}
case asyncGeneratorYieldInnerResultReturn: {
Completion returnValue = resumeYield(frame);
if (returnValue.isNormal()) {
received = Completion.forReturn(returnValue.getValue());
} else {
assert returnValue.isThrow();
received = returnValue;
}
state = loopBegin;
break;
}
case returnAwaitReceivedValue: {
Object awaited = resumeAwait(frame);
reset(frame);
return returnValue(frame, awaited);
}
case throwAwaitReturnResult: {
resumeAwait(frame);
throw Errors.createTypeErrorYieldStarThrowMethodMissing(this);
}
default:
throw Errors.shouldNotReachHere();
}
assert state == loopBegin;
}
}
private void awaitWithNext(VirtualFrame frame, Object value, int nextState) {
setState(frame, nextState);
suspendAwait(frame, value);
}
private Object yieldWithNext(VirtualFrame frame, Object value, int nextState) {
setState(frame, nextState);
return suspendYield(frame, value);
}
private void reset(VirtualFrame frame) {
setState(frame, 0);
writeIteratorTemp.executeWrite(frame, Undefined.instance);
}
private Object callThrowMethod(Object throwMethod, DynamicObject iterator, Object received) {
return callThrowNode.executeCall(JSArguments.createOneArg(iterator, throwMethod, received));
}
private Object callReturnMethod(Object returnMethod, DynamicObject iterator, Object received) {
return callReturnNode.executeCall(JSArguments.createOneArg(iterator, returnMethod, received));
}
private DynamicObject checkcastIterResult(Object iterResult) {
if (!JSRuntime.isObject(iterResult)) {
throw Errors.createTypeErrorIterResultNotAnObject(iterResult, this);
}
return (DynamicObject) iterResult;
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return createYieldStar(context, cloneUninitialized(expression, materializedTags), cloneUninitialized(readAsyncContextNode, materializedTags),
cloneUninitialized(readAsyncResultNode, materializedTags), cloneUninitialized(returnNode, materializedTags),
cloneUninitialized(readIteratorTemp, materializedTags), (WriteNode) cloneUninitialized((JavaScriptNode) writeIteratorTemp, materializedTags));
}
}