use crate::host::with_host;
use fusevm::Value;
use super::common::{arg, str_arg, throws};
fn object_arg_type_error(func: &str, v: &Value) -> String {
let t = with_host(|h| h.type_name_for_error(v));
throws(
"TypeError",
format!("{func}(): Argument #1 ($object) must be of type object, {t} given"),
)
}
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
let v = match name {
"class_exists" => {
let n = str_arg(args, 0);
with_host(|h| Value::bool(h.class_exists(&n)))
}
"interface_exists" | "trait_exists" | "enum_exists" => Value::bool(false),
"method_exists" => {
let a = arg(args, 0);
let method = str_arg(args, 1);
with_host(|h| {
let class = h.object_class(&a).unwrap_or_else(|| h.to_str(&a));
Value::bool(h.class_has_method(&class, &method))
})
}
"property_exists" => {
let a = arg(args, 0);
let prop = str_arg(args, 1);
with_host(|h| {
if h.is_object(&a) {
let class = h.object_class(&a).unwrap_or_default();
let declared = h.class_has_prop(&class, &prop);
let dynamic = h.object_props(&a).iter().any(|(n, _)| *n == prop);
Value::bool(declared || dynamic)
} else {
let class = h.to_str(&a);
Value::bool(h.class_has_prop(&class, &prop))
}
})
}
"get_class" if args.is_empty() => {
let cls = with_host(|h| h.magic_class());
if cls.is_empty() {
return Some(Err(crate::builtins::throws_bare(
"Error",
"get_class() without arguments must be called from within a class",
)));
}
with_host(|h| h.deprecated("Calling get_class() without arguments is deprecated"));
Value::str(cls)
}
"get_class" => {
let a = arg(args, 0);
match with_host(|h| h.object_class(&a)) {
Some(c) => Value::str(c),
None => return Some(Err(object_arg_type_error("get_class", &a))),
}
}
"get_parent_class" => {
let a = arg(args, 0);
with_host(|h| {
let class = if h.is_object(&a) {
h.object_class(&a)
} else if matches!(a, Value::Undef) {
None
} else {
Some(h.to_str(&a))
};
match class.and_then(|c| h.class_parent(&c)) {
Some(p) => Value::str(p),
None => Value::bool(false),
}
})
}
"get_object_vars" => {
let a = arg(args, 0);
if !with_host(|h| h.is_object(&a)) {
return Some(Err(object_arg_type_error("get_object_vars", &a)));
}
with_host(|h| {
let arr = h.new_array();
for (k, val) in h.object_props_visible(&a) {
h.arr_set_key(&arr, &Value::str(k), val);
}
arr
})
}
"get_class_methods" => {
let a = arg(args, 0);
with_host(|h| {
let class = h.object_class(&a).unwrap_or_else(|| h.to_str(&a));
let arr = h.new_array();
for m in h.class_method_names(&class) {
h.arr_push_auto(&arr, Value::str(m));
}
arr
})
}
"is_a" => {
let a = arg(args, 0);
let target = str_arg(args, 1);
let allow_string = args.get(2).map(|v| with_host(|h| h.is_truthy(v)));
with_host(|h| match class_of(h, &a, allow_string.unwrap_or(false)) {
Some(class) => Value::bool(h.is_a_class(&class, &target)),
None => Value::bool(false),
})
}
"is_subclass_of" => {
let a = arg(args, 0);
let target = str_arg(args, 1);
let allow_string = args.get(2).map(|v| with_host(|h| h.is_truthy(v)));
with_host(|h| match class_of(h, &a, allow_string.unwrap_or(true)) {
Some(class) => Value::bool(
!class.eq_ignore_ascii_case(&target) && h.is_a_class(&class, &target),
),
None => Value::bool(false),
})
}
"class_parents" => {
let a = arg(args, 0);
with_host(|h| {
let Some(start) = resolve_named_class(h, &a) else {
return Value::bool(false);
};
let arr = h.new_array();
let mut cur = h.class_parent(&start);
while let Some(p) = cur {
h.arr_set_key(&arr, &Value::str(p.clone()), Value::str(p.clone()));
cur = h.class_parent(&p);
}
arr
})
}
"class_implements" => {
let a = arg(args, 0);
with_host(|h| match resolve_named_class(h, &a) {
Some(_) => h.new_array(),
None => Value::bool(false),
})
}
"class_uses" => {
let a = arg(args, 0);
with_host(|h| match resolve_named_class(h, &a) {
Some(_) => h.new_array(),
None => Value::bool(false),
})
}
"get_class_vars" => {
let name = str_arg(args, 0);
with_host(|h| {
if h.class_exists(&name) {
h.new_array()
} else {
Value::bool(false)
}
})
}
"get_declared_classes" => with_host(|h| {
let names: Vec<Value> = h.all_class_names().into_iter().map(Value::str).collect();
let arr = h.new_array();
for n in names {
h.arr_push_auto(&arr, n);
}
arr
}),
_ => return None,
};
Some(Ok(v))
}
fn class_of(h: &crate::host::PhpHost, a: &Value, allow_string: bool) -> Option<String> {
if let Some(c) = h.object_class(a) {
return Some(c);
}
match a {
Value::Str(_) if allow_string => {
let s = h.to_str(a);
h.class_exists(&s).then_some(s)
}
_ => None,
}
}
fn resolve_named_class(h: &crate::host::PhpHost, a: &Value) -> Option<String> {
if let Some(c) = h.object_class(a) {
return Some(c);
}
match a {
Value::Str(_) => {
let s = h.to_str(a);
h.class_exists(&s).then_some(s)
}
_ => None,
}
}