use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::runtime::{Runtime, RuntimeError, RuntimeFailure, Value};
const THE_PID: &str = "\u{1}scriptprocess pid";
pub(crate) fn names() -> Vec<&'static str> {
vec![
"hasexited",
"exitcode",
"exittime",
"kill",
"stdout",
"stderr",
"stdin",
]
}
pub(crate) fn construct(
_class: &str,
arguments: &[Value],
_rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let Some(first) = arguments.first() else {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: "Too few parameters passed to function.".to_string(),
line,
})));
};
let pid = match first {
Value::Int(number) => *number as u32,
other => {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: crate::runtime::requires(
1,
"ScriptProcess",
crate::runtime::TypeName::Number,
other,
),
line,
})))
}
};
let Value::Object(object) = crate::classes::object("scriptprocess") else {
return None;
};
object.borrow_mut().put(THE_PID, Value::Int(pid as i64));
Some(Ok(Value::Object(object)))
}
fn the_pid(this: &Value, line: u32) -> Result<u32, RuntimeFailure> {
let Value::Object(object) = this else {
return Err(RuntimeFailure::Error(RuntimeError {
message: "the value is not an object.".to_string(),
line,
}));
};
let pid = match object.borrow().value_of(THE_PID) {
Some(Value::Int(pid)) => Some(pid as u32),
_ => None,
};
match pid {
Some(pid) => Ok(pid),
None => {
let object = object.borrow();
Err(RuntimeFailure::Error(RuntimeError {
message: format!(
"the object of the class {} carries {} properties and no pid",
object.class,
object.properties.len()
),
line,
}))
}
}
}
pub(crate) fn property(
_class: &str,
name: &str,
this: &Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let lowered = name.to_lowercase();
match lowered.as_str() {
"hasexited" | "exitcode" | "exittime" => {}
_ => return None,
}
let pid = match the_pid(this, line) {
Ok(pid) => pid,
Err(failure) => return Some(Err(failure)),
};
let state = match rt.desktop().the_state_of_a_process(pid) {
Ok(state) => state,
Err(reason) => {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: reason,
line,
})))
}
};
let Some(state) = state else {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: "the desktop of this run does not know the state of that process.".to_string(),
line,
})));
};
let answer = match lowered.as_str() {
"hasexited" => Value::Int(if state.has_exited { 1 } else { 0 }),
"exitcode" => match state.exit_code {
Some(code) => Value::Int(code as i64),
None => Value::Unset,
},
"exittime" => match state.exit_time {
Some(moment) => Value::Text(moment),
None => Value::Unset,
},
_ => Value::Unset,
};
Some(Ok(answer))
}
pub(crate) fn call(
_class: &str,
method: &str,
this: &Value,
_arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if !method.eq_ignore_ascii_case("kill") {
return None;
}
let pid = match the_pid(this, line) {
Ok(pid) => pid,
Err(failure) => return Some(Err(failure)),
};
if let Err(reason) = rt.desktop().kill_a_process(pid) {
return Some(Err(RuntimeFailure::Error(RuntimeError { message: reason, line })));
}
Some(Ok(Value::Text(String::new())))
}