Search in sources :

Example 1 with Builder

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);
    }
}
Also used : CaptureOutput(com.oracle.truffle.llvm.tests.pipe.CaptureOutput) Context(org.graalvm.polyglot.Context) Builder(org.graalvm.polyglot.Context.Builder) Value(org.graalvm.polyglot.Value) LLVMLinkerException(com.oracle.truffle.llvm.runtime.except.LLVMLinkerException)

Example 2 with Builder

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);
}
Also used : Context(org.graalvm.polyglot.Context) Builder(org.graalvm.polyglot.Context.Builder) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EnvironmentProvider(org.graalvm.tools.lsp.instrument.EnvironmentProvider) CompletableFuture(java.util.concurrent.CompletableFuture) TruffleAdapter(org.graalvm.tools.lsp.server.TruffleAdapter) Instrument(org.graalvm.polyglot.Instrument) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) ContextAwareExecutor(org.graalvm.tools.lsp.server.ContextAwareExecutor) TimeoutException(java.util.concurrent.TimeoutException) Before(org.junit.Before)

Aggregations

Context (org.graalvm.polyglot.Context)2 Builder (org.graalvm.polyglot.Context.Builder)2 LLVMLinkerException (com.oracle.truffle.llvm.runtime.except.LLVMLinkerException)1 CaptureOutput (com.oracle.truffle.llvm.tests.pipe.CaptureOutput)1 IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 Instrument (org.graalvm.polyglot.Instrument)1 Value (org.graalvm.polyglot.Value)1 EnvironmentProvider (org.graalvm.tools.lsp.instrument.EnvironmentProvider)1 ContextAwareExecutor (org.graalvm.tools.lsp.server.ContextAwareExecutor)1 TruffleAdapter (org.graalvm.tools.lsp.server.TruffleAdapter)1 Before (org.junit.Before)1