From fbb0b41fdc029cc182102cd39806341d78a91ee3 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 24 Oct 2023 20:16:53 +0200 Subject: [PATCH 1/4] Forward ClassEntry in create_object See #138 --- src/builders/class.rs | 4 ++-- src/types/class_object.rs | 14 +++++++------- src/zend/handlers.rs | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/builders/class.rs b/src/builders/class.rs index 91a6a010f..28e846384 100644 --- a/src/builders/class.rs +++ b/src/builders/class.rs @@ -161,10 +161,10 @@ impl ClassBuilder { /// Panics if the class name associated with `T` is not the same as the /// class name specified when creating the builder. pub fn object_override(mut self) -> Self { - extern "C" fn create_object(_: *mut ClassEntry) -> *mut ZendObject { + extern "C" fn create_object(ce: *mut ClassEntry) -> *mut ZendObject { // SAFETY: After calling this function, PHP will always call the constructor // defined below, which assumes that the object is uninitialized. - let obj = unsafe { ZendClassObject::::new_uninit() }; + let obj = unsafe { ZendClassObject::::new_uninit(ce.as_ref()) }; obj.into_raw().get_mut_zend_obj() } diff --git a/src/types/class_object.rs b/src/types/class_object.rs index e9b116529..c9b8b482c 100644 --- a/src/types/class_object.rs +++ b/src/types/class_object.rs @@ -15,10 +15,10 @@ use crate::{ error::{Error, Result}, ffi::{ ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init, - zend_object, zend_object_std_init, zend_objects_clone_members, + zend_object, zend_object_std_init, zend_objects_clone_members, _zend_class_entry, }, flags::DataType, - types::{ZendObject, Zval}, + types::{ZendObject, Zval}, zend::ClassEntry, }; /// Representation of a Zend class object in memory. @@ -43,7 +43,7 @@ impl ZendClassObject { /// Panics if memory was unable to be allocated for the new object. pub fn new(val: T) -> ZBox { // SAFETY: We are providing a value to initialize the object with. - unsafe { Self::internal_new(Some(val)) } + unsafe { Self::internal_new(Some(val), None) } } /// Creates a new [`ZendClassObject`] of type `T`, with an uninitialized @@ -67,8 +67,8 @@ impl ZendClassObject { /// # Panics /// /// Panics if memory was unable to be allocated for the new object. - pub unsafe fn new_uninit() -> ZBox { - Self::internal_new(None) + pub unsafe fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox { + Self::internal_new(None, ce) } /// Creates a new [`ZendObject`] of type `T`, storing the given (and @@ -102,10 +102,10 @@ impl ZendClassObject { /// # Panics /// /// Panics if memory was unable to be allocated for the new object. - unsafe fn internal_new(val: Option) -> ZBox { + unsafe fn internal_new(val: Option, ce: Option<&'static ClassEntry>) -> ZBox { let size = mem::size_of::>(); let meta = T::get_metadata(); - let ce = meta.ce() as *const _ as *mut _; + let ce = ce.unwrap_or_else(||meta.ce()) as *const _ as *mut _; let obj = ext_php_rs_zend_object_alloc(size as _, ce) as *mut ZendClassObject; let obj = obj .as_mut() diff --git a/src/zend/handlers.rs b/src/zend/handlers.rs index 11d220ba0..f2d71b0ae 100644 --- a/src/zend/handlers.rs +++ b/src/zend/handlers.rs @@ -52,13 +52,13 @@ impl ZendObjectHandlers { } unsafe extern "C" fn free_obj(object: *mut ZendObject) { - let obj = object + object .as_mut() .and_then(|obj| ZendClassObject::::from_zend_obj_mut(obj)) - .expect("Invalid object pointer given for `free_obj`"); + .map(|obj|ptr::drop_in_place(&mut obj.obj)); // Manually drop the object as we don't want to free the underlying memory. - ptr::drop_in_place(&mut obj.obj); + zend_object_std_dtor(object) } From 3688aac183369bb948e53230612c4571c2654fbd Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 25 Oct 2023 20:34:49 +0200 Subject: [PATCH 2/4] Check instance_of() so subclasses also work --- src/types/class_object.rs | 2 +- src/zend/handlers.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/types/class_object.rs b/src/types/class_object.rs index c9b8b482c..099b72673 100644 --- a/src/types/class_object.rs +++ b/src/types/class_object.rs @@ -167,7 +167,7 @@ impl ZendClassObject { (ptr as *mut Self).as_mut()? }; - if ptr.std.is_instance::() { + if ptr.std.instance_of(T::get_metadata().ce()) { Some(ptr) } else { None diff --git a/src/zend/handlers.rs b/src/zend/handlers.rs index f2d71b0ae..11d220ba0 100644 --- a/src/zend/handlers.rs +++ b/src/zend/handlers.rs @@ -52,13 +52,13 @@ impl ZendObjectHandlers { } unsafe extern "C" fn free_obj(object: *mut ZendObject) { - object + let obj = object .as_mut() .and_then(|obj| ZendClassObject::::from_zend_obj_mut(obj)) - .map(|obj|ptr::drop_in_place(&mut obj.obj)); + .expect("Invalid object pointer given for `free_obj`"); // Manually drop the object as we don't want to free the underlying memory. - + ptr::drop_in_place(&mut obj.obj); zend_object_std_dtor(object) } From 8009a91e1389ec1c0598b85db8b120998bf8c28f Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 25 Oct 2023 20:40:11 +0200 Subject: [PATCH 3/4] Cargo fmt --- src/types/class_object.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/types/class_object.rs b/src/types/class_object.rs index 099b72673..811d446cb 100644 --- a/src/types/class_object.rs +++ b/src/types/class_object.rs @@ -14,11 +14,12 @@ use crate::{ convert::{FromZendObject, FromZendObjectMut, FromZval, FromZvalMut, IntoZval}, error::{Error, Result}, ffi::{ - ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init, - zend_object, zend_object_std_init, zend_objects_clone_members, _zend_class_entry, + _zend_class_entry, ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, + object_properties_init, zend_object, zend_object_std_init, zend_objects_clone_members, }, flags::DataType, - types::{ZendObject, Zval}, zend::ClassEntry, + types::{ZendObject, Zval}, + zend::ClassEntry, }; /// Representation of a Zend class object in memory. @@ -105,7 +106,7 @@ impl ZendClassObject { unsafe fn internal_new(val: Option, ce: Option<&'static ClassEntry>) -> ZBox { let size = mem::size_of::>(); let meta = T::get_metadata(); - let ce = ce.unwrap_or_else(||meta.ce()) as *const _ as *mut _; + let ce = ce.unwrap_or_else(|| meta.ce()) as *const _ as *mut _; let obj = ext_php_rs_zend_object_alloc(size as _, ce) as *mut ZendClassObject; let obj = obj .as_mut() From 20c0d9c8cc66d139241e20a8a524657b3982b523 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Thu, 26 Oct 2023 16:05:55 +0200 Subject: [PATCH 4/4] Remove unused class --- src/types/class_object.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types/class_object.rs b/src/types/class_object.rs index 811d446cb..f6124e606 100644 --- a/src/types/class_object.rs +++ b/src/types/class_object.rs @@ -14,8 +14,8 @@ use crate::{ convert::{FromZendObject, FromZendObjectMut, FromZval, FromZvalMut, IntoZval}, error::{Error, Result}, ffi::{ - _zend_class_entry, ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, - object_properties_init, zend_object, zend_object_std_init, zend_objects_clone_members, + ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init, + zend_object, zend_object_std_init, zend_objects_clone_members, }, flags::DataType, types::{ZendObject, Zval},