Change ABI from pair of u32s to single u64

This commit is contained in:
Isaac Clayton 2022-06-09 15:49:55 +02:00
parent 7edcf7c423
commit 5b40734f80
5 changed files with 152 additions and 151 deletions

View file

@ -5,7 +5,7 @@ use std::fs;
use std::path::PathBuf;
// #[import]
// fn my_command(string: &str) -> Option<String>;
// fn command(string: &str) -> Option<String>;
// #[no_mangle]
// // TODO: switch len from usize to u32?
@ -29,39 +29,34 @@ use std::path::PathBuf;
// }
extern "C" {
fn __command(ptr: *const u8, len: usize) -> *mut ::plugin::__Buffer;
fn __command(buffer: u64) -> u64;
}
#[no_mangle]
fn command(string: &str) -> Option<String> {
fn command(string: &str) -> Option<Vec<u8>> {
dbg!("executing command: {}", string);
// serialize data
// setup
let data = string;
let data = ::plugin::bincode::serialize(&data).unwrap();
let buffer = unsafe { ::plugin::__Buffer::from_vec(data) };
let ptr = buffer.ptr;
let len = buffer.len;
// leak data to heap
buffer.leak_to_heap();
// call extern function
let result = unsafe { __command(ptr, len) };
// get result
let new_buffer = unsafe { Box::from_raw(result) }; // convert into box
let new_data = unsafe { new_buffer.to_vec() };
// deserialize data
let new_data: Option<String> = match ::plugin::bincode::deserialize(&new_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;
}
// extern "C" {
// pub fn hello(item: u32) -> u32;
// pub fn bye(item: u32) -> u32;
// }
// #[bind]
// pub async fn name(u32) -> u32 {
@ -99,11 +94,15 @@ pub fn fetch_latest_server_version() -> Option<String> {
}
// TODO: command returns error code
let output = command("npm info vscode-json-languageserver --json")?;
let output =
command("npm info vscode-json-languageserver --json").expect("could not run command");
// if !output.is_ok() {
// return None;
// }
let output = String::from_utf8(output).unwrap();
dbg!(&output);
let mut info: NpmInfo = serde_json::from_str(&output).ok()?;
info.versions.pop()
}
@ -120,6 +119,8 @@ pub fn fetch_server_binary(container_dir: PathBuf, version: String) -> Result<Pa
"npm install vscode-json-languageserver@{}",
version
));
let output = output.map(String::from_utf8);
dbg!(&output);
if output.is_none() {
return Err("failed to install vscode-json-languageserver".to_string());
}
@ -142,20 +143,28 @@ 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);
Some(bin_path)
} else {
println!("no binary found");
None
}
}