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(); + }); + } +}