Skip to content

Commit 13e514d

Browse files
committed
Refactor Array to Val.
1 parent 9e00b63 commit 13e514d

File tree

10 files changed

+81
-66
lines changed

10 files changed

+81
-66
lines changed

examples/hello/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use phper::{
22
arrays::Array,
3-
classes::{StdClass, This},
3+
classes::StdClass,
44
functions::Argument,
55
ini::Policy,
66
modules::{Module, ModuleArgs},
7+
objects::This,
78
php_get_module,
89
values::{SetVal, Val},
910
};

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ void phper_zend_string_release(zend_string *s) {
7777
void phper_zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData) {
7878
zend_hash_str_update(ht, key, len, pData);
7979
}
80+
81+
void phper_array_init(zval *arg) {
82+
array_init(arg);
83+
}

phper-sys/php_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void phper_zend_string_release(zend_string *s);
3434

3535
void phper_zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
3636

37+
void phper_array_init(zval *arg);
3738

3839

3940
#endif //PHPER_PHP_WRAPPER_H

phper/src/arrays.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ impl Array {
1212
pub fn new() -> Self {
1313
let mut inner = Box::new(unsafe { zeroed::<zend_array>() });
1414
unsafe {
15-
// TODO should destroy in module shutdown hook.
1615
_zend_hash_init(&mut *inner, 0, None, true.into());
1716
}
1817
Self { inner }
@@ -59,10 +58,10 @@ impl AsMut<zend_array> for Array {
5958
}
6059
}
6160

62-
// impl Drop for Array {
63-
// fn drop(&mut self) {
64-
// unsafe {
65-
// zend_hash_graceful_destroy(&mut *self.inner);
66-
// }
67-
// }
68-
// }
61+
impl Drop for Array {
62+
fn drop(&mut self) {
63+
unsafe {
64+
zend_hash_graceful_destroy(&mut *self.inner);
65+
}
66+
}
67+
}

phper/src/classes.rs

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
use crate::{
2-
functions::{Argument, Callable, FunctionEntity, FunctionEntry, Method},
3-
sys::*,
4-
values::{SetVal, Val},
5-
};
6-
use once_cell::sync::OnceCell;
71
use std::{
82
mem::zeroed,
93
os::raw::c_int,
104
ptr::null_mut,
115
sync::atomic::{AtomicPtr, Ordering},
126
};
137

8+
use once_cell::sync::OnceCell;
9+
10+
use crate::{
11+
functions::{Argument, Callable, FunctionEntity, FunctionEntry, Method},
12+
sys::*,
13+
values::{SetVal, Val},
14+
};
15+
1416
pub trait Class: Send + Sync {
1517
fn methods(&mut self) -> &mut [FunctionEntity];
1618
fn properties(&mut self) -> &mut [PropertyEntity];
@@ -161,51 +163,6 @@ impl ClassEntity {
161163
}
162164
}
163165

164-
pub struct This {
165-
val: *mut Val,
166-
class: *mut ClassEntry,
167-
}
168-
169-
impl This {
170-
pub(crate) fn new<'a>(val: *mut Val, class: *mut ClassEntry) -> This {
171-
assert!(!val.is_null());
172-
assert!(!class.is_null());
173-
Self { val, class }
174-
}
175-
176-
pub fn get_property(&self, name: impl AsRef<str>) -> &mut Val {
177-
let name = name.as_ref();
178-
179-
let prop = unsafe {
180-
#[cfg(phper_major_version = "8")]
181-
{
182-
zend_read_property(
183-
self.class as *mut _,
184-
(*self.val).inner.value.obj,
185-
name.as_ptr().cast(),
186-
name.len(),
187-
false.into(),
188-
null_mut(),
189-
)
190-
}
191-
192-
#[cfg(phper_major_version = "7")]
193-
{
194-
zend_read_property(
195-
self.class as *mut _,
196-
self.val as *mut _,
197-
name.as_ptr().cast(),
198-
name.len(),
199-
false.into(),
200-
null_mut(),
201-
)
202-
}
203-
};
204-
205-
unsafe { Val::from_mut(prop) }
206-
}
207-
}
208-
209166
pub struct PropertyEntity {
210167
name: String,
211168
value: Box<dyn SetVal + Send + Sync>,

phper/src/functions.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
use crate::{
2-
classes::{ClassEntry, This},
3-
sys::*,
4-
values::{ExecuteData, SetVal, Val},
5-
};
61
use std::{
72
mem::zeroed,
83
os::raw::c_char,
94
ptr::null,
105
sync::atomic::{AtomicPtr, Ordering},
116
};
127

8+
use crate::{
9+
classes::ClassEntry,
10+
objects::This,
11+
sys::*,
12+
values::{ExecuteData, SetVal, Val},
13+
};
14+
1315
pub trait Function: Send + Sync {
1416
fn call(&self, arguments: &mut [Val], return_value: &mut Val);
1517
}

phper/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub mod functions;
130130
pub mod ini;
131131
pub mod logs;
132132
pub mod modules;
133+
pub mod objects;
133134
pub mod strings;
134135
mod utils;
135136
pub mod values;

phper/src/modules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ use std::{
1919

2020
static GLOBAL_MODULE: Lazy<RwLock<Module>> = Lazy::new(Default::default);
2121

22+
#[doc(hidden)]
2223
pub fn read_global_module<R>(f: impl FnOnce(&Module) -> R) -> R {
2324
let module = (&*GLOBAL_MODULE).read().expect("get write lock failed");
2425
f(&module)
2526
}
2627

28+
#[doc(hidden)]
2729
pub fn write_global_module<R>(f: impl FnOnce(&mut Module) -> R) -> R {
2830
let mut module = (&*GLOBAL_MODULE).write().expect("get write lock failed");
2931
f(&mut module)

phper/src/objects.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::{classes::ClassEntry, sys::*, values::Val};
2+
use std::ptr::null_mut;
3+
4+
pub struct This {
5+
val: *mut Val,
6+
class: *mut ClassEntry,
7+
}
8+
9+
impl This {
10+
pub(crate) fn new<'a>(val: *mut Val, class: *mut ClassEntry) -> This {
11+
assert!(!val.is_null());
12+
assert!(!class.is_null());
13+
Self { val, class }
14+
}
15+
16+
pub fn get_property(&self, name: impl AsRef<str>) -> &mut Val {
17+
let name = name.as_ref();
18+
19+
let prop = unsafe {
20+
#[cfg(phper_major_version = "8")]
21+
{
22+
zend_read_property(
23+
self.class as *mut _,
24+
(*self.val).inner.value.obj,
25+
name.as_ptr().cast(),
26+
name.len(),
27+
false.into(),
28+
null_mut(),
29+
)
30+
}
31+
32+
#[cfg(phper_major_version = "7")]
33+
{
34+
zend_read_property(
35+
self.class as *mut _,
36+
self.val as *mut _,
37+
name.as_ptr().cast(),
38+
name.len(),
39+
false.into(),
40+
null_mut(),
41+
)
42+
}
43+
};
44+
45+
unsafe { Val::from_mut(prop) }
46+
}
47+
}

phper/src/values.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ impl SetVal for String {
195195
impl SetVal for Array {
196196
fn set_val(&self, val: &mut Val) {
197197
unsafe {
198-
phper_zval_arr(&mut val.inner, self.as_ptr() as *mut _);
198+
phper_array_init(val.as_mut());
199+
zend_hash_copy((*val.as_mut()).value.arr, self.as_ptr() as *mut _, None);
199200
}
200201
}
201202
}

0 commit comments

Comments
 (0)