1
1
use mlua:: prelude:: * ;
2
- use std:: env;
2
+ use std:: { env, path :: { Path , PathBuf } } ;
3
3
4
4
#[ tokio:: main( flavor = "current_thread" ) ]
5
5
async fn main ( ) -> LuaResult < ( ) > {
@@ -8,23 +8,33 @@ async fn main() -> LuaResult<()> {
8
8
let lua = unsafe { Lua :: unsafe_new ( ) } ;
9
9
luals_basic:: lua_preload ( & lua) ?;
10
10
11
- build_args ( & lua) ;
12
- let current_path = std:: env:: current_dir ( ) ?;
13
- let main_path = current_path. join ( "main.lua" ) ;
14
- let main = lua. load ( main_path) ;
11
+ let start_file_name = build_args ( & lua) ;
12
+ let main = lua. load ( start_file_name) ;
15
13
main. call_async ( ( ) ) . await ?;
16
14
Ok ( ( ) )
17
15
}
18
16
19
- fn build_args ( lua : & Lua ) {
17
+ fn build_args ( lua : & Lua ) -> PathBuf {
20
18
let args = std:: env:: args ( ) . skip ( 1 ) . collect :: < Vec < _ > > ( ) ;
19
+ if args. len ( ) > 0 && args[ 0 ] == "-e" {
20
+ let code = args[ 1 ] . clone ( ) ;
21
+ let chunk = lua. load ( code) ;
22
+ chunk. call :: < ( ) > ( mlua:: MultiValue :: new ( ) ) . unwrap ( ) ;
23
+
24
+ let start_file_name = args[ 2 ] . clone ( ) ;
25
+ return start_file_name. into ( ) ;
26
+ }
27
+
21
28
let table = lua. create_table ( ) . unwrap ( ) ;
22
29
for ( i, arg) in args. iter ( ) . enumerate ( ) {
23
30
table. set ( i + 1 , arg. clone ( ) ) . unwrap ( ) ;
24
31
}
25
32
let exe_path = env:: current_exe ( ) . unwrap ( ) ;
26
33
table. set ( -1 , exe_path. to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
27
34
lua. globals ( ) . set ( "arg" , table) . unwrap ( ) ;
35
+ let current_path = std:: env:: current_dir ( ) . unwrap ( ) ;
36
+ let main_path = current_path. join ( "main.lua" ) ;
37
+ main_path
28
38
}
29
39
30
40
fn dynamic_set_root ( ) {
0 commit comments