Skip to content

Commit fbb0b41

Browse files
committed
Forward ClassEntry in create_object
See #138
1 parent dddc07f commit fbb0b41

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/builders/class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ impl ClassBuilder {
161161
/// Panics if the class name associated with `T` is not the same as the
162162
/// class name specified when creating the builder.
163163
pub fn object_override<T: RegisteredClass>(mut self) -> Self {
164-
extern "C" fn create_object<T: RegisteredClass>(_: *mut ClassEntry) -> *mut ZendObject {
164+
extern "C" fn create_object<T: RegisteredClass>(ce: *mut ClassEntry) -> *mut ZendObject {
165165
// SAFETY: After calling this function, PHP will always call the constructor
166166
// defined below, which assumes that the object is uninitialized.
167-
let obj = unsafe { ZendClassObject::<T>::new_uninit() };
167+
let obj = unsafe { ZendClassObject::<T>::new_uninit(ce.as_ref()) };
168168
obj.into_raw().get_mut_zend_obj()
169169
}
170170

src/types/class_object.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::{
1515
error::{Error, Result},
1616
ffi::{
1717
ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init,
18-
zend_object, zend_object_std_init, zend_objects_clone_members,
18+
zend_object, zend_object_std_init, zend_objects_clone_members, _zend_class_entry,
1919
},
2020
flags::DataType,
21-
types::{ZendObject, Zval},
21+
types::{ZendObject, Zval}, zend::ClassEntry,
2222
};
2323

2424
/// Representation of a Zend class object in memory.
@@ -43,7 +43,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
4343
/// Panics if memory was unable to be allocated for the new object.
4444
pub fn new(val: T) -> ZBox<Self> {
4545
// SAFETY: We are providing a value to initialize the object with.
46-
unsafe { Self::internal_new(Some(val)) }
46+
unsafe { Self::internal_new(Some(val), None) }
4747
}
4848

4949
/// Creates a new [`ZendClassObject`] of type `T`, with an uninitialized
@@ -67,8 +67,8 @@ impl<T: RegisteredClass> ZendClassObject<T> {
6767
/// # Panics
6868
///
6969
/// Panics if memory was unable to be allocated for the new object.
70-
pub unsafe fn new_uninit() -> ZBox<Self> {
71-
Self::internal_new(None)
70+
pub unsafe fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox<Self> {
71+
Self::internal_new(None, ce)
7272
}
7373

7474
/// Creates a new [`ZendObject`] of type `T`, storing the given (and
@@ -102,10 +102,10 @@ impl<T: RegisteredClass> ZendClassObject<T> {
102102
/// # Panics
103103
///
104104
/// Panics if memory was unable to be allocated for the new object.
105-
unsafe fn internal_new(val: Option<T>) -> ZBox<Self> {
105+
unsafe fn internal_new(val: Option<T>, ce: Option<&'static ClassEntry>) -> ZBox<Self> {
106106
let size = mem::size_of::<ZendClassObject<T>>();
107107
let meta = T::get_metadata();
108-
let ce = meta.ce() as *const _ as *mut _;
108+
let ce = ce.unwrap_or_else(||meta.ce()) as *const _ as *mut _;
109109
let obj = ext_php_rs_zend_object_alloc(size as _, ce) as *mut ZendClassObject<T>;
110110
let obj = obj
111111
.as_mut()

src/zend/handlers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ impl ZendObjectHandlers {
5252
}
5353

5454
unsafe extern "C" fn free_obj<T: RegisteredClass>(object: *mut ZendObject) {
55-
let obj = object
55+
object
5656
.as_mut()
5757
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
58-
.expect("Invalid object pointer given for `free_obj`");
58+
.map(|obj|ptr::drop_in_place(&mut obj.obj));
5959

6060
// Manually drop the object as we don't want to free the underlying memory.
61-
ptr::drop_in_place(&mut obj.obj);
61+
6262

6363
zend_object_std_dtor(object)
6464
}

0 commit comments

Comments
 (0)