Skip to content

Commit

Permalink
Fix argument type allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Nov 24, 2023
1 parent 465c905 commit 86c9074
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> Arg<'a> {
{
self.zval
.as_mut()
.and_then(|zv| T::from_zval_mut(zv))
.and_then(|zv| T::from_zval_mut(zv.dereference_mut()))
.ok_or(self)
}

Expand All @@ -98,7 +98,9 @@ impl<'a> Arg<'a> {
where
T: FromZvalMut<'a>,
{
self.zval.as_mut().and_then(|zv| T::from_zval_mut(zv))
self.zval
.as_mut()
.and_then(|zv| T::from_zval_mut(zv.dereference_mut()))
}

/// Attempts to return a reference to the arguments internal Zval.
Expand Down
19 changes: 19 additions & 0 deletions src/types/zval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ impl Zval {
}
}

/// Dereference the zval, if it is a reference.
pub fn dereference(&self) -> &Self {
return self.reference().or_else(|| self.indirect()).unwrap_or(self);
}

/// Dereference the zval mutable, if it is a reference.
pub fn dereference_mut(&mut self) -> &mut Self {
// TODO: probably more ZTS work is needed here
if self.is_reference() {
#[allow(clippy::unwrap_used)]
return self.reference_mut().unwrap();
}
if self.is_indirect() {
#[allow(clippy::unwrap_used)]
return self.indirect_mut().unwrap();
}
self
}

/// Returns the value of the zval if it is a long.
pub fn long(&self) -> Option<ZendLong> {
if self.is_long() {
Expand Down
8 changes: 3 additions & 5 deletions src/zend/_type.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{
ffi::{c_void, CString},
ptr,
};
use std::{ffi::c_void, ptr};

use crate::{
ffi::{
zend_type, IS_MIXED, MAY_BE_ANY, MAY_BE_BOOL, _IS_BOOL, _ZEND_IS_VARIADIC_BIT,
_ZEND_SEND_MODE_SHIFT, _ZEND_TYPE_NAME_BIT, _ZEND_TYPE_NULLABLE_BIT,
},
flags::DataType,
types::ZendStr,
};

/// Internal Zend type.
Expand Down Expand Up @@ -82,7 +80,7 @@ impl ZendType {
allow_null: bool,
) -> Option<Self> {
Some(Self {
ptr: CString::new(class_name).ok()?.into_raw() as *mut c_void,
ptr: ZendStr::new(class_name, true).into_raw().as_ptr() as *mut c_void,
type_mask: _ZEND_TYPE_NAME_BIT
| (if allow_null {
_ZEND_TYPE_NULLABLE_BIT
Expand Down
7 changes: 4 additions & 3 deletions src/zend/try_catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ pub fn try_catch<R, F: FnMut() -> R + RefUnwindSafe>(func: F) -> Result<R, Catch

/// PHP propose a try catch mechanism in C using setjmp and longjmp (bailout)
/// It store the arg of setjmp into the bailout field of the global executor
/// If a bailout is triggered, the executor will jump to the setjmp and restore the previous setjmp
/// If a bailout is triggered, the executor will jump to the setjmp and restore
/// the previous setjmp
///
/// try_catch_first allow to use this mechanism
///
/// This functions differs from ['try_catch'] as it also initialize the bailout mechanism
/// for the first time
/// This functions differs from ['try_catch'] as it also initialize the bailout
/// mechanism for the first time
///
/// # Returns
///
Expand Down

0 comments on commit 86c9074

Please sign in to comment.