-
Notifications
You must be signed in to change notification settings - Fork 20
improve Interface::extends and ClassEntry::extends #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bdf7caa
improve interface.extends
brettmc 40c3503
improve ClassEntry::extends
brettmc ee4fd40
extends StateClass
brettmc 91d9bfd
fmt, clippy
brettmc 4b29427
extends(StateClass)
brettmc 76c20a1
Merge branch 'master' into extends
brettmc 6c555ce
fix doc for class extends class
brettmc 32b44c6
Merge branch 'extends' of github.com:brettmc/phper into extends
brettmc 13ad462
remove iterator + array_access methods
brettmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -28,26 +28,14 @@ use std::{ | |||||||
ffi::{CString, c_char, c_void}, | ||||||||
fmt::Debug, | ||||||||
marker::PhantomData, | ||||||||
mem::{ManuallyDrop, replace, size_of, zeroed}, | ||||||||
mem::{ManuallyDrop, replace, size_of, transmute, zeroed}, | ||||||||
os::raw::c_int, | ||||||||
ptr, | ||||||||
ptr::null_mut, | ||||||||
rc::Rc, | ||||||||
slice, | ||||||||
}; | ||||||||
|
||||||||
/// Predefined interface `Iterator`. | ||||||||
#[inline] | ||||||||
pub fn iterator_class<'a>() -> &'a ClassEntry { | ||||||||
unsafe { ClassEntry::from_ptr(zend_ce_iterator) } | ||||||||
} | ||||||||
|
||||||||
/// Predefined interface `ArrayAccess`. | ||||||||
#[inline] | ||||||||
pub fn array_access_class<'a>() -> &'a ClassEntry { | ||||||||
unsafe { ClassEntry::from_ptr(zend_ce_arrayaccess) } | ||||||||
} | ||||||||
|
||||||||
/// Wrapper of [zend_class_entry]. | ||||||||
#[derive(Clone)] | ||||||||
#[repr(transparent)] | ||||||||
|
@@ -287,13 +275,26 @@ fn find_global_class_entry_ptr(name: impl AsRef<str>) -> *mut zend_class_entry { | |||||||
/// ``` | ||||||||
pub struct StateClass<T> { | ||||||||
inner: Rc<RefCell<*mut zend_class_entry>>, | ||||||||
name: Option<String>, | ||||||||
_p: PhantomData<T>, | ||||||||
} | ||||||||
|
||||||||
impl StateClass<()> { | ||||||||
/// Create from name, which will be looked up from globals. | ||||||||
pub fn from_name(name: impl Into<String>) -> Self { | ||||||||
Self { | ||||||||
inner: Rc::new(RefCell::new(null_mut())), | ||||||||
name: Some(name.into()), | ||||||||
_p: PhantomData, | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
impl<T> StateClass<T> { | ||||||||
fn null() -> Self { | ||||||||
Self { | ||||||||
inner: Rc::new(RefCell::new(null_mut())), | ||||||||
name: None, | ||||||||
_p: PhantomData, | ||||||||
} | ||||||||
} | ||||||||
|
@@ -304,7 +305,11 @@ impl<T> StateClass<T> { | |||||||
|
||||||||
/// Converts to class entry. | ||||||||
pub fn as_class_entry(&self) -> &ClassEntry { | ||||||||
unsafe { ClassEntry::from_mut_ptr(*self.inner.borrow()) } | ||||||||
if let Some(name) = &self.name { | ||||||||
ClassEntry::from_globals(name).unwrap() | ||||||||
} else { | ||||||||
unsafe { ClassEntry::from_mut_ptr(*self.inner.borrow()) } | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/// Create the object from class and call `__construct` with arguments. | ||||||||
|
@@ -333,6 +338,7 @@ impl<T> Clone for StateClass<T> { | |||||||
fn clone(&self) -> Self { | ||||||||
Self { | ||||||||
inner: self.inner.clone(), | ||||||||
name: self.name.clone(), | ||||||||
_p: self._p, | ||||||||
} | ||||||||
} | ||||||||
|
@@ -427,7 +433,7 @@ pub struct ClassEntity<T: 'static> { | |||||||
state_constructor: Rc<StateConstructor>, | ||||||||
method_entities: Vec<MethodEntity>, | ||||||||
property_entities: Vec<PropertyEntity>, | ||||||||
parent: Option<Box<dyn Fn() -> &'static ClassEntry>>, | ||||||||
parent: Option<StateClass<()>>, | ||||||||
interfaces: Vec<Interface>, | ||||||||
constants: Vec<ConstantEntity>, | ||||||||
bind_class: StateClass<T>, | ||||||||
|
@@ -550,19 +556,40 @@ impl<T: 'static> ClassEntity<T> { | |||||||
/// Register class to `extends` the parent class. | ||||||||
/// | ||||||||
/// *Because in the `MINIT` phase, the class starts to register, so the* | ||||||||
/// *closure is used to return the `ClassEntry` to delay the acquisition of* | ||||||||
/// *`ClassEntry` is looked up by name to delay the acquisition of* | ||||||||
/// *the class.* | ||||||||
/// | ||||||||
/// # Examples | ||||||||
/// | ||||||||
/// ```no_run | ||||||||
/// use phper::classes::{ClassEntity, ClassEntry}; | ||||||||
/// use phper::{ | ||||||||
/// classes::{ClassEntity, ClassEntry, StateClass}, | ||||||||
/// modules::Module, | ||||||||
/// php_get_module, | ||||||||
/// }; | ||||||||
/// | ||||||||
/// let mut class = ClassEntity::new("MyException"); | ||||||||
/// class.extends(|| ClassEntry::from_globals("Exception").unwrap()); | ||||||||
/// #[php_get_module] | ||||||||
/// pub fn get_module() -> Module { | ||||||||
/// let mut module = Module::new( | ||||||||
/// env!("CARGO_CRATE_NAME"), | ||||||||
/// env!("CARGO_PKG_VERSION"), | ||||||||
/// env!("CARGO_PKG_AUTHORS"), | ||||||||
/// ); | ||||||||
/// | ||||||||
/// let foo = module.add_class(ClassEntity::new("Foo")); | ||||||||
/// let mut bar = ClassEntity::new("Bar"); | ||||||||
/// bar.extends(foo); | ||||||||
/// module.add_class(bar); | ||||||||
/// | ||||||||
/// let mut ex = ClassEntity::new("MyException"); | ||||||||
/// ex.extends(StateClass::from_name("Exception")); | ||||||||
/// module.add_class(ex); | ||||||||
/// | ||||||||
/// module | ||||||||
/// } | ||||||||
/// ``` | ||||||||
pub fn extends(&mut self, parent: impl Fn() -> &'static ClassEntry + 'static) { | ||||||||
self.parent = Some(Box::new(parent)); | ||||||||
pub fn extends<S: 'static>(&mut self, parent: StateClass<S>) { | ||||||||
self.parent = Some(unsafe { transmute::<StateClass<S>, StateClass<()>>(parent) }); | ||||||||
} | ||||||||
|
||||||||
/// Register class to `implements` the interface, due to the class can | ||||||||
|
@@ -634,7 +661,7 @@ impl<T: 'static> ClassEntity<T> { | |||||||
let parent: *mut zend_class_entry = self | ||||||||
.parent | ||||||||
.as_ref() | ||||||||
.map(|parent| parent()) | ||||||||
.map(|parent| parent.as_class_entry()) | ||||||||
.map(|entry| entry.as_ptr() as *mut _) | ||||||||
.unwrap_or(null_mut()); | ||||||||
|
||||||||
|
@@ -800,20 +827,24 @@ impl InterfaceEntity { | |||||||
/// Register interface to `extends` the interfaces, due to the interface can | ||||||||
/// extends multi interface, so this method can be called multi time. | ||||||||
/// | ||||||||
/// *Because in the `MINIT` phase, the class starts to register, so the* | ||||||||
/// *Because in the `MINIT` phase, the class starts to register, a* | ||||||||
/// *closure is used to return the `ClassEntry` to delay the acquisition of* | ||||||||
/// *the class.* | ||||||||
/// | ||||||||
/// # Examples | ||||||||
/// | ||||||||
/// ```no_run | ||||||||
/// use phper::classes::{ClassEntry, InterfaceEntity}; | ||||||||
/// use phper::classes::{Interface, InterfaceEntity}; | ||||||||
/// | ||||||||
/// let mut interface = InterfaceEntity::new("MyInterface"); | ||||||||
/// interface.extends(|| ClassEntry::from_globals("Stringable").unwrap()); | ||||||||
/// interface.extends(Interface::from_name("Stringable")); | ||||||||
/// ``` | ||||||||
pub fn extends(&mut self, interface: impl Fn() -> &'static ClassEntry + 'static) { | ||||||||
self.extends.push(Box::new(interface)); | ||||||||
pub fn extends(&mut self, interface: Interface) { | ||||||||
self.extends.push(Box::new(move || { | ||||||||
let entry: &'static ClassEntry = | ||||||||
unsafe { std::mem::transmute(interface.as_class_entry()) }; | ||||||||
Comment on lines
+844
to
+845
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using unsafe transmute in this context may introduce subtle lifetime issues when converting interface's ClassEntry. It is recommended to explore a safer alternative to handle the conversion explicitly.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
entry | ||||||||
})); | ||||||||
} | ||||||||
|
||||||||
#[allow(clippy::useless_conversion)] | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of unsafe transmute here can lead to undefined behavior if the lifetime conversion between StateClass
and StateClass<()> is not guaranteed to be safe. Consider using a safer, more explicit conversion method.Copilot uses AI. Check for mistakes.