use alloc::string::String;
use alloc::vec::Vec;
use crate::runtime::{
BOUND, BOUND_TARGET, CLOSURE, METHOD_CLASS, METHOD_NAME, Route, Runtime, RuntimeFailure, Value,
};
const PROPERTIES: [&str; 5] = ["name", "minparams", "maxparams", "isbuiltin", "isvariadic"];
const METHODS: [&str; 4] = ["call", "isoptional", "isbyref", "bind"];
pub(crate) fn names() -> Vec<&'static str> {
PROPERTIES.iter().chain(METHODS.iter()).copied().collect()
}
pub(crate) fn call(
method: &str,
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let Value::Object(_) = this else { return None };
match method {
"call" => call_the_function(this, arguments, rt, line, Route::ThroughTheCall),
"isoptional" => Some(Ok(Value::Int(0))),
"isbyref" => Some(Ok(Value::Int(0))),
"bind" => {
let object = arguments
.first()
.cloned()
.unwrap_or(Value::Text(String::new()));
Some(Ok(rt.bound_function(object, this.clone())))
}
_ => None,
}
}
pub(crate) fn call_the_value(
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
call_the_function(this, arguments, rt, line, Route::ThroughValue)
}
fn call_the_function(
this: &Value,
arguments: &[Value],
rt: &mut Runtime,
line: u32,
route: Route,
) -> Option<Result<Value, RuntimeFailure>> {
let Value::Object(object) = this else {
return None;
};
let bound = object.borrow().value_of(BOUND);
let target = object.borrow().value_of(BOUND_TARGET);
if let (Some(bound), Some(target)) = (bound, target) {
return Some(rt.call_a_bound_function(&bound, &target, arguments, line));
}
let kept = object
.borrow()
.value_of(CLOSURE)
.map(|value| value.to_text());
let name = match kept {
Some(kept) => kept,
None => object
.borrow()
.value_of("name")
.map(|value| value.to_text())
.unwrap_or_default(),
};
let reference = {
let borrowed = object.borrow();
match (
borrowed.value_of(METHOD_CLASS),
borrowed.value_of(METHOD_NAME),
) {
(Some(class), Some(method)) => Some((class.to_text(), method.to_text())),
_ => None,
}
};
if let Some((class, method)) = reference {
if crate::classes::is_type(&class) {
let failure = |message: &str| match route {
Route::ThroughTheCall => {
crate::classes::the_arity_failure_of_a_method(&class, &method, message, line)
}
_ => crate::classes::the_arity_failure(&class, &method, message, line),
};
if let Some((min, max)) = crate::classes::the_arity_of(&class, &method) {
let count = arguments.len() as i64;
if count < min + 1 || (max >= 0 && count > max + 1) {
let message = if count < min + 1 {
"Too few parameters passed to function."
} else {
"Too many parameters passed to function."
};
return Some(Err(failure(message)));
}
}
let Some((this, rest)) = arguments.split_first() else {
return Some(Err(failure("Too few parameters passed to function.")));
};
return Some(
match crate::classes::call(&class, &method, this, rest, rt, line) {
Some(result) => result,
None => Err(rt.no_method(this, &method, line)),
},
);
}
return Some(rt.call_a_method_reference(&class, &method, arguments, line));
}
Some(rt.call_named(&name, arguments, line, route))
}
pub(crate) fn property(
_name: &str,
_this: &Value,
_rt: &mut Runtime,
_line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
None
}
pub(crate) fn set_property(
_name: &str,
_this: &Value,
_value: Value,
_rt: &mut Runtime,
_line: u32,
) -> Option<Result<(), RuntimeFailure>> {
None
}