Skip to content

Commit 8bbe7db

Browse files
committed
Modify Array to EBox<Array>.
1 parent 8594092 commit 8bbe7db

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

examples/hello/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use phper::{
2-
alloc::EBox,
32
arrays::Array,
43
classes::DynamicClass,
54
functions::Argument,
@@ -57,7 +56,7 @@ pub fn get_module() -> Module {
5756
let hello_description = Val::new(Module::get_str_ini("hello.description"));
5857
arr.insert("hello.description", hello_description);
5958

60-
EBox::new(arr)
59+
arr
6160
},
6261
vec![],
6362
);

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,7 @@ zend_string *phper_get_function_or_method_name(const zend_function *func) {
125125
return func->common.function_name ? zend_string_copy(func->common.function_name) : zend_string_init("main", sizeof("main") - 1, 0);
126126
#endif
127127
}
128+
129+
void phper_zval_ptr_dtor(zval *pDest) {
130+
ZVAL_PTR_DTOR(pDest);
131+
}

phper-sys/php_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ void phper_zval_obj(zval *z, zend_object *o);
4242

4343
zend_string *phper_get_function_or_method_name(const zend_function *func);
4444

45+
void phper_zval_ptr_dtor(zval *pDest);
46+
4547
#endif //PHPER_PHP_WRAPPER_H

phper/src/arrays.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ pub struct Array {
3232
}
3333

3434
impl Array {
35-
// New array in the stack.
36-
pub fn new() -> Self {
35+
pub fn new() -> EBox<Self> {
3736
unsafe {
38-
let mut array = zeroed::<Array>();
39-
let dtor = {
40-
#[cfg(phper_major_version = "8")]
41-
{
42-
zval_ptr_dtor
43-
}
44-
#[cfg(phper_major_version = "7")]
45-
{
46-
_zval_ptr_dtor
47-
}
48-
};
49-
_zend_hash_init(array.as_mut_ptr(), 0, Some(dtor), false.into());
37+
let mut array = EBox::new(zeroed::<Array>());
38+
_zend_hash_init(
39+
array.as_mut_ptr(),
40+
0,
41+
Some(phper_zval_ptr_dtor),
42+
false.into(),
43+
);
5044
array
5145
}
5246
}
@@ -105,6 +99,14 @@ impl Array {
10599
pub fn len(&mut self) -> usize {
106100
unsafe { zend_array_count(&mut self.inner) as usize }
107101
}
102+
103+
pub fn clone(&self) -> EBox<Self> {
104+
let mut other = Self::new();
105+
unsafe {
106+
zend_hash_copy(other.as_mut_ptr(), self.as_ptr() as *mut _, None);
107+
}
108+
other
109+
}
108110
}
109111

110112
impl EAllocatable for Array {
@@ -117,18 +119,6 @@ impl EAllocatable for Array {
117119

118120
impl Drop for Array {
119121
fn drop(&mut self) {
120-
unsafe {
121-
zend_hash_destroy(&mut self.inner);
122-
}
123-
}
124-
}
125-
126-
impl Clone for Array {
127-
fn clone(&self) -> Self {
128-
let mut other = Self::new();
129-
unsafe {
130-
zend_hash_copy(other.as_mut_ptr(), self.as_ptr() as *mut _, None);
131-
}
132-
other
122+
unreachable!("Allocation on the stack is not allowed")
133123
}
134124
}

tests/integration/src/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn integrate_arguments(module: &mut Module) {
4646
let mut a = a.clone();
4747
a.insert("a", Val::new(1));
4848
a.insert("foo", Val::new("bar"));
49-
Ok(EBox::new(a))
49+
Ok(a)
5050
},
5151
vec![Argument::by_val("a")],
5252
);

tests/integration/src/arrays.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn integrate(module: &mut Module) {
99
let foo = a1.get("foo").unwrap();
1010
let foo = foo.as_string()?;
1111

12-
let mut a2 = EBox::new(Array::new());
12+
let mut a2 = Array::new();
1313
a2.insert("bar", Val::new("BAR"));
1414
let bar = a2.get("bar").unwrap();
1515
let bar = bar.as_string()?;
@@ -33,7 +33,7 @@ pub fn integrate(module: &mut Module) {
3333
let mut arr = Array::new();
3434
arr.insert(0, Val::new(0));
3535
arr.insert(1, Val::new(1));
36-
EBox::new(arr)
36+
arr
3737
}),
3838
);
3939
a.insert(

tests/integration/src/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn integration_values_return_array(_: &mut [Val]) -> EBox<Array> {
184184
let mut arr = Array::new();
185185
arr.insert("a", Val::new(1));
186186
arr.insert("b", Val::new("foo"));
187-
EBox::new(arr)
187+
arr
188188
}
189189

190190
fn integration_values_return_object(_: &mut [Val]) -> EBox<Object<()>> {

0 commit comments

Comments
 (0)