
com.oracle.truffle.api.exception.ExceptionAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truffle-api Show documentation
Show all versions of truffle-api Show documentation
Truffle is a multi-language framework for executing dynamic languages
that achieves high performance when combined with Graal.
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api.exception;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Function;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractHostLanguageService;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.TruffleStackTraceElement;
import com.oracle.truffle.api.impl.Accessor;
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.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.SourceSection;
final class ExceptionAccessor extends Accessor {
static final ExceptionAccessor ACCESSOR = new ExceptionAccessor();
private ExceptionAccessor() {
}
static final class ExceptionSupportImpl extends ExceptionSupport {
@Override
public Throwable getLazyStackTrace(Throwable exception) {
return ((AbstractTruffleException) exception).getLazyStackTrace();
}
@Override
public void setLazyStackTrace(Throwable exception, Throwable stackTrace) {
((AbstractTruffleException) exception).setLazyStackTrace(stackTrace);
}
@Override
public Object createDefaultStackTraceElementObject(RootNode rootNode, SourceSection sourceSection) {
return new DefaultStackTraceElementObject(rootNode, sourceSection);
}
@Override
public boolean isException(Object receiver) {
return receiver instanceof AbstractTruffleException;
}
@Override
public RuntimeException throwException(Object receiver) {
throw (AbstractTruffleException) receiver;
}
@Override
public Object getExceptionType(Object receiver) {
return ExceptionType.RUNTIME_ERROR;
}
@Override
public boolean isExceptionIncompleteSource(Object receiver) {
return false;
}
@Override
public int getExceptionExitStatus(Object receiver) {
throw throwUnsupportedMessageException();
}
@Override
public boolean hasExceptionCause(Object receiver) {
return isException(((AbstractTruffleException) receiver).getCause());
}
@Override
public Object getExceptionCause(Object receiver) {
Throwable throwable = ((AbstractTruffleException) receiver).getCause();
if (isException(throwable)) {
return throwable;
} else {
throw throwUnsupportedMessageException();
}
}
@Override
@TruffleBoundary
public boolean hasExceptionMessage(Object receiver) {
return ((AbstractTruffleException) receiver).getMessage() != null;
}
@Override
@TruffleBoundary
public Object getExceptionMessage(Object receiver) {
String message = ((AbstractTruffleException) receiver).getMessage();
if (message == null) {
throw throwUnsupportedMessageException();
} else {
return message;
}
}
@Override
public boolean hasExceptionStackTrace(Object receiver) {
return true;
}
@Override
@TruffleBoundary
public Object getExceptionStackTrace(Object receiver, Object polyglotContext) {
Throwable throwable = (Throwable) receiver;
List stack = TruffleStackTrace.getStackTrace(throwable);
if (stack == null) {
stack = Collections.emptyList();
}
boolean hasGuestToHostCalls = false;
boolean inHost = true;
for (TruffleStackTraceElement element : stack) {
if (ACCESSOR.hostSupport().isGuestToHostRootNode(element.getTarget().getRootNode())) {
hasGuestToHostCalls = true;
break;
} else {
inHost = false;
}
}
Object[] items;
if (hasGuestToHostCalls) {
// If we have guest to host calls, we need to merge in (or filter) the host frames.
Object polyglotEngine = polyglotContext == null
? ACCESSOR.engineSupport().getCurrentPolyglotEngine()
: ACCESSOR.engineSupport().getEngineFromPolyglotObject(polyglotContext);
items = mergeHostGuestFrames(throwable, stack, inHost, polyglotEngine);
} else {
// If there are no guest to host calls, there is no need for any extra processing.
items = new Object[stack.size()];
for (int i = 0; i < items.length; i++) {
items[i] = stack.get(i).getGuestObject();
}
}
return new InteropList(items);
}
private static Object[] mergeHostGuestFrames(Throwable throwable, List guestStack, boolean inHost, Object polyglotEngine) {
StackTraceElement[] hostStack = null;
AbstractHostLanguageService hostService = ACCESSOR.engineSupport().getHostService(polyglotEngine);
if (hostService.isHostException(throwable)) {
Throwable original = hostService.unboxHostException(throwable);
hostStack = original.getStackTrace();
} else if (throwable instanceof AbstractTruffleException) {
Throwable lazyStackTrace = ((AbstractTruffleException) throwable).getLazyStackTrace();
if (lazyStackTrace != null) {
hostStack = ACCESSOR.languageSupport().getInternalStackTraceElements(lazyStackTrace);
}
} else {
// Internal error.
hostStack = throwable.getStackTrace();
}
if (hostStack == null) {
// protect against getStackTrace() returning null
hostStack = new StackTraceElement[0];
}
ListIterator guestStackIterator = guestStack.listIterator();
boolean includeHostFrames = ACCESSOR.engineSupport().getHostService(polyglotEngine).isHostStackTraceVisibleToGuest();
int lastGuestToHostIndex = includeHostFrames ? indexOfLastGuestToHostFrame(guestStack) : -1;
Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy