package com.oracle.truffle.js.nodes.control;
import java.util.Set;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.exception.AbstractTruffleException;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.StandardTags.TryBlockTag;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.interop.ExceptionType;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.ControlFlowException;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.access.InitErrorObjectNode;
import com.oracle.truffle.js.nodes.access.JSWriteFrameSlotNode;
import com.oracle.truffle.js.nodes.cast.JSToBooleanNode;
import com.oracle.truffle.js.nodes.function.BlockScopeNode;
import com.oracle.truffle.js.nodes.instrumentation.JSTags;
import com.oracle.truffle.js.nodes.instrumentation.JSTags.ControlFlowRootTag;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.GraalJSException;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSErrorType;
import com.oracle.truffle.js.runtime.JSException;
import com.oracle.truffle.js.runtime.JSFrameUtil;
import com.oracle.truffle.js.runtime.JSRealm;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.UserScriptException;
import com.oracle.truffle.js.runtime.builtins.JSError;
import com.oracle.truffle.js.runtime.objects.Undefined;
@NodeInfo(shortName = "try-catch")
public class TryCatchNode extends StatementNode implements ResumableNode {
@Child private JavaScriptNode tryBlock;
@Child private JavaScriptNode catchBlock;
@Child private JSWriteFrameSlotNode writeErrorVar;
@Child private BlockScopeNode blockScope;
@Child private JavaScriptNode destructuring;
@Child private JavaScriptNode conditionExpression;
@Child private GetErrorObjectNode getErrorObjectNode;
@Child private InteropLibrary exceptions;
private final JSContext context;
protected TryCatchNode(JSContext context, JavaScriptNode tryBlock, JavaScriptNode catchBlock, JSWriteFrameSlotNode writeErrorVar, BlockScopeNode blockScope, JavaScriptNode destructuring,
JavaScriptNode conditionExpression) {
this.context = context;
this.tryBlock = tryBlock;
this.writeErrorVar = writeErrorVar;
this.catchBlock = catchBlock;
this.blockScope = blockScope;
this.destructuring = destructuring;
this.conditionExpression = conditionExpression == null ? null : JSToBooleanNode.create(conditionExpression);
assert blockScope != null || writeErrorVar == null;
}
public static TryCatchNode create(JSContext context, JavaScriptNode tryBlock, JavaScriptNode catchBlock, JSWriteFrameSlotNode writeErrorVar, BlockScopeNode blockScope,
JavaScriptNode destructuring, JavaScriptNode conditionExpression) {
return new TryCatchNode(context, tryBlock, catchBlock, writeErrorVar, blockScope, destructuring, conditionExpression);
}
@Override
public boolean hasTag(Class<? extends Tag> tag) {
if (tag == ControlFlowRootTag.class || tag == TryBlockTag.class) {
return true;
}
return super.hasTag(tag);
}
@Override
public Object getNodeObject() {
return JSTags.createNodeObjectDescriptor("type", ControlFlowRootTag.Type.ExceptionHandler.name());
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return create(context, cloneUninitialized(tryBlock, materializedTags), cloneUninitialized(catchBlock, materializedTags), cloneUninitialized(writeErrorVar, materializedTags),
cloneUninitialized(blockScope, materializedTags), cloneUninitialized(destructuring, materializedTags),
cloneUninitialized(conditionExpression, materializedTags));
}
@Override
public boolean isResultAlwaysOfType(Class<?> clazz) {
return tryBlock.isResultAlwaysOfType(clazz) && catchBlock.isResultAlwaysOfType(clazz);
}
@Override
public final Object execute(VirtualFrame frame) {
try {
return tryBlock.execute(frame);
} catch (ControlFlowException cfe) {
throw cfe;
} catch (Throwable ex) {
if (shouldCatch(ex, exceptions())) {
return executeCatch(frame, ex);
} else {
throw ex;
}
}
}
@Override
public final void executeVoid(VirtualFrame frame) {
try {
tryBlock.executeVoid(frame);
} catch (ControlFlowException cfe) {
throw cfe;
} catch (Throwable ex) {
if (shouldCatch(ex, exceptions())) {
executeCatch(frame, ex);
} else {
throw ex;
}
}
}
public static boolean shouldCatch(Throwable ex, InteropLibrary exceptions) {
if (exceptions.isException(ex)) {
try {
ExceptionType exceptionType = exceptions.getExceptionType(ex);
return exceptionType != ExceptionType.EXIT && exceptionType != ExceptionType.INTERRUPT;
} catch (UnsupportedMessageException e) {
throw Errors.createTypeErrorInteropException(ex, e, "getExceptionType", null);
}
} else if (ex instanceof StackOverflowError) {
return true;
} else {
return false;
}
}
public static boolean shouldCatch(Throwable ex) {
return shouldCatch(ex, InteropLibrary.getUncached());
}
private Object executeCatch(VirtualFrame frame, Throwable ex) {
VirtualFrame catchFrame = blockScope == null ? frame : blockScope.appendScopeFrame(frame);
try {
return executeCatchInner(catchFrame, ex);
} finally {
if (blockScope != null) {
blockScope.exitScope(catchFrame);
}
}
}
private Object executeCatchInner(VirtualFrame catchFrame, Throwable ex) {
if (writeErrorVar != null) {
if (getErrorObjectNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
getErrorObjectNode = insert(GetErrorObjectNode.create(context));
}
Object exceptionObject = getErrorObjectNode.execute(ex);
writeErrorVar.executeWrite(catchFrame, exceptionObject);
if (destructuring != null) {
destructuring.execute(catchFrame);
}
}
if (conditionExpression == null || executeConditionAsBoolean(catchFrame, conditionExpression)) {
return catchBlock.execute(catchFrame);
} else {
throw JSRuntime.rethrow(ex);
}
}
@Override
public Object resume(VirtualFrame frame) {
Object state = getStateAndReset(frame);
if (state == Undefined.instance) {
try {
return tryBlock.execute(frame);
} catch (ControlFlowException cfe) {
throw cfe;
} catch (Throwable ex) {
if (shouldCatch(ex, exceptions())) {
VirtualFrame catchFrame = blockScope == null ? frame : blockScope.appendScopeFrame(frame);
try {
return executeCatchInner(catchFrame, ex);
} catch (YieldException e) {
setState(frame, catchFrame.materialize());
throw e;
} finally {
if (blockScope != null) {
blockScope.exitScope(catchFrame);
}
}
} else {
throw ex;
}
}
} else {
VirtualFrame catchFrame = JSFrameUtil.castMaterializedFrame(state);
try {
return catchBlock.execute(catchFrame);
} catch (YieldException e) {
setState(frame, catchFrame);
throw e;
}
}
}
private InteropLibrary exceptions() {
InteropLibrary e = exceptions;
if (e == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
exceptions = e = insert(InteropLibrary.getFactory().createDispatched(JSConfig.InteropLibraryLimit));
}
return e;
}
public static final class GetErrorObjectNode extends JavaScriptBaseNode {
@Child private InitErrorObjectNode initErrorObjectNode;
private final ConditionProfile isJSError = ConditionProfile.createBinaryProfile();
private final ConditionProfile isJSException = ConditionProfile.createBinaryProfile();
private final BranchProfile truffleExceptionBranch = BranchProfile.create();
private final BranchProfile legacyTruffleExceptionBranch = BranchProfile.create();
private final JSContext context;
private GetErrorObjectNode(JSContext context) {
this.context = context;
this.initErrorObjectNode = InitErrorObjectNode.create(context, context.isOptionNashornCompatibilityMode());
}
public static GetErrorObjectNode create(JSContext context) {
return new GetErrorObjectNode(context);
}
@SuppressWarnings("deprecation")
public Object execute(Throwable ex) {
if (isJSError.profile(ex instanceof JSException)) {
TruffleStackTrace.fillIn(ex);
return doJSException((JSException) ex);
} else if (isJSException.profile(ex instanceof UserScriptException)) {
return ((UserScriptException) ex).getErrorObject();
} else if (ex instanceof StackOverflowError) {
CompilerDirectives.transferToInterpreter();
JSException rangeException = Errors.createRangeErrorStackOverflow(this);
return doJSException(rangeException);
} else {
truffleExceptionBranch.enter();
assert !(ex instanceof GraalJSException) && (ex instanceof AbstractTruffleException || ex instanceof com.oracle.truffle.api.TruffleException) : ex;
if (ex instanceof AbstractTruffleException) {
return ex;
} else {
legacyTruffleExceptionBranch.enter();
Object exceptionObject = ((com.oracle.truffle.api.TruffleException) ex).getExceptionObject();
if (exceptionObject != null) {
return exceptionObject;
}
return Errors.createErrorFromException(ex);
}
}
}
private Object doJSException(JSException exception) {
DynamicObject errorObj = exception.getErrorObject();
if (errorObj == null) {
JSRealm realm = exception.getRealm();
if (realm == null) {
realm = context.getRealm();
}
String message = exception.getRawMessage();
assert message != null;
errorObj = createErrorFromJSException(context, realm, exception.getErrorType());
initErrorObjectNode.execute(errorObj, exception, message);
exception.setErrorObject(errorObj);
}
return errorObj;
}
@TruffleBoundary
private static DynamicObject createErrorFromJSException(JSContext context, JSRealm realm, JSErrorType errorType) {
return JSError.createErrorObject(context, realm, errorType);
}
}
}