Skip to content

Commit

Permalink
Merge pull request #277 from davidcole1340/subclass-ce
Browse files Browse the repository at this point in the history
Forward ClassEntry in create_object
  • Loading branch information
danog authored Nov 24, 2023
2 parents 27d4aa8 + 20c0d9c commit 4186cc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/builders/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: RegisteredClass>(mut self) -> Self {
extern "C" fn create_object<T: RegisteredClass>(_: *mut ClassEntry) -> *mut ZendObject {
extern "C" fn create_object<T: RegisteredClass>(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::<T>::new_uninit() };
let obj = unsafe { ZendClassObject::<T>::new_uninit(ce.as_ref()) };
obj.into_raw().get_mut_zend_obj()
}

Expand Down
13 changes: 7 additions & 6 deletions src/types/class_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
},
flags::DataType,
types::{ZendObject, Zval},
zend::ClassEntry,
};

/// Representation of a Zend class object in memory.
Expand All @@ -44,7 +45,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
/// Panics if memory was unable to be allocated for the new object.
pub fn new(val: T) -> ZBox<Self> {
// 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
Expand All @@ -68,8 +69,8 @@ impl<T: RegisteredClass> ZendClassObject<T> {
/// # Panics
///
/// Panics if memory was unable to be allocated for the new object.
pub unsafe fn new_uninit() -> ZBox<Self> {
Self::internal_new(None)
pub unsafe fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox<Self> {
Self::internal_new(None, ce)
}

/// Creates a new [`ZendObject`] of type `T`, storing the given (and
Expand Down Expand Up @@ -103,10 +104,10 @@ impl<T: RegisteredClass> ZendClassObject<T> {
/// # Panics
///
/// Panics if memory was unable to be allocated for the new object.
unsafe fn internal_new(val: Option<T>) -> ZBox<Self> {
unsafe fn internal_new(val: Option<T>, ce: Option<&'static ClassEntry>) -> ZBox<Self> {
let size = mem::size_of::<ZendClassObject<T>>();
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<T>;
let obj = obj
.as_mut()
Expand Down Expand Up @@ -168,7 +169,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
(ptr as *mut Self).as_mut()?
};

if ptr.std.is_instance::<T>() {
if ptr.std.instance_of(T::get_metadata().ce()) {
Some(ptr)
} else {
None
Expand Down

0 comments on commit 4186cc4

Please sign in to comment.