Skip to content

Commit 2f394ce

Browse files
committed
impl bee.subprocess
1 parent fb89663 commit 2f394ce

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

resources/testmain.lua

-20
This file was deleted.

src/bee/lua_subprocess.rs

+80-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,93 @@
11
use mlua::prelude::LuaResult;
22
use mlua::prelude::*;
3-
use std::process;
3+
use std::borrow::BorrowMut;
4+
use std::io::{self};
5+
use std::process::{id as process_id, Child, Command, Stdio};
46

5-
fn bee_subprocess_spawn(_: &Lua, _: ()) -> LuaResult<()> {
6-
Ok(())
7+
struct LuaSubprocess {
8+
child: Option<Child>,
9+
}
10+
11+
impl LuaSubprocess {
12+
fn new() -> Self {
13+
LuaSubprocess { child: None }
14+
}
15+
16+
fn start(&mut self, command: &str, args: &[String]) -> io::Result<()> {
17+
let child = Command::new(command)
18+
.arg(args.join(" "))
19+
.stdin(Stdio::piped())
20+
.stdout(Stdio::inherit())
21+
.stderr(Stdio::inherit())
22+
.spawn()?;
23+
24+
self.child = Some(child);
25+
Ok(())
26+
}
27+
28+
fn wait(&mut self) {
29+
if let Some(child) = self.child.borrow_mut() {
30+
child.wait().unwrap();
31+
}
32+
}
33+
fn get_id(&self) -> u64 {
34+
if let Some(child) = &self.child {
35+
child.id() as u64
36+
} else {
37+
0
38+
}
39+
}
40+
41+
fn is_running(&self) -> bool {
42+
self.child.is_some()
43+
}
44+
}
45+
46+
impl LuaUserData for LuaSubprocess {
47+
fn add_methods<M: LuaUserDataMethods<Self>>(methods: &mut M) {
48+
methods.add_method_mut("wait", |_, this, _: ()| {
49+
this.wait();
50+
Ok(())
51+
});
52+
53+
methods.add_method("get_id", |_, this, _: ()| {
54+
Ok(this.get_id())
55+
});
56+
57+
methods.add_method("is_running", |_, this, _: ()| {
58+
Ok(this.is_running())
59+
});
60+
}
61+
}
62+
63+
64+
fn bee_subprocess_spawn(_: &Lua, args: mlua::Table) -> LuaResult<LuaSubprocess> {
65+
let mut exe: String = String::new();
66+
let mut args_string: Vec<String> = vec![];
67+
for pair in args.pairs::<i32, String>() {
68+
if let Ok((i, arg)) = pair {
69+
if i == 1 {
70+
exe = arg;
71+
continue;
72+
}
73+
74+
args_string.push(arg);
75+
}
76+
}
77+
78+
let mut subprocess = LuaSubprocess::new();
79+
subprocess.start(&exe, &args_string).unwrap();
80+
81+
Ok(subprocess)
782
}
883

984
fn bee_subprocess_get_id(_: &Lua, _: ()) -> LuaResult<u64> {
10-
Ok(process::id() as u64)
85+
Ok(process_id() as u64)
1186
}
1287

1388
pub fn bee_subprocess(lua: &Lua) -> LuaResult<LuaTable> {
1489
let table = lua.create_table()?;
1590
table.set("spawn", lua.create_function(bee_subprocess_spawn)?)?;
1691
table.set("get_id", lua.create_function(bee_subprocess_get_id)?)?;
1792
Ok(table)
18-
}
93+
}

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> LuaResult<()> {
2323
}
2424

2525
build_args(&lua);
26-
let main = lua.load(path::Path::new("resources/main.lua"));
26+
let main = lua.load(path::Path::new("resources/testmain.lua"));
2727
main.call_async(()).await?;
2828
Ok(())
2929
}

0 commit comments

Comments
 (0)