1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::{ffi::zend_class_entry, flags::ClassFlags, types::ZendStr, zend::ExecutorGlobals};
use std::{convert::TryInto, fmt::Debug, ops::DerefMut};
pub type ClassEntry = zend_class_entry;
impl ClassEntry {
pub fn try_find(name: &str) -> Option<&'static Self> {
ExecutorGlobals::get().class_table()?;
let mut name = ZendStr::new(name, false).ok()?;
unsafe {
crate::ffi::zend_lookup_class_ex(name.deref_mut(), std::ptr::null_mut(), 0).as_ref()
}
}
pub fn flags(&self) -> ClassFlags {
ClassFlags::from_bits_truncate(self.ce_flags)
}
pub fn is_interface(&self) -> bool {
self.flags().contains(ClassFlags::Interface)
}
pub fn instance_of(&self, other: &ClassEntry) -> bool {
if self == other {
return true;
}
if other.is_interface() {
return self
.interfaces()
.map_or(false, |mut it| it.any(|ce| ce == other));
}
std::iter::successors(self.parent(), |p| p.parent()).any(|ce| ce == other)
}
pub fn interfaces(&self) -> Option<impl Iterator<Item = &ClassEntry>> {
self.flags()
.contains(ClassFlags::ResolvedInterfaces)
.then(|| unsafe {
(0..self.num_interfaces)
.into_iter()
.map(move |i| *self.__bindgen_anon_3.interfaces.offset(i as _))
.filter_map(|ptr| ptr.as_ref())
})
}
pub fn parent(&self) -> Option<&Self> {
if self.flags().contains(ClassFlags::ResolvedParent) {
unsafe { self.__bindgen_anon_1.parent.as_ref() }
} else {
let name = unsafe { self.__bindgen_anon_1.parent_name.as_ref()? };
Self::try_find(name.as_str()?)
}
}
}
impl PartialEq for ClassEntry {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl Debug for ClassEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name: String = unsafe { self.name.as_ref() }
.and_then(|s| s.try_into().ok())
.ok_or(std::fmt::Error)?;
f.debug_struct("ClassEntry")
.field("name", &name)
.field("flags", &self.flags())
.field("is_interface", &self.is_interface())
.field(
"interfaces",
&self.interfaces().map(|iter| iter.collect::<Vec<_>>()),
)
.field("parent", &self.parent())
.finish()
}
}