use fusevm::Value;
pub mod arrays;
pub mod bcmath;
pub mod callable;
pub mod constants;
pub mod ctype;
pub mod datefn;
pub mod datetime;
pub mod encoding;
pub mod fileio;
pub mod fileres;
pub mod filter;
pub mod gmp;
pub mod hash;
pub mod hashext;
pub mod json;
pub mod math;
pub mod mbstring;
pub mod misc;
pub mod preg;
pub mod reflection;
pub mod runtime;
pub mod strings;
pub mod system;
pub mod textx;
pub mod types;
pub mod url;
pub(crate) mod csprng;
pub(crate) mod output;
pub(crate) mod zsort;
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
strings::dispatch(name, args)
.or_else(|| arrays::dispatch(name, args))
.or_else(|| math::dispatch(name, args))
.or_else(|| ctype::dispatch(name, args))
.or_else(|| types::dispatch(name, args))
.or_else(|| preg::dispatch(name, args))
.or_else(|| datetime::dispatch(name, args))
.or_else(|| datefn::dispatch(name, args))
.or_else(|| hash::dispatch(name, args))
.or_else(|| encoding::dispatch(name, args))
.or_else(|| url::dispatch(name, args))
.or_else(|| json::dispatch(name, args))
.or_else(|| fileio::dispatch(name, args))
.or_else(|| reflection::dispatch(name, args))
.or_else(|| callable::dispatch(name, args))
.or_else(|| filter::dispatch(name, args))
.or_else(|| mbstring::dispatch(name, args))
.or_else(|| misc::dispatch(name, args))
.or_else(|| constants::dispatch(name, args))
.or_else(|| system::dispatch(name, args))
.or_else(|| fileres::dispatch(name, args))
.or_else(|| gmp::dispatch(name, args))
.or_else(|| bcmath::dispatch(name, args))
.or_else(|| hashext::dispatch(name, args))
.or_else(|| textx::dispatch(name, args))
.or_else(|| runtime::dispatch(name, args))
}
#[allow(dead_code)]
pub(crate) mod common {
use crate::host::with_host;
use fusevm::Value;
pub use crate::builtins::throws;
pub fn arg(args: &[Value], i: usize) -> Value {
args.get(i).cloned().unwrap_or(Value::Undef)
}
pub fn str_arg(args: &[Value], i: usize) -> String {
with_host(|h| h.to_str(&arg(args, i)))
}
pub fn bytes_arg(args: &[Value], i: usize) -> Vec<u8> {
crate::bytes::to_bytes(&str_arg(args, i)).into_owned()
}
pub fn bytes_value(b: &[u8]) -> Value {
Value::str(crate::bytes::from_bytes(b))
}
pub fn text_value(text: &str) -> Value {
Value::str(crate::bytes::from_text(text).into_owned())
}
pub fn int_arg(args: &[Value], i: usize) -> i64 {
with_host(|h| h.to_number(&arg(args, i)).to_int())
}
pub fn float_arg(args: &[Value], i: usize) -> f64 {
with_host(|h| h.to_number(&arg(args, i)).to_float())
}
pub fn charmask(h: &mut crate::host::PhpHost, list: &[u8], fname: &str) -> [bool; 256] {
let mut mask = [false; 256];
let n = list.len();
let mut i = 0;
while i < n {
let c = list[i];
if i + 3 < n && list[i + 1] == b'.' && list[i + 2] == b'.' && list[i + 3] >= c {
for x in c..=list[i + 3] {
mask[x as usize] = true;
}
i += 4;
} else if i + 1 < n && list[i] == b'.' && list[i + 1] == b'.' {
let why = if i == 0 {
"no character to the left of '..'"
} else if i + 2 >= n {
"no character to the right of '..'"
} else if list[i - 1] > list[i + 2] {
"'..'-range needs to be incrementing"
} else {
""
};
let msg = if why.is_empty() {
format!("{fname}(): Invalid '..'-range")
} else {
format!("{fname}(): Invalid '..'-range, {why}")
};
h.warn(msg);
i += 1;
} else {
mask[c as usize] = true;
i += 1;
}
}
mask
}
pub fn make_list(vals: Vec<Value>) -> Value {
with_host(|h| {
let arr = h.new_array();
for v in vals {
h.arr_push_auto(&arr, v);
}
arr
})
}
pub fn make_map(pairs: Vec<(Value, Value)>) -> Value {
with_host(|h| {
let arr = h.new_array();
for (k, v) in pairs {
h.arr_set_key(&arr, &k, v);
}
arr
})
}
}