Search in sources :

Example 1 with APIAccess

use of org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess in project graal by oracle.

the class PolyglotHostAccess method rethrowPolyglotException.

@Override
public void rethrowPolyglotException(Object internalContext, PolyglotException e) {
    PolyglotContextImpl context = (PolyglotContextImpl) internalContext;
    APIAccess api = polyglot.getAPIAccess();
    PolyglotExceptionImpl exceptionImpl = ((PolyglotExceptionImpl) api.getReceiver(e));
    if (exceptionImpl.context == context || exceptionImpl.context == null || exceptionImpl.isHostException()) {
        // for values of the same context the TruffleException is allowed to be unboxed
        // for host exceptions no guest values are bound therefore it can also be
        // unboxed
        Throwable original = ((PolyglotExceptionImpl) api.getReceiver(e)).exception;
        if (original instanceof RuntimeException) {
            throw (RuntimeException) original;
        } else if (original instanceof Error) {
            throw (Error) original;
        }
    }
// fall-through and treat it as any other host exception
}
Also used : APIAccess(org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess)

Example 2 with APIAccess

use of org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess in project graal by oracle.

the class PolyglotExceptionImpl method createStackFrameIterator.

static Iterator<StackFrame> createStackFrameIterator(PolyglotExceptionImpl impl) {
    APIAccess apiAccess = impl.polyglot.getAPIAccess();
    Throwable cause = findCause(impl.engine, impl.exception);
    StackTraceElement[] hostStack;
    if (EngineAccessor.LANGUAGE.isTruffleStackTrace(cause)) {
        hostStack = EngineAccessor.LANGUAGE.getInternalStackTraceElements(cause);
    } else if (cause.getStackTrace() == null || cause.getStackTrace().length == 0) {
        hostStack = impl.exception.getStackTrace();
    } else {
        hostStack = cause.getStackTrace();
    }
    Iterator<TruffleStackTraceElement> guestFrames = impl.guestFrames == null ? Collections.emptyIterator() : impl.guestFrames.iterator();
    // we always start in some host stack frame
    boolean inHostLanguage = impl.isHostException() || impl.isInternalError();
    if (TRACE_STACK_TRACE_WALKING) {
        // To mark the beginning of the stack trace and separate from the previous one
        PrintStream out = System.out;
        out.println();
    }
    return new MergedHostGuestIterator<>(impl.engine, hostStack, guestFrames, inHostLanguage, new Function<StackTraceElement, StackFrame>() {

        @Override
        public StackFrame apply(StackTraceElement element) {
            return apiAccess.newPolyglotStackTraceElement(PolyglotExceptionFrame.createHost(impl, element), impl.api);
        }
    }, new Function<TruffleStackTraceElement, StackFrame>() {

        private boolean firstGuestFrame = true;

        @Override
        public StackFrame apply(TruffleStackTraceElement guestFrame) {
            boolean first = this.firstGuestFrame;
            this.firstGuestFrame = false;
            PolyglotExceptionFrame guest = PolyglotExceptionFrame.createGuest(impl, guestFrame, first);
            if (guest != null) {
                return apiAccess.newPolyglotStackTraceElement(guest, impl.api);
            } else {
                return null;
            }
        }
    });
}
Also used : PrintStream(java.io.PrintStream) TruffleStackTraceElement(com.oracle.truffle.api.TruffleStackTraceElement) TruffleStackTraceElement(com.oracle.truffle.api.TruffleStackTraceElement) APIAccess(org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame)

Example 3 with APIAccess

use of org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess in project graal by oracle.

the class HostContextDispatch method eval.

@Override
public Value eval(Object receiver, String language, Source source) {
    HostContext context = (HostContext) receiver;
    APIAccess apiAccess = polyglot.getAPIAccess();
    AbstractSourceDispatch sourceDispatch = apiAccess.getDispatch(source);
    Object sourceImpl = apiAccess.getReceiver(source);
    String languageId = sourceDispatch.getLanguage(sourceImpl);
    String characters = sourceDispatch.getCharacters(sourceImpl).toString();
    long remoteValue = hostToGuest.remoteEval(context.remoteContext, languageId, characters);
    return context.localContext.asValue(new HostGuestValue(hostToGuest, context.remoteContext, remoteValue));
}
Also used : AbstractSourceDispatch(org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractSourceDispatch) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) APIAccess(org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess)

Aggregations

APIAccess (org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess)3 TruffleStackTraceElement (com.oracle.truffle.api.TruffleStackTraceElement)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 PrintStream (java.io.PrintStream)1 StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)1 AbstractSourceDispatch (org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractSourceDispatch)1