use of org.graalvm.polyglot.Context.Builder in project graal by oracle.
the class ProcessUtil method executeSulongTestMainSameEngine.
public static ProcessResult executeSulongTestMainSameEngine(File bitcodeFile, String[] args, Map<String, String> options, Function<Context.Builder, CaptureOutput> captureOutput, Engine engine) throws IOException {
if (TestOptions.TEST_AOT_IMAGE == null) {
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.newBuilder(LLVMLanguage.ID, bitcodeFile).build();
Builder builder = Context.newBuilder();
try (CaptureOutput out = captureOutput.apply(builder)) {
int result;
try (Context context = builder.engine(engine).arguments(LLVMLanguage.ID, args).options(options).allowAllAccess(true).build()) {
Value main = context.eval(source);
if (!main.canExecute()) {
throw new LLVMLinkerException("No main function found.");
}
result = main.execute().asInt();
}
return new ProcessResult(bitcodeFile.getName(), result, out.getStdErr(), out.getStdOut());
}
} else {
String aotArgs = TestOptions.TEST_AOT_ARGS == null ? "" : TestOptions.TEST_AOT_ARGS + " ";
String cmdline = TestOptions.TEST_AOT_IMAGE + " " + aotArgs + concatOptions(options) + bitcodeFile.getAbsolutePath() + " " + concatCommand(args);
return executeNativeCommand(cmdline);
}
}
use of org.graalvm.polyglot.Context.Builder in project graal by oracle.
the class TruffleLSPTest method setup.
@Before
public void setup() {
engine = Engine.newBuilder().allowExperimentalOptions(true).build();
Instrument instrument = engine.getInstruments().get("lsp");
EnvironmentProvider envProvider = instrument.lookup(EnvironmentProvider.class);
truffleAdapter = new TruffleAdapter(envProvider.getEnvironment(), true);
Builder contextBuilder = Context.newBuilder();
contextBuilder.allowAllAccess(true);
contextBuilder.fileSystem(LSPFileSystem.newReadOnlyFileSystem(truffleAdapter));
contextBuilder.engine(engine);
context = contextBuilder.build();
context.enter();
ContextAwareExecutor executorWrapper = new ContextAwareExecutor() {
@Override
public <T> Future<T> executeWithDefaultContext(Callable<T> taskWithResult) {
try {
return CompletableFuture.completedFuture(taskWithResult.call());
} catch (Exception e) {
CompletableFuture<T> cf = new CompletableFuture<>();
cf.completeExceptionally(e);
return cf;
}
}
@Override
public <T> Future<T> executeWithNestedContext(Callable<T> taskWithResult, boolean cached) {
try (Context newContext = contextBuilder.build()) {
newContext.enter();
newContext.initialize("sl");
try {
return CompletableFuture.completedFuture(taskWithResult.call());
} catch (Exception e) {
CompletableFuture<T> cf = new CompletableFuture<>();
cf.completeExceptionally(e);
return cf;
} finally {
newContext.leave();
}
}
}
@Override
public <T> Future<T> executeWithNestedContext(Callable<T> taskWithResult, int timeoutMillis, Callable<T> onTimeoutTask) {
if (timeoutMillis <= 0) {
return executeWithNestedContext(taskWithResult, false);
} else {
CompletableFuture<Future<T>> future = CompletableFuture.supplyAsync(() -> executeWithNestedContext(taskWithResult, false));
try {
return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
future.cancel(true);
try {
return CompletableFuture.completedFuture(onTimeoutTask.call());
} catch (Exception timeoutTaskException) {
CompletableFuture<T> cf = new CompletableFuture<>();
cf.completeExceptionally(timeoutTaskException);
return cf;
}
} catch (InterruptedException | ExecutionException e) {
CompletableFuture<T> cf = new CompletableFuture<>();
cf.completeExceptionally(e);
return cf;
}
}
}
@Override
public void shutdown() {
}
@Override
public void resetContextCache() {
}
};
truffleAdapter.register(envProvider.getEnvironment(), executorWrapper);
}
Aggregations