Skip to content

Commit 4186cc4

Browse files
authored
Merge pull request #277 from davidcole1340/subclass-ce
Forward ClassEntry in create_object
2 parents 27d4aa8 + 20c0d9c commit 4186cc4

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
},
2121
flags::DataType,
2222
types::{ZendObject, Zval},
23+
zend::ClassEntry,
2324
};
2425

2526
/// Representation of a Zend class object in memory.
@@ -44,7 +45,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
4445
/// Panics if memory was unable to be allocated for the new object.
4546
pub fn new(val: T) -> ZBox<Self> {
4647
// SAFETY: We are providing a value to initialize the object with.
47-
unsafe { Self::internal_new(Some(val)) }
48+
unsafe { Self::internal_new(Some(val), None) }
4849
}
4950

5051
/// Creates a new [`ZendClassObject`] of type `T`, with an uninitialized
@@ -68,8 +69,8 @@ impl<T: RegisteredClass> ZendClassObject<T> {
6869
/// # Panics
6970
///
7071
/// Panics if memory was unable to be allocated for the new object.
71-
pub unsafe fn new_uninit() -> ZBox<Self> {
72-
Self::internal_new(None)
72+
pub unsafe fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox<Self> {
73+
Self::internal_new(None, ce)
7374
}
7475

7576
/// Creates a new [`ZendObject`] of type `T`, storing the given (and
@@ -103,10 +104,10 @@ impl<T: RegisteredClass> ZendClassObject<T> {
103104
/// # Panics
104105
///
105106
/// Panics if memory was unable to be allocated for the new object.
106-
unsafe fn internal_new(val: Option<T>) -> ZBox<Self> {
107+
unsafe fn internal_new(val: Option<T>, ce: Option<&'static ClassEntry>) -> ZBox<Self> {
107108
let size = mem::size_of::<ZendClassObject<T>>();
108109
let meta = T::get_metadata();
109-
let ce = meta.ce() as *const _ as *mut _;
110+
let ce = ce.unwrap_or_else(|| meta.ce()) as *const _ as *mut _;
110111
let obj = ext_php_rs_zend_object_alloc(size as _, ce) as *mut ZendClassObject<T>;
111112
let obj = obj
112113
.as_mut()
@@ -168,7 +169,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
168169
(ptr as *mut Self).as_mut()?
169170
};
170171

171-
if ptr.std.is_instance::<T>() {
172+
if ptr.std.instance_of(T::get_metadata().ce()) {
172173
Some(ptr)
173174
} else {
174175
None

0 commit comments

Comments
 (0)