use alloc::string::{String, ToString};
use alloc::vec::Vec;
use alloc::vec;
use crate::runtime::{Runtime, RuntimeFailure, Value};
pub(crate) fn names() -> Vec<&'static str> {
vec![
"id",
"index",
"isactive",
"kind",
"elapsed",
"priority",
"allowtimers",
"critical",
"paused",
"isinterruptible",
"underlying",
"exitcode",
"exit",
]
}
pub(crate) fn construct(
_arguments: &[Value],
rt: &mut Runtime,
_line: u32,
) -> Result<Value, RuntimeFailure> {
Ok(rt.the_current_thread())
}
pub(crate) fn property(
name: &str,
this: &Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let _ = (this, line);
let lowered = name.to_lowercase();
let answer = match lowered.as_str() {
"id" => Value::Int(0),
"index" => Value::Int(1),
"isactive" => Value::Int(1),
"kind" => Value::Text("Auto".to_string()),
"elapsed" => Value::Int(rt.elapsed_ms() as i64),
"priority" => Value::Int(0),
"allowtimers" => Value::Int(1),
"critical" => Value::Int(0),
"paused" => Value::Int(0),
"isinterruptible" => Value::Int(1),
"underlying" => Value::Text(String::new()),
"exitcode" => Value::Unset,
_ => return None,
};
Some(Ok(answer))
}
pub(crate) fn call(
method: &str,
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let _ = (this, line);
let lowered = method.to_lowercase();
match lowered.as_str() {
"exit" => {
let code = arguments
.first()
.map(|value| value.to_text().trim().parse::<i32>())
.transpose()
.ok()
.flatten()
.unwrap_or(0);
rt.exit_with(code);
Some(Ok(Value::Int(0)))
}
"id" | "index" | "isactive" | "kind" | "elapsed" | "priority" | "allowtimers"
| "critical" | "paused" | "isinterruptible" | "underlying" | "exitcode" => {
property(method, this, rt, line)
}
_ => None,
}
}