use alloc::string::String;
use alloc::vec::Vec;
use crate::runtime::{
BOUND, BOUND_ARGS, 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 };
if rt
.dialect()
.carries_the_member_of_the_extension("func", method)
{
return the_extension_answer(method, this, rt, line);
}
match method {
"call" => call_the_function(this, arguments, rt, line, Route::ThroughTheCall),
"isoptional" => Some(the_flag_of_the_parameter(this, arguments, true, line)),
"isbyref" => Some(the_flag_of_the_parameter(this, arguments, false, line)),
"bind" => {
if arguments.len() > 1 {
return Some(Ok(rt.bound_function_with_args(arguments.to_vec(), this.clone())));
}
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);
let bound_args = object.borrow().value_of(BOUND_ARGS);
if let (Some(bound), Some(target)) = (bound, target) {
if let Some(Value::List(args)) = bound_args {
return Some(rt.call_the_bound_args(&args, &target, arguments, line));
}
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));
}
if !matches!(route, Route::ThroughTheCall) {
if let Some(result) = rt.the_rewritten_call_of(this, arguments, line) {
return Some(result);
}
}
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
}
fn the_flag_of_the_parameter(
this: &Value,
arguments: &[Value],
optional: bool,
line: u32,
) -> Result<Value, RuntimeFailure> {
let Value::Object(object) = this else {
return Ok(Value::Int(0));
};
let flags = object.borrow().value_of(PARAMETERS);
let place = arguments
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let Ok(place) = place.trim().parse::<usize>() else {
return Ok(Value::Int(0));
};
let Some(Value::List(values)) = flags else {
return Ok(Value::Int(0));
};
let Some(Value::Int(bits)) = values.get(place.saturating_sub(1)) else {
let name = if optional { "IsOptional" } else { "IsByRef" };
return Err(RuntimeFailure::Error(RuntimeError {
message: format!("Parameter #1 of Func.Prototype.{name} is invalid."),
line,
}));
};
let answer = if optional {
(bits & 2) != 0
} else {
(bits & 1) != 0
};
Ok(Value::Int(answer as i64))
}
fn the_extension_answer(
method: &str,
this: &Value,
rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
let Value::Object(object) = this else {
return None;
};
match method {
"isclosure" => Some(Ok(Value::Int(
object.borrow().value_of(crate::runtime::CLOSURE).is_some() as i64,
))),
"ismethod" => Some(Ok(Value::Int(
object
.borrow()
.value_of(crate::runtime::METHOD_CLASS)
.is_some() as i64,
))),
"params" => {
let names: Vec<Value> = rt
.the_names_of_the_parameters_of_a_function(this)
.into_iter()
.map(Value::Text)
.collect();
Some(crate::classes::array::construct(&names, rt, line))
}
_ => None,
}
}
use crate::runtime::{PARAMETERS, RuntimeError};
use alloc::format;