use crate::ported::exec::dispatch_function_call;
use crate::ported::params::setsparam;
pub fn _call_function(args: &[String]) -> i32 {
let _fn_scope = crate::compsys::ported::shared::FnScope::enter("_call_function");
let result_name = args.first().cloned().unwrap_or_default();
let store_into: Option<String> = if result_name.is_empty() || result_name == "-" {
None
} else {
Some(result_name)
};
let rest: &[String] = if args.is_empty() { &[] } else { &args[1..] };
let fn_name = match rest.first() {
Some(n) => n.clone(),
None => return 1,
};
if !crate::compsys::ported::shared::plus_functions(&fn_name) {
return 1;
}
let call_args: &[String] = if rest.len() > 1 { &rest[1..] } else { &[] };
let ret = dispatch_function_call(&fn_name, call_args).unwrap_or(1);
if let Some(name) = store_into {
let _ = setsparam(&name, &ret.to_string());
}
let _ = crate::ported::params::setsparam("compstate[restore]", "");
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn missing_fn_returns_one() {
let _g = crate::test_util::global_state_lock();
assert_eq!(
_call_function(&["ret".to_string(), "nonexistent_fn".to_string()]),
1
);
}
#[test]
fn empty_args_returns_one() {
let _g = crate::test_util::global_state_lock();
assert_eq!(_call_function(&[]), 1);
}
}