Skip to content

Commit ae03f48

Browse files
committed
Add wrapper for zend_exec_stringl with exception handling
1 parent 8b0adf1 commit ae03f48

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ const ALLOWED_BINDINGS: &[&str] = &[
191191
"zend_declare_property",
192192
"zend_do_implement_interface",
193193
"zend_execute_data",
194+
"zend_eval_stringl",
194195
"zend_function_entry",
195196
"zend_hash_clean",
196197
"zend_hash_index_del",

src/errors.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
//! Error and result types returned from the library functions.
22
3-
use std::{error::Error as ErrorTrait, ffi::NulError, fmt::Display};
3+
use std::{error::Error as ErrorTrait, ffi::NulError, fmt::Display, rc::Rc};
44

55
use crate::php::{
66
enums::DataType,
77
exceptions::PhpException,
88
flags::{ClassFlags, ZvalTypeFlags},
9+
types::object::ZendObject,
910
};
1011

1112
/// The main result type which is passed by the library.
1213
pub type Result<T> = std::result::Result<T, Error>;
1314

1415
/// The main error type which is passed by the library inside the custom
1516
/// [`Result`] type.
16-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
17+
#[derive(Debug, Clone)]
1718
#[non_exhaustive]
1819
pub enum Error {
1920
/// An incorrect number of arguments was given to a PHP function.
@@ -50,6 +51,8 @@ pub enum Error {
5051
InvalidException(ClassFlags),
5152
/// Converting integer arguments resulted in an overflow.
5253
IntegerOverflow,
54+
CompileFailed,
55+
UncaughtException(Rc<Box<ZendObject>>),
5356
}
5457

5558
impl Display for Error {
@@ -83,6 +86,8 @@ impl Display for Error {
8386
Error::IntegerOverflow => {
8487
write!(f, "Converting integer arguments resulted in an overflow.")
8588
}
89+
Error::CompileFailed => write!(f, "Syntax error."),
90+
Error::UncaughtException(exception) => write!(f, "Uncaught Exception: {:?}", exception),
8691
}
8792
}
8893
}

src/php/globals.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::bindings::{_zend_executor_globals, ext_php_rs_executor_globals};
44

5-
use super::types::array::ZendHashTable;
5+
use super::types::{array::ZendHashTable, object::ZendObject};
66

77
/// Stores global variables used in the PHP executor.
88
pub type ExecutorGlobals = _zend_executor_globals;
@@ -16,6 +16,14 @@ impl ExecutorGlobals {
1616
.expect("Static executor globals were invalid")
1717
}
1818

19+
fn get_mut() -> &'static mut Self {
20+
// SAFETY: PHP executor globals are statically declared therefore should never
21+
// return an invalid pointer.
22+
// TODO: Should this be syncronized?
23+
unsafe { ext_php_rs_executor_globals().as_mut() }
24+
.expect("Static executor globals were invalid")
25+
}
26+
1927
/// Attempts to retrieve the global class hash table.
2028
pub fn class_table(&self) -> Option<ZendHashTable> {
2129
if self.class_table.is_null() {
@@ -24,4 +32,17 @@ impl ExecutorGlobals {
2432

2533
unsafe { ZendHashTable::from_ptr(self.class_table, false) }.ok()
2634
}
35+
36+
pub fn take_exception() -> Option<Box<ZendObject>> {
37+
let globals = Self::get_mut();
38+
39+
let mut exception_ptr = std::ptr::null_mut();
40+
std::mem::swap(&mut exception_ptr, &mut globals.exception);
41+
42+
if !exception_ptr.is_null() {
43+
Some(unsafe { Box::from_raw(exception_ptr) })
44+
} else {
45+
None
46+
}
47+
}
2748
}

src/php/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pub mod globals;
1616
pub mod module;
1717
pub mod pack;
1818
pub mod types;
19+
pub mod eval;

0 commit comments

Comments
 (0)