Skip to content

Commit b5adbb9

Browse files
committed
Merge branch 'streams-api' of github.com:davidcole1340/ext-php-rs into streams-api
2 parents 87ab718 + 7803a45 commit b5adbb9

File tree

9 files changed

+88
-10
lines changed

9 files changed

+88
-10
lines changed

allowed_bindings.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ bind! {
217217
_ZEND_TYPE_NULLABLE_BIT,
218218
ts_rsrc_id,
219219
_ZEND_TYPE_NAME_BIT,
220+
ZEND_INTERNAL_FUNCTION,
221+
ZEND_USER_FUNCTION,
222+
ZEND_EVAL_CODE,
220223
zval_ptr_dtor,
221224
zend_refcounted_h,
222225
zend_is_true,
@@ -252,6 +255,7 @@ bind! {
252255
sapi_globals_offset,
253256
php_file_globals,
254257
file_globals,
258+
file_globals_id,
255259
TRACK_VARS_POST,
256260
TRACK_VARS_GET,
257261
TRACK_VARS_COOKIE,
@@ -270,5 +274,7 @@ bind! {
270274
php_unregister_url_stream_wrapper_volatile,
271275
php_register_url_stream_wrapper_volatile,
272276
php_stream_wrapper,
273-
php_stream_stdio_ops
277+
php_stream_stdio_ops,
278+
zend_atomic_bool_store,
279+
zend_interrupt_function
274280
}

docsrs_bindings.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ pub const ZEND_ACC_GENERATOR: u32 = 16777216;
171171
pub const ZEND_ACC_DONE_PASS_TWO: u32 = 33554432;
172172
pub const ZEND_ACC_HEAP_RT_CACHE: u32 = 67108864;
173173
pub const ZEND_ACC_STRICT_TYPES: u32 = 2147483648;
174+
pub const ZEND_INTERNAL_FUNCTION: u32 = 1;
175+
pub const ZEND_USER_FUNCTION: u32 = 2;
176+
pub const ZEND_EVAL_CODE: u32 = 4;
174177
pub const ZEND_ISEMPTY: u32 = 1;
175178
pub const _ZEND_SEND_MODE_SHIFT: u32 = 25;
176179
pub const _ZEND_IS_VARIADIC_BIT: u32 = 134217728;
@@ -901,6 +904,10 @@ pub struct _zend_class_entry__bindgen_ty_4__bindgen_ty_2 {
901904
pub builtin_functions: *const _zend_function_entry,
902905
pub module: *mut _zend_module_entry,
903906
}
907+
extern "C" {
908+
pub static mut zend_interrupt_function:
909+
::std::option::Option<unsafe extern "C" fn(execute_data: *mut zend_execute_data)>;
910+
}
904911
extern "C" {
905912
pub static mut zend_standard_class_def: *mut zend_class_entry;
906913
}
@@ -1284,6 +1291,9 @@ pub struct zend_atomic_bool_s {
12841291
pub value: u8,
12851292
}
12861293
pub type zend_atomic_bool = zend_atomic_bool_s;
1294+
extern "C" {
1295+
pub fn zend_atomic_bool_store(obj: *mut zend_atomic_bool, desired: bool);
1296+
}
12871297
#[repr(C)]
12881298
#[derive(Debug, Copy, Clone)]
12891299
pub struct _zend_stack {

src/convert.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,13 @@ impl<T: IntoZval + Clone> IntoZvalDyn for T {
218218
Self::TYPE
219219
}
220220
}
221+
222+
impl IntoZvalDyn for Zval {
223+
fn as_zval(&self, _persistent: bool) -> Result<Zval> {
224+
Ok(self.shallow_clone())
225+
}
226+
227+
fn get_type(&self) -> DataType {
228+
self.get_type()
229+
}
230+
}

src/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ extern "C" {
2828
pub fn ext_php_rs_executor_globals() -> *mut zend_executor_globals;
2929
pub fn ext_php_rs_process_globals() -> *mut php_core_globals;
3030
pub fn ext_php_rs_sapi_globals() -> *mut sapi_globals_struct;
31+
pub fn ext_php_rs_file_globals() -> *mut php_file_globals;
3132
}
3233

3334
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

src/flags.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use crate::ffi::{
2020
ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
2121
ZEND_ACC_RESOLVED_PARENT, ZEND_ACC_RETURN_REFERENCE, ZEND_ACC_STATIC, ZEND_ACC_STRICT_TYPES,
2222
ZEND_ACC_TOP_LEVEL, ZEND_ACC_TRAIT, ZEND_ACC_TRAIT_CLONE, ZEND_ACC_UNRESOLVED_VARIANCE,
23-
ZEND_ACC_USES_THIS, ZEND_ACC_USE_GUARDS, ZEND_ACC_VARIADIC, ZEND_HAS_STATIC_IN_METHODS,
24-
Z_TYPE_FLAGS_SHIFT, _IS_BOOL,
23+
ZEND_ACC_USES_THIS, ZEND_ACC_USE_GUARDS, ZEND_ACC_VARIADIC, ZEND_EVAL_CODE,
24+
ZEND_HAS_STATIC_IN_METHODS, ZEND_INTERNAL_FUNCTION, ZEND_USER_FUNCTION, Z_TYPE_FLAGS_SHIFT,
25+
_IS_BOOL,
2526
};
2627

2728
use std::{convert::TryFrom, fmt::Display};
@@ -193,6 +194,24 @@ bitflags! {
193194
const UserDeprecated = E_USER_DEPRECATED;
194195
}
195196
}
197+
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
198+
pub enum FunctionType {
199+
Internal,
200+
User,
201+
Eval,
202+
}
203+
204+
impl From<u8> for FunctionType {
205+
#[allow(clippy::bad_bit_mask)]
206+
fn from(value: u8) -> Self {
207+
match value as _ {
208+
ZEND_INTERNAL_FUNCTION => Self::Internal,
209+
ZEND_USER_FUNCTION => Self::User,
210+
ZEND_EVAL_CODE => Self::Eval,
211+
_ => panic!("Unknown function type: {}", value),
212+
}
213+
}
214+
}
196215

197216
/// Valid data types for PHP.
198217
#[repr(C, u8)]

src/wrapper.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ sapi_globals_struct *ext_php_rs_sapi_globals() {
6464
return &sapi_globals;
6565
#endif
6666
}
67+
68+
69+
php_file_globals *ext_php_rs_file_globals() {
70+
#ifdef ZTS
71+
return TSRMG_FAST_BULK(file_globals_id, php_file_globals *);
72+
#else
73+
return &file_globals;
74+
#endif
75+
}

src/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ void ext_php_rs_zend_object_release(zend_object *obj);
3636
zend_executor_globals *ext_php_rs_executor_globals();
3737
php_core_globals *ext_php_rs_process_globals();
3838
sapi_globals_struct *ext_php_rs_sapi_globals();
39+
php_file_globals *ext_php_rs_file_globals();

src/zend/class.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ impl ClassEntry {
7979
Self::try_find(name.as_str().ok()?)
8080
}
8181
}
82+
83+
pub fn name(&self) -> Option<&str> {
84+
unsafe { self.name.as_ref().and_then(|s| s.as_str().ok()) }
85+
}
8286
}
8387

8488
impl PartialEq for ClassEntry {

src/zend/globals.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use std::str;
77
use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard};
88

99
use crate::boxed::ZBox;
10+
#[cfg(php82)]
11+
use crate::ffi::zend_atomic_bool_store;
1012
use crate::ffi::{
11-
_zend_executor_globals, ext_php_rs_executor_globals, ext_php_rs_process_globals,
12-
ext_php_rs_sapi_globals, file_globals, php_core_globals, php_file_globals, sapi_globals_struct,
13-
sapi_header_struct, sapi_headers_struct, sapi_request_info, zend_is_auto_global,
14-
TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET, TRACK_VARS_POST,
15-
TRACK_VARS_REQUEST, TRACK_VARS_SERVER,
13+
_zend_executor_globals, ext_php_rs_executor_globals, ext_php_rs_file_globals,
14+
ext_php_rs_process_globals, ext_php_rs_sapi_globals, php_core_globals, php_file_globals,
15+
sapi_globals_struct, sapi_header_struct, sapi_headers_struct, sapi_request_info,
16+
zend_is_auto_global, TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET,
17+
TRACK_VARS_POST, TRACK_VARS_REQUEST, TRACK_VARS_SERVER,
1618
};
1719

1820
use crate::types::{ZendHashTable, ZendObject, ZendStr};
@@ -80,6 +82,21 @@ impl ExecutorGlobals {
8082
// SAFETY: `as_mut` checks for null.
8183
Some(unsafe { ZBox::from_raw(exception_ptr.as_mut()?) })
8284
}
85+
86+
/// Request an interrupt of the PHP VM. This will call the registered
87+
/// interrupt handler function.
88+
/// set with [`crate::ffi::zend_interrupt_function`].
89+
pub fn request_interrupt(&mut self) {
90+
cfg_if::cfg_if! {
91+
if #[cfg(php82)] {
92+
unsafe {
93+
zend_atomic_bool_store(&mut self.vm_interrupt, true);
94+
}
95+
} else {
96+
self.vm_interrupt = true;
97+
}
98+
}
99+
}
83100
}
84101

85102
/// Stores global variables used in the PHP executor.
@@ -378,7 +395,8 @@ impl FileGlobals {
378395
pub fn get() -> GlobalReadGuard<Self> {
379396
// SAFETY: PHP executor globals are statically declared therefore should never
380397
// return an invalid pointer.
381-
let globals = unsafe { &file_globals };
398+
let globals = unsafe { ext_php_rs_file_globals().as_ref() }
399+
.expect("Static file globals were invalid");
382400
let guard = FILE_GLOBALS_LOCK.read();
383401
GlobalReadGuard { globals, guard }
384402
}
@@ -393,7 +411,7 @@ impl FileGlobals {
393411
pub fn get_mut() -> GlobalWriteGuard<Self> {
394412
// SAFETY: PHP executor globals are statically declared therefore should never
395413
// return an invalid pointer.
396-
let globals = unsafe { &mut file_globals };
414+
let globals = unsafe { &mut *ext_php_rs_file_globals() };
397415
let guard = SAPI_GLOBALS_LOCK.write();
398416
GlobalWriteGuard { globals, guard }
399417
}

0 commit comments

Comments
 (0)