From 981e7218b5bf1a23898bf959496b82c7f696793b Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 11 Sep 2024 17:49:09 +0200 Subject: [PATCH] fix(lua): actually still need userdata what's up with weird userdata NULL ?? lets actually get serialized but return userdata so we get best of all worlds. in the future we could make overloads with multiple args which bypass serialization --- src/ffi/lua.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/ffi/lua.rs b/src/ffi/lua.rs index 0b62c26..87ffe41 100644 --- a/src/ffi/lua.rs +++ b/src/ffi/lua.rs @@ -173,6 +173,18 @@ macro_rules! a_sync { }; } +macro_rules! from_lua_serde { + ($($t:ty)*) => { + $( + impl FromLua for $t { + fn from_lua(value: LuaValue, lua: &Lua) -> LuaResult<$t> { + lua.from_value(value) + } + } + )* + }; +} + fn spawn_runtime_driver(_: &Lua, ():()) -> LuaResult { let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel(); let handle = std::thread::spawn(move || tokio().block_on(async move { @@ -308,6 +320,25 @@ impl LuaUserData for CodempWorkspace { } } +from_lua_serde! { CodempEvent } +impl LuaUserData for CodempEvent { + fn add_methods>(methods: &mut M) { + methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); + } + + fn add_fields>(fields: &mut F) { + fields.add_field_method_get("type", |_, this| match this { + CodempEvent::FileTreeUpdated(_) => Ok("filetree"), + CodempEvent::UserJoin(_) | CodempEvent::UserLeave(_) => Ok("user"), + }); + fields.add_field_method_get("value", |_, this| match this { + CodempEvent::FileTreeUpdated(x) => Ok(x.clone()), + CodempEvent::UserJoin(x) | CodempEvent::UserLeave(x) => Ok(x.clone()), + }); + } +} + + impl LuaUserData for CodempCursorController { fn add_methods>(methods: &mut M) { methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); @@ -331,6 +362,43 @@ impl LuaUserData for CodempCursorController { } } +from_lua_serde! { CodempCursor } +impl LuaUserData for Cursor { + fn add_methods>(methods: &mut M) { + methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); + } + + fn add_fields>(fields: &mut F) { + fields.add_field_method_get("user", |_, this| Ok(this.user.clone())); + fields.add_field_method_get("buffer", |_, this| Ok(this.buffer.clone())); + fields.add_field_method_get("start", |_, this| Ok(RowCol::from(this.start))); + fields.add_field_method_get("finish", |_, this| Ok(RowCol::from(this.end))); + } +} + +#[derive(Debug, Clone, Copy)] +struct RowCol { + row: i32, + col: i32, +} + +impl From<(i32, i32)> for RowCol { + fn from((row, col): (i32, i32)) -> Self { + Self { row, col } + } +} + +impl LuaUserData for RowCol { + fn add_methods>(methods: &mut M) { + methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); + } + + fn add_fields>(fields: &mut F) { + fields.add_field_method_get("row", |_, this| Ok(this.row)); + fields.add_field_method_get("col", |_, this| Ok(this.col)); + } +} + impl LuaUserData for CodempBufferController { fn add_methods>(methods: &mut M) { methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); @@ -355,6 +423,7 @@ impl LuaUserData for CodempBufferController { } } +from_lua_serde! { CodempTextChange } impl LuaUserData for CodempTextChange { fn add_fields>(fields: &mut F) { fields.add_field_method_get("content", |_, this| Ok(this.content.clone())); @@ -369,7 +438,16 @@ impl LuaUserData for CodempTextChange { } } - +from_lua_serde! { CodempConfig } +impl LuaUserData for CodempConfig { + fn add_fields>(fields: &mut F) { + fields.add_field_method_get("username", |_, this| Ok(this.username.clone())); + fields.add_field_method_get("password", |_, this| Ok(this.password.clone())); + fields.add_field_method_get("host", |_, this| Ok(this.host.clone())); + fields.add_field_method_get("port", |_, this| Ok(this.port)); + fields.add_field_method_get("tls", |_, this| Ok(this.tls)); + } +} #[derive(Debug, Clone)] struct LuaLoggerProducer(mpsc::UnboundedSender);