Skip to content

Commit 7548f5c

Browse files
authored
Fix php classes extends from phper classes. (#151)
1 parent f6be43e commit 7548f5c

File tree

12 files changed

+50
-14
lines changed

12 files changed

+50
-14
lines changed

examples/http-server/src/server.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn make_server_class() -> ClassEntity<()> {
9191
let app = Router::new().route(
9292
"/",
9393
any(move |req: Request<Body>| async move {
94-
match (async move {
94+
let fut = async move {
9595
let (parts, body) = req.into_parts();
9696

9797
// Read all request body content.
@@ -132,9 +132,8 @@ pub fn make_server_class() -> ClassEntity<()> {
132132

133133
Ok::<Response<Body>, phper::Error>(response)
134134
})
135-
})
136-
.await
137-
{
135+
};
136+
match fut.await {
138137
Ok(response) => response,
139138
Err(e) => {
140139
// If failed, simply return 500 as http status code, and the error

phper-alloc/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod macros;
1818
use phper_sys::*;
1919
use std::{
2020
borrow::Borrow,
21-
convert::TryInto,
2221
mem::{size_of, ManuallyDrop},
2322
ops::{Deref, DerefMut},
2423
};

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ bool phper_instanceof_function(const zend_class_entry *instance_ce,
375375
return instanceof_function(instance_ce, ce) != 0;
376376
}
377377

378+
zend_class_entry *phper_get_parent_class(zend_class_entry *ce) {
379+
return ce->parent;
380+
}
381+
378382
// ==================================================
379383
// function apis:
380384
// ==================================================

phper/src/arrays.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::{alloc::ToRefOwned, strings::ZStr, sys::*, values::ZVal};
1414
use derive_more::From;
1515
use std::{
1616
borrow::Borrow,
17-
convert::TryInto,
1817
fmt::{self, Debug},
1918
marker::PhantomData,
2019
mem::ManuallyDrop,

phper/src/classes.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
arrays::ZArr,
1515
errors::{ClassNotFoundError, InitializeObjectError, Throwable},
1616
functions::{Function, FunctionEntry, Method, MethodEntity},
17+
modules::global_module,
1718
objects::{StateObj, StateObject, ZObject},
1819
strings::ZStr,
1920
sys::*,
@@ -23,8 +24,6 @@ use crate::{
2324
};
2425
use std::{
2526
any::Any,
26-
borrow::ToOwned,
27-
convert::TryInto,
2827
ffi::{c_void, CString},
2928
fmt::Debug,
3029
marker::PhantomData,
@@ -849,12 +848,15 @@ pub(crate) type RawVisibility = u32;
849848

850849
#[allow(clippy::useless_conversion)]
851850
unsafe extern "C" fn create_object(ce: *mut zend_class_entry) -> *mut zend_object {
851+
// Get real ce which hold state_constructor.
852+
let real_ce = find_real_ce(ce).unwrap();
853+
852854
// Alloc more memory size to store state data.
853855
let state_object = phper_zend_object_alloc(size_of::<StateObj<()>>().try_into().unwrap(), ce);
854856
let state_object = StateObj::<()>::from_mut_ptr(state_object);
855857

856858
// Find the hack elements hidden behind null builtin_function.
857-
let mut func_ptr = (*ce).info.internal.builtin_functions;
859+
let mut func_ptr = (*real_ce).info.internal.builtin_functions;
858860
while !(*func_ptr).fname.is_null() {
859861
func_ptr = func_ptr.offset(1);
860862
}
@@ -904,14 +906,15 @@ unsafe extern "C" fn clone_object(object: *mut zval) -> *mut zend_object {
904906
#[allow(clippy::useless_conversion)]
905907
unsafe fn clone_object_common(object: *mut zend_object) -> *mut zend_object {
906908
let ce = (*object).ce;
909+
let real_ce = find_real_ce(ce).unwrap();
907910

908911
// Alloc more memory size to store state data.
909912
let new_state_object =
910913
phper_zend_object_alloc(size_of::<StateObj<()>>().try_into().unwrap(), ce);
911914
let new_state_object = StateObj::<()>::from_mut_ptr(new_state_object);
912915

913916
// Find the hack elements hidden behind null builtin_function.
914-
let mut func_ptr = (*(*object).ce).info.internal.builtin_functions;
917+
let mut func_ptr = (*real_ce).info.internal.builtin_functions;
915918
while !(*func_ptr).fname.is_null() {
916919
func_ptr = func_ptr.offset(1);
917920
}
@@ -947,3 +950,19 @@ unsafe extern "C" fn free_object(object: *mut zend_object) {
947950
// Original destroy call.
948951
zend_object_std_dtor(object);
949952
}
953+
954+
/// Find the class that registered by phper.
955+
unsafe fn find_real_ce(mut ce: *mut zend_class_entry) -> Option<*mut zend_class_entry> {
956+
let class_entities = global_module().class_entities();
957+
958+
while !ce.is_null() {
959+
for entity in class_entities {
960+
if ClassEntry::from_ptr(ce).get_name().to_c_str() == Ok(&entity.class_name) {
961+
return Some(ce);
962+
}
963+
}
964+
ce = phper_get_parent_class(ce);
965+
}
966+
967+
None
968+
}

phper/src/functions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//! TODO Add lambda.
1414
1515
use crate::{
16-
cg,
1716
classes::{ClassEntry, RawVisibility, Visibility},
1817
errors::{throw, ArgumentCountError, ExceptionGuard, ThrowObject, Throwable},
1918
objects::{StateObj, ZObj, ZObject},

phper/src/modules.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static mut GLOBAL_MODULE: *mut Module = null_mut();
3737

3838
static mut GLOBAL_MODULE_ENTRY: *mut zend_module_entry = null_mut();
3939

40+
#[inline]
41+
pub(crate) unsafe fn global_module<'a>() -> &'a Module {
42+
GLOBAL_MODULE.as_ref().unwrap()
43+
}
44+
4045
unsafe extern "C" fn module_startup(_type: c_int, module_number: c_int) -> c_int {
4146
let module = GLOBAL_MODULE.as_mut().unwrap();
4247

@@ -285,4 +290,9 @@ impl Module {
285290

286291
Box::into_raw(entries.into_boxed_slice()).cast()
287292
}
293+
294+
#[inline]
295+
pub(crate) fn class_entities(&self) -> &[ClassEntity<()>] {
296+
&self.class_entities
297+
}
288298
}

phper/src/objects.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use phper_alloc::{RefClone, ToRefOwned};
2020
use std::{
2121
any::Any,
2222
borrow::Borrow,
23-
convert::TryInto,
2423
ffi::c_void,
2524
fmt::{self, Debug},
2625
marker::PhantomData,

phper/src/strings.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::sys::*;
1414
use phper_alloc::ToRefOwned;
1515
use std::{
1616
borrow::Borrow,
17-
convert::TryInto,
1817
ffi::{CStr, FromBytesWithNulError},
1918
fmt::{self, Debug},
2019
marker::PhantomData,

phper/src/values.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::{
2424
};
2525
use phper_alloc::RefClone;
2626
use std::{
27-
convert::TryInto,
2827
ffi::CStr,
2928
fmt,
3029
fmt::Debug,

0 commit comments

Comments
 (0)