From 12fa705af77caa11eaa663bb3330d3d370604285 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Thu, 12 Sep 2024 17:21:45 +0800 Subject: [PATCH 1/5] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f609dd8..9f92c9d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Project Status +# Lua Language Server Rust Port -This project is currently a work in progress. It is an exploration of using Rust as a host. +This is a Rust port of the Lua Language Server. Not all code is implemented in Rust; only the host program has been rewritten in Rust, while some C code is still used. The main goal of this port is to ensure compatibility with more platforms. # Build Support From fb89663c01f3ac6c0ae22743608e68c501fec253 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Thu, 12 Sep 2024 19:53:16 +0800 Subject: [PATCH 2/5] clean code --- Cargo.toml | 1 + build.rs | 3 ++- src/bee/lua_filewatch.rs | 5 +---- src/bee/socket/lua_select.rs | 5 ----- src/bee/socket/lua_socket.rs | 2 ++ src/bee/socket/lua_socket_pool.rs | 3 +-- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3854f6f..90e0ba1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ path = "src/main.rs" [features] no_format = [] + diff --git a/build.rs b/build.rs index b1e118d..8a019c3 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,13 @@ fn main() { std::env::set_var("CC_LOG", "1"); - build_lua(); + // build_lua(); build_lua_seri(); build_lpeglabel(); cfg!(windows).then(|| build_setfilemode()); cfg!(not(feature = "no_format")).then(|| build_emmyluacodestyle()); } +#[allow(unused)] fn build_lua() { cc::Build::new() .include("3rd/lua") diff --git a/src/bee/lua_filewatch.rs b/src/bee/lua_filewatch.rs index b854ff9..09cef67 100644 --- a/src/bee/lua_filewatch.rs +++ b/src/bee/lua_filewatch.rs @@ -1,12 +1,9 @@ use mlua::prelude::LuaResult; use mlua::prelude::*; use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher}; -// use std::path::PathBuf; use std::sync::mpsc::{channel, Receiver}; - use super::lua_filesystem::LuaFilePath; -// use std::time::Duration; -// use tokio::task; + struct LuaFileWatch { watcher: Option, diff --git a/src/bee/socket/lua_select.rs b/src/bee/socket/lua_select.rs index 8072344..468c503 100644 --- a/src/bee/socket/lua_select.rs +++ b/src/bee/socket/lua_select.rs @@ -1,12 +1,7 @@ use mlua::prelude::LuaResult; use mlua::prelude::*; -use std::borrow::BorrowMut; -// use std::cell::RefCell; use std::collections::HashMap; -// use std::rc::Rc; -// use std::result; use tokio::select; - use super::lua_socket::LuaSocket; use super::lua_socket_pool::SOCKET_POOL; diff --git a/src/bee/socket/lua_socket.rs b/src/bee/socket/lua_socket.rs index ac7f80e..fb3f6f8 100644 --- a/src/bee/socket/lua_socket.rs +++ b/src/bee/socket/lua_socket.rs @@ -8,6 +8,7 @@ use tokio::net::{UnixListener, UnixStream}; use super::lua_socket_pool::SOCKET_POOL; pub enum SocketStream { + #[allow(unused)] None, Tcp(TcpStream), TcpListener(TcpListener), @@ -19,6 +20,7 @@ pub enum SocketStream { pub enum SocketStreamData { Socket(LuaSocket), + #[allow(unused)] None, } diff --git a/src/bee/socket/lua_socket_pool.rs b/src/bee/socket/lua_socket_pool.rs index 1cb5069..8640469 100644 --- a/src/bee/socket/lua_socket_pool.rs +++ b/src/bee/socket/lua_socket_pool.rs @@ -1,8 +1,6 @@ use std::collections::HashMap; use std::io; use std::sync::Arc; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; -use tokio::net::{TcpListener, TcpStream}; #[cfg(unix)] use tokio::net::{UnixListener, UnixStream}; use tokio::sync::Mutex; @@ -32,6 +30,7 @@ impl LuaSocketPool { self.socket_stream_pool.insert(fd, socket_stream); } + #[allow(unused)] pub fn remove_socket_stream(&mut self, fd: i32) { self.socket_stream_pool.remove(&fd); } From 2f394ced01db8859af74f8eb19b0875d5d6211db Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Fri, 13 Sep 2024 10:30:14 +0800 Subject: [PATCH 3/5] impl bee.subprocess --- resources/testmain.lua | 20 --------- src/bee/lua_subprocess.rs | 85 ++++++++++++++++++++++++++++++++++++--- src/main.rs | 2 +- 3 files changed, 81 insertions(+), 26 deletions(-) delete mode 100644 resources/testmain.lua diff --git a/resources/testmain.lua b/resources/testmain.lua deleted file mode 100644 index f964094..0000000 --- a/resources/testmain.lua +++ /dev/null @@ -1,20 +0,0 @@ -local thread = require 'bee.thread' - -local d = thread.channel('taskpad') -thread.thread([[ -local thread = require 'bee.thread' - print("hello world1") -local taskpad = thread.channel('taskpad') - print("hello world2") -local counter = 0 -taskpad:push("hello world", "hello world2", counter) -]]) -while true do - local a, err, c, d = d:pop() - if a then - print(err, c, d) - else - print("sleep 1000") - thread.sleep(1000) - end -end \ No newline at end of file diff --git a/src/bee/lua_subprocess.rs b/src/bee/lua_subprocess.rs index 09c35b5..fe8bc4b 100644 --- a/src/bee/lua_subprocess.rs +++ b/src/bee/lua_subprocess.rs @@ -1,13 +1,88 @@ use mlua::prelude::LuaResult; use mlua::prelude::*; -use std::process; +use std::borrow::BorrowMut; +use std::io::{self}; +use std::process::{id as process_id, Child, Command, Stdio}; -fn bee_subprocess_spawn(_: &Lua, _: ()) -> LuaResult<()> { - Ok(()) +struct LuaSubprocess { + child: Option, +} + +impl LuaSubprocess { + fn new() -> Self { + LuaSubprocess { child: None } + } + + fn start(&mut self, command: &str, args: &[String]) -> io::Result<()> { + let child = Command::new(command) + .arg(args.join(" ")) + .stdin(Stdio::piped()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .spawn()?; + + self.child = Some(child); + Ok(()) + } + + fn wait(&mut self) { + if let Some(child) = self.child.borrow_mut() { + child.wait().unwrap(); + } + } + fn get_id(&self) -> u64 { + if let Some(child) = &self.child { + child.id() as u64 + } else { + 0 + } + } + + fn is_running(&self) -> bool { + self.child.is_some() + } +} + +impl LuaUserData for LuaSubprocess { + fn add_methods>(methods: &mut M) { + methods.add_method_mut("wait", |_, this, _: ()| { + this.wait(); + Ok(()) + }); + + methods.add_method("get_id", |_, this, _: ()| { + Ok(this.get_id()) + }); + + methods.add_method("is_running", |_, this, _: ()| { + Ok(this.is_running()) + }); + } +} + + +fn bee_subprocess_spawn(_: &Lua, args: mlua::Table) -> LuaResult { + let mut exe: String = String::new(); + let mut args_string: Vec = vec![]; + for pair in args.pairs::() { + if let Ok((i, arg)) = pair { + if i == 1 { + exe = arg; + continue; + } + + args_string.push(arg); + } + } + + let mut subprocess = LuaSubprocess::new(); + subprocess.start(&exe, &args_string).unwrap(); + + Ok(subprocess) } fn bee_subprocess_get_id(_: &Lua, _: ()) -> LuaResult { - Ok(process::id() as u64) + Ok(process_id() as u64) } pub fn bee_subprocess(lua: &Lua) -> LuaResult { @@ -15,4 +90,4 @@ pub fn bee_subprocess(lua: &Lua) -> LuaResult { table.set("spawn", lua.create_function(bee_subprocess_spawn)?)?; table.set("get_id", lua.create_function(bee_subprocess_get_id)?)?; Ok(table) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 184a52e..95a6951 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ async fn main() -> LuaResult<()> { } build_args(&lua); - let main = lua.load(path::Path::new("resources/main.lua")); + let main = lua.load(path::Path::new("resources/testmain.lua")); main.call_async(()).await?; Ok(()) } From b87374abb5cafb8610e3f3ccc99bdbddd5878ae3 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Fri, 13 Sep 2024 10:32:03 +0800 Subject: [PATCH 4/5] fix main --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 95a6951..184a52e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ async fn main() -> LuaResult<()> { } build_args(&lua); - let main = lua.load(path::Path::new("resources/testmain.lua")); + let main = lua.load(path::Path::new("resources/main.lua")); main.call_async(()).await?; Ok(()) } From e08fb7394c35447917597a6244ded616f80a2cca Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Fri, 13 Sep 2024 11:27:01 +0800 Subject: [PATCH 5/5] fix some api --- .gitignore | 3 ++- src/bee/lua_filesystem.rs | 8 ++++++-- src/bee/lua_platform.rs | 26 -------------------------- src/main.rs | 25 ++++++++++++++++++++++--- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index cfe97d6..ddb456f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /dist -/resources/log \ No newline at end of file +/resources/log +/log diff --git a/src/bee/lua_filesystem.rs b/src/bee/lua_filesystem.rs index 1b87b02..bf6a8ab 100644 --- a/src/bee/lua_filesystem.rs +++ b/src/bee/lua_filesystem.rs @@ -361,7 +361,7 @@ fn symlink_status(_: &Lua, path: LuaFilePath) -> LuaResult { fn pairs(lua: &Lua, path: LuaFilePath) -> LuaResult<(mlua::Function, mlua::Table, mlua::Value)> { let table = lua.create_table()?; - if let Ok(_) = std::fs::exists(&path.path) { + if let Ok(true) = std::fs::exists(&path.path) { for entry in std::fs::read_dir(&path.path)? { let entry = entry?; let path = entry.path(); @@ -388,6 +388,10 @@ fn pairs(lua: &Lua, path: LuaFilePath) -> LuaResult<(mlua::Function, mlua::Table Ok((next, table, mlua::Nil)) } +fn exe_path(_: &Lua, (): ()) -> LuaResult { + Ok(LuaFilePath::new(std::env::current_exe().unwrap().to_str().unwrap().to_string())) +} + pub fn bee_filesystem(lua: &Lua) -> LuaResult { let exports = lua.create_table()?; @@ -429,6 +433,6 @@ pub fn bee_filesystem(lua: &Lua) -> LuaResult
{ )?; exports.set("fullpath", lua.create_function(full_path)?)?; exports.set("symlink_status", lua.create_function(symlink_status)?)?; - + exports.set("exe_path", lua.create_function(exe_path)?)?; Ok(exports) } diff --git a/src/bee/lua_platform.rs b/src/bee/lua_platform.rs index 9a4dfb9..ed51d78 100644 --- a/src/bee/lua_platform.rs +++ b/src/bee/lua_platform.rs @@ -35,32 +35,6 @@ pub fn bee_platform(lua: &Lua) -> LuaResult { }; exports.set("os", os_name)?; - // // 设置编译器信息 - // let (compiler, compiler_version) = if cfg!(target_env = "msvc") { - // ("msvc", format!("MSVC {}", env!("VC_REVISION"))) - // } else if cfg!(target_env = "gnu") { - // ("gcc", format!("GCC {}.{}.{}", env!("CARGO_CFG_GNUC_VERSION_MAJOR"), env!("CARGO_CFG_GNUC_VERSION_MINOR"), env!("CARGO_CFG_GNUC_VERSION_PATCH"))) - // } else if cfg!(target_env = "clang") { - // ("clang", format!("Clang {}.{}.{}", env!("CARGO_CFG_CLANG_VERSION_MAJOR"), env!("CARGO_CFG_CLANG_VERSION_MINOR"), env!("CARGO_CFG_CLANG_VERSION_PATCH"))) - // } else { - // ("unknown", "unknown".to_string()) - // }; - // exports.set("Compiler", compiler)?; - // exports.set("CompilerVersion", compiler_version)?; - - // // 设置 C 运行时库信息 - // let (crt, crt_version) = if cfg!(target_env = "msvc") { - // ("msvc", format!("MSVC STL {}", env!("CARGO_CFG_MSC_VER"))) - // } else if cfg!(target_env = "gnu") { - // ("libstdc++", format!("libstdc++ {}", env!("CARGO_CFG_GLIBCXX_VERSION"))) - // } else if cfg!(target_env = "musl") { - // ("musl", format!("musl {}", env!("CARGO_CFG_MUSL_VERSION"))) - // } else { - // ("unknown", "unknown".to_string()) - // }; - // exports.set("CRT", crt)?; - // exports.set("CRTVersion", crt_version)?; - // 设置架构信息 let arch = if cfg!(target_arch = "x86") { "x86" diff --git a/src/main.rs b/src/main.rs index 184a52e..4aeea86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ mod bee; +mod codestyle; mod lua_preload; mod lua_seri; mod override_lua; -mod codestyle; - #[macro_use] extern crate lazy_static; -use std::{env, path}; use mlua::prelude::*; +use std::{env, path}; #[tokio::main(flavor = "current_thread")] async fn main() -> LuaResult<()> { @@ -38,3 +37,23 @@ fn build_args(lua: &Lua) { table.set(-1, exe_path.to_str().unwrap()).unwrap(); lua.globals().set("arg", table).unwrap(); } + +#[cfg(test)] +mod tests { + use tokio::runtime::Builder; + + use super::*; + #[test] + fn test_main() { + let rt = Builder::new_current_thread().enable_all().build().unwrap(); + rt.block_on(async move { + let lua = unsafe { Lua::unsafe_new() }; + if let Err(e) = lua_preload::lua_preload(&lua) { + eprintln!("Error during lua_preload: {:?}", e); + return; + } + let main = lua.load(path::Path::new("resources/test.lua")); + main.call_async::<()>(()).await.unwrap(); + }); + } +}