Switch from std::process::Command to smol::process::Command

This commit is contained in:
Isaac Clayton 2022-06-13 17:21:55 +02:00
parent 42fc278913
commit e5481e2e65
3 changed files with 13 additions and 77 deletions

View File

@ -66,6 +66,7 @@ impl PluginBuilder {
/// Create a new [`PluginBuilder`] with the given WASI context.
/// Using the default context is a safe bet, see [`new_with_default_context`].
pub fn new(wasi_ctx: WasiCtx) -> Result<Self, Error> {
dbg!("new plugin");
let mut config = Config::default();
config.async_support(true);
let engine = Engine::new(&config)?;
@ -231,6 +232,7 @@ impl PluginBuilder {
/// Initializes a [`Plugin`] from a given compiled Wasm module.
/// Both binary (`.wasm`) and text (`.wat`) module formats are supported.
pub async fn init<T: AsRef<[u8]>>(self, module: T) -> Result<Plugin, Error> {
dbg!("initializing plugin");
Plugin::init(module.as_ref().to_vec(), self).await
}
}
@ -296,6 +298,7 @@ impl Plugin {
impl Plugin {
async fn init(module: Vec<u8>, plugin: PluginBuilder) -> Result<Self, Error> {
dbg!("started initialization");
// initialize the WebAssembly System Interface context
let engine = plugin.engine;
let mut linker = plugin.linker;
@ -311,6 +314,7 @@ impl Plugin {
},
);
let module = Module::new(&engine, module)?;
dbg!("created store");
// load the provided module into the asynchronous runtime
linker.module_async(&mut store, "", &module).await?;
@ -325,6 +329,8 @@ impl Plugin {
free_buffer,
});
dbg!("bound funcitons");
Ok(Plugin {
engine,
module,

View File

@ -10,16 +10,20 @@ use util::ResultExt;
pub async fn new_json(executor: Arc<Background>) -> Result<PluginLspAdapter> {
let plugin = PluginBuilder::new_with_default_ctx()?
.host_function("command", |command: String| {
.host_function_async("command", |command: String| async move {
// TODO: actual thing
dbg!(&command);
let mut args = command.split(' ');
let command = args.next().unwrap();
std::process::Command::new(command)
smol::process::Command::new(command)
.args(args)
.output()
.await
.log_err()
.map(|output| dbg!(output.stdout))
.map(|output| {
dbg!("done running command");
output.stdout
})
})?
.init(include_bytes!("../../../../plugins/bin/json_language.wasm"))
.await?;

View File

@ -7,77 +7,11 @@ use std::path::PathBuf;
#[import]
fn command(string: &str) -> Option<Vec<u8>>;
// #[no_mangle]
// // TODO: switch len from usize to u32?
// pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const ::plugin::__Buffer {
// // setup
// let buffer = ::plugin::__Buffer { ptr, len };
// let data = unsafe { buffer.to_vec() };
// // operation
// let data: #ty = match ::plugin::bincode::deserialize(&data) {
// Ok(d) => d,
// Err(e) => panic!("Data passed to function not deserializable."),
// };
// let result = #inner_fn_name(#args);
// let new_data: Result<Vec<u8>, _> = ::plugin::bincode::serialize(&result);
// let new_data = new_data.unwrap();
// // teardown
// let new_buffer = unsafe { ::plugin::__Buffer::from_vec(new_data) };
// return new_buffer.leak_to_heap();
// }
// extern "C" {
// fn __command(buffer: u64) -> u64;
// }
// #[no_mangle]
// fn command(string: &str) -> Option<Vec<u8>> {
// dbg!("executing command: {}", string);
// // setup
// let data = string;
// let data = ::plugin::bincode::serialize(&data).unwrap();
// let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
// // operation
// let new_buffer = unsafe { __command(buffer.into_u64()) };
// let new_data = unsafe { ::plugin::__Buffer::from_u64(new_buffer).to_vec() };
// let new_data: Option<Vec<u8>> = match ::plugin::bincode::deserialize(&new_data) {
// Ok(d) => d,
// Err(e) => panic!("Data returned from function not deserializable."),
// };
// // teardown
// return new_data;
// }
// 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 {
// }
// #[no_mangle]
// pub extern "C" fn very_unique_name_of_course() -> impl std::future::Future<Output = u32> {
// async move {
// std::fs::read_to_string("heck.txt").unwrap().len() as u32
// }
// }
const BIN_PATH: &'static str =
"node_modules/vscode-json-languageserver/bin/vscode-json-languageserver";
#[export]
pub fn name() -> &'static str {
// let number = unsafe { hello(27) };
// let number = unsafe { bye(28) };
// println!("got: {}", number);
"vscode-json-languageserver"
}
@ -101,7 +35,6 @@ pub fn fetch_latest_server_version() -> Option<String> {
// }
let output = String::from_utf8(output).unwrap();
dbg!(&output);
let mut info: NpmInfo = serde_json::from_str(&output).ok()?;
info.versions.pop()
@ -120,7 +53,6 @@ pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<Pa
version
));
let output = output.map(String::from_utf8);
dbg!(&output);
if output.is_none() {
return Err("failed to install vscode-json-languageserver".to_string());
}
@ -143,22 +75,16 @@ pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<Pa
#[export]
pub fn cached_server_binary(container_dir: PathBuf) -> Option<PathBuf> {
let mut last_version_dir = None;
println!("reading directory");
let mut entries = fs::read_dir(&container_dir).ok()?;
while let Some(entry) = entries.next() {
println!("looking at entries");
let entry = entry.ok()?;
println!("some more stuff");
if entry.file_type().ok()?.is_dir() {
println!("this is it!");
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir?;
println!("here we go");
let bin_path = last_version_dir.join(BIN_PATH);
if bin_path.exists() {
dbg!(&bin_path);