Skip to content

Commit 941224b

Browse files
authored
Add references support, and rename ZendFunction. (#91)
1 parent de67662 commit 941224b

File tree

14 files changed

+311
-154
lines changed

14 files changed

+311
-154
lines changed

phper-sys/php_wrapper.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ zend_array *phper_z_arr_p(const zval *zv) {
308308
return Z_ARR_P(zv);
309309
}
310310

311-
zend_long phper_z_lval_p(const zval *zv) {
312-
return Z_LVAL_P(zv);
311+
zend_long *phper_z_lval_p(zval *zv) {
312+
return &(Z_LVAL_P(zv));
313313
}
314314

315-
double phper_z_dval_p(const zval *zv) {
316-
return Z_DVAL_P(zv);
315+
double *phper_z_dval_p(zval *zv) {
316+
return &(Z_DVAL_P(zv));
317317
}
318318

319319
zend_string *phper_z_str_p(const zval *zv) {
@@ -324,6 +324,10 @@ zend_resource *phper_z_res_p(const zval *zv) {
324324
return Z_RES_P(zv);
325325
}
326326

327+
zend_reference *phper_z_ref_p(const zval *zv) {
328+
return Z_REF_P(zv);
329+
}
330+
327331
zend_string *phper_zend_string_copy(zend_string *s) {
328332
return zend_string_copy(s);
329333
}
@@ -411,3 +415,21 @@ void phper_zend_hash_foreach_key_val(zend_array *array,
411415
}
412416
ZEND_HASH_FOREACH_END();
413417
}
418+
419+
zend_internal_arg_info
420+
phper_zend_begin_arg_info_ex(bool return_reference,
421+
uintptr_t required_num_args) {
422+
#define static
423+
#define const
424+
ZEND_BEGIN_ARG_INFO_EX(info, 0, return_reference, required_num_args)
425+
ZEND_END_ARG_INFO()
426+
return info[0];
427+
#undef static
428+
#undef const
429+
}
430+
431+
zend_internal_arg_info phper_zend_arg_info(bool pass_by_ref, const char *name) {
432+
zend_internal_arg_info info[] = {ZEND_ARG_INFO(pass_by_ref, )};
433+
info[0].name = name;
434+
return info[0];
435+
}

phper/src/functions.rs

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use std::{
2828
ffi::{CStr, CString},
2929
marker::PhantomData,
3030
mem::{size_of, transmute, zeroed},
31-
os::raw::c_char,
3231
ptr::null_mut,
3332
rc::Rc,
3433
};
@@ -137,15 +136,12 @@ impl FunctionEntry {
137136
let mut infos = Vec::new();
138137

139138
let require_arg_count = arguments.iter().filter(|arg| arg.required).count();
140-
infos.push(create_zend_arg_info(
141-
require_arg_count as *const c_char,
142-
false,
143-
));
139+
infos.push(phper_zend_begin_arg_info_ex(false, require_arg_count));
144140

145141
for arg in arguments {
146-
infos.push(create_zend_arg_info(
147-
arg.name.as_ptr().cast(),
142+
infos.push(phper_zend_arg_info(
148143
arg.pass_by_ref,
144+
arg.name.as_ptr().cast(),
149145
));
150146
}
151147

@@ -287,12 +283,12 @@ impl Argument {
287283
}
288284

289285
#[repr(transparent)]
290-
pub struct ZendFunction {
286+
pub struct ZendFunc {
291287
inner: zend_function,
292288
}
293289

294-
impl ZendFunction {
295-
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_function) -> &'a mut ZendFunction {
290+
impl ZendFunc {
291+
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_function) -> &'a mut ZendFunc {
296292
let ptr = ptr as *mut Self;
297293
ptr.as_mut().expect("ptr shouldn't be null")
298294
}
@@ -446,55 +442,6 @@ unsafe extern "C" fn invoke(execute_data: *mut zend_execute_data, return_value:
446442
handler.call(execute_data, transmute(arguments), return_value);
447443
}
448444

449-
pub(crate) const fn create_zend_arg_info(
450-
name: *const c_char, _pass_by_ref: bool,
451-
) -> zend_internal_arg_info {
452-
#[cfg(phper_major_version = "8")]
453-
{
454-
zend_internal_arg_info {
455-
name,
456-
type_: zend_type {
457-
ptr: null_mut(),
458-
type_mask: 0,
459-
},
460-
default_value: null_mut(),
461-
}
462-
}
463-
464-
#[cfg(all(
465-
phper_major_version = "7",
466-
any(
467-
phper_minor_version = "4",
468-
phper_minor_version = "3",
469-
phper_minor_version = "2",
470-
)
471-
))]
472-
{
473-
#[allow(clippy::unnecessary_cast)]
474-
zend_internal_arg_info {
475-
name,
476-
type_: 0 as crate::sys::zend_type,
477-
pass_by_reference: _pass_by_ref as zend_uchar,
478-
is_variadic: 0,
479-
}
480-
}
481-
482-
#[cfg(all(
483-
phper_major_version = "7",
484-
any(phper_minor_version = "1", phper_minor_version = "0")
485-
))]
486-
{
487-
zend_internal_arg_info {
488-
name,
489-
class_name: std::ptr::null(),
490-
type_hint: 0,
491-
allow_null: 0,
492-
pass_by_reference: _pass_by_ref as zend_uchar,
493-
is_variadic: 0,
494-
}
495-
}
496-
}
497-
498445
/// Call user function by name.
499446
///
500447
/// # Examples

phper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod ini;
2323
pub mod modules;
2424
pub mod objects;
2525
pub mod output;
26+
pub mod references;
2627
pub mod resources;
2728
pub mod strings;
2829
pub mod types;

phper/src/objects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use crate::{
1414
alloc::EBox,
1515
classes::ClassEntry,
16-
functions::{call_internal, ZendFunction},
16+
functions::{call_internal, ZendFunc},
1717
sys::*,
1818
values::ZVal,
1919
};
@@ -231,7 +231,7 @@ impl ZObj {
231231
if f.is_null() {
232232
Ok(false)
233233
} else {
234-
let zend_fn = ZendFunction::from_mut_ptr(f);
234+
let zend_fn = ZendFunc::from_mut_ptr(f);
235235
zend_fn.call(Some(self), arguments)?;
236236
Ok(true)
237237
}

phper/src/references.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2022 PHPER Framework Team
2+
// PHPER is licensed under Mulan PSL v2.
3+
// You can use this software according to the terms and conditions of the Mulan
4+
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
5+
// http://license.coscl.org.cn/MulanPSL2
6+
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
7+
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
// See the Mulan PSL v2 for more details.
10+
11+
//! Apis relate to [crate::sys::zend_resource].
12+
13+
use crate::{sys::*, values::ZVal};
14+
15+
/// Wrapper of [crate::sys::zend_resource].
16+
#[repr(transparent)]
17+
pub struct ZRef {
18+
inner: zend_reference,
19+
}
20+
21+
impl ZRef {
22+
/// # Safety
23+
///
24+
/// Create from raw pointer.
25+
pub unsafe fn from_ptr<'a>(ptr: *const zend_reference) -> &'a Self {
26+
(ptr as *const Self)
27+
.as_ref()
28+
.expect("ptr should not be null")
29+
}
30+
31+
/// # Safety
32+
///
33+
/// Create from raw pointer.
34+
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_reference) -> Option<&'a Self> {
35+
(ptr as *const Self).as_ref()
36+
}
37+
38+
/// # Safety
39+
///
40+
/// Create from raw pointer.
41+
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_reference) -> &'a mut Self {
42+
(ptr as *mut Self).as_mut().expect("ptr should not be null")
43+
}
44+
45+
/// # Safety
46+
///
47+
/// Create from raw pointer.
48+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_reference) -> Option<&'a mut Self> {
49+
(ptr as *mut Self).as_mut()
50+
}
51+
52+
pub const fn as_ptr(&self) -> *const zend_reference {
53+
&self.inner
54+
}
55+
56+
#[inline]
57+
pub fn as_mut_ptr(&mut self) -> *mut zend_reference {
58+
&mut self.inner
59+
}
60+
61+
pub fn val(&self) -> &ZVal {
62+
unsafe { ZVal::from_ptr(&self.inner.val) }
63+
}
64+
65+
pub fn val_mut(&mut self) -> &mut ZVal {
66+
unsafe { ZVal::from_mut_ptr(&mut self.inner.val) }
67+
}
68+
}

0 commit comments

Comments
 (0)