33use crate :: {
44 alloc:: EBox ,
55 arrays:: Array ,
6- errors:: { ClassNotFoundError , StateTypeError } ,
6+ errors:: { ClassNotFoundError , InitializeObjectError , StateTypeError } ,
77 functions:: { Argument , Function , FunctionEntity , FunctionEntry , Method } ,
88 objects:: { ExtendObject , Object } ,
99 strings:: ZendString ,
@@ -16,7 +16,7 @@ use once_cell::sync::OnceCell;
1616use std:: {
1717 any:: { Any , TypeId } ,
1818 marker:: PhantomData ,
19- mem:: { size_of, zeroed, ManuallyDrop } ,
19+ mem:: { forget , size_of, zeroed, ManuallyDrop } ,
2020 os:: raw:: c_int,
2121 ptr:: null_mut,
2222 sync:: {
@@ -229,25 +229,25 @@ impl<T: 'static> ClassEntry<T> {
229229
230230 /// Create the object from class and call `__construct` with arguments.
231231 pub fn new_object ( & self , arguments : & mut [ Val ] ) -> crate :: Result < EBox < Object < T > > > {
232- unsafe {
233- let ptr = self . as_ptr ( ) as * mut _ ;
234- let f = ( * phper_get_create_object ( ptr) ) . unwrap_or ( zend_objects_new) ;
235- let object = f ( ptr) ;
236- let mut object: EBox < Object < T > > = EBox :: from_raw ( object. cast ( ) ) ;
237- let _ = object. call_construct ( arguments) ?;
238- Ok ( object)
239- }
232+ let mut object = self . init_object ( ) ?;
233+ object. call_construct ( arguments) ?;
234+ Ok ( object)
240235 }
241236
242237 /// Create the object from class, without calling `__construct`, be careful when `__construct`
243238 /// is necessary.
244- pub fn new_object_without_construct ( & self ) -> EBox < Object < T > > {
239+ pub fn init_object ( & self ) -> crate :: Result < EBox < Object < T > > > {
245240 unsafe {
246241 let ptr = self . as_ptr ( ) as * mut _ ;
247- let f = ( * phper_get_create_object ( ptr) ) . unwrap_or ( zend_objects_new) ;
248- let object = f ( ptr) ;
249- let object: EBox < Object < T > > = EBox :: from_raw ( object. cast ( ) ) ;
250- object
242+ let mut val = Val :: undef ( ) ;
243+ if !phper_object_init_ex ( val. as_mut_ptr ( ) , ptr) {
244+ Err ( InitializeObjectError :: new ( self . get_name ( ) . as_str ( ) ?. to_owned ( ) ) . into ( ) )
245+ } else {
246+ let object = ( * val. as_mut_ptr ( ) ) . value . obj ;
247+ forget ( val) ;
248+ let object: EBox < Object < T > > = EBox :: from_raw ( object. cast ( ) ) ;
249+ Ok ( object)
250+ }
251251 }
252252 }
253253
0 commit comments