Skip to content

Commit 751d4f4

Browse files
committed
Refactoring
1 parent 07ce829 commit 751d4f4

File tree

13 files changed

+409
-121
lines changed

13 files changed

+409
-121
lines changed

allowed_bindings.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ bind! {
5454
zend_array_destroy,
5555
zend_array_dup,
5656
zend_call_known_function,
57+
zend_fetch_function_str,
58+
zend_hash_str_find_ptr_lc,
5759
zend_ce_argument_count_error,
5860
zend_ce_arithmetic_error,
5961
zend_ce_compile_error,

crates/macros/src/method.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,22 @@ pub fn parser(
194194
#hack_tokens
195195
let future = async move #stmts;
196196

197-
let future = GLOBAL_CONNECTION.with_borrow_mut(move |c| {
197+
let future = ::ext_php_rs::zend::EVENTLOOP.with_borrow_mut(move |c| {
198198
let c = c.as_mut().unwrap();
199-
let f = ::ext_php_rs::get_current_suspension!();
200-
let idx = c.fibers.len() as u64;
201-
let mut callable = Zval::new();
202-
callable.set_array(vec![f, "resume".into_zval(false).unwrap()]).unwrap();
203-
c.fibers.insert_at_index(idx, callable).unwrap();
199+
let idx = c.prepare_resume();
204200

205201
let sender = c.sender.clone();
206202
let mut notifier = c.notify_sender.try_clone().unwrap();
207203

208-
::ext_php_rs::zend::RUNTIME.spawn(async move {
204+
let res = ::ext_php_rs::zend::RUNTIME.spawn(async move {
209205
let res = future.await;
210206
sender.send(idx).unwrap();
211-
notifier.write_all(&[0]).unwrap();
207+
::std::io::Write::write_all(&mut notifier, &[0]).unwrap();
212208
res
213209
})
214210
});
215211

216-
let mut callable = Zval::new();
217-
callable.set_array(vec![::ext_php_rs::get_current_suspension!(), "suspend".into_zval(false).unwrap()]).unwrap();
218-
call_user_func!(callable).unwrap();
212+
::ext_php_rs::zend::EVENTLOOP.suspend();
219213

220214
return ::ext_php_rs::zend::RUNTIME
221215
.block_on(future).unwrap();

docsrs_bindings.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* automatically generated by rust-bindgen 0.65.1 */
22

3-
pub const ZEND_DEBUG: u32 = 1;
3+
pub const ZEND_DEBUG: u32 = 0;
44
pub const _ZEND_TYPE_NAME_BIT: u32 = 16777216;
55
pub const _ZEND_TYPE_NULLABLE_BIT: u32 = 2;
66
pub const HT_MIN_SIZE: u32 = 8;
@@ -87,6 +87,11 @@ pub const CONST_CS: u32 = 0;
8787
pub const CONST_PERSISTENT: u32 = 1;
8888
pub const CONST_NO_FILE_CACHE: u32 = 2;
8989
pub const CONST_DEPRECATED: u32 = 4;
90+
#[repr(C)]
91+
#[derive(Debug, Copy, Clone)]
92+
pub struct __sigset_t {
93+
pub __val: [::std::os::raw::c_ulong; 16usize],
94+
}
9095
pub type zend_long = i64;
9196
pub type zend_ulong = u64;
9297
pub type zend_uchar = ::std::os::raw::c_uchar;
@@ -285,22 +290,10 @@ pub struct _zend_ast_ref {
285290
pub gc: zend_refcounted_h,
286291
}
287292
extern "C" {
288-
pub fn _emalloc(
289-
size: usize,
290-
__zend_filename: *const ::std::os::raw::c_char,
291-
__zend_lineno: u32,
292-
__zend_orig_filename: *const ::std::os::raw::c_char,
293-
__zend_orig_lineno: u32,
294-
) -> *mut ::std::os::raw::c_void;
293+
pub fn _emalloc(size: usize) -> *mut ::std::os::raw::c_void;
295294
}
296295
extern "C" {
297-
pub fn _efree(
298-
ptr: *mut ::std::os::raw::c_void,
299-
__zend_filename: *const ::std::os::raw::c_char,
300-
__zend_lineno: u32,
301-
__zend_orig_filename: *const ::std::os::raw::c_char,
302-
__zend_orig_lineno: u32,
303-
);
296+
pub fn _efree(ptr: *mut ::std::os::raw::c_void);
304297
}
305298
extern "C" {
306299
pub fn __zend_malloc(len: usize) -> *mut ::std::os::raw::c_void;
@@ -387,6 +380,13 @@ extern "C" {
387380
extern "C" {
388381
pub fn zend_array_destroy(ht: *mut HashTable);
389382
}
383+
extern "C" {
384+
pub fn zend_hash_str_find_ptr_lc(
385+
ht: *const HashTable,
386+
str_: *const ::std::os::raw::c_char,
387+
len: usize,
388+
) -> *mut ::std::os::raw::c_void;
389+
}
390390
extern "C" {
391391
pub fn gc_possible_root(ref_: *mut zend_refcounted);
392392
}
@@ -1027,7 +1027,15 @@ pub struct _zend_execute_data {
10271027
pub run_time_cache: *mut *mut ::std::os::raw::c_void,
10281028
pub extra_named_params: *mut zend_array,
10291029
}
1030-
pub type sigjmp_buf = [::std::os::raw::c_int; 49usize];
1030+
pub type __jmp_buf = [::std::os::raw::c_long; 8usize];
1031+
#[repr(C)]
1032+
#[derive(Debug, Copy, Clone)]
1033+
pub struct __jmp_buf_tag {
1034+
pub __jmpbuf: __jmp_buf,
1035+
pub __mask_was_saved: ::std::os::raw::c_int,
1036+
pub __saved_mask: __sigset_t,
1037+
}
1038+
pub type jmp_buf = [__jmp_buf_tag; 1usize];
10311039
pub type zend_executor_globals = _zend_executor_globals;
10321040
extern "C" {
10331041
pub static mut executor_globals: zend_executor_globals;
@@ -1094,7 +1102,7 @@ pub struct _zend_executor_globals {
10941102
pub symtable_cache_ptr: *mut *mut zend_array,
10951103
pub symbol_table: zend_array,
10961104
pub included_files: HashTable,
1097-
pub bailout: *mut sigjmp_buf,
1105+
pub bailout: *mut jmp_buf,
10981106
pub error_reporting: ::std::os::raw::c_int,
10991107
pub exit_status: ::std::os::raw::c_int,
11001108
pub function_table: *mut HashTable,
@@ -1237,6 +1245,12 @@ pub struct _zend_vm_stack {
12371245
pub end: *mut zval,
12381246
pub prev: zend_vm_stack,
12391247
}
1248+
extern "C" {
1249+
pub fn zend_fetch_function_str(
1250+
name: *const ::std::os::raw::c_char,
1251+
len: usize,
1252+
) -> *mut zend_function;
1253+
}
12401254
#[repr(C)]
12411255
#[derive(Copy, Clone)]
12421256
pub struct _zend_function_entry {

src/builders/module.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
error::Result,
33
ffi::{ext_php_rs_php_build_id, ZEND_MODULE_API_NO},
4-
zend::{FunctionEntry, ModuleEntry},
5-
PHP_DEBUG, PHP_ZTS,
4+
zend::{FunctionEntry, ModuleEntry, request_shutdown, request_startup},
5+
PHP_DEBUG, PHP_ZTS, types::ZendClassObject,
66
};
77

88
use std::{ffi::CString, mem, ptr};
@@ -64,8 +64,8 @@ impl ModuleBuilder {
6464
functions: ptr::null(),
6565
module_startup_func: None,
6666
module_shutdown_func: None,
67-
request_startup_func: None,
68-
request_shutdown_func: None,
67+
request_startup_func: Some(request_startup),
68+
request_shutdown_func: Some(request_shutdown),
6969
info_func: None,
7070
version: ptr::null(),
7171
globals_size: 0,

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub enum Error {
5252
InvalidUtf8,
5353
/// Could not call the given function.
5454
Callable,
55+
/// An object was expected.
56+
Object,
5557
/// An invalid exception type was thrown.
5658
InvalidException(ClassFlags),
5759
/// Converting integer arguments resulted in an overflow.
@@ -84,6 +86,7 @@ impl Display for Error {
8486
),
8587
Error::InvalidUtf8 => write!(f, "Invalid Utf8 byte sequence."),
8688
Error::Callable => write!(f, "Could not call given function."),
89+
Error::Object => write!(f, "An object was expected."),
8790
Error::InvalidException(flags) => {
8891
write!(f, "Invalid exception type was thrown: {flags:?}")
8992
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(non_snake_case)]
66
#![cfg_attr(docs, feature(doc_cfg))]
77
#![cfg_attr(windows, feature(abi_vectorcall))]
8+
#![feature(thread_local, local_key_cell_methods)]
89

910
pub mod alloc;
1011
pub mod args;
@@ -35,6 +36,7 @@ pub mod zend;
3536
/// A module typically glob-imported containing the typically required macros
3637
/// and imports.
3738
pub mod prelude {
39+
use crate::boxed::ZBox;
3840
pub use crate::builders::ModuleBuilder;
3941
#[cfg(any(docs, feature = "closure"))]
4042
#[cfg_attr(docs, doc(cfg(feature = "closure")))]

src/macros.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,6 @@ macro_rules! call_user_func {
7272
};
7373
}
7474

75-
#[macro_export]
76-
macro_rules! call_static_method {
77-
($clazz: expr, $fn: expr) => {{
78-
let mut callable = ext_php_rs::types::Zval::new();
79-
callable
80-
.set_array(vec![$clazz, $fn])
81-
.unwrap();
82-
call_user_func!(callable)
83-
}};
84-
85-
($clazz: expr, $fn: expr, $($param: expr),*) => {{
86-
let mut callable = ext_php_rs::types::Zval::new();
87-
callable
88-
.set_array(vec![$clazz, $fn])
89-
.unwrap();
90-
call_user_func!(callable, $(&$param),*)
91-
}};
92-
}
93-
94-
#[macro_export]
95-
macro_rules! get_current_suspension {
96-
() => { ext_php_rs::call_static_method!("\\Revolt\\EventLoop", "getSuspension").unwrap() }
97-
}
98-
99-
10075
/// Parses a given list of arguments using the [`ArgParser`] class.
10176
///
10277
/// # Examples

src/types/array.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,33 @@ impl ZendHashTable {
187187
unsafe { zend_hash_str_find(self, str.as_ptr(), key.len() as _).as_ref() }
188188
}
189189

190+
/// Attempts to retrieve a value from the hash table with a string key.
191+
///
192+
/// # Parameters
193+
///
194+
/// * `key` - The key to search for in the hash table.
195+
///
196+
/// # Returns
197+
///
198+
/// * `Some(&Zval)` - A reference to the zval at the position in the hash
199+
/// table.
200+
/// * `None` - No value at the given position was found.
201+
///
202+
/// # Example
203+
///
204+
/// ```no_run
205+
/// use ext_php_rs::types::ZendHashTable;
206+
///
207+
/// let mut ht = ZendHashTable::new();
208+
///
209+
/// ht.insert("test", "hello world");
210+
/// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
211+
/// ```
212+
pub fn get_mut(&self, key: &'_ str) -> Option<&mut Zval> {
213+
let str = CString::new(key).ok()?;
214+
unsafe { zend_hash_str_find(self, str.as_ptr(), key.len() as _).as_mut() }
215+
}
216+
190217
/// Attempts to retrieve a value from the hash table with an index.
191218
///
192219
/// # Parameters
@@ -213,6 +240,32 @@ impl ZendHashTable {
213240
unsafe { zend_hash_index_find(self, key).as_ref() }
214241
}
215242

243+
/// Attempts to retrieve a value from the hash table with an index.
244+
///
245+
/// # Parameters
246+
///
247+
/// * `key` - The key to search for in the hash table.
248+
///
249+
/// # Returns
250+
///
251+
/// * `Some(&Zval)` - A reference to the zval at the position in the hash
252+
/// table.
253+
/// * `None` - No value at the given position was found.
254+
///
255+
/// # Example
256+
///
257+
/// ```no_run
258+
/// use ext_php_rs::types::ZendHashTable;
259+
///
260+
/// let mut ht = ZendHashTable::new();
261+
///
262+
/// ht.push(100);
263+
/// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
264+
/// ```
265+
pub fn get_index_mut(&self, key: u64) -> Option<&mut Zval> {
266+
unsafe { zend_hash_index_find(self, key).as_mut() }
267+
}
268+
216269
/// Attempts to remove a value from the hash table with a string key.
217270
///
218271
/// # Parameters

src/zend/borrow_unchecked.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::binary_slice::{BinarySlice, PackSlice};
2+
3+
#[inline(always)]
4+
pub unsafe fn borrow_unchecked<
5+
'original,
6+
'unbounded,
7+
Ref: BorrowUnchecked<'original, 'unbounded>,
8+
>(
9+
reference: Ref,
10+
) -> Ref::Unbounded {
11+
unsafe { BorrowUnchecked::borrow_unchecked(reference) }
12+
}
13+
14+
#[doc(hidden)]
15+
pub unsafe trait BorrowUnchecked<'original, 'unbounded> {
16+
type Unbounded;
17+
18+
unsafe fn borrow_unchecked(self) -> Self::Unbounded;
19+
}
20+
21+
unsafe impl<'original, 'unbounded, T: 'unbounded> BorrowUnchecked<'original, 'unbounded>
22+
for &'original T
23+
{
24+
type Unbounded = &'unbounded T;
25+
26+
#[inline(always)]
27+
unsafe fn borrow_unchecked(self) -> Self::Unbounded {
28+
unsafe { ::core::mem::transmute(self) }
29+
}
30+
}
31+
32+
unsafe impl<'original, 'unbounded, T: 'unbounded> BorrowUnchecked<'original, 'unbounded>
33+
for &'original mut T
34+
{
35+
type Unbounded = &'unbounded mut T;
36+
37+
#[inline(always)]
38+
unsafe fn borrow_unchecked(self) -> Self::Unbounded {
39+
unsafe { ::core::mem::transmute(self) }
40+
}
41+
}
42+
43+
unsafe impl<'original, 'unbounded, T: 'unbounded + PackSlice> BorrowUnchecked<'original, 'unbounded>
44+
for BinarySlice<'original, T>
45+
{
46+
type Unbounded = BinarySlice<'unbounded, T>;
47+
48+
#[inline(always)]
49+
unsafe fn borrow_unchecked(self) -> Self::Unbounded {
50+
unsafe { ::core::mem::transmute(self) }
51+
}
52+
}

0 commit comments

Comments
 (0)