Skip to content

Commit

Permalink
impl bee.subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Sep 13, 2024
1 parent fb89663 commit 2f394ce
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 26 deletions.
20 changes: 0 additions & 20 deletions resources/testmain.lua

This file was deleted.

85 changes: 80 additions & 5 deletions src/bee/lua_subprocess.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,93 @@
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<Child>,
}

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<M: LuaUserDataMethods<Self>>(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<LuaSubprocess> {
let mut exe: String = String::new();
let mut args_string: Vec<String> = vec![];
for pair in args.pairs::<i32, String>() {
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<u64> {
Ok(process::id() as u64)
Ok(process_id() as u64)
}

pub fn bee_subprocess(lua: &Lua) -> LuaResult<LuaTable> {
let table = lua.create_table()?;
table.set("spawn", lua.create_function(bee_subprocess_spawn)?)?;
table.set("get_id", lua.create_function(bee_subprocess_get_id)?)?;
Ok(table)
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down

0 comments on commit 2f394ce

Please sign in to comment.