use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class LanguageSPITest method testExceptionGetSourceLocation.
@Test
public void testExceptionGetSourceLocation() {
try (final Context context = Context.create(LanguageSPITestLanguage.ID)) {
final String text = "0123456789";
LanguageSPITestLanguage.runinside = (env) -> {
Source src = Source.newBuilder(text).mimeType(LanguageSPITestLanguage.ID).name("test.txt").build();
throw new ParseException(src, 1, 2);
};
try {
context.eval(LanguageSPITestLanguage.ID, text);
Assert.fail("PolyglotException expected.");
} catch (PolyglotException pe) {
Assert.assertTrue(pe.isSyntaxError());
Assert.assertEquals("12", pe.getSourceLocation().getCharacters().toString());
} finally {
LanguageSPITestLanguage.runinside = null;
}
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class LanguageSPITest method testCancelExecutionWhileSleeping.
@Test
public void testCancelExecutionWhileSleeping() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
try {
Engine engine = Engine.create();
Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
CountDownLatch beforeSleep = new CountDownLatch(1);
CountDownLatch interrupt = new CountDownLatch(1);
AtomicInteger gotInterrupt = new AtomicInteger(0);
Function<Env, Object> f = new Function<Env, Object>() {
public Object apply(Env t) {
try {
beforeSleep.countDown();
Thread.sleep(5000);
} catch (InterruptedException e) {
gotInterrupt.incrementAndGet();
interrupt.countDown();
throw new Interrupted();
}
return null;
}
};
Future<Value> future = service.submit(() -> eval(context, f));
beforeSleep.await(10000, TimeUnit.MILLISECONDS);
context.close(true);
interrupt.await(10000, TimeUnit.MILLISECONDS);
assertEquals(1, gotInterrupt.get());
try {
future.get();
fail();
} catch (ExecutionException e) {
PolyglotException polyglotException = (PolyglotException) e.getCause();
assertTrue(polyglotException.isCancelled());
}
engine.close();
} finally {
service.shutdown();
}
}
use of org.graalvm.polyglot.PolyglotException in project sulong by graalvm.
the class LLVMLauncher method execute.
protected int execute(Context.Builder contextBuilder) {
contextBuilder.arguments(getLanguageId(), programArgs);
try (Context context = contextBuilder.build()) {
runVersionAction(versionAction, context.getEngine());
Value library = context.eval(Source.newBuilder(getLanguageId(), file).build());
if (!library.canExecute()) {
throw abort("no main function found");
}
int status = library.execute().asInt();
if (printResult) {
System.out.println("Result: " + status);
}
return status;
} catch (PolyglotException e) {
if (e.isExit()) {
return e.getExitStatus();
}
throw e;
} catch (IOException e) {
throw abort(e);
}
}
Aggregations