Skip to content

Commit 6a42591

Browse files
authored
Add object and function methods. (#32)
* Add object handle method. * Distince function name methods. * Get this.
1 parent 75a7a80 commit 6a42591

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ zend_string *phper_get_function_or_method_name(const zend_function *func) {
151151
#endif
152152
}
153153

154+
zend_string *phper_get_function_name(const zend_function *func) {
155+
return func->common.function_name;
156+
}
157+
154158
void phper_zval_ptr_dtor(zval *zv) {
155159
ZVAL_PTR_DTOR(zv);
156160
}

phper/src/functions.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
errors::{ArgumentCountError, CallFunctionError, CallMethodError},
1919
exceptions::Exception,
2020
objects::{StatefulObj, ZObj},
21-
strings::ZStr,
21+
strings::{ZStr, ZString},
2222
sys::*,
2323
utils::ensure_end_with_zero,
2424
values::{ExecuteData, ZVal},
@@ -92,7 +92,7 @@ where
9292
&self, execute_data: &mut ExecuteData, arguments: &mut [ZVal], return_value: &mut ZVal,
9393
) {
9494
unsafe {
95-
let this = execute_data.get_this().unwrap();
95+
let this = execute_data.get_this_mut().unwrap();
9696
let this = StatefulObj::from_z_obj(this);
9797
let r = (self.f)(this, arguments);
9898
*return_value = r.into();
@@ -235,13 +235,20 @@ impl ZendFunction {
235235
&mut self.inner
236236
}
237237

238-
pub fn get_name(&self) -> &ZStr {
238+
pub fn get_function_name(&self) -> &ZStr {
239239
unsafe {
240-
let s = phper_get_function_or_method_name(self.as_ptr());
240+
let s = phper_get_function_name(self.as_ptr());
241241
ZStr::from_ptr(s)
242242
}
243243
}
244244

245+
pub fn get_function_or_method_name(&self) -> ZString {
246+
unsafe {
247+
let s = phper_get_function_or_method_name(self.as_ptr());
248+
ZString::from_raw(s)
249+
}
250+
}
251+
245252
pub(crate) fn call(
246253
&mut self, mut object: Option<&mut ZObj>, mut arguments: impl AsMut<[ZVal]>,
247254
) -> crate::Result<ZVal> {
@@ -301,7 +308,7 @@ impl ZendFunction {
301308

302309
zend_call_function(&mut fci, &mut fcc) == ZEND_RESULT_CODE_SUCCESS
303310
},
304-
|| Ok(self.get_name().to_str()?.to_owned()),
311+
|| Ok(self.get_function_or_method_name().to_str()?.to_owned()),
305312
object,
306313
)
307314
}
@@ -334,7 +341,7 @@ unsafe extern "C" fn invoke(execute_data: *mut zend_execute_data, return_value:
334341
let num_args = execute_data.num_args() as usize;
335342
let required_num_args = execute_data.common_required_num_args() as usize;
336343
if num_args < required_num_args {
337-
let func_name = execute_data.func().get_name();
344+
let func_name = execute_data.func().get_function_or_method_name();
338345
let result = func_name
339346
.to_str()
340347
.map(|func_name| {

phper/src/objects.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ impl ZObj {
6565
eo.state.downcast_mut().unwrap()
6666
}
6767

68+
#[inline]
69+
pub fn handle(&self) -> u32 {
70+
self.inner.handle
71+
}
72+
6873
pub fn get_class(&self) -> &ClassEntry {
6974
unsafe { ClassEntry::from_ptr(self.inner.ce) }
7075
}

phper/src/values.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ impl ExecuteData {
9999
ZendFunction::from_mut_ptr(self.inner.func)
100100
}
101101

102-
/// # Safety
103-
///
104-
/// The type of `T` should be careful.
105-
pub unsafe fn get_this(&mut self) -> Option<&mut ZObj> {
106-
let val = ZVal::from_mut_ptr(phper_get_this(&mut self.inner));
107-
val.as_mut_z_obj()
102+
pub fn get_this(&mut self) -> Option<&ZObj> {
103+
unsafe {
104+
let val = ZVal::from_ptr(phper_get_this(&mut self.inner));
105+
val.as_z_obj()
106+
}
107+
}
108+
109+
pub fn get_this_mut(&mut self) -> Option<&mut ZObj> {
110+
unsafe {
111+
let val = ZVal::from_mut_ptr(phper_get_this(&mut self.inner));
112+
val.as_mut_z_obj()
113+
}
108114
}
109115

110116
/// TODO Do not return owned object, because usually Val should not be drop.

0 commit comments

Comments
 (0)