mirror of
https://github.com/danog/ext-php-rs.git
synced 2024-12-14 01:57:33 +01:00
parent
dddc07f587
commit
fbb0b41fdc
@ -161,10 +161,10 @@ impl ClassBuilder {
|
|||||||
/// Panics if the class name associated with `T` is not the same as the
|
/// Panics if the class name associated with `T` is not the same as the
|
||||||
/// class name specified when creating the builder.
|
/// class name specified when creating the builder.
|
||||||
pub fn object_override<T: RegisteredClass>(mut self) -> Self {
|
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
|
// SAFETY: After calling this function, PHP will always call the constructor
|
||||||
// defined below, which assumes that the object is uninitialized.
|
// 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()
|
obj.into_raw().get_mut_zend_obj()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,10 +15,10 @@ use crate::{
|
|||||||
error::{Error, Result},
|
error::{Error, Result},
|
||||||
ffi::{
|
ffi::{
|
||||||
ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init,
|
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,
|
flags::DataType,
|
||||||
types::{ZendObject, Zval},
|
types::{ZendObject, Zval}, zend::ClassEntry,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Representation of a Zend class object in memory.
|
/// Representation of a Zend class object in memory.
|
||||||
@ -43,7 +43,7 @@ impl<T: RegisteredClass> ZendClassObject<T> {
|
|||||||
/// Panics if memory was unable to be allocated for the new object.
|
/// Panics if memory was unable to be allocated for the new object.
|
||||||
pub fn new(val: T) -> ZBox<Self> {
|
pub fn new(val: T) -> ZBox<Self> {
|
||||||
// SAFETY: We are providing a value to initialize the object with.
|
// 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
|
/// Creates a new [`ZendClassObject`] of type `T`, with an uninitialized
|
||||||
@ -67,8 +67,8 @@ impl<T: RegisteredClass> ZendClassObject<T> {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if memory was unable to be allocated for the new object.
|
/// Panics if memory was unable to be allocated for the new object.
|
||||||
pub unsafe fn new_uninit() -> ZBox<Self> {
|
pub unsafe fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox<Self> {
|
||||||
Self::internal_new(None)
|
Self::internal_new(None, ce)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new [`ZendObject`] of type `T`, storing the given (and
|
/// Creates a new [`ZendObject`] of type `T`, storing the given (and
|
||||||
@ -102,10 +102,10 @@ impl<T: RegisteredClass> ZendClassObject<T> {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if memory was unable to be allocated for the new object.
|
/// 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 size = mem::size_of::<ZendClassObject<T>>();
|
||||||
let meta = T::get_metadata();
|
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 = ext_php_rs_zend_object_alloc(size as _, ce) as *mut ZendClassObject<T>;
|
||||||
let obj = obj
|
let obj = obj
|
||||||
.as_mut()
|
.as_mut()
|
||||||
|
@ -52,13 +52,13 @@ impl ZendObjectHandlers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn free_obj<T: RegisteredClass>(object: *mut ZendObject) {
|
unsafe extern "C" fn free_obj<T: RegisteredClass>(object: *mut ZendObject) {
|
||||||
let obj = object
|
object
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
|
.and_then(|obj| ZendClassObject::<T>::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.
|
// 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)
|
zend_object_std_dtor(object)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user