Add getpid, exec_bg, spawn_thread, sleep_secs builtins; fix fs_write arity

This commit is contained in:
Will Anderson
2026-04-28 14:00:44 -05:00
parent e56174b756
commit f546ed47df
2 changed files with 64 additions and 1 deletions
+55
View File
@@ -2411,6 +2411,61 @@ fn dispatch_builtin(
BuiltinResult::Handled
}
// ── Process / thread builtins ─────────────────────────────────────────
"getpid" => {
stack.push(Value::Int(std::process::id() as i64));
BuiltinResult::Handled
}
"exec_bg" => {
let cmd_str = match stack.pop().unwrap_or(Value::Nil) {
Value::Str(s) => s,
_ => { stack.push(Value::Int(-1)); return BuiltinResult::Handled; }
};
let mut parts = cmd_str.split_whitespace();
let prog = match parts.next() {
Some(p) => p.to_string(),
None => { stack.push(Value::Int(-1)); return BuiltinResult::Handled; }
};
let args_vec: Vec<String> = parts.map(|s| s.to_string()).collect();
let pid = std::process::Command::new(&prog)
.args(&args_vec)
.spawn()
.map(|child| child.id() as i64)
.unwrap_or(-1);
stack.push(Value::Int(pid));
BuiltinResult::Handled
}
"spawn_thread" => {
let fn_name = match stack.pop().unwrap_or(Value::Nil) {
Value::Str(s) => s,
_ => { stack.push(Value::Nil); return BuiltinResult::Handled; }
};
let instr_arc = SERVE_INSTRUCTIONS.with(|si| si.borrow().clone());
let fn_arc = SERVE_FN_TABLE.with(|sf| sf.borrow().clone());
if let (Some(instr_arc), Some(fn_arc)) = (instr_arc, fn_arc) {
std::thread::spawn(move || {
if let Some(&entry) = fn_arc.get(&fn_name) {
run_sub_interpreter(&instr_arc, &fn_arc, entry);
}
});
}
stack.push(Value::Nil);
BuiltinResult::Handled
}
"sleep_secs" => {
let n = match stack.pop().unwrap_or(Value::Nil) {
Value::Int(n) => n as u64,
_ => 1,
};
std::thread::sleep(std::time::Duration::from_secs(n));
stack.push(Value::Nil);
BuiltinResult::Handled
}
// ── String utility builtins ───────────────────────────────────────────
"str_replace" => {