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.profiles.BranchProfile;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.IteratorCloseNode;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.objects.IteratorRecord;
public class IteratorCloseWrapperNode extends JavaScriptNode implements ResumableNode {
@Child private JavaScriptNode blockNode;
@Child private JavaScriptNode iteratorNode;
@Child private IteratorCloseNode iteratorCloseNode;
private final JSContext context;
private final BranchProfile throwBranch = BranchProfile.create();
private final BranchProfile exitBranch = BranchProfile.create();
private final BranchProfile notDoneBranch = BranchProfile.create();
@Child private InteropLibrary exceptions;
protected IteratorCloseWrapperNode(JSContext context, JavaScriptNode block, JavaScriptNode iterator) {
this.context = context;
this.blockNode = block;
this.iteratorNode = iterator;
}
public static JavaScriptNode create(JSContext context, JavaScriptNode block, JavaScriptNode iterator) {
return new IteratorCloseWrapperNode(context, block, iterator);
}
@Override
public Object execute(VirtualFrame frame) {
Object result;
try {
result = blockNode.execute(frame);
} catch (YieldException e) {
throw e;
} catch (ControlFlowException e) {
exitBranch.enter();
IteratorRecord iteratorRecord = getIteratorRecord(frame);
if (!iteratorRecord.isDone()) {
iteratorClose().executeVoid(iteratorRecord.getIterator());
}
throw e;
} catch (Throwable e) {
if (TryCatchNode.shouldCatch(e, exceptions())) {
throwBranch.enter();
IteratorRecord iteratorRecord = getIteratorRecord(frame);
if (!iteratorRecord.isDone()) {
iteratorClose().executeAbrupt(iteratorRecord.getIterator());
}
}
throw e;
}
IteratorRecord iteratorRecord = getIteratorRecord(frame);
if (!iteratorRecord.isDone()) {
notDoneBranch.enter();
iteratorClose().executeVoid(iteratorRecord.getIterator());
}
return result;
}
private IteratorRecord getIteratorRecord(VirtualFrame frame) {
return (IteratorRecord) iteratorNode.execute(frame);
}
@Override
public Object resume(VirtualFrame frame) {
return execute(frame);
}
private IteratorCloseNode iteratorClose() {
if (iteratorCloseNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
iteratorCloseNode = insert(IteratorCloseNode.create(context));
}
return iteratorCloseNode;
}
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 IteratorCloseWrapperNode(context, cloneUninitialized(blockNode, materializedTags), cloneUninitialized(iteratorNode, materializedTags));
}
}