diff --git a/Cargo.lock b/Cargo.lock index baaf744..377d862 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -811,6 +811,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "erased-serde" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da96524cc884f6558f1769b6c46686af2fe8e8b4cd253bd5a3cdba8181b8e070" +dependencies = [ + "serde", +] + [[package]] name = "errno" version = "0.3.1" @@ -1646,6 +1655,7 @@ checksum = "07366ed2cd22a3b000aed076e2b68896fb46f06f1f5786c5962da73c0af01577" dependencies = [ "bstr", "cc", + "erased-serde", "futures-core", "futures-task", "futures-util", @@ -1653,6 +1663,7 @@ dependencies = [ "once_cell", "pkg-config", "rustc-hash", + "serde", ] [[package]] @@ -2693,7 +2704,6 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot 0.12.1", "pin-project-lite", "socket2", "tokio-macros", diff --git a/Cargo.toml b/Cargo.toml index 465b983..e3d79a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ tokio = { version = "1.29", features = ["macros", "rt-multi-thread"] } # lua stuff language_macros = { path = "./language_macros" } -mlua = { version = "0.8.9", features = ["lua54", "async", "send"] } +mlua = { version = "0.8.9", features = ["lua54", "async", "send", "serialize"] } once_cell = "1.18.0" # tui feature specific parts diff --git a/language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs b/language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs index 706e463..2557a5f 100644 --- a/language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs +++ b/language_macros/src/generate/lua_wrapper/rust_wrapper_functions/mod.rs @@ -126,12 +126,71 @@ fn get_function_body(field: &Field, has_input: bool, output_type: &Option) let function_return = if let Some(_) = output_type { quote! { - let output: mlua::Value = lua.to_value(output).expect("This conversion should (indirectely) be checked at compile time"); - return Ok(output); + let converted_output = lua + .to_value(&output) + .expect("This conversion should (indirectely) be checked at compile time"); + if let mlua::Value::Table(table) = converted_output { + let real_output: mlua::Value = match output { + CommandTransferValue::Nil => table + .get("Nil") + .expect("This should exist"), + CommandTransferValue::Boolean(_) => table + .get("Boolean") + .expect("This should exist"), + CommandTransferValue::Integer(_) => table + .get("Integer") + .expect("This should exist"), + CommandTransferValue::Number(_) => table + .get("Number") + .expect("This should exist"), + CommandTransferValue::String(_) => table + .get("String") + .expect("This should exist"), + CommandTransferValue::Table(_) => { + todo!() + // FIXME(@Soispha): This returns a table with the values wrapped the + // same way the values above are wrapped. That is (from the greet_multiple + // function): + // ```json + // { + // "Table": { + // "UserName1": { + // "Integer": 2 + // } + // } + // } + // ``` + // whilst the output should be: + // ```json + // { + // "UserName1": 2 + // } + // ``` + // That table would need to be unpacked, but this requires some recursive + // function, which seems not very performance oriented. + // + // My first (quick) attempt: + //let mut output_table = lua.create_table().expect("This should work?"); + //let initial_table: mlua::Value = table + // .get("Table") + // .expect("This should exist"); + //while let mlua::Value::Table(table) = initial_table { + // for pair in table.pairs() { + // let (key, value) = pair.expect("This should also work?"); + // output_table.set(key, value); + // } + //} + }, + }; + return Ok(real_output); + } else { + unreachable!("Lua serializes these things always in a table"); + } + } } else { quote! { - return Ok(()); + return Ok(mlua::Value::Nil); } }; let does_function_expect_output = if output_type.is_some() { @@ -145,7 +204,7 @@ fn get_function_body(field: &Field, has_input: bool, output_type: &Option) } else { quote! { // We didn't receive output and didn't expect output. Everything went well! - return Ok(()); + return Ok(mlua::Value::Nil); } }; diff --git a/src/tui_app/app/command_interface/lua_command_manager/mod.rs b/src/tui_app/app/command_interface/lua_command_manager/mod.rs new file mode 100644 index 0000000..3116f0c --- /dev/null +++ b/src/tui_app/app/command_interface/lua_command_manager/mod.rs @@ -0,0 +1,189 @@ +use std::{collections::HashMap, fmt::Display, thread}; + +use anyhow::{Context, Result}; +use cli_log::{error, info, debug}; +use mlua::{Function, Value}; +use once_cell::sync::OnceCell; +use serde::{Deserialize, Serialize}; +use tokio::{ + runtime::Builder, + sync::{mpsc, Mutex}, + task::{self, LocalSet}, +}; + +use crate::app::{ + command_interface::{add_lua_functions_to_globals, Command}, + events::event_types::Event, +}; + +static LUA: OnceCell> = OnceCell::new(); +pub type Table = HashMap; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub enum CommandTransferValue { + /// `nil` or `null` or `undefined`; anything which goes in that group of types. + Nil, + + /// `true` or `false`. + Boolean(bool), + + // A "light userdata" object, equivalent to a raw pointer. + // /*TODO*/ LightUserData(LightUserData), + + /// An integer number. + Integer(i64), + + /// A floating point number. + Number(f64), + + /// A string + String(String), + + /// A table, directory or HashMap + Table(HashMap), + + // Reference to a Lua function (or closure). + // /* TODO */ Function(Function), + + // Reference to a Lua thread (or coroutine). + // /* TODO */ Thread(Thread<'lua>), + + // Reference to a userdata object that holds a custom type which implements `UserData`. + // Special builtin userdata types will be represented as other `Value` variants. + // /* TODO */ UserData(AnyUserData), + + // `Error` is a special builtin userdata type. When received from Lua it is implicitly cloned. + // /* TODO */ Error(Error), +} + +impl Display for CommandTransferValue { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + CommandTransferValue::Nil => f.write_str("Nil"), + CommandTransferValue::Boolean(bool) => f.write_str(&format!("{}", bool)), + CommandTransferValue::Integer(int) => f.write_str(&format!("{}", int)), + CommandTransferValue::Number(num) => f.write_str(&format!("{}", num)), + CommandTransferValue::String(str) => f.write_str(&format!("{}", str)), + // TODO(@Soispha): The following line should be a real display call, but how do you + // format a HashMap? + CommandTransferValue::Table(table) => f.write_str(&format!("{:#?}", table)), + } + } +} + +pub struct LuaCommandManager { + lua_command_tx: mpsc::Sender, +} + +impl From for CommandTransferValue { + fn from(s: String) -> Self { + Self::String(s.to_owned()) + } +} +impl From for CommandTransferValue { + fn from(s: f64) -> Self { + Self::Number(s.to_owned()) + } +} +impl From for CommandTransferValue { + fn from(s: i64) -> Self { + Self::Integer(s.to_owned()) + } +} +impl From> for CommandTransferValue { + fn from(s: HashMap) -> Self { + Self::Table(s.to_owned()) + } +} +impl From for CommandTransferValue { + fn from(s: bool) -> Self { + Self::Boolean(s.to_owned()) + } +} +impl From<()> for CommandTransferValue { + fn from(_: ()) -> Self { + Self::Nil + } +} + +impl LuaCommandManager { + pub async fn execute_code(&self, code: String) { + self.lua_command_tx + .send(code) + .await + .expect("The receiver should not be dropped at this time"); + } + + pub fn new(event_call_tx: mpsc::Sender) -> Self { + info!("Spawning lua code execution thread..."); + let (lua_command_tx, mut lua_command_rx) = mpsc::channel::(256); + thread::spawn(move || { + let rt = Builder::new_current_thread().enable_all().build().expect( + "Should always be able to build \ + tokio runtime for lua command handling", + ); + let local = LocalSet::new(); + local.spawn_local(async move { + info!( + "Lua command handling initialized, \ + waiting for commands.." + ); + while let Some(command) = lua_command_rx.recv().await { + debug!("Recieved lua code: {}", &command); + let local_event_call_tx = event_call_tx.clone(); + + task::spawn_local(async move { + exec_lua_command(&command, local_event_call_tx) + .await + .expect( + "This should return all relevent errors \ + by other messages, \ + this should never error", + ); + }); + } + }); + rt.block_on(local); + }); + + LuaCommandManager { lua_command_tx } + } +} + +async fn exec_lua_command(command: &str, event_call_tx: mpsc::Sender) -> Result<()> { + let second_event_call_tx = event_call_tx.clone(); + let lua = LUA + .get_or_init(|| { + Mutex::new(add_lua_functions_to_globals( + mlua::Lua::new(), + second_event_call_tx, + )) + }) + .lock() + .await; + + info!("Recieved code to execute: `{}`, executing...", &command); + let output = lua.load(command).eval_async::().await; + match output { + Ok(out) => { + let to_string_fn: Function = lua.globals().get("tostring").expect("This always exists"); + let output: String = to_string_fn.call(out).expect("tostring should not error"); + info!("Function `{}` returned: `{}`", command, &output); + + event_call_tx + .send(Event::CommandEvent(Command::DisplayOutput(output), None)) + .await + .context("Failed to send lua output command")? + } + Err(err) => { + error!("Function `{}` returned error: `{}`", command, err); + event_call_tx + .send(Event::CommandEvent( + Command::RaiseError(err.to_string()), + None, + )) + .await?; + } + }; + Ok(()) +} diff --git a/src/tui_app/app/command_interface/mod.rs b/src/tui_app/app/command_interface/mod.rs index 80d4ca7..25efa4a 100644 --- a/src/tui_app/app/command_interface/mod.rs +++ b/src/tui_app/app/command_interface/mod.rs @@ -1,9 +1,14 @@ // Use `cargo expand app::command_interface` for an overview of the file contents -pub mod lua_command_manger; +pub mod lua_command_manager; use language_macros::ci_command_enum; +// TODO(@Soispha): Should these paths be moved to the proc macro? +// As they are not static, it could be easier for other people, +// if they stay here +use lua_command_manager::CommandTransferValue; +use mlua::LuaSerdeExt; use crate::app::Event; #[ci_command_enum] diff --git a/src/tui_app/app/events/event_types/event/handlers/command.rs b/src/tui_app/app/events/event_types/event/handlers/command.rs index 44d71ed..3757b8f 100644 --- a/src/tui_app/app/events/event_types/event/handlers/command.rs +++ b/src/tui_app/app/events/event_types/event/handlers/command.rs @@ -1,14 +1,24 @@ -use crate::app::{command_interface::Command, events::event_types::EventStatus, App}; -use anyhow::{Context, Result}; -use cli_log::{info, warn, trace}; -use tokio::sync::mpsc; +use std::collections::HashMap; + +use anyhow::{Error, Result}; +use cli_log::{trace, warn}; +use tokio::sync::oneshot; + +use crate::app::{ + command_interface::{ + lua_command_manager::{CommandTransferValue, Table}, + Command, + }, + events::event_types::EventStatus, + App, +}; pub async fn handle( app: &mut App<'_>, command: &Command, - output_callback: &Option>, + output_callback: Option>, ) -> Result { - // A command returns both _status output_ (what you would normally print to stderr) + // A command can both return _status output_ (what you would normally print to stderr) // and _main output_ (the output which is normally printed to stdout). // We simulate these by returning the main output to the lua function, and printing the // status output to a status ui field. @@ -36,17 +46,15 @@ pub async fn handle( ($str:expr) => { if let Some(sender) = output_callback { sender - .send($str.to_owned()) - .await - .context("Failed to send command main output")?; + .send(CommandTransferValue::from($str)) + .map_err(|e| Error::msg(format!("Failed to send command main output: `{}`", e)))?; } }; ($str:expr, $($args:ident),+) => { if let Some(sender) = output_callback { sender - .send(format!($str, $($args),+)) - .await - .context("Failed to send command main output")?; + .send(CommandTransferValue::from(format!($str, $($args),+))) + .map_err(|e| Error::msg(format!("Failed to send command main output: `{}`", e)))?; } }; } diff --git a/src/tui_app/app/events/event_types/event/handlers/lua_command.rs b/src/tui_app/app/events/event_types/event/handlers/lua_command.rs index 6e7460e..9111330 100644 --- a/src/tui_app/app/events/event_types/event/handlers/lua_command.rs +++ b/src/tui_app/app/events/event_types/event/handlers/lua_command.rs @@ -5,10 +5,13 @@ use crate::app::{events::event_types::EventStatus, App}; // This function is here mainly to reserve this spot for further processing of the lua command. // TODO(@Soispha): Move the lua executor thread code from app to this module -pub async fn handle(app: &mut App<'_>, command: String) -> Result { +pub async fn handle( + app: &mut App<'_>, + command: String, +) -> Result { trace!("Recieved ci command: `{command}`; executing.."); - app.lua_command_tx.send(command).await?; + app.lua.execute_code(command).await; Ok(EventStatus::Ok) } diff --git a/src/tui_app/app/events/event_types/event/handlers/matrix.rs b/src/tui_app/app/events/event_types/event/handlers/matrix.rs index 5215a28..2a86034 100644 --- a/src/tui_app/app/events/event_types/event/handlers/matrix.rs +++ b/src/tui_app/app/events/event_types/event/handlers/matrix.rs @@ -1,9 +1,12 @@ -use matrix_sdk::deserialized_responses::SyncResponse; use anyhow::Result; +use matrix_sdk::deserialized_responses::SyncResponse; use crate::app::{events::event_types::EventStatus, App}; -pub async fn handle<'a>(app: &mut App<'a>, sync: &SyncResponse) -> Result { +pub async fn handle( + app: &mut App<'_>, + sync: &SyncResponse, +) -> Result { for (m_room_id, m_room) in sync.rooms.join.iter() { let room = match app.status.get_room_mut(m_room_id) { Some(r) => r, diff --git a/src/tui_app/app/events/event_types/event/handlers/setup.rs b/src/tui_app/app/events/event_types/event/handlers/setup.rs index 8e22128..d879e99 100644 --- a/src/tui_app/app/events/event_types/event/handlers/setup.rs +++ b/src/tui_app/app/events/event_types/event/handlers/setup.rs @@ -1,9 +1,15 @@ use anyhow::{bail, Context, Result}; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent}; -use crate::{app::{events::event_types::EventStatus, App}, ui::setup}; +use crate::{ + app::{events::event_types::EventStatus, App}, + ui::setup, +}; -pub async fn handle(app: &mut App<'_>, input_event: &CrosstermEvent) -> Result { +pub async fn handle( + app: &mut App<'_>, + input_event: &CrosstermEvent, +) -> Result { let ui = match &mut app.ui.setup_ui { Some(ui) => ui, None => bail!("SetupUI instance not found"), diff --git a/src/tui_app/app/events/event_types/event/mod.rs b/src/tui_app/app/events/event_types/event/mod.rs index add2cbf..7accee7 100644 --- a/src/tui_app/app/events/event_types/event/mod.rs +++ b/src/tui_app/app/events/event_types/event/mod.rs @@ -3,9 +3,9 @@ mod handlers; use anyhow::{Context, Result}; use cli_log::trace; use crossterm::event::Event as CrosstermEvent; -use tokio::sync::mpsc::Sender; +use tokio::sync::oneshot; -use crate::app::{command_interface::Command, status::State, App}; +use crate::app::{command_interface::{Command, lua_command_manager::CommandTransferValue}, status::State, App}; use self::handlers::{command, lua_command, main, matrix, setup}; @@ -15,19 +15,19 @@ use super::EventStatus; pub enum Event { InputEvent(CrosstermEvent), MatrixEvent(matrix_sdk::deserialized_responses::SyncResponse), - CommandEvent(Command, Option>), + CommandEvent(Command, Option>), LuaCommand(String), } impl Event { - pub async fn handle(&self, app: &mut App<'_>) -> Result { + pub async fn handle(self, app: &mut App<'_>) -> Result { trace!("Recieved event to handle: `{:#?}`", &self); - match &self { - Event::MatrixEvent(event) => matrix::handle(app, event) + match self { + Event::MatrixEvent(event) => matrix::handle(app, &event) .await .with_context(|| format!("Failed to handle matrix event: `{:#?}`", event)), - Event::CommandEvent(event, callback_tx) => command::handle(app, event, callback_tx) + Event::CommandEvent(event, callback_tx) => command::handle(app, &event, callback_tx) .await .with_context(|| format!("Failed to handle command event: `{:#?}`", event)), Event::LuaCommand(lua_code) => lua_command::handle(app, lua_code.to_owned()) @@ -38,10 +38,10 @@ impl Event { State::None => unreachable!( "This state should not be available, when we are in the input handling" ), - State::Main => main::handle(app, event) + State::Main => main::handle(app, &event) .await .with_context(|| format!("Failed to handle input event: `{:#?}`", event)), - State::Setup => setup::handle(app, event) + State::Setup => setup::handle(app, &event) .await .with_context(|| format!("Failed to handle input event: `{:#?}`", event)), }, diff --git a/src/tui_app/app/mod.rs b/src/tui_app/app/mod.rs index 8b26ee3..3ac2bd3 100644 --- a/src/tui_app/app/mod.rs +++ b/src/tui_app/app/mod.rs @@ -2,30 +2,24 @@ pub mod command_interface; pub mod events; pub mod status; -use std::{path::Path, thread}; +use std::path::Path; use anyhow::{Context, Error, Result}; -use cli_log::{error, info}; +use cli_log::info; use matrix_sdk::Client; -use once_cell::sync::OnceCell; -use status::{State, Status}; -use tokio::{ - runtime::Builder, - sync::{mpsc, Mutex}, - task::{self, LocalSet}, -}; +use tokio::sync::mpsc; use tokio_util::sync::CancellationToken; use crate::{ accounts::{Account, AccountsManager}, app::{ - command_interface::{generate_ci_functions, Command}, events::event_types::Event, + status::{State, Status}, }, ui::{central, setup}, }; -use self::events::event_types; +use self::{command_interface::lua_command_manager::LuaCommandManager, events::event_types}; pub struct App<'ui> { ui: central::UI<'ui>, @@ -38,76 +32,11 @@ pub struct App<'ui> { input_listener_killer: CancellationToken, matrix_listener_killer: CancellationToken, - lua_command_tx: mpsc::Sender, + lua: LuaCommandManager, } impl App<'_> { pub fn new() -> Result { - fn set_up_lua(event_call_tx: mpsc::Sender) -> mpsc::Sender { - async fn exec_lua_command( - command: &str, - event_call_tx: mpsc::Sender, - ) -> Result<()> { - let second_event_call_tx = event_call_tx.clone(); - let lua = LUA - .get_or_init(|| { - Mutex::new(generate_ci_functions( - mlua::Lua::new(), - second_event_call_tx, - )) - }) - .lock() - .await; - info!("Recieved command to execute: `{}`", &command); - let output = lua - .load(command) - // FIXME this assumes string output only - .eval_async::() - .await; - match output { - Ok(out) => { - info!("Function `{}` returned: `{}`", command, out); - } - Err(err) => { - error!("Function `{}` returned error: `{}`", command, err); - event_call_tx - .send(Event::CommandEvent( - Command::RaiseError(err.to_string()), - None, - )) - .await?; - } - }; - Ok(()) - } - - info!("Setting up Lua context.."); - static LUA: OnceCell> = OnceCell::new(); - - let (lua_command_tx, mut rx) = mpsc::channel::(256); - - thread::spawn(move || { - let rt = Builder::new_current_thread().enable_all().build().expect( - "Should always be able to build tokio runtime for lua command handling", - ); - let local = LocalSet::new(); - local.spawn_local(async move { - info!("Lua command handling initialized, waiting for commands.."); - while let Some(command) = rx.recv().await { - info!("Recieved lua command: {}", &command); - let local_event_call_tx = event_call_tx.clone(); - - task::spawn_local(async move { - exec_lua_command(&command, local_event_call_tx).await.expect("This should return all relevent errors by other messages, this should never error"); - }); - } - }); - rt.block_on(local); - }); - - lua_command_tx - } - let path: &std::path::Path = Path::new("userdata/accounts.json"); let config = if path.exists() { info!("Reading account config (userdata/accounts.json)"); @@ -127,7 +56,7 @@ impl App<'_> { input_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(), - lua_command_tx: set_up_lua(tx), + lua: LuaCommandManager::new(tx), }) }