Skip to content

Commit 56e0657

Browse files
authored
adding functions for interacting with execution context (#221)
1 parent 2e01785 commit 56e0657

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

phper-sys/php_wrapper.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ phper_init_class_entry_handler(zend_class_entry *class_ce, void *argument);
5050
#define IS_VOID 0x1D
5151
#endif
5252

53+
#ifndef ZEND_CALL_MAY_HAVE_UNDEF
54+
#define ZEND_CALL_MAY_HAVE_UNDEF (1 << 26)
55+
#endif
56+
5357
// ==================================================
5458
// zval apis:
5559
// ==================================================
@@ -198,6 +202,10 @@ void phper_zval_str(zval *zv, zend_string *s) {
198202
ZVAL_STR(zv, s);
199203
}
200204

205+
void phper_zval_undef(zval *zv) {
206+
ZVAL_UNDEF(zv);
207+
}
208+
201209
void phper_convert_to_long(zval *op) {
202210
convert_to_long(op);
203211
}
@@ -348,6 +356,10 @@ zval *phper_get_this(zend_execute_data *execute_data) {
348356
return getThis();
349357
}
350358

359+
zend_class_entry *phper_get_called_scope(zend_execute_data *execute_data) {
360+
return zend_get_called_scope(execute_data);
361+
}
362+
351363
size_t phper_zend_object_properties_size(zend_class_entry *ce) {
352364
return zend_object_properties_size(ce);
353365
}
@@ -447,11 +459,39 @@ uint32_t phper_zend_num_args(const zend_execute_data *execute_data) {
447459
return ZEND_NUM_ARGS();
448460
}
449461

462+
uint32_t phper_zend_call_num_args(const zend_execute_data *execute_data) {
463+
return ZEND_CALL_NUM_ARGS(execute_data);
464+
}
465+
466+
void phper_zend_set_call_num_args(zend_execute_data *execute_data, uint32_t num) {
467+
ZEND_CALL_NUM_ARGS(execute_data) = num;
468+
}
469+
470+
uint32_t phper_zend_call_info(zend_execute_data *execute_data) {
471+
return ZEND_CALL_INFO(execute_data);
472+
}
473+
474+
void phper_zend_add_call_flag(zend_execute_data *execute_data, uint32_t flag) {
475+
ZEND_ADD_CALL_FLAG(execute_data, flag);
476+
}
477+
450478
bool phper_zend_get_parameters_array_ex(uint32_t param_count,
451479
zval *argument_array) {
452480
return zend_get_parameters_array_ex(param_count, argument_array) != 0;
453481
}
454482

483+
uint32_t phper_zend_call_may_have_undef() {
484+
return ZEND_CALL_MAY_HAVE_UNDEF;
485+
}
486+
487+
int phper_zend_result_success() {
488+
return SUCCESS;
489+
}
490+
491+
int phper_zend_result_failure() {
492+
return FAILURE;
493+
}
494+
455495
// ==================================================
456496
// memory apis:
457497
// ==================================================

phper/src/functions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,11 @@ impl ZFunc {
629629
}
630630
}
631631

632+
/// Detects if the function is static.
633+
pub fn is_static(&self) -> bool {
634+
unsafe { (self.inner.op_array.fn_flags & ZEND_ACC_STATIC) != 0 }
635+
}
636+
632637
/// Get the type of the function (sys::ZEND_USER_FUNCTION,
633638
/// sys::ZEND_INTERNAL_FUNCTION, or sys::ZEND_EVAL_CODE).
634639
pub fn get_type(&self) -> u32 {

phper/src/values.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use crate::{
1414
arrays::{ZArr, ZArray},
15+
classes::ClassEntry,
1516
errors::ExpectTypeError,
1617
functions::{ZFunc, call_internal},
1718
objects::{StateObject, ZObj, ZObject},
@@ -177,17 +178,23 @@ impl ExecuteData {
177178

178179
/// Gets associated `$this` object if exists.
179180
pub fn get_this(&mut self) -> Option<&ZObj> {
180-
unsafe {
181-
let val = ZVal::from_ptr(phper_get_this(&mut self.inner));
182-
val.as_z_obj()
183-
}
181+
unsafe { ZVal::try_from_ptr(phper_get_this(&mut self.inner))?.as_z_obj() }
184182
}
185183

186184
/// Gets associated mutable `$this` object if exists.
187185
pub fn get_this_mut(&mut self) -> Option<&mut ZObj> {
186+
unsafe { ZVal::try_from_mut_ptr(phper_get_this(&mut self.inner))?.as_mut_z_obj() }
187+
}
188+
189+
/// Gets associated called scope if it exists
190+
pub fn get_called_scope(&mut self) -> Option<&ClassEntry> {
188191
unsafe {
189-
let val = ZVal::from_mut_ptr(phper_get_this(&mut self.inner));
190-
val.as_mut_z_obj()
192+
let ptr = phper_get_called_scope(&mut self.inner);
193+
if ptr.is_null() {
194+
None
195+
} else {
196+
Some(ClassEntry::from_ptr(ptr))
197+
}
191198
}
192199
}
193200

0 commit comments

Comments
 (0)