Search in sources :

Example 1 with TruffleAdapter

use of org.graalvm.tools.lsp.server.TruffleAdapter in project graal by oracle.

the class LSPInstrument method onCreate.

@Override
protected void onCreate(Env env) {
    env.registerService(this);
    this.environment = env;
    options = env.getOptions();
    if (options.hasSetOptions()) {
        final TruffleAdapter truffleAdapter = launchServer(new PrintWriter(env.out(), true), new PrintWriter(env.err(), true));
        SourceSectionFilter eventFilter = SourceSectionFilter.newBuilder().includeInternal(options.get(Internal)).build();
        eventFactoryBinding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, new ExecutionEventNodeFactory() {

            private final long creatorThreadId = Thread.currentThread().getId();

            @Override
            public ExecutionEventNode create(final EventContext eventContext) {
                final SourceSection section = eventContext.getInstrumentedSourceSection();
                if (section != null && section.isAvailable()) {
                    final Node instrumentedNode = eventContext.getInstrumentedNode();
                    return new CoverageEventNode(section, instrumentedNode, null, truffleAdapter.surrogateGetter(instrumentedNode.getRootNode().getLanguageInfo()), creatorThreadId);
                } else {
                    return null;
                }
            }
        });
    }
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) TruffleAdapter(org.graalvm.tools.lsp.server.TruffleAdapter) ExecutionEventNodeFactory(com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) SourceSection(com.oracle.truffle.api.source.SourceSection) PrintWriter(java.io.PrintWriter)

Example 2 with TruffleAdapter

use of org.graalvm.tools.lsp.server.TruffleAdapter in project graal by oracle.

the class LSPInstrument method launchServer.

private TruffleAdapter launchServer(PrintWriter info, PrintWriter err) {
    assert options != null;
    assert options.hasSetOptions();
    TruffleAdapter truffleAdapter = new TruffleAdapter(environment, options.get(DeveloperMode));
    Context.Builder builder = Context.newBuilder();
    builder.allowAllAccess(true);
    builder.engine(Engine.create());
    builder.fileSystem(LSPFileSystem.newReadOnlyFileSystem(truffleAdapter));
    ContextAwareExecutor executorWrapper = new ContextAwareExecutorImpl(builder);
    setWaitForClose();
    executorWrapper.executeWithDefaultContext(() -> {
        HostAndPort hostAndPort = options.get(Lsp);
        try {
            Context context = builder.build();
            context.enter();
            Instrument instrument = context.getEngine().getInstruments().get(ID);
            EnvironmentProvider envProvider = instrument.lookup(EnvironmentProvider.class);
            truffleAdapter.register(envProvider.getEnvironment(), executorWrapper);
            InetSocketAddress socketAddress = hostAndPort.createSocket();
            int port = socketAddress.getPort();
            Integer backlog = options.get(SocketBacklogSize);
            InetAddress address = socketAddress.getAddress();
            ServerSocket serverSocket = new ServerSocket(port, backlog, address);
            List<Pair<String, SocketAddress>> delegates = createDelegateSockets(options.get(Delegates));
            LanguageServerImpl.create(truffleAdapter, info, err).start(serverSocket, delegates).thenRun(() -> {
                try {
                    executorWrapper.executeWithDefaultContext(() -> {
                        context.leave();
                        return null;
                    }).get();
                } catch (ExecutionException | InterruptedException ex) {
                }
                executorWrapper.shutdown();
                notifyClose();
            }).exceptionally((throwable) -> {
                throwable.printStackTrace(err);
                notifyClose();
                return null;
            });
        } catch (ThreadDeath td) {
            throw td;
        } catch (Throwable e) {
            String message = String.format("[Graal LSP] Starting server on %s failed: %s", hostAndPort.getHostPort(), e.getLocalizedMessage());
            new LSPIOException(message, e).printStackTrace(err);
        }
        return null;
    });
    return truffleAdapter;
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) Context(org.graalvm.polyglot.Context) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) TruffleAdapter(org.graalvm.tools.lsp.server.TruffleAdapter) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) LSPIOException(org.graalvm.tools.lsp.exceptions.LSPIOException) InetAddress(java.net.InetAddress) ContextAwareExecutor(org.graalvm.tools.lsp.server.ContextAwareExecutor) Pair(org.graalvm.collections.Pair)

Example 3 with TruffleAdapter

use of org.graalvm.tools.lsp.server.TruffleAdapter 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

TruffleAdapter (org.graalvm.tools.lsp.server.TruffleAdapter)3 EventContext (com.oracle.truffle.api.instrumentation.EventContext)2 Context (org.graalvm.polyglot.Context)2 Instrument (org.graalvm.polyglot.Instrument)2 ContextAwareExecutor (org.graalvm.tools.lsp.server.ContextAwareExecutor)2 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)1 ExecutionEventNodeFactory (com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory)1 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)1 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)1 Node (com.oracle.truffle.api.nodes.Node)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)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