use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ScriptTest method testScript.
@Test
public void testScript() {
Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
boolean success = false;
try {
try {
final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
TestUtil.validateResult(testRun, result, null);
success = true;
} catch (PolyglotException pe) {
TestUtil.validateResult(testRun, null, pe);
success = true;
}
} finally {
TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class DebugALot method logThrowable.
private void logThrowable(Throwable t) {
logger.print("\nERROR: Thrown: '");
logger.print(t.getLocalizedMessage());
logger.print("', throwable class = ");
logger.println(t.getClass());
if (t instanceof PolyglotException) {
PolyglotException pe = (PolyglotException) t;
logger.print(" Polyglot Message: '");
logger.print(pe.getMessage());
logger.println("'");
logger.print(" canceled = ");
logger.print(pe.isCancelled());
logger.print(", exited = ");
logger.print(pe.isExit());
logger.print(", guest ex. = ");
logger.print(pe.isGuestException());
logger.print(", host ex. = ");
logger.print(pe.isHostException());
logger.print(", incompl. source = ");
logger.print(pe.isIncompleteSource());
logger.print(", internal = ");
logger.print(pe.isInternalError());
logger.print(", syntax error = ");
logger.println(pe.isSyntaxError());
logger.print(" Source Section: ");
logSourceSection(pe.getSourceLocation());
if (pe.isExit()) {
logger.print(" Exit Status = ");
logger.println(pe.getExitStatus());
}
if (pe.isGuestException()) {
Value guestObject = pe.getGuestObject();
logger.print(" Guest Object = ");
logger.println(guestObject.toString());
}
if (pe.isHostException()) {
logger.println(" Host Exception:");
pe.asHostException().printStackTrace(logger);
}
logger.println(" Polyglot Stack Trace:");
for (PolyglotException.StackFrame sf : pe.getPolyglotStackTrace()) {
logger.print(" Language ID: ");
logger.println(sf.getLanguage().getId());
logger.print(" Root Name: ");
logger.println(sf.getRootName());
logger.print(" Source Location: ");
logSourceSection(sf.getSourceLocation());
logger.print(" Guest Frame: ");
logger.println(sf.isGuestFrame());
logger.print(" Host Frame: ");
if (sf.isHostFrame()) {
logger.println(sf.toHostFrame());
} else {
logger.println(false);
}
}
} else {
t.printStackTrace(logger);
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class IdentityFunctionTest method testIdentityFunction.
@Test
public void testIdentityFunction() {
Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
boolean success = false;
try {
try {
final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
TestUtil.validateResult(testRun, result, null);
success = true;
} catch (PolyglotException pe) {
TestUtil.validateResult(testRun, null, pe);
success = true;
}
} finally {
TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class EvalLauncher method main.
public static void main(String[] args) {
Map<String, String> options = new HashMap<>();
List<String> scripts = new ArrayList<>();
for (String option : args) {
if (option.equals("--version")) {
printVersions();
return;
} else if (option.equals("--help")) {
printHelp(OptionCategory.USER);
return;
} else if (option.equals("--experthelp")) {
printHelp(OptionCategory.EXPERT);
return;
} else if (option.equals("--debughelp")) {
printHelp(OptionCategory.DEBUG);
return;
} else if (option.startsWith("--")) {
int equalIndex = option.indexOf("=");
int keyEndIndex;
String value = "";
if (equalIndex == -1) {
keyEndIndex = option.length();
} else {
keyEndIndex = equalIndex;
value = option.substring(equalIndex + 1, option.length());
}
String key = option.substring(2, keyEndIndex);
options.put(key, value);
} else {
scripts.add(option);
}
}
if (scripts.isEmpty()) {
System.err.println("Error: No files to execute specified.\nUse --help for usage information.");
return;
}
Context context = Context.newBuilder().options(options).build();
for (String script : scripts) {
int index = script.indexOf(':');
if (index == -1) {
System.err.println(String.format("Error: Invalid script %s provided.\nUse --help for usage information.", script));
return;
}
String languageId = script.substring(0, index);
if (context.getEngine().getLanguages().containsKey(languageId)) {
System.err.println(String.format("Error: Invalid language %s provided.\nUse --help for usage information.", languageId));
return;
}
String code = script.substring(index + 1, script.length());
System.out.println("Script: " + script + ": ");
try {
Value evalValue = context.eval(languageId, code);
System.out.println("Result type: " + evalValue.getMetaObject());
System.out.println("Result value: " + evalValue);
} catch (PolyglotException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class CancelExecution method main.
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
try {
Context context = Context.create("js");
// we submit a harmful infinite script to the executor
Future<Value> future = service.submit(() -> context.eval("js", "while(true);"));
// wait some time to let the execution start.
Thread.sleep(1000);
/*
* closes the context and cancels the running execution. This can be done on any
* parallel thread. Alternatively context.close(true) can be used to close all running
* contexts of an engine.
*/
context.close(true);
try {
future.get();
} catch (ExecutionException e) {
PolyglotException polyglotException = (PolyglotException) e.getCause();
polyglotException.printStackTrace();
/*
* After the execution got cancelled the executing thread stops by throwning a
* PolyglotException with the cancelled flag set.
*/
assert polyglotException.isCancelled();
}
} finally {
service.shutdown();
}
}
Aggregations