33use crate :: {
44 errors:: ClassNotFoundError ,
55 functions:: { Argument , Callable , FunctionEntity , FunctionEntry , Method } ,
6+ objects:: Object ,
67 sys:: * ,
78 utils:: ensure_end_with_zero,
9+ values:: { SetVal , Val } ,
810} ;
911use once_cell:: sync:: OnceCell ;
1012use std:: {
13+ marker:: PhantomData ,
1114 mem:: zeroed,
1215 os:: raw:: c_int,
1316 ptr:: null_mut,
1417 sync:: atomic:: { AtomicPtr , Ordering } ,
1518} ;
1619
17- pub trait Class : Send + Sync {
20+ pub trait Classifiable {
1821 fn methods ( & mut self ) -> & mut [ FunctionEntity ] ;
1922 fn properties ( & mut self ) -> & mut [ PropertyEntity ] ;
2023 fn parent ( & self ) -> Option < & str > ;
2124}
2225
23- pub struct StdClass {
26+ pub struct DynamicClass < T : Send + Sync + ' static > {
2427 pub ( crate ) method_entities : Vec < FunctionEntity > ,
2528 pub ( crate ) property_entities : Vec < PropertyEntity > ,
2629 pub ( crate ) parent : Option < String > ,
30+ _p : PhantomData < T > ,
2731}
2832
29- impl StdClass {
33+ impl < T : Send + Sync + ' static > DynamicClass < T > {
3034 pub fn new ( ) -> Self {
3135 Self {
3236 method_entities : Vec :: new ( ) ,
3337 property_entities : Vec :: new ( ) ,
3438 parent : None ,
39+ _p : Default :: default ( ) ,
3540 }
3641 }
3742
38- pub fn add_method (
39- & mut self ,
40- name : impl ToString ,
41- handler : impl Method + ' static ,
42- arguments : Vec < Argument > ,
43- ) {
43+ pub fn add_method < F , R > ( & mut self , name : impl ToString , handler : F , arguments : Vec < Argument > )
44+ where
45+ F : Fn ( & mut Object < T > , & mut [ Val ] ) -> R + Send + Sync + ' static ,
46+ R : SetVal + ' static ,
47+ {
4448 self . method_entities . push ( FunctionEntity :: new (
4549 name,
46- Callable :: Method ( Box :: new ( handler ) , AtomicPtr :: new ( null_mut ( ) ) ) ,
50+ Box :: new ( Method :: new ( handler ) ) ,
4751 arguments,
4852 ) ) ;
4953 }
@@ -59,7 +63,7 @@ impl StdClass {
5963 }
6064}
6165
62- impl Class for StdClass {
66+ impl < T : Send + Sync > Classifiable for DynamicClass < T > {
6367 fn methods ( & mut self ) -> & mut [ FunctionEntity ] {
6468 & mut self . method_entities
6569 }
@@ -117,12 +121,12 @@ fn find_global_class_entry_ptr(name: impl AsRef<str>) -> *mut zend_class_entry {
117121pub struct ClassEntity {
118122 pub ( crate ) name : String ,
119123 pub ( crate ) entry : AtomicPtr < ClassEntry > ,
120- pub ( crate ) class : Box < dyn Class > ,
124+ pub ( crate ) class : Box < dyn Classifiable > ,
121125 pub ( crate ) function_entries : OnceCell < AtomicPtr < FunctionEntry > > ,
122126}
123127
124128impl ClassEntity {
125- pub ( crate ) unsafe fn new ( name : impl ToString , class : impl Class + ' static ) -> Self {
129+ pub ( crate ) unsafe fn new ( name : impl ToString , class : impl Classifiable + ' static ) -> Self {
126130 Self {
127131 name : name. to_string ( ) ,
128132 entry : AtomicPtr :: new ( null_mut ( ) ) ,
@@ -138,26 +142,28 @@ impl ClassEntity {
138142 self . function_entries ( ) . load ( Ordering :: SeqCst ) . cast ( ) ,
139143 ) ;
140144
141- let parent = self . class . parent ( ) . map ( |s| match s {
142- "Exception" | " \\ Exception" => zend_ce_exception ,
143- _ => todo ! ( ) ,
144- } ) ;
145+ let parent = self
146+ . class
147+ . parent ( )
148+ . map ( |s| ClassEntry :: from_globals ( s ) . unwrap ( ) ) ;
145149
146150 let ptr = match parent {
147- Some ( parent) => zend_register_internal_class_ex ( & mut class_ce, parent) . cast ( ) ,
151+ Some ( parent) => {
152+ zend_register_internal_class_ex ( & mut class_ce, parent. as_ptr ( ) as * mut _ ) . cast ( )
153+ }
148154 None => zend_register_internal_class ( & mut class_ce) . cast ( ) ,
149155 } ;
150156 self . entry . store ( ptr, Ordering :: SeqCst ) ;
151157
152- let methods = self . class . methods ( ) ;
153- for method in methods {
154- match & method. handler {
155- Callable :: Method ( _, class) => {
156- class. store ( ptr, Ordering :: SeqCst ) ;
157- }
158- _ => unreachable ! ( ) ,
159- }
160- }
158+ // let methods = self.class.methods();
159+ // for method in methods {
160+ // match &method.handler {
161+ // Callable::Method(_, class) => {
162+ // class.store(ptr, Ordering::SeqCst);
163+ // }
164+ // _ => unreachable!(),
165+ // }
166+ // }
161167 }
162168
163169 pub ( crate ) unsafe fn declare_properties ( & mut self ) {
0 commit comments