use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::runtime::{Runtime, RuntimeFailure, Value};
const THE_FUNCTION: &str = "\u{1}timer function";
const THE_PERIOD: &str = "\u{1}timer period";
const THE_LAST: &str = "\u{1}timer last";
pub(crate) fn names() -> Vec<&'static str> {
vec!["function", "period", "last"]
}
pub(crate) fn the_timer(function: &str, period: u64, last: u64) -> Value {
let Value::Object(object) = crate::classes::object("timer") else {
return Value::Text(String::new());
};
object
.borrow_mut()
.put(THE_FUNCTION, Value::Text(function.to_string()));
object.borrow_mut().put(THE_PERIOD, Value::Int(period as i64));
object.borrow_mut().put(THE_LAST, Value::Int(last as i64));
Value::Object(object)
}
pub(crate) fn property(
_class: &str,
name: &str,
this: &Value,
_rt: &mut Runtime,
_line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if !_class.eq_ignore_ascii_case("timer") {
return None;
}
let Value::Object(object) = this else {
return None;
};
let answer = match name.to_lowercase().as_str() {
"function" => object.borrow().value_of(THE_FUNCTION).unwrap_or(Value::Unset),
"period" => object.borrow().value_of(THE_PERIOD).unwrap_or(Value::Unset),
"last" => object.borrow().value_of(THE_LAST).unwrap_or(Value::Unset),
_ => return None,
};
Some(Ok(answer))
}