Skip to content

Commit fd0c825

Browse files
committed
Modify ZVal as_* methods.
1 parent 44c1d4c commit fd0c825

File tree

11 files changed

+160
-149
lines changed

11 files changed

+160
-149
lines changed

phper-sys/php_wrapper.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,27 @@ void phper_zval_str(zval *zv, zend_string *s) {
265265
zend_array *phper_zend_new_array(uint32_t nSize) {
266266
return zend_new_array(nSize);
267267
}
268+
269+
zend_array *phper_zend_array_dup(zend_array *source) {
270+
return zend_array_dup(source);
271+
}
272+
273+
zend_array *phper_z_arr_p(const zval *zv) {
274+
return Z_ARR_P(zv);
275+
}
276+
277+
zend_long phper_z_lval_p(const zval *zv) {
278+
return Z_LVAL_P(zv);
279+
}
280+
281+
double phper_z_dval_p(const zval *zv) {
282+
return Z_DVAL_P(zv);
283+
}
284+
285+
zend_string *phper_z_str_p(const zval *zv) {
286+
return Z_STR_P(zv);
287+
}
288+
289+
zend_resource *phper_z_res_p(const zval *zv) {
290+
return Z_RES_P(zv);
291+
}

phper/src/arrays.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
};
1919
use derive_more::From;
2020
use std::{
21+
borrow::Borrow,
2122
convert::TryInto,
2223
marker::PhantomData,
2324
mem::{forget, zeroed},
@@ -163,14 +164,6 @@ impl ZArr {
163164
}
164165
}
165166

166-
pub fn clone_arr(&self) -> EBox<Self> {
167-
let mut other = Self::new();
168-
unsafe {
169-
zend_hash_copy(other.as_mut_ptr(), self.as_ptr() as *mut _, None);
170-
}
171-
other
172-
}
173-
174167
pub fn iter(&self) -> Iter<'_> {
175168
Iter {
176169
index: 0,
@@ -179,19 +172,29 @@ impl ZArr {
179172
}
180173
}
181174

175+
impl ToOwned for ZArr {
176+
type Owned = ZArray;
177+
178+
fn to_owned(&self) -> Self::Owned {
179+
unsafe {
180+
// TODO The source really immutable?
181+
let dest = phper_zend_array_dup(self.as_ptr() as *mut _);
182+
ZArray::from_raw(dest)
183+
}
184+
}
185+
}
186+
182187
/// Wrapper of [crate::sys::zend_array].
183188
#[repr(transparent)]
184189
pub struct ZArray {
185-
inner: *mut zend_array,
190+
inner: *mut ZArr,
186191
}
187192

188193
impl ZArray {
189194
pub fn new() -> Self {
190195
unsafe {
191196
let ptr = phper_zend_new_array(0);
192-
Self {
193-
inner: ZArr::from_mut_ptr(ptr),
194-
}
197+
Self::from_raw(ptr)
195198
}
196199
}
197200

@@ -224,6 +227,18 @@ impl DerefMut for ZArray {
224227
}
225228
}
226229

230+
impl Borrow<ZArr> for ZArray {
231+
fn borrow(&self) -> &ZArr {
232+
self.deref()
233+
}
234+
}
235+
236+
impl Clone for ZArray {
237+
fn clone(&self) -> Self {
238+
self.deref().to_owned()
239+
}
240+
}
241+
227242
impl Drop for ZArray {
228243
fn drop(&mut self) {
229244
unsafe {
@@ -235,7 +250,7 @@ impl Drop for ZArray {
235250
/// Iter created by [Array::iter].
236251
pub struct Iter<'a> {
237252
index: isize,
238-
array: &'a ZArray,
253+
array: &'a ZArr,
239254
}
240255

241256
impl<'a> Iterator for Iter<'a> {

phper/src/classes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use crate::{
1414
alloc::EBox,
15-
arrays::ZArray,
15+
arrays::{ZArr, ZArray},
1616
errors::{ClassNotFoundError, InitializeObjectError, StateTypeError},
1717
functions::{Argument, Function, FunctionEntity, FunctionEntry, Method},
1818
objects::{ExtendObject, Object},
@@ -89,7 +89,7 @@ impl<T: Send + 'static> DynamicClass<T> {
8989
&mut self, name: impl ToString, vis: Visibility, handler: F, arguments: Vec<Argument>,
9090
) where
9191
F: Fn(&mut Object<T>, &mut [ZVal]) -> R + Send + Sync + 'static,
92-
R: SetVal + 'static,
92+
R: Into<ZVal> + 'static,
9393
{
9494
self.method_entities.push(FunctionEntity::new(
9595
name,
@@ -104,7 +104,7 @@ impl<T: Send + 'static> DynamicClass<T> {
104104
&mut self, name: impl ToString, vis: Visibility, handler: F, arguments: Vec<Argument>,
105105
) where
106106
F: Fn(&mut [ZVal]) -> R + Send + Sync + 'static,
107-
R: SetVal + 'static,
107+
R: Into<ZVal> + 'static,
108108
{
109109
self.method_entities.push(FunctionEntity::new(
110110
name,
@@ -233,7 +233,7 @@ impl<T: 'static> ClassEntry<T> {
233233
pub fn init_object(&self) -> crate::Result<EBox<Object<T>>> {
234234
unsafe {
235235
let ptr = self.as_ptr() as *mut _;
236-
let mut val = ZVal::undef();
236+
let mut val = ZVal::from(());
237237
if !phper_object_init_ex(val.as_mut_ptr(), ptr) {
238238
Err(InitializeObjectError::new(self.get_name().to_str()?.to_owned()).into())
239239
} else {
@@ -251,7 +251,7 @@ impl<T: 'static> ClassEntry<T> {
251251

252252
pub fn has_method(&self, method_name: &str) -> bool {
253253
unsafe {
254-
let function_table = ZArray::from_ptr(&self.inner.function_table).unwrap();
254+
let function_table = ZArr::from_ptr(&self.inner.function_table);
255255
function_table.exists(method_name)
256256
}
257257
}

phper/src/functions.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
strings::{ZStr, ZString},
2323
sys::*,
2424
utils::ensure_end_with_zero,
25-
values::{ExecuteData, SetVal, ZVal},
25+
values::{ExecuteData, ZVal},
2626
};
2727
use std::{
2828
convert::TryInto,
@@ -39,12 +39,12 @@ pub(crate) trait Callable {
3939
pub(crate) struct Function<F, R>(F)
4040
where
4141
F: Fn(&mut [ZVal]) -> R + Send + Sync,
42-
R: SetVal;
42+
R: Into<ZVal>;
4343

4444
impl<F, R> Function<F, R>
4545
where
4646
F: Fn(&mut [ZVal]) -> R + Send + Sync,
47-
R: SetVal,
47+
R: Into<ZVal>,
4848
{
4949
pub fn new(f: F) -> Self {
5050
Self(f)
@@ -54,20 +54,18 @@ where
5454
impl<F, R> Callable for Function<F, R>
5555
where
5656
F: Fn(&mut [ZVal]) -> R + Send + Sync,
57-
R: SetVal,
57+
R: Into<ZVal>,
5858
{
5959
fn call(&self, _: &mut ExecuteData, arguments: &mut [ZVal], return_value: &mut ZVal) {
6060
let r = (self.0)(arguments);
61-
unsafe {
62-
r.set_val(return_value);
63-
}
61+
*return_value = r.into();
6462
}
6563
}
6664

6765
pub(crate) struct Method<F, R, T>
6866
where
6967
F: Fn(&mut Object<T>, &mut [ZVal]) -> R + Send + Sync,
70-
R: SetVal,
68+
R: Into<ZVal>,
7169
{
7270
f: F,
7371
_p0: PhantomData<R>,
@@ -77,7 +75,7 @@ where
7775
impl<F, R, T> Method<F, R, T>
7876
where
7977
F: Fn(&mut Object<T>, &mut [ZVal]) -> R + Send + Sync,
80-
R: SetVal,
78+
R: Into<ZVal>,
8179
{
8280
pub(crate) fn new(f: F) -> Self {
8381
Self {
@@ -91,15 +89,15 @@ where
9189
impl<F, R, T: 'static> Callable for Method<F, R, T>
9290
where
9391
F: Fn(&mut Object<T>, &mut [ZVal]) -> R + Send + Sync,
94-
R: SetVal,
92+
R: Into<ZVal>,
9593
{
9694
fn call(
9795
&self, execute_data: &mut ExecuteData, arguments: &mut [ZVal], return_value: &mut ZVal,
9896
) {
9997
unsafe {
10098
let this = execute_data.get_this::<T>().unwrap();
10199
let r = (self.f)(this, arguments);
102-
r.set_val(return_value);
100+
*return_value = r.into();
103101
}
104102
}
105103
}
@@ -272,7 +270,7 @@ impl ZendFunction {
272270
|ret| unsafe {
273271
let mut fci = zend_fcall_info {
274272
size: size_of::<zend_fcall_info>().try_into().unwrap(),
275-
function_name: ZVal::undef().into_inner(),
273+
function_name: ZVal::from(()).into_inner(),
276274
retval: ret.as_mut_ptr(),
277275
params: arguments.as_mut_ptr().cast(),
278276
object: object_ptr,
@@ -349,7 +347,7 @@ unsafe extern "C" fn invoke(execute_data: *mut zend_execute_data, return_value:
349347
))
350348
})
351349
.map_err(crate::Error::Utf8);
352-
SetVal::set_val(result, return_value);
350+
*return_value = ZVal::from(result);
353351
return;
354352
}
355353

@@ -426,7 +424,7 @@ pub(crate) const fn create_zend_arg_info(
426424
/// }
427425
/// ```
428426
pub fn call(fn_name: &str, arguments: impl AsMut<[ZVal]>) -> crate::Result<EBox<ZVal>> {
429-
let mut func = ZVal::new(fn_name);
427+
let mut func = fn_name.into();
430428
let none: Option<&mut StatelessObject> = None;
431429
call_internal(&mut func, none, arguments)
432430
}
@@ -437,7 +435,7 @@ pub(crate) fn call_internal<T: 'static>(
437435
let func_ptr = func.as_mut_ptr();
438436
let arguments = arguments.as_mut();
439437

440-
let mut object_val = ZVal::undef();
438+
let mut object_val = ZVal::from(());
441439
let mut object_val = object.as_mut().map(|o| unsafe {
442440
phper_zval_obj(object_val.as_mut_ptr(), o.as_mut_ptr());
443441
&mut object_val
@@ -474,7 +472,7 @@ pub(crate) fn call_raw_common<T: 'static>(
474472
call_fn: impl FnOnce(&mut ZVal) -> bool, name_fn: impl FnOnce() -> crate::Result<String>,
475473
object: Option<&mut Object<T>>,
476474
) -> crate::Result<EBox<ZVal>> {
477-
let mut ret = EBox::new(ZVal::undef());
475+
let mut ret = EBox::new(ZVal::from(()));
478476

479477
if call_fn(&mut ret) && !ret.get_type_info().is_undef() {
480478
return Ok(ret);

phper/src/modules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
ini::Ini,
1818
sys::*,
1919
utils::ensure_end_with_zero,
20-
values::{SetVal, ZVal},
20+
values::ZVal,
2121
};
2222
use std::{
2323
mem::{replace, size_of, zeroed},
@@ -148,7 +148,7 @@ impl Module {
148148
pub fn add_function<F, R>(&mut self, name: impl ToString, handler: F, arguments: Vec<Argument>)
149149
where
150150
F: Fn(&mut [ZVal]) -> R + Send + Sync + 'static,
151-
R: SetVal + 'static,
151+
R: Into<ZVal> + 'static,
152152
{
153153
self.function_entities.push(FunctionEntity::new(
154154
name,

phper/src/objects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ impl<T: 'static> Object<T> {
196196
pub fn call(
197197
&mut self, method_name: &str, arguments: impl AsMut<[ZVal]>,
198198
) -> crate::Result<EBox<ZVal>> {
199-
let mut method = ZVal::new(method_name);
199+
let mut method = method_name.into();
200200

201201
unsafe {
202-
let mut val = ZVal::undef();
202+
let mut val = ZVal::from(());
203203
phper_zval_obj(val.as_mut_ptr(), self.as_mut_ptr());
204204
call_internal(&mut method, Some(self), arguments)
205205
}

phper/src/resources.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ use crate::sys::*;
1414

1515
/// Wrapper of [crate::sys::zend_resource].
1616
#[repr(transparent)]
17-
pub struct Resource {
17+
pub struct ZRes {
1818
inner: zend_resource,
1919
}
2020

21-
impl Resource {
22-
/// # Safety
21+
impl ZRes {
22+
pub unsafe fn from_ptr<'a>(ptr: *const zend_resource) -> &'a Self {
23+
(ptr as *const Self)
24+
.as_ref()
25+
.expect("ptr should not be null")
26+
}
27+
2328
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_resource) -> &'a mut Self {
2429
(ptr as *mut Self).as_mut().expect("ptr should not be null")
2530
}

phper/src/strings.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ impl ZString {
9898
s.len().try_into().unwrap(),
9999
false.into(),
100100
);
101-
Self {
102-
inner: ZStr::from_mut_ptr(ptr.cast()),
103-
}
101+
Self::from_raw(ptr)
104102
}
105103
}
106104

0 commit comments

Comments
 (0)