Start switching JSON LSP adapter to plugin (take 2)

This commit is contained in:
Isaac Clayton 2022-06-03 14:42:50 +02:00
parent 35b2eff29c
commit 7dd3114a7a
22 changed files with 372 additions and 100 deletions

View file

@ -1,5 +1,5 @@
[package]
name = "runner"
name = "plugin_runtime"
version = "0.1.0"
edition = "2021"

View file

@ -1,4 +1,3 @@
use std::env;
use std::process::Command;
fn main() {

View file

@ -17,23 +17,23 @@ impl Runtime for Lua {
return Ok(lua);
}
fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T> {
let val: Value = self.globals().get(handle.inner())?;
Ok(self.from_value(val)?)
}
// fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T> {
// let val: Value = self.globals().get(handle.inner())?;
// Ok(self.from_value(val)?)
// }
fn call<A: Serialize, R: DeserializeOwned>(&mut self, handle: &Handle, arg: A) -> Result<R> {
let fun: Function = self.globals().get(handle.inner())?;
fn call<A: Serialize, R: DeserializeOwned>(&mut self, handle: &str, arg: A) -> Result<R> {
let fun: Function = self.globals().get(handle.to_string())?;
let arg: Value = self.to_value(&arg)?;
let result = fun.call(arg)?;
Ok(self.from_value(result)?)
}
fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool {
self.globals()
.contains_key(name.as_ref().to_string())
.unwrap_or(false)
}
// fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool {
// self.globals()
// .contains_key(name.as_ref().to_string())
// .unwrap_or(false)
// }
}
pub struct LuaPlugin {

View file

@ -1,6 +1,6 @@
use mlua::Lua;
use runner::*;
use plugin_runtime::*;
pub fn main() -> anyhow::Result<()> {
let plugin = WasmPlugin {
@ -12,7 +12,10 @@ pub fn main() -> anyhow::Result<()> {
};
let mut sum = Wasm::init(plugin)?;
let strings = "I hope you have a nice day".split(" ").iter().collect();
let strings = "I hope you have a nice day"
.split(" ")
.map(|x| x.to_string())
.collect();
let result = sum.sum_lengths(strings);
dbg!(result);
@ -45,8 +48,7 @@ trait SumLengths {
impl<T: Runtime> SumLengths for T {
fn sum_lengths(&mut self, strings: Vec<String>) -> usize {
let handle = self.handle_for("sum_lengths").unwrap();
let result = self.call(&handle, strings).ok().unwrap();
let result = self.call("sum_lengths", strings).ok().unwrap();
return result;
}
}

View file

@ -2,26 +2,26 @@
use serde::{de::DeserializeOwned, Serialize};
/// Represents a handle to a constant or function in the Runtime.
/// Should be constructed by calling [`Runtime::handle_for`].
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Handle(String);
// /// Represents a handle to a constant or function in the Runtime.
// /// Should be constructed by calling [`Runtime::handle_for`].
// #[derive(Debug, Clone, Hash, PartialEq, Eq)]
// pub struct Handle(String);
impl Handle {
pub fn inner(&self) -> &str {
&self.0
}
}
// impl Handle {
// pub fn inner(&self) -> &str {
// &self.0
// }
// }
/// Represents an interface that can be implemented by a plugin.
pub trait Interface
where
Self: Sized,
{
/// Create an interface from a given runtime.
/// All handles to be used by the interface should be registered and stored in `Self`.
fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self>;
}
// /// Represents an interface that can be implemented by a plugin.
// pub trait Interface
// where
// Self: Sized,
// {
// /// Create an interface from a given runtime.
// /// All handles to be used by the interface should be registered and stored in `Self`.
// fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self>;
// }
pub trait Runtime
where
@ -39,39 +39,39 @@ where
/// Note that if you have any configuration,
fn init(plugin: Self::Plugin) -> Result<Self, Self::Error>;
/// Returns a top-level constant from the module.
/// This can be used to extract configuration information from the module, for example.
/// Before calling this function, get a handle into the runtime using [`handle_for`].
fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T, Self::Error>;
// /// Returns a top-level constant from the module.
// /// This can be used to extract configuration information from the module, for example.
// /// Before calling this function, get a handle into the runtime using [`handle_for`].
// fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T, Self::Error>;
/// Call a function defined in the module.
fn call<A: Serialize, R: DeserializeOwned>(
&mut self,
handle: &Handle,
handle: &str,
arg: A,
) -> Result<R, Self::Error>;
/// Registers a handle with the runtime.
/// This is a mutable item if needed, but generally
/// this should be an immutable operation.
/// Returns whether the handle exists/was successfully registered.
fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool;
// /// Registers a handle with the runtime.
// /// This is a mutable item if needed, but generally
// /// this should be an immutable operation.
// /// Returns whether the handle exists/was successfully registered.
// fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool;
/// Returns the handle for a given name if the handle is defined.
/// Will only return an error if there was an error while trying to register the handle.
/// This function uses [`register_handle`], no need to implement this one.
fn handle_for<T: AsRef<str>>(&mut self, name: T) -> Option<Handle> {
if self.register_handle(&name) {
Some(Handle(name.as_ref().to_string()))
} else {
None
}
}
// /// Returns the handle for a given name if the handle is defined.
// /// Will only return an error if there was an error while trying to register the handle.
// /// This function uses [`register_handle`], no need to implement this one.
// fn handle_for<T: AsRef<str>>(&mut self, name: T) -> Option<Handle> {
// if self.register_handle(&name) {
// Some(Handle(name.as_ref().to_string()))
// } else {
// None
// }
// }
/// Creates the given interface from the current module.
/// Returns [`Error`] if the provided plugin does not match the expected interface.
/// Essentially wraps the [`Interface`] trait.
fn as_interface<T: Interface>(&mut self) -> Option<T> {
Interface::from_runtime(self)
}
// /// Creates the given interface from the current module.
// /// Returns [`Error`] if the provided plugin does not match the expected interface.
// /// Essentially wraps the [`Interface`] trait.
// fn as_interface<T: Interface>(&mut self) -> Option<T> {
// Interface::from_runtime(self)
// }
}

View file

@ -62,14 +62,14 @@ impl<S> Runtime for Wasm<S> {
})
}
fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T, Self::Error> {
let export = self
.instance
.get_export(&mut self.store, handle.inner())
.ok_or_else(|| anyhow!("Could not get export"))?;
// fn constant<T: DeserializeOwned>(&mut self, handle: &Handle) -> Result<T, Self::Error> {
// let export = self
// .instance
// .get_export(&mut self.store, handle.inner())
// .ok_or_else(|| anyhow!("Could not get export"))?;
todo!()
}
// todo!()
// }
// So this call function is kinda a dance, I figured it'd be a good idea to document it.
// the high level is we take a serde type, serialize it to a byte array,
@ -117,7 +117,7 @@ impl<S> Runtime for Wasm<S> {
// TODO: dont' use as for conversions
fn call<A: Serialize, R: DeserializeOwned>(
&mut self,
handle: &Handle,
handle: &str,
arg: A,
) -> Result<R, Self::Error> {
// serialize the argument using bincode
@ -136,7 +136,7 @@ impl<S> Runtime for Wasm<S> {
// get the webassembly function we want to actually call
// TODO: precompute handle
let fun_name = format!("__{}", handle.inner());
let fun_name = format!("__{}", handle);
let fun = self
.instance
.get_typed_func::<(i32, i32), i32, _>(&mut self.store, &fun_name)?;
@ -170,9 +170,9 @@ impl<S> Runtime for Wasm<S> {
return Ok(result);
}
fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool {
self.instance
.get_export(&mut self.store, name.as_ref())
.is_some()
}
// fn register_handle<T: AsRef<str>>(&mut self, name: T) -> bool {
// self.instance
// .get_export(&mut self.store, name.as_ref())
// .is_some()
// }
}