11use crate :: {
2+ alloc:: { EAllocatable , EBox } ,
23 classes:: ClassEntry ,
4+ errors:: ClassNotFoundError ,
35 sys:: * ,
46 values:: { SetVal , Val } ,
5- ClassNotFoundError ,
67} ;
7- use phper_alloc:: EBox ;
88use std:: {
99 mem:: { forget, zeroed} ,
1010 ptr:: null_mut,
@@ -17,21 +17,21 @@ pub struct Object {
1717}
1818
1919impl Object {
20- pub fn new ( class_entry : & ClassEntry ) -> Self {
20+ pub fn new ( class_entry : & ClassEntry ) -> EBox < Self > {
2121 unsafe {
22- let mut object = zeroed :: < Object > ( ) ;
23- zend_object_std_init ( object. as_mut_ptr ( ) , class_entry. as_ptr ( ) as * mut _ ) ;
24- object. inner . handlers = & std_object_handlers;
25- object
22+ let ptr = zend_objects_new ( class_entry. as_ptr ( ) as * mut _ ) ;
23+ EBox :: from_raw ( ptr. cast ( ) )
2624 }
2725 }
2826
29- pub fn new_by_class_name ( class_name : impl AsRef < str > ) -> Result < Self , ClassNotFoundError > {
27+ pub fn new_by_class_name (
28+ class_name : impl AsRef < str > ,
29+ ) -> Result < EBox < Self > , ClassNotFoundError > {
3030 let class_entry = ClassEntry :: from_globals ( class_name) ?;
3131 Ok ( Self :: new ( class_entry) )
3232 }
3333
34- pub fn new_by_std_class ( ) -> Self {
34+ pub fn new_by_std_class ( ) -> EBox < Self > {
3535 Self :: new_by_class_name ( "stdclass" ) . unwrap ( )
3636 }
3737
@@ -49,7 +49,7 @@ impl Object {
4949 & mut self . inner
5050 }
5151
52- pub fn get_property ( & self , name : impl AsRef < str > ) -> & mut Val {
52+ pub fn get_property ( & self , name : impl AsRef < str > ) -> & Val {
5353 let name = name. as_ref ( ) ;
5454
5555 let prop = unsafe {
@@ -82,9 +82,9 @@ impl Object {
8282 unsafe { Val :: from_mut_ptr ( prop) }
8383 }
8484
85- pub fn set_property ( & mut self , name : impl AsRef < str > , value : impl SetVal ) {
85+ pub fn set_property ( & mut self , name : impl AsRef < str > , val : Val ) {
8686 let name = name. as_ref ( ) ;
87- let mut val = Val :: new ( value ) ;
87+ let val = EBox :: new ( val ) ;
8888 unsafe {
8989 #[ cfg( phper_major_version = "8" ) ]
9090 {
@@ -93,7 +93,7 @@ impl Object {
9393 & mut self . inner ,
9494 name. as_ptr ( ) . cast ( ) ,
9595 name. len ( ) ,
96- val. as_mut_ptr ( ) ,
96+ EBox :: into_raw ( val) . cast ( ) ,
9797 )
9898 }
9999 #[ cfg( phper_major_version = "7" ) ]
@@ -105,7 +105,7 @@ impl Object {
105105 & mut zv,
106106 name. as_ptr ( ) . cast ( ) ,
107107 name. len ( ) ,
108- val. as_mut_ptr ( ) ,
108+ EBox :: into_raw ( val) . cast ( ) ,
109109 )
110110 }
111111 }
@@ -119,10 +119,16 @@ impl Object {
119119 }
120120}
121121
122- impl Drop for Object {
123- fn drop ( & mut self ) {
122+ impl EAllocatable for Object {
123+ fn free ( ptr : * mut Self ) {
124124 unsafe {
125- zend_objects_destroy_object ( & mut self . inner ) ;
125+ zend_objects_destroy_object ( ptr . cast ( ) ) ;
126126 }
127127 }
128128}
129+
130+ impl Drop for Object {
131+ fn drop ( & mut self ) {
132+ unreachable ! ( "Allocation on the stack is not allowed" )
133+ }
134+ }
0 commit comments