use of com.oracle.truffle.api.TruffleException in project graal by oracle.
the class TruffleExecutionContext method executeInSuspendThread.
<T> T executeInSuspendThread(SuspendThreadExecutable<T> executable) throws NoSuspendedThreadException, GuestLanguageException, CommandProcessException {
CompletableFuture<T> cf = new CompletableFuture<>();
suspendThreadExecutor.execute(new CancellableRunnable() {
@Override
public void run() {
T params = null;
try {
params = executable.executeCommand();
cf.complete(params);
} catch (ThreadDeath td) {
cf.completeExceptionally(td);
throw td;
} catch (Throwable t) {
cf.completeExceptionally(t);
}
}
@Override
public void cancel() {
cf.completeExceptionally(new NoSuspendedThreadException("Resuming..."));
}
});
T params;
try {
params = cf.get();
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
if (cause instanceof TruffleException && !((TruffleException) cause).isInternalError()) {
throw new GuestLanguageException((TruffleException) cause);
}
if (cause instanceof CommandProcessException) {
throw (CommandProcessException) cause;
}
if (cause instanceof NoSuspendedThreadException) {
throw (NoSuspendedThreadException) cause;
}
if (err != null) {
cause.printStackTrace(err);
}
throw new CommandProcessException(ex.getLocalizedMessage());
} catch (InterruptedException ex) {
throw new CommandProcessException(ex.getLocalizedMessage());
}
return params;
}
use of com.oracle.truffle.api.TruffleException in project graal by oracle.
the class PolyglotExceptionTest method testLanguageExceptionUnwrapping.
@Test
public void testLanguageExceptionUnwrapping() {
Context context = Context.create();
Value throwError = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
throw new RuntimeException();
}
});
VerifyErrorTruffleObject checkErrorObj = new VerifyErrorTruffleObject();
Value checkError = context.asValue(checkErrorObj);
checkErrorObj.verifyError = (e) -> {
Assert.assertTrue(e instanceof TruffleException);
Assert.assertEquals("HostException", e.getClass().getSimpleName());
};
Assert.assertTrue(checkError.execute(throwError).asBoolean());
context.close();
}
use of com.oracle.truffle.api.TruffleException in project graal by oracle.
the class TruffleDebugger method evaluateOnCallFrame.
@Override
public Params evaluateOnCallFrame(String callFrameId, String expression, String objectGroup, boolean includeCommandLineAPI, boolean silent, boolean returnByValue, boolean generatePreview, boolean throwOnSideEffect) throws CommandProcessException {
if (callFrameId == null) {
throw new CommandProcessException("A callFrameId required.");
}
if (expression == null) {
throw new CommandProcessException("An expression required.");
}
int frameId;
try {
frameId = Integer.parseInt(callFrameId);
} catch (NumberFormatException ex) {
throw new CommandProcessException(ex.getLocalizedMessage());
}
JSONObject jsonResult;
try {
jsonResult = context.executeInSuspendThread(new SuspendThreadExecutable<JSONObject>() {
@Override
public JSONObject executeCommand() throws CommandProcessException {
if (frameId >= suspendedInfo.getCallFrames().length) {
throw new CommandProcessException("Too big callFrameId: " + frameId);
}
CallFrame cf = suspendedInfo.getCallFrames()[frameId];
JSONObject json = new JSONObject();
try {
DebugValue value = cf.getFrame().eval(expression);
RemoteObject ro = new RemoteObject(value, context.getErr());
context.getRemoteObjectsHandler().register(ro);
json.put("result", ro.toJSON());
} catch (Throwable t) {
if (t instanceof TruffleException && !((TruffleException) t).isInternalError()) {
JSONObject err = new JSONObject();
err.putOpt("value", t.getLocalizedMessage());
json.put("result", err);
} else {
throw t;
}
}
return json;
}
});
} catch (NoSuspendedThreadException e) {
jsonResult = new JSONObject();
JSONObject err = new JSONObject();
err.putOpt("value", e.getLocalizedMessage());
jsonResult.put("result", err);
} catch (GuestLanguageException e) {
jsonResult = new JSONObject();
TruffleRuntime.fillExceptionDetails(jsonResult, e);
}
return new Params(jsonResult);
}
Aggregations