use of org.graalvm.tools.lsp.instrument.EnvironmentProvider 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