-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.rs
More file actions
78 lines (66 loc) · 2.16 KB
/
main.rs
File metadata and controls
78 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use mlua::prelude::*;
use std::{env, path::{Path, PathBuf}};
#[tokio::main(flavor = "current_thread")]
async fn main() -> LuaResult<()> {
unsafe {
env::set_var("LUALS_BIN", env!("CARGO_PKG_NAME"));
}
dynamic_set_root();
let lua = unsafe { Lua::unsafe_new() };
luals_basic::lua_preload(&lua)?;
let start_file_name = build_args(&lua);
let main = lua.load(start_file_name);
main.call_async(()).await?;
Ok(())
}
fn build_args(lua: &Lua) -> PathBuf {
let args = std::env::args().skip(1).collect::<Vec<_>>();
if args.len() > 0 && args[0] == "-e" {
let code = args[1].clone();
let chunk = lua.load(code);
chunk.call::<()>(mlua::MultiValue::new()).unwrap();
let start_file_name = args[2].clone();
return start_file_name.into();
}
let table = lua.create_table().unwrap();
for (i, arg) in args.iter().enumerate() {
table.set(i + 1, arg.clone()).unwrap();
}
let exe_path = env::current_exe().unwrap();
table.set(-1, exe_path.to_str().unwrap()).unwrap();
lua.globals().set("arg", table).unwrap();
let current_path = std::env::current_dir().unwrap();
let main_path = current_path.join("main.lua");
main_path
}
fn dynamic_set_root() {
let exe_path = env::current_exe().unwrap();
let mut current_dir = exe_path.parent().unwrap();
while !current_dir.join("main.lua").exists() {
if let Some(parent) = current_dir.parent() {
current_dir = parent;
} else {
break;
}
}
std::env::set_current_dir(current_dir).unwrap();
}
#[cfg(test)]
mod tests {
use std::path;
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) = luals_basic::lua_preload(&lua) {
eprintln!("Error during lua_preload: {:?}", e);
return;
}
let main = lua.load(path::Path::new("test.lua"));
main.call_async::<()>(()).await.unwrap();
});
}
}