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
}
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;
}
}
});
}
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));
}
Aggregations