Get JSON LSP running, still work to be done

This commit is contained in:
Isaac Clayton 2022-06-06 16:02:54 +02:00
parent 38d7321511
commit fbaff615a3
7 changed files with 269 additions and 88 deletions

2
Cargo.lock generated
View File

@ -3718,6 +3718,8 @@ dependencies = [
"anyhow", "anyhow",
"bincode", "bincode",
"serde", "serde",
"serde_json",
"wasi-common",
"wasmtime", "wasmtime",
"wasmtime-wasi", "wasmtime-wasi",
] ]

View File

@ -2,7 +2,7 @@ use core::panic;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use syn::{parse_macro_input, ItemFn, Visibility}; use syn::{parse_macro_input, FnArg, ItemFn, Type, Visibility};
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream { pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
@ -21,7 +21,17 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
let variadic = inner_fn.sig.inputs.len(); let variadic = inner_fn.sig.inputs.len();
let i = (0..variadic).map(syn::Index::from); let i = (0..variadic).map(syn::Index::from);
let t = (0..variadic).map(|_| quote! { _ }); let t: Vec<Type> = inner_fn
.sig
.inputs
.iter()
.map(|x| match x {
FnArg::Receiver(_) => {
panic!("all arguments must have specified types, no `self` allowed")
}
FnArg::Typed(item) => *item.ty.clone(),
})
.collect();
// this is cursed... // this is cursed...
let (args, ty) = if variadic != 1 { let (args, ty) = if variadic != 1 {
@ -34,7 +44,8 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
}, },
) )
} else { } else {
(quote! { data }, quote! { _ }) let ty = &t[0];
(quote! { data }, quote! { #ty })
}; };
TokenStream::from(quote! { TokenStream::from(quote! {
@ -48,7 +59,14 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
let data = unsafe { buffer.to_vec() }; let data = unsafe { buffer.to_vec() };
// operation // operation
let data: #ty = ::plugin::bincode::deserialize(&data).unwrap(); let data: #ty = match ::plugin::bincode::deserialize(&data) {
Ok(d) => d,
Err(e) => {
println!("data: {:?}", data);
println!("error: {}", e);
panic!("Data passed to function not deserializable.")
},
};
let result = #inner_fn_name(#args); let result = #inner_fn_name(#args);
let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result); let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result);
let new_data = new_data.unwrap(); let new_data = new_data.unwrap();

View File

@ -6,6 +6,8 @@ edition = "2021"
[dependencies] [dependencies]
wasmtime = "0.37.0" wasmtime = "0.37.0"
wasmtime-wasi = "0.37.0" wasmtime-wasi = "0.37.0"
wasi-common = "0.37.0"
anyhow = { version = "1.0", features = ["std"] } anyhow = { version = "1.0", features = ["std"] }
serde = "1.0" serde = "1.0"
serde_json = "1.0"
bincode = "1.3" bincode = "1.3"

View File

@ -1,10 +1,13 @@
use std::{collections::HashMap, fs::File, path::Path}; use std::{fs::File, os::unix::prelude::AsRawFd, path::Path};
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use wasmtime::{Engine, Func, Instance, Linker, Memory, MemoryType, Module, Store, TypedFunc}; use wasi_common::{dir, file};
use wasmtime_wasi::{dir, Dir, WasiCtx, WasiCtxBuilder}; use wasmtime::{Engine, Instance, Linker, Module, Store, TypedFunc};
use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
pub struct WasiResource(u32);
pub struct Wasi { pub struct Wasi {
engine: Engine, engine: Engine,
@ -50,8 +53,13 @@ impl Wasi {
pub fn init(plugin: WasiPlugin) -> Result<Self, Error> { pub fn init(plugin: WasiPlugin) -> Result<Self, Error> {
let engine = Engine::default(); let engine = Engine::default();
let mut linker = Linker::new(&engine); let mut linker = Linker::new(&engine);
linker.func_wrap("env", "hello", |x: u32| x * 2).unwrap();
linker.func_wrap("env", "bye", |x: u32| x / 2).unwrap();
println!("linking"); println!("linking");
wasmtime_wasi::add_to_linker(&mut linker, |s| s)?; wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
println!("linked"); println!("linked");
let mut store: Store<_> = Store::new(&engine, plugin.wasi_ctx); let mut store: Store<_> = Store::new(&engine, plugin.wasi_ctx);
println!("moduling"); println!("moduling");
@ -60,13 +68,13 @@ impl Wasi {
linker.module(&mut store, "", &module)?; linker.module(&mut store, "", &module)?;
println!("linked again"); println!("linked again");
let instance = linker.instantiate(&mut store, &module)?; let instance = linker.instantiate(&mut store, &module)?;
println!("instantiated"); println!("instantiated");
let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?; let alloc_buffer = instance.get_typed_func(&mut store, "__alloc_buffer")?;
// let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?; // let free_buffer = instance.get_typed_func(&mut store, "__free_buffer")?;
println!("can alloc"); println!("can alloc");
Ok(Wasi { Ok(Wasi {
engine, engine,
module, module,
@ -77,20 +85,48 @@ impl Wasi {
}) })
} }
pub fn attach_file<T: AsRef<Path>>(&mut self, path: T) -> Result<(), Error> { /// Attaches a file or directory the the given system path to the runtime.
/// Note that the resource must be freed by calling `remove_resource` afterwards.
pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<WasiResource, Error> {
// grab the WASI context
let ctx = self.store.data_mut(); let ctx = self.store.data_mut();
// open the file we want, and convert it into the right type
// this is a footgun and a half
let file = File::open(&path).unwrap(); let file = File::open(&path).unwrap();
let dir = Dir::from_std_file(file); let dir = Dir::from_std_file(file);
// this is a footgun and a half. let dir = Box::new(wasmtime_wasi::dir::Dir::from_cap_std(dir));
let dir = dir::Dir::from_cap_std(dir);
ctx.push_preopened_dir(Box::new(dir), path)?; // grab an empty file descriptor, specify capabilities
let fd = ctx.table().push(Box::new(()))?;
dbg!(fd);
let caps = dir::DirCaps::all();
let file_caps = file::FileCaps::all();
// insert the directory at the given fd,
// return a handle to the resource
ctx.insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
Ok(WasiResource(fd))
}
/// Returns `true` if the resource existed and was removed.
pub fn remove_resource(&mut self, resource: WasiResource) -> Result<(), Error> {
self.store
.data_mut()
.table()
.delete(resource.0)
.ok_or_else(|| anyhow!("Resource did not exist, but a valid handle was passed in"))?;
Ok(()) Ok(())
} }
// pub fn remove_file<T: AsRef<Path>>(&mut self, path: T) -> Result<(), Error> { // pub fn with_resource<T>(
// let ctx = self.store.data_mut(); // &mut self,
// ctx.remove // resource: WasiResource,
// Ok(()) // callback: fn(&mut Self) -> Result<T, Error>,
// ) -> Result<T, Error> {
// let result = callback(self);
// self.remove_resource(resource)?;
// return result;
// } // }
// So this call function is kinda a dance, I figured it'd be a good idea to document it. // So this call function is kinda a dance, I figured it'd be a good idea to document it.
@ -142,6 +178,9 @@ impl Wasi {
handle: &str, handle: &str,
arg: A, arg: A,
) -> Result<R, Error> { ) -> Result<R, Error> {
dbg!(&handle);
// dbg!(serde_json::to_string(&arg)).unwrap();
// serialize the argument using bincode // serialize the argument using bincode
let arg = bincode::serialize(&arg)?; let arg = bincode::serialize(&arg)?;
let arg_buffer_len = arg.len(); let arg_buffer_len = arg.len();

View File

@ -2,6 +2,7 @@ use super::installation::{npm_install_packages, npm_package_latest_version};
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use client::http::HttpClient; use client::http::HttpClient;
use futures::{future::BoxFuture, FutureExt, StreamExt}; use futures::{future::BoxFuture, FutureExt, StreamExt};
use isahc::http::version;
use language::{LanguageServerName, LspAdapter}; use language::{LanguageServerName, LspAdapter};
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use plugin_runtime::{Wasi, WasiPlugin}; use plugin_runtime::{Wasi, WasiPlugin};
@ -42,7 +43,7 @@ impl LspAdapter for LanguagePluginLspAdapter {
} }
fn server_args<'a>(&'a self) -> Vec<String> { fn server_args<'a>(&'a self) -> Vec<String> {
self.runtime.lock().call("args", ()).unwrap() self.runtime.lock().call("server_args", ()).unwrap()
} }
fn fetch_latest_server_version( fn fetch_latest_server_version(
@ -65,27 +66,38 @@ impl LspAdapter for LanguagePluginLspAdapter {
fn fetch_server_binary( fn fetch_server_binary(
&self, &self,
versions: Box<dyn 'static + Send + Any>, version: Box<dyn 'static + Send + Any>,
_: Arc<dyn HttpClient>, _: Arc<dyn HttpClient>,
container_dir: PathBuf, container_dir: PathBuf,
) -> BoxFuture<'static, Result<PathBuf>> { ) -> BoxFuture<'static, Result<PathBuf>> {
// TODO: async runtime let version = version.downcast::<String>().unwrap();
let mut runtime = self.runtime.lock(); let mut runtime = self.runtime.lock();
let result = runtime.attach_file(&container_dir);
let result = match result { let result: Result<PathBuf, _> = (|| {
Ok(_) => runtime.call("fetch_server_binary", container_dir), let handle = runtime.attach_path(&container_dir)?;
Err(e) => Err(e), let result = runtime
}; .call::<_, Option<PathBuf>>("fetch_server_binary", container_dir)?
.ok_or_else(|| anyhow!("Could not load cached server binary"));
runtime.remove_resource(handle)?;
result
})();
async move { result }.boxed() async move { result }.boxed()
} }
fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> { fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
let result = self let mut runtime = self.runtime.lock();
.runtime
.lock() let result: Option<PathBuf> = (|| {
.call("cached_server_binary", container_dir); let handle = runtime.attach_path(&container_dir).ok()?;
async move { result }.log_err().boxed() let result = runtime
.call::<_, Option<PathBuf>>("cached_server_binary", container_dir)
.ok()?;
runtime.remove_resource(handle).ok()?;
result
})();
async move { result }.boxed()
} }
fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
@ -113,11 +125,13 @@ impl LspAdapter for LanguagePluginLspAdapter {
} }
fn initialization_options(&self) -> Option<serde_json::Value> { fn initialization_options(&self) -> Option<serde_json::Value> {
let result = self // self.runtime
.runtime // .lock()
.lock() // .call::<_, Option<serde_json::Value>>("initialization_options", ())
.call("initialization_options", ()) // .unwrap()
.unwrap();
Some(result) Some(json!({
"provideFormatter": true
}))
} }
} }

View File

@ -3,11 +3,26 @@ use serde_json::json;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
// import // #[import]
// fn command(string: String) -> Option<String>; // fn command(string: String) -> Option<String>;
// TODO: some sort of macro to generate ABI bindings
extern "C" {
pub fn hello(item: u32) -> u32;
pub fn bye(item: u32) -> u32;
}
// #[bind]
// pub async fn name(u32) -> u32 {
// }
#[bind] #[bind]
pub fn name() -> &'static str { pub fn name() -> &'static str {
let number = unsafe { hello(27) };
println!("got: {}", number);
let number = unsafe { bye(28) };
println!("got: {}", number);
"vscode-json-languageserver" "vscode-json-languageserver"
} }
@ -34,65 +49,68 @@ pub fn fetch_latest_server_version() -> Option<String> {
Some("1.3.4".into()) Some("1.3.4".into())
} }
// #[bind] #[bind]
// pub fn fetch_server_binary(version: String) -> Option<PathBuf> { pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Option<PathBuf> {
// let version_dir = container_dir.join(version.as_str()); println!("Fetching server binary");
// fs::create_dir_all(&version_dir) return None;
// .await // let version_dir = container_dir.join(version.as_str());
// .context("failed to create version directory")?; // fs::create_dir_all(&version_dir)
// let binary_path = version_dir.join(Self::BIN_PATH); // .await
// .context("failed to create version directory")?;
// let binary_path = version_dir.join(Self::BIN_PATH);
// if fs::metadata(&binary_path).await.is_err() { // if fs::metadata(&binary_path).await.is_err() {
// let output = smol::process::Command::new("npm") // let output = smol::process::Command::new("npm")
// .current_dir(&version_dir) // .current_dir(&version_dir)
// .arg("install") // .arg("install")
// .arg(format!("vscode-json-languageserver@{}", version)) // .arg(format!("vscode-json-languageserver@{}", version))
// .output() // .output()
// .await // .await
// .context("failed to run npm install")?; // .context("failed to run npm install")?;
// if !output.status.success() { // if !output.status.success() {
// Err(anyhow!("failed to install vscode-json-languageserver"))?; // Err(anyhow!("failed to install vscode-json-languageserver"))?;
// } // }
// if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() { // if let Some(mut entries) = fs::read_dir(&container_dir).await.log_err() {
// while let Some(entry) = entries.next().await { // while let Some(entry) = entries.next().await {
// if let Some(entry) = entry.log_err() { // if let Some(entry) = entry.log_err() {
// let entry_path = entry.path(); // let entry_path = entry.path();
// if entry_path.as_path() != version_dir { // if entry_path.as_path() != version_dir {
// fs::remove_dir_all(&entry_path).await.log_err(); // fs::remove_dir_all(&entry_path).await.log_err();
// } // }
// } // }
// } // }
// } // }
// } // }
// Ok(binary_path) // Ok(binary_path)
// } }
const BIN_PATH: &'static str = const BIN_PATH: &'static str =
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver"; "node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
// #[bind] #[bind]
// pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> { pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
// println!("Finding cached server binary..."); println!("Finding cached server binary...");
// let mut last_version_dir = None; let mut last_version_dir = None;
// let mut entries = fs::read_dir(&container_dir).ok()?; println!("{}", container_dir.exists());
// println!("Read Entries..."); let mut entries = fs::read_dir(&container_dir).ok()?;
// while let Some(entry) = entries.next() { println!("Read Entries...");
// let entry = entry.ok()?; while let Some(entry) = entries.next() {
// if entry.file_type().ok()?.is_dir() { let entry = entry.ok()?;
// last_version_dir = Some(entry.path()); if entry.file_type().ok()?.is_dir() {
// } last_version_dir = Some(entry.path());
// } }
// let last_version_dir = last_version_dir?; }
// let bin_path = last_version_dir.join(BIN_PATH); let last_version_dir = last_version_dir?;
// if bin_path.exists() { let bin_path = last_version_dir.join(BIN_PATH);
// println!("{}", bin_path.display()); if bin_path.exists() {
// Some(bin_path) println!("this is the path: {}", bin_path.display());
// } else { Some(bin_path)
// None } else {
// } None
// } }
}
#[bind] #[bind]
pub fn initialization_options() -> Option<serde_json::Value> { pub fn initialization_options() -> Option<serde_json::Value> {

View File

@ -0,0 +1,88 @@
use zed_plugin::RopeRef;
// Host
struct Handles {
items: Vec<Box<dyn Any>>,
}
struct Rope;
impl Link for Rope {
fn link(linker: &mut Linker) -> Result<()> {
linker.add(|this: &mut Rope| {
});
linker.func_wrap("env", "len", |handles, arg| {
let rope = handles.downcast::<Rope>(arg.0);
let rope = Arc::from_raw(ptr);
let result = rope.len();
Arc::leak(rope);
result
});
}
fn to_handle(self) -> Handle<Rope> {
todo!()
}
}
// -- Host
pub fn edit_rope(&mut self) {
let rope: &mut Rope = self......rope();
let handle: RopeRef = self.runtime.to_handle(rope);
self.runtime.call("edit_rope", handle);
}
// Guest
extern "C" long rope__len(u32 handle);
struct RopeRef(u32);
impl RopeRef {
fn len(&self) -> usize {
rope__len(self.0);
}
}
pub fn edit_rope(rope: RopeRef) {
rope.len()
}
// Host side ---
pub struct Rope { .. }
RopeRef(u32);
impl Link for RopeRef {
pub fn init(&mut something) {
something.add("length", |rope| )
}
}
// ---
extern "C" {
pub fn length(item: u32) -> u32;
}
struct RopeRef {
handle: u32,
}
pub fn length(ref: RopeRef) -> u32 {
ref.length()
}
// Host side
#[plugin_interface]
trait LspAdapter {
name() -> &'static str;
}
// Guest side