1- use crate :: { classes:: ClassEntry , sys:: * , values:: Val } ;
2- use std:: ptr:: null_mut;
1+ use crate :: {
2+ classes:: { get_global_class_entry_ptr, ClassEntry } ,
3+ sys:: * ,
4+ values:: { SetVal , Val } ,
5+ ClassNotFoundError ,
6+ } ;
7+ use std:: {
8+ mem:: zeroed,
9+ ptr:: null_mut,
10+ sync:: atomic:: { AtomicPtr , Ordering } ,
11+ } ;
312
13+ /// Wrapper of [crate::sys::zend_object].
14+ #[ repr( transparent) ]
415pub struct Object {
5- val : * mut Val ,
6- class : * mut ClassEntry ,
16+ inner : zend_object ,
717}
818
919impl Object {
10- pub ( crate ) fn new < ' a > ( val : * mut Val , class : * mut ClassEntry ) -> Object {
11- assert ! ( !val. is_null( ) ) ;
12- assert ! ( !class. is_null( ) ) ;
13- Self { val, class }
20+ pub fn new ( class_name : impl AsRef < str > ) -> Result < Self , ClassNotFoundError > {
21+ unsafe {
22+ let mut object = zeroed :: < Object > ( ) ;
23+ let class_name = class_name. as_ref ( ) ;
24+ let ce = get_global_class_entry_ptr ( class_name) ;
25+ if ce. is_null ( ) {
26+ Err ( ClassNotFoundError :: new ( class_name. to_string ( ) ) )
27+ } else {
28+ zend_object_std_init ( object. as_mut_ptr ( ) , ce) ;
29+ object. inner . handlers = & std_object_handlers;
30+ Ok ( object)
31+ }
32+ }
33+ }
34+
35+ pub fn new_std ( ) -> Self {
36+ Self :: new ( "stdClass" ) . expect ( "stdClass not found" )
37+ }
38+
39+ pub unsafe fn from_mut_ptr < ' a > ( ptr : * mut zend_object ) -> & ' a mut Object {
40+ ( ptr as * mut Object )
41+ . as_mut ( )
42+ . expect ( "ptr should not be null" )
43+ }
44+
45+ pub fn as_ptr ( & self ) -> * const zend_object {
46+ & self . inner
47+ }
48+
49+ pub fn as_mut_ptr ( & mut self ) -> * mut zend_object {
50+ & mut self . inner
1451 }
1552
1653 pub fn get_property ( & self , name : impl AsRef < str > ) -> & mut Val {
@@ -20,8 +57,8 @@ impl Object {
2057 #[ cfg( phper_major_version = "8" ) ]
2158 {
2259 zend_read_property (
23- self . class as * mut _ ,
24- ( * self . val ) . inner . value . obj ,
60+ self . inner . ce ,
61+ & self . inner as * const _ as * mut _ ,
2562 name. as_ptr ( ) . cast ( ) ,
2663 name. len ( ) ,
2764 false . into ( ) ,
@@ -32,8 +69,8 @@ impl Object {
3269 #[ cfg( phper_major_version = "7" ) ]
3370 {
3471 zend_read_property (
35- self . class as * mut _ ,
36- self . val as * mut _ ,
72+ self . inner . ce ,
73+ & self . inner as * const _ as * mut _ ,
3774 name. as_ptr ( ) . cast ( ) ,
3875 name. len ( ) ,
3976 false . into ( ) ,
@@ -42,6 +79,28 @@ impl Object {
4279 }
4380 } ;
4481
45- unsafe { Val :: from_mut ( prop) }
82+ unsafe { Val :: from_mut_ptr ( prop) }
83+ }
84+
85+ pub fn set_property ( & mut self , name : impl AsRef < str > , value : impl SetVal ) {
86+ let name = name. as_ref ( ) ;
87+ let mut val = Val :: new ( value) ;
88+ unsafe {
89+ zend_update_property (
90+ self . inner . ce ,
91+ & mut self . inner as * mut _ ,
92+ name. as_ptr ( ) . cast ( ) ,
93+ name. len ( ) ,
94+ val. as_mut_ptr ( ) ,
95+ )
96+ }
97+ }
98+ }
99+
100+ impl Drop for Object {
101+ fn drop ( & mut self ) {
102+ unsafe {
103+ zend_objects_destroy_object ( & mut self . inner ) ;
104+ }
46105 }
47106}
0 commit comments