use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use crate::runtime::{Runtime, RuntimeError, RuntimeFailure, Value};
pub(crate) fn names() -> Vec<&'static str> {
vec!["target", "name", "args"]
}
pub(crate) fn construct(
_class: &str,
arguments: &[Value],
_rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if arguments.len() < 2 {
return Some(Err(RuntimeFailure::Error(RuntimeError {
message: "Too few parameters passed to function.".to_string(),
line,
})));
}
let target = arguments[0].clone();
let name = arguments[1].to_text();
let rest: Vec<Value> = arguments[2..].to_vec();
let Value::Object(object) = crate::classes::object("propref") else {
return None;
};
object.borrow_mut().put(THE_TARGET, target);
object.borrow_mut().put(THE_NAME, Value::Text(name));
let args = match crate::classes::array::construct(&rest, _rt, line) {
Ok(args) => args,
Err(failure) => return Some(Err(failure)),
};
object.borrow_mut().put(THE_ARGS, args);
Some(Ok(Value::Object(object)))
}
pub(crate) fn property(
_class: &str,
name: &str,
this: &Value,
_line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
if !_class.eq_ignore_ascii_case("propref") {
return None;
}
let Value::Object(object) = this else {
return None;
};
let answer = match name.to_lowercase().as_str() {
"target" => object.borrow().value_of(THE_TARGET).unwrap_or(Value::Unset),
"name" => object.borrow().value_of(THE_NAME).unwrap_or(Value::Unset),
"args" => object.borrow().value_of(THE_ARGS).unwrap_or(Value::Unset),
_ => return None,
};
Some(Ok(answer))
}
const THE_TARGET: &str = "\u{1}propref target";
const THE_NAME: &str = "\u{1}propref name";
const THE_ARGS: &str = "\u{1}propref args";