use of org.graalvm.polyglot.Context 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.Context 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();
}
}
use of org.graalvm.polyglot.Context in project sulong by graalvm.
the class ParserTortureSuite method test.
@Test
public void test() throws Exception {
List<Path> testCandidates = Files.walk(path).filter(BaseTestHarness.isFile).filter(BaseTestHarness.isSulong).collect(Collectors.toList());
for (Path candidate : testCandidates) {
if (!candidate.toAbsolutePath().toFile().exists()) {
throw new AssertionError("File " + candidate.toAbsolutePath().toFile() + " does not exist.");
}
try {
Context context = Context.create();
context.eval(org.graalvm.polyglot.Source.newBuilder(LLVMLanguage.NAME, candidate.toFile()).build());
context.close();
} catch (Throwable e) {
throw e;
}
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class PolyglotEngineOptionsTest method testCompilationThreshold.
private static void testCompilationThreshold(int value, String optionValue, Runnable doWhile) {
Context.Builder builder = Context.newBuilder("sl");
if (optionValue != null) {
builder.option(COMPILATION_THRESHOLD_OPTION, optionValue);
}
Context context = builder.build();
// installs isOptimized
installSLBuiltin(context, SLIsOptimizedBuiltinFactory.getInstance());
context.eval("sl", "function test() {}");
Value test = context.getBindings("sl").getMember("test");
Value isOptimized = context.getBindings("sl").getMember("isOptimized");
Assert.assertFalse(isOptimized.execute(test).asBoolean());
for (int i = 0; i < value - 1; i++) {
Assert.assertFalse(isOptimized.execute(test).asBoolean());
test.execute();
}
if (doWhile != null) {
doWhile.run();
}
Assert.assertFalse(isOptimized.execute(test).asBoolean());
test.execute();
Assert.assertTrue(isOptimized.execute(test).asBoolean());
test.execute();
Assert.assertTrue(isOptimized.execute(test).asBoolean());
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SplittingStrategyTest method testSplitLimitIsContextSpecific.
@Test
public void testSplitLimitIsContextSpecific() {
Context c1 = Context.create();
Context c2 = Context.create();
// Use up the c1 budget
useUpTheBudget(c1);
final int c1BaseSplitCount = listener.splitCount;
// Try to split some more in c1
for (int i = 0; i < 10; i++) {
eval(c1, "exec");
}
Assert.assertEquals("Splitting over budget!", c1BaseSplitCount, listener.splitCount);
// Try to split in c2
for (int i = 0; i < 10; i++) {
eval(c2, "exec");
}
Assert.assertTrue("No splitting in different context", c1BaseSplitCount < listener.splitCount);
}
Aggregations