use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::CString;
use std::rc::Rc;
use ext_php_rs::args::Arg;
use ext_php_rs::builders::FunctionBuilder;
use ext_php_rs::ffi::{
IS_ARRAY, IS_FALSE, IS_LONG, IS_OBJECT, ext_php_rs_executor_globals, zend_ce_exception,
zend_class_entry, zend_hash_index_find, zend_hash_str_find, zend_throw_exception, zval,
};
use ext_php_rs::flags::DataType;
use ext_php_rs::types::Zval;
use ext_php_rs::zend::ExecuteData;
use speedyspec_hooks::Value;
use crate::boundary;
use crate::convert;
#[derive(Clone, Debug, PartialEq)]
pub enum NativeError {
Throw { class: String, message: String },
Rethrow(Value),
Silent,
}
impl NativeError {
pub fn throw(class: &str, message: impl Into<String>) -> NativeError {
NativeError::Throw {
class: class.to_owned(),
message: message.into(),
}
}
}
pub type Handler = Rc<dyn Fn(&[Value]) -> Result<Value, NativeError>>;
pub type RawHandler = Rc<dyn Fn(&Args<'_>) -> Result<Value, NativeError>>;
#[derive(Clone)]
enum Body {
Converted(Handler),
Raw(RawHandler),
}
thread_local! {
static HANDLERS: RefCell<HashMap<String, Body>> = RefCell::new(HashMap::new());
static BY_FUNCTION: RefCell<HashMap<usize, Body>> = RefCell::new(HashMap::new());
}
fn register(name: &str, body: Body) -> FunctionBuilder<'static> {
HANDLERS.with(|h| h.borrow_mut().insert(name.to_ascii_lowercase(), body));
BY_FUNCTION.with(|h| h.borrow_mut().clear());
FunctionBuilder::new(name, dispatch)
.arg(Arg::new("args", DataType::Mixed).is_variadic())
.returns(DataType::Mixed, false, true)
}
pub fn function(
name: &str,
handler: impl Fn(&[Value]) -> Result<Value, NativeError> + 'static,
) -> FunctionBuilder<'static> {
register(name, Body::Converted(Rc::new(handler)))
}
pub fn raw_function(
name: &str,
handler: impl Fn(&Args<'_>) -> Result<Value, NativeError> + 'static,
) -> FunctionBuilder<'static> {
register(name, Body::Raw(Rc::new(handler)))
}
pub struct Args<'a> {
ex: &'a ExecuteData,
count: usize,
}
impl Args<'_> {
pub fn len(&self) -> usize {
self.count
}
pub fn is_empty(&self) -> bool {
self.count == 0
}
fn slot(&self, i: usize) -> Option<*const zval> {
if i >= self.count {
return None;
}
let zv = unsafe { self.ex.zend_call_arg(i) }?;
Some(unsafe { convert::deref(&raw const *zv) })
}
pub fn value(&self, i: usize) -> Result<Value, NativeError> {
let Some(zv) = self.slot(i) else {
return Ok(Value::Null);
};
unsafe { convert::to_value(zv) }.map_err(|why| {
NativeError::throw(
"TypeError",
format!(
"{}(): Argument #{} cannot be passed to Rust: {why}",
frame(self.ex).0,
i + 1
),
)
})
}
pub fn pass(&self, i: usize) -> Value {
match self.slot(i) {
Some(zv) => unsafe { convert::pass_through(zv) },
None => Value::Null,
}
}
pub fn pass_from(&self, i: usize) -> Vec<Value> {
(i..self.count).map(|i| self.pass(i)).collect()
}
pub fn items(&self, i: usize) -> Vec<Value> {
let Some(zv) = self.slot(i) else {
return Vec::new();
};
unsafe {
if u32::from(convert::type_of(zv)) != IS_ARRAY {
return Vec::new();
}
convert::array_values((*zv).value.arr)
.into_iter()
.map(|slot| convert::pass_through(slot))
.collect()
}
}
pub fn string_hash(&self, i: usize) -> Option<u64> {
self.slot(i)
.and_then(|zv| unsafe { convert::string_hash(zv) })
}
pub fn int(&self, i: usize) -> Option<i64> {
let zv = self.slot(i)?;
unsafe { (u32::from(convert::type_of(zv)) == IS_LONG).then(|| (*zv).value.lval) }
}
pub fn is_false(&self, i: usize) -> bool {
self.slot(i)
.is_some_and(|zv| unsafe { u32::from(convert::type_of(zv)) == IS_FALSE })
}
pub fn single_object(&self, i: usize) -> Option<Value> {
let zv = self.slot(i)?;
unsafe {
if u32::from(convert::type_of(zv)) != IS_ARRAY {
return None;
}
let ht = (*zv).value.arr;
if (*ht).nNumOfElements != 1 {
return None;
}
let first = zend_hash_index_find(ht, 0);
if first.is_null() {
return None;
}
let first = convert::deref(first);
(u32::from(convert::type_of(first)) == IS_OBJECT).then(|| convert::pass_through(first))
}
}
}
extern "C" fn dispatch(ex: &mut ExecuteData, retval: &mut Zval) {
boundary::native_entry(ex, retval, dispatch_body);
}
fn body_of(ex: &ExecuteData) -> Result<Body, String> {
let func = ex.func as usize;
if let Some(body) = BY_FUNCTION.with(|h| h.borrow().get(&func).cloned()) {
return Ok(body);
}
let (name, _) = frame(ex);
let body = HANDLERS
.with(|h| h.borrow().get(&name.to_ascii_lowercase()).cloned())
.ok_or(name)?;
BY_FUNCTION.with(|h| h.borrow_mut().insert(func, body.clone()));
Ok(body)
}
fn dispatch_body(ex: &mut ExecuteData, retval: &mut Zval) {
let body = match body_of(ex) {
Ok(body) => body,
Err(name) => {
throw(NativeError::throw(
"Error",
format!("{name}() has no native handler"),
));
return;
}
};
let result = match body {
Body::Raw(handler) => {
let count = unsafe { ex.This.u2.num_args } as usize;
handler(&Args { ex, count })
}
Body::Converted(handler) => match converted_args(ex) {
Ok(args) => handler(&args),
Err(err) => Err(err),
},
};
write_result(ex, retval, result);
}
fn converted_args(ex: &ExecuteData) -> Result<Vec<Value>, NativeError> {
let (name, count) = frame(ex);
let mut args = Vec::with_capacity(count);
for i in 0..count {
let Some(zv) = (unsafe { ex.zend_call_arg(i) }) else {
args.push(Value::Null);
continue;
};
match unsafe { convert::to_value(convert::deref(&raw const *zv)) } {
Ok(value) => args.push(value),
Err(why) => {
return Err(NativeError::throw(
"TypeError",
format!(
"{name}(): Argument #{} cannot be passed to Rust: {why}",
i + 1
),
));
}
}
}
Ok(args)
}
fn write_result(ex: &ExecuteData, retval: &mut Zval, result: Result<Value, NativeError>) {
match result {
Ok(value) => {
let out: *mut zval = retval;
if unsafe { convert::write_without_engine(out, &value) } {
return;
}
let written = boundary::guard(|| {
unsafe { convert::write_value(out, &value) }
});
if let Ok(Err(why)) = written {
let (name, _) = frame(ex);
throw(NativeError::throw(
"Error",
format!("{name}(): the result cannot be returned to PHP: {why}"),
));
}
}
Err(err) => throw(err),
}
}
fn frame(ex: &ExecuteData) -> (String, usize) {
unsafe {
let func = ex.func;
let mut name = String::new();
if !func.is_null() {
let fname = (*func).common.function_name;
if !fname.is_null() {
name = String::from_utf8_lossy(convert::str_bytes(fname)).into_owned();
}
}
(name, ex.This.u2.num_args as usize)
}
}
fn throw(err: NativeError) {
let (class, message) = match err {
NativeError::Throw { class, message } => (class, message),
NativeError::Rethrow(object) => {
if let Some(handle) = convert::handle_of(&object) {
let copy = unsafe { convert::ZvalHandle::copy_of(handle.as_ptr()) };
boundary::rethrow(copy);
}
return;
}
NativeError::Silent => return,
};
if !boundary::can_enter() {
return;
}
let ce = find_class(&class);
let message = CString::new(message.replace('\0', "\\0")).unwrap_or_default();
let message_ptr = message.as_ptr();
let _ = boundary::guard(|| {
unsafe { zend_throw_exception(ce, message_ptr, 0) };
});
}
fn find_class(name: &str) -> *mut zend_class_entry {
let key = name.trim_start_matches('\\').to_ascii_lowercase();
unsafe {
let table = (*ext_php_rs_executor_globals()).class_table;
let zv = zend_hash_str_find(table, key.as_ptr().cast(), key.len());
if zv.is_null() {
zend_ce_exception
} else {
(*zv).value.ce
}
}
}