Skip to content

Commit 064ef74

Browse files
committed
Fixes
1 parent e0ae481 commit 064ef74

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

src/types/object.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::{convert::TryInto, fmt::Debug, ops::DerefMut};
66
use crate::{
77
boxed::{ZBox, ZBoxable},
88
class::RegisteredClass,
9-
convert::{FromZendObject, FromZval, FromZvalMut, IntoZval},
9+
convert::{FromZendObject, FromZval, FromZvalMut, IntoZval, IntoZvalDyn},
1010
error::{Error, Result},
1111
ffi::{
1212
ext_php_rs_zend_object_release, zend_call_known_function, zend_object, zend_objects_new,
13-
HashTable, ZEND_ISEMPTY, ZEND_PROPERTY_EXISTS, ZEND_PROPERTY_ISSET,
13+
HashTable, ZEND_ISEMPTY, ZEND_PROPERTY_EXISTS, ZEND_PROPERTY_ISSET, zend_hash_str_find_ptr_lc, zend_function,
1414
},
1515
flags::DataType,
1616
rc::PhpRc,
@@ -121,6 +121,39 @@ impl ZendObject {
121121
(self.ce as *const ClassEntry).eq(&(T::get_metadata().ce() as *const _))
122122
}
123123

124+
125+
#[inline(always)]
126+
pub fn try_call_method(&self, name: &str, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
127+
let mut retval = Zval::new();
128+
let len = params.len();
129+
let params = params
130+
.into_iter()
131+
.map(|val| val.as_zval(false))
132+
.collect::<Result<Vec<_>>>()?;
133+
let packed = params.into_boxed_slice();
134+
135+
unsafe {
136+
let res = zend_hash_str_find_ptr_lc(
137+
&(*self.ce).function_table,
138+
name.as_ptr() as *const i8,
139+
name.len()
140+
) as *mut zend_function;
141+
if res.is_null() {
142+
return Err(Error::Callable)
143+
}
144+
zend_call_known_function(
145+
res,
146+
self as *const _ as *mut _,
147+
self.ce,
148+
&mut retval,
149+
len as _,
150+
packed.as_ptr() as *mut _,
151+
std::ptr::null_mut(),
152+
)
153+
};
154+
155+
Ok(retval)
156+
}
124157
/// Attempts to read a property from the Object. Returns a result containing
125158
/// the value of the property if it exists and can be read, and an
126159
/// [`Error`] otherwise.

src/types/zval.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ impl Zval {
206206
}
207207
}
208208

209+
#[inline(always)]
210+
pub fn try_call_method(&self, name: &str, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
211+
self.object().ok_or(Error::Object)?.try_call_method(name, params)
212+
}
213+
209214
/// Returns the value of the zval if it is a reference.
210215
pub fn reference(&self) -> Option<&Zval> {
211216
if self.is_reference() {

src/zend/fibers.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub struct EventLoop {
3838
notify_receiver: File,
3939

4040
get_current_suspension: Function,
41-
suspend: Function,
42-
resume: Function,
43-
41+
4442
dummy: [u8; 1],
4543
}
4644

@@ -61,8 +59,6 @@ impl EventLoop {
6159
notify_receiver: unsafe { File::from_raw_fd(notify_receiver) },
6260
dummy: [0; 1],
6361
get_current_suspension: Function::try_from_method("\\Revolt\\EventLoop", "getSuspension").ok_or("\\Revolt\\EventLoop::getSuspension does not exist")?,
64-
suspend: Function::try_from_method("\\Revolt\\EventLoop\\Suspension", "suspend").ok_or("\\Revolt\\Suspension::suspend does not exist")?,
65-
resume: Function::try_from_method("\\Revolt\\EventLoop\\Suspension", "resume").ok_or("\\Revolt\\Suspension::resume does not exist")?
6662
})
6763
}
6864

@@ -75,7 +71,7 @@ impl EventLoop {
7571

7672
for fiber_id in self.receiver.try_iter() {
7773
if let Some(fiber) = self.fibers.get_index_mut(fiber_id) {
78-
self.resume.try_call_obj(fiber, vec![])?;
74+
fiber.object_mut().unwrap().try_call_method("resume", vec![])?;
7975
self.fibers.remove_index(fiber_id);
8076
}
8177
}
@@ -92,9 +88,8 @@ impl EventLoop {
9288
pub fn suspend() {
9389
EVENTLOOP.with_borrow_mut(|c| {
9490
let c = c.as_mut().unwrap();
95-
let mut suspension = call_user_func!(c.get_current_suspension).unwrap();
96-
c.suspend.try_call_obj(&mut suspension, vec![])
97-
}).unwrap();
91+
call_user_func!(c.get_current_suspension).unwrap().try_call_method("suspend", vec![]).unwrap();
92+
});
9893
}
9994

10095
pub fn get_sender(&self) -> Sender<u64> {

src/zend/function.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{fmt::Debug, os::raw::c_char, ptr::self};
44

5-
use crate::{ffi::{zend_function_entry, zend_fetch_function_str, zend_function, zend_hash_str_find_ptr_lc, zend_call_known_function}, convert::IntoZvalDyn, types::Zval, error::{Result, Error}};
5+
use crate::{ffi::{zend_function_entry, zend_fetch_function_str, zend_function, zend_hash_str_find_ptr_lc, zend_call_known_function}, convert::IntoZvalDyn, types::Zval, error::Result};
66

77
use super::ClassEntry;
88

@@ -101,7 +101,7 @@ impl Function {
101101
/// assert_eq!(result.long(), Some(1));
102102
/// ```
103103
#[inline(always)]
104-
pub fn try_call(&mut self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
104+
pub fn try_call(&self, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
105105
let mut retval = Zval::new();
106106
let len = params.len();
107107
let params = params
@@ -112,7 +112,7 @@ impl Function {
112112

113113
unsafe {
114114
zend_call_known_function(
115-
self as *mut _,
115+
self as *const _ as *mut _,
116116
std::ptr::null_mut(),
117117
std::ptr::null_mut(),
118118
&mut retval,
@@ -124,30 +124,4 @@ impl Function {
124124

125125
Ok(retval)
126126
}
127-
128-
#[inline(always)]
129-
pub fn try_call_obj(&mut self, obj: &mut Zval, params: Vec<&dyn IntoZvalDyn>) -> Result<Zval> {
130-
let obj = obj.object_mut().ok_or(Error::Object)?;
131-
let mut retval = Zval::new();
132-
let len = params.len();
133-
let params = params
134-
.into_iter()
135-
.map(|val| val.as_zval(false))
136-
.collect::<Result<Vec<_>>>()?;
137-
let packed = params.into_boxed_slice();
138-
139-
unsafe {
140-
zend_call_known_function(
141-
self as *mut _,
142-
obj as *mut _,
143-
obj.ce,
144-
&mut retval,
145-
len as _,
146-
packed.as_ptr() as *mut _,
147-
std::ptr::null_mut(),
148-
)
149-
};
150-
151-
Ok(retval)
152-
}
153-
}
127+
}

0 commit comments

Comments
 (0)