Skip to content

Commit 4605749

Browse files
committed
Chanage trait SetVal api.
1 parent 4c2f0f2 commit 4605749

File tree

5 files changed

+47
-59
lines changed

5 files changed

+47
-59
lines changed

examples/hello/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn get_module(module: &mut Module) {
7575
"setFoo",
7676
|this: &mut Object, arguments: &mut [Val]| {
7777
let prop = this.get_property("foo");
78-
prop.set(&arguments[0]);
78+
prop.set(&mut arguments[0]);
7979
},
8080
vec![Argument::by_val("foo")],
8181
);

phper/src/arrays.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,59 @@
11
use crate::{sys::*, values::Val};
2-
use std::{
3-
mem::zeroed,
4-
ops::{Deref, DerefMut},
5-
};
2+
use std::mem::size_of;
63

74
pub struct Array {
8-
inner: Box<zend_array>,
5+
inner: *mut zend_array,
6+
leak: bool,
97
}
108

119
impl Array {
1210
pub fn new() -> Self {
13-
let mut inner = Box::new(unsafe { zeroed::<zend_array>() });
1411
unsafe {
15-
_zend_hash_init(&mut *inner, 0, None, true.into());
12+
let inner = _emalloc(size_of::<zend_array>()).cast();
13+
_zend_hash_init(inner, 0, None, true.into());
14+
Self { inner, leak: false }
1615
}
17-
Self { inner }
1816
}
1917

2018
pub fn as_ptr(&self) -> *const zend_array {
21-
self.inner.deref()
19+
self.inner
20+
}
21+
22+
pub fn as_mut_ptr(&mut self) -> *mut zend_array {
23+
self.inner
2224
}
2325

2426
pub fn insert(&mut self, key: impl AsRef<str>, value: &mut Val) {
2527
let key = key.as_ref();
2628
unsafe {
27-
phper_zend_hash_str_update(
28-
self.inner.deref_mut(),
29-
key.as_ptr().cast(),
30-
key.len(),
31-
value.as_mut(),
32-
);
29+
phper_zend_hash_str_update(self.inner, key.as_ptr().cast(), key.len(), value.as_mut());
3330
}
3431
}
3532

3633
pub fn get(&mut self, key: impl AsRef<str>) -> &mut Val {
3734
let key = key.as_ref();
3835
unsafe {
39-
let value = zend_hash_str_find(&mut *self.inner, key.as_ptr().cast(), key.len());
36+
let value = zend_hash_str_find(self.inner, key.as_ptr().cast(), key.len());
4037
Val::from_mut(value)
4138
}
4239
}
4340

4441
pub fn len(&mut self) -> usize {
45-
unsafe { zend_array_count(&mut *self.inner) as usize }
46-
}
47-
}
48-
49-
impl AsRef<zend_array> for Array {
50-
fn as_ref(&self) -> &zend_array {
51-
self.inner.deref()
42+
unsafe { zend_array_count(self.inner) as usize }
5243
}
53-
}
5444

55-
impl AsMut<zend_array> for Array {
56-
fn as_mut(&mut self) -> &mut zend_array {
57-
self.inner.deref_mut()
45+
pub(crate) fn leak(&mut self) -> &mut bool {
46+
&mut self.leak
5847
}
5948
}
6049

6150
impl Drop for Array {
6251
fn drop(&mut self) {
6352
unsafe {
64-
zend_hash_graceful_destroy(&mut *self.inner);
53+
if !self.leak {
54+
zend_hash_destroy(self.inner);
55+
_efree(self.inner.cast());
56+
}
6557
}
6658
}
6759
}

phper/src/classes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl ClassEntity {
137137
let properties = self.class.properties();
138138
for property in properties {
139139
let mut val = Val::null();
140-
val.set(&property.value);
140+
val.set(&mut property.value);
141141
zend_declare_property(
142142
self.entry.load(Ordering::SeqCst).cast(),
143143
property.name.as_ptr().cast(),

phper/src/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ where
2222
R: SetVal,
2323
{
2424
fn call(&self, arguments: &mut [Val], return_value: &mut Val) {
25-
let r = self(arguments);
25+
let mut r = self(arguments);
2626
r.set_val(return_value);
2727
}
2828
}
@@ -37,7 +37,7 @@ where
3737
R: SetVal,
3838
{
3939
fn call(&self, this: &mut Object, arguments: &mut [Val], return_value: &mut Val) {
40-
let r = self(this, arguments);
40+
let mut r = self(this, arguments);
4141
r.set_val(return_value);
4242
}
4343
}

phper/src/values.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Val {
8484

8585
pub fn null() -> Self {
8686
let mut val = Self::empty();
87-
val.set(&());
87+
val.set(());
8888
val
8989
}
9090

@@ -94,7 +94,7 @@ impl Val {
9494
val
9595
}
9696

97-
pub fn from_val(other: &Val) -> Self {
97+
pub fn from_val(other: &mut Val) -> Self {
9898
let mut val = Self::empty();
9999
val.set(other);
100100
val
@@ -104,7 +104,7 @@ impl Val {
104104
&mut self.inner
105105
}
106106

107-
pub fn set(&mut self, v: impl SetVal) {
107+
pub fn set(&mut self, mut v: impl SetVal) {
108108
v.set_val(self);
109109
}
110110

@@ -127,39 +127,39 @@ impl Val {
127127
}
128128

129129
pub trait SetVal {
130-
fn set_val(&self, val: &mut Val);
130+
fn set_val(&mut self, val: &mut Val);
131131
}
132132

133133
impl SetVal for () {
134-
fn set_val(&self, val: &mut Val) {
134+
fn set_val(&mut self, val: &mut Val) {
135135
unsafe {
136136
*val.type_info() = IS_NULL;
137137
}
138138
}
139139
}
140140

141141
impl SetVal for bool {
142-
fn set_val(&self, val: &mut Val) {
142+
fn set_val(&mut self, val: &mut Val) {
143143
unsafe {
144144
*val.type_info() = if *self { IS_TRUE } else { IS_FALSE };
145145
}
146146
}
147147
}
148148

149149
impl SetVal for i32 {
150-
fn set_val(&self, val: &mut Val) {
150+
fn set_val(&mut self, val: &mut Val) {
151151
(*self as i64).set_val(val)
152152
}
153153
}
154154

155155
impl SetVal for u32 {
156-
fn set_val(&self, val: &mut Val) {
156+
fn set_val(&mut self, val: &mut Val) {
157157
(*self as i64).set_val(val)
158158
}
159159
}
160160

161161
impl SetVal for i64 {
162-
fn set_val(&self, val: &mut Val) {
162+
fn set_val(&mut self, val: &mut Val) {
163163
unsafe {
164164
(*val.as_mut()).value.lval = *self;
165165
(*val.as_mut()).u1.type_info = IS_LONG;
@@ -168,7 +168,7 @@ impl SetVal for i64 {
168168
}
169169

170170
impl SetVal for f64 {
171-
fn set_val(&self, val: &mut Val) {
171+
fn set_val(&mut self, val: &mut Val) {
172172
unsafe {
173173
(*val.as_mut()).value.dval = *self;
174174
(*val.as_mut()).u1.type_info = IS_DOUBLE;
@@ -177,32 +177,34 @@ impl SetVal for f64 {
177177
}
178178

179179
impl SetVal for str {
180-
fn set_val(&self, val: &mut Val) {
180+
fn set_val(&mut self, val: &mut Val) {
181181
unsafe {
182182
phper_zval_stringl(val.as_mut(), self.as_ptr().cast(), self.len());
183183
}
184184
}
185185
}
186186

187187
impl SetVal for String {
188-
fn set_val(&self, val: &mut Val) {
188+
fn set_val(&mut self, val: &mut Val) {
189189
unsafe {
190190
phper_zval_stringl(val.as_mut(), self.as_ptr().cast(), self.len());
191191
}
192192
}
193193
}
194194

195195
impl SetVal for Array {
196-
fn set_val(&self, val: &mut Val) {
196+
fn set_val(&mut self, val: &mut Val) {
197197
unsafe {
198-
phper_array_init(val.as_mut());
199-
zend_hash_copy((*val.as_mut()).value.arr, self.as_ptr() as *mut _, None);
198+
let mut new_val = zeroed::<zval>();
199+
phper_zval_arr(&mut new_val, self.as_mut_ptr());
200+
*self.leak() = true;
201+
phper_zval_zval(val.as_mut(), &mut new_val, true.into(), false.into());
200202
}
201203
}
202204
}
203205

204206
impl<T: SetVal> SetVal for Option<T> {
205-
fn set_val(&self, val: &mut Val) {
207+
fn set_val(&mut self, val: &mut Val) {
206208
match self {
207209
Some(t) => t.set_val(val),
208210
None => ().set_val(val),
@@ -211,7 +213,7 @@ impl<T: SetVal> SetVal for Option<T> {
211213
}
212214

213215
impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
214-
fn set_val(&self, val: &mut Val) {
216+
fn set_val(&mut self, val: &mut Val) {
215217
match self {
216218
Ok(t) => t.set_val(val),
217219
Err(e) => unsafe {
@@ -232,27 +234,21 @@ impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
232234
}
233235

234236
impl SetVal for Val {
235-
fn set_val(&self, val: &mut Val) {
237+
fn set_val(&mut self, val: &mut Val) {
236238
unsafe {
237-
phper_zval_copy_value(val.as_mut(), &self.inner as *const _ as *mut _);
239+
phper_zval_copy_value(val.as_mut(), &mut self.inner);
238240
}
239241
}
240242
}
241243

242244
impl<T: SetVal + ?Sized> SetVal for Box<T> {
243-
fn set_val(&self, val: &mut Val) {
244-
T::set_val(&self, val)
245-
}
246-
}
247-
248-
impl<T: SetVal + ?Sized> SetVal for &T {
249-
fn set_val(&self, val: &mut Val) {
245+
fn set_val(&mut self, val: &mut Val) {
250246
T::set_val(self, val)
251247
}
252248
}
253249

254250
impl<T: SetVal + ?Sized> SetVal for &mut T {
255-
fn set_val(&self, val: &mut Val) {
251+
fn set_val(&mut self, val: &mut Val) {
256252
T::set_val(self, val)
257253
}
258254
}

0 commit comments

Comments
 (0)