Skip to content

Commit 7743100

Browse files
committed
Remove ZVal bug method into_raw.
1 parent a961b1f commit 7743100

File tree

4 files changed

+67
-49
lines changed

4 files changed

+67
-49
lines changed

phper-alloc/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use phper_sys::*;
1818
use std::{
1919
borrow::Borrow,
2020
convert::TryInto,
21-
mem::{forget, size_of},
21+
mem::{size_of, ManuallyDrop},
2222
ops::{Deref, DerefMut},
2323
};
2424

@@ -59,9 +59,7 @@ impl<T> EBox<T> {
5959
///
6060
/// Will leak memory.
6161
pub fn into_raw(b: EBox<T>) -> *mut T {
62-
let ptr = b.ptr;
63-
forget(b);
64-
ptr
62+
ManuallyDrop::new(b).ptr
6563
}
6664

6765
pub fn into_inner(self) -> T {

phper-sys/php_wrapper.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,34 @@ const zend_object_handlers *phper_z_obj_ht_p(const zval *zv) {
320320
return Z_OBJ_HT_P(zv);
321321
}
322322

323-
zend_object *phper_z_obj_p(const zval *zv) {
324-
return Z_OBJ_P(zv);
325-
}
323+
zend_object *phper_z_obj_p(const zval *zv) {
324+
return Z_OBJ_P(zv);
325+
}
326326

327327
uint32_t phper_z_addref_p(zval *zv) {
328328
return Z_ADDREF_P(zv);
329329
}
330+
331+
zval* phper_zend_hash_index_find(const HashTable *ht, zend_ulong h) {
332+
return zend_hash_index_find(ht, h);
333+
}
334+
335+
bool phper_zend_hash_index_del(HashTable *ht, zend_ulong h) {
336+
return zend_hash_index_del(ht, h) == SUCCESS;
337+
}
338+
339+
zval *phper_zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData) {
340+
return zend_symtable_str_update(ht, str, len, pData);
341+
}
342+
343+
bool phper_zend_symtable_str_del(HashTable *ht, const char *str, size_t len) {
344+
return zend_symtable_str_del(ht, str, len) == SUCCESS;
345+
}
346+
347+
zval *phper_zend_symtable_str_find(HashTable *ht, const char *str, size_t len) {
348+
return zend_symtable_str_find(ht, str, len);
349+
}
350+
351+
bool phper_zend_symtable_str_exists(HashTable *ht, const char *str, size_t len) {
352+
return zend_symtable_str_exists(ht, str, len) != 0;
353+
}

phper/src/arrays.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
//! Apis relate to [crate::sys::zend_array].
1212
13-
use crate::{strings::ZStr, sys::*, values::ZVal};
13+
use crate::{alloc::ToRefOwned, strings::ZStr, sys::*, values::ZVal};
1414
use derive_more::From;
15-
use phper_alloc::ToRefOwned;
1615
use std::{
1716
borrow::Borrow,
1817
convert::TryInto,
@@ -87,42 +86,46 @@ impl ZArr {
8786
}
8887

8988
/// Add or update item by key.
90-
pub fn insert<'a>(&mut self, key: impl Into<InsertKey<'a>>, value: ZVal) {
89+
pub fn insert<'a>(&mut self, key: impl Into<InsertKey<'a>>, mut value: ZVal) {
9190
let key = key.into();
91+
let val = value.as_mut_ptr();
92+
9293
unsafe {
9394
match key {
9495
InsertKey::NextIndex => {
95-
phper_zend_hash_next_index_insert(self.as_mut_ptr(), value.into_raw());
96+
phper_zend_hash_next_index_insert(self.as_mut_ptr(), val);
9697
}
9798
InsertKey::Index(i) => {
98-
phper_zend_hash_index_update(self.as_mut_ptr(), i, value.into_raw());
99+
phper_zend_hash_index_update(self.as_mut_ptr(), i, val);
99100
}
100101
InsertKey::Str(s) => {
101-
phper_zend_hash_str_update(
102+
phper_zend_symtable_str_update(
102103
self.as_mut_ptr(),
103104
s.as_ptr().cast(),
104105
s.len().try_into().unwrap(),
105-
value.into_raw(),
106+
val,
106107
);
107108
}
108109
InsertKey::Bytes(b) => {
109-
phper_zend_hash_str_update(
110+
phper_zend_symtable_str_update(
110111
self.as_mut_ptr(),
111112
b.as_ptr().cast(),
112113
b.len().try_into().unwrap(),
113-
value.into_raw(),
114+
val,
114115
);
115116
}
116117
InsertKey::ZStr(s) => {
117-
phper_zend_hash_str_update(
118+
phper_zend_symtable_str_update(
118119
self.as_mut_ptr(),
119120
s.as_c_str_ptr().cast(),
120121
s.len().try_into().unwrap(),
121-
value.into_raw(),
122+
val,
122123
);
123124
}
124125
}
125126
}
127+
128+
forget(value);
126129
}
127130

128131
// Get item by key.
@@ -137,21 +140,22 @@ impl ZArr {
137140

138141
fn inner_get<'a>(&self, key: impl Into<Key<'a>>) -> Option<&'a mut ZVal> {
139142
let key = key.into();
143+
let ptr = self.as_ptr() as *mut _;
140144
unsafe {
141145
let value = match key {
142-
Key::Index(i) => zend_hash_index_find(self.as_ptr(), i),
143-
Key::Str(s) => zend_hash_str_find(
144-
self.as_ptr(),
146+
Key::Index(i) => phper_zend_hash_index_find(ptr, i),
147+
Key::Str(s) => phper_zend_symtable_str_find(
148+
ptr,
145149
s.as_ptr().cast(),
146150
s.len().try_into().unwrap(),
147151
),
148-
Key::Bytes(b) => zend_hash_str_find(
149-
self.as_ptr(),
152+
Key::Bytes(b) => phper_zend_symtable_str_find(
153+
ptr,
150154
b.as_ptr().cast(),
151155
b.len().try_into().unwrap(),
152156
),
153157
Key::ZStr(s) => {
154-
zend_hash_str_find(self.as_ptr(), s.as_c_str_ptr(), s.len().try_into().unwrap())
158+
phper_zend_symtable_str_find(ptr, s.as_c_str_ptr(), s.len().try_into().unwrap())
155159
}
156160
};
157161
if value.is_null() {
@@ -164,21 +168,22 @@ impl ZArr {
164168

165169
pub fn exists<'a>(&self, key: impl Into<Key<'a>>) -> bool {
166170
let key = key.into();
171+
let ptr = self.as_ptr() as *mut _;
167172
unsafe {
168173
match key {
169-
Key::Index(i) => phper_zend_hash_index_exists(&self.inner, i),
170-
Key::Str(s) => phper_zend_hash_str_exists(
171-
&self.inner,
174+
Key::Index(i) => phper_zend_hash_index_exists(ptr, i),
175+
Key::Str(s) => phper_zend_symtable_str_exists(
176+
ptr,
172177
s.as_ptr().cast(),
173178
s.len().try_into().unwrap(),
174179
),
175-
Key::Bytes(b) => phper_zend_hash_str_exists(
176-
&self.inner,
180+
Key::Bytes(b) => phper_zend_symtable_str_exists(
181+
ptr,
177182
b.as_ptr().cast(),
178183
b.len().try_into().unwrap(),
179184
),
180-
Key::ZStr(s) => phper_zend_hash_str_exists(
181-
&self.inner,
185+
Key::ZStr(s) => phper_zend_symtable_str_exists(
186+
ptr,
182187
s.to_bytes().as_ptr().cast(),
183188
s.len().try_into().unwrap(),
184189
),
@@ -189,24 +194,24 @@ impl ZArr {
189194
pub fn remove<'a>(&mut self, key: impl Into<Key<'a>>) -> bool {
190195
let key = key.into();
191196
unsafe {
192-
(match key {
193-
Key::Index(i) => zend_hash_index_del(&mut self.inner, i),
194-
Key::Str(s) => zend_hash_str_del(
197+
match key {
198+
Key::Index(i) => phper_zend_hash_index_del(&mut self.inner, i),
199+
Key::Str(s) => phper_zend_symtable_str_del(
195200
&mut self.inner,
196201
s.as_ptr().cast(),
197202
s.len().try_into().unwrap(),
198203
),
199-
Key::Bytes(b) => zend_hash_str_del(
204+
Key::Bytes(b) => phper_zend_symtable_str_del(
200205
&mut self.inner,
201206
b.as_ptr().cast(),
202207
b.len().try_into().unwrap(),
203208
),
204-
Key::ZStr(s) => zend_hash_str_del(
209+
Key::ZStr(s) => phper_zend_symtable_str_del(
205210
&mut self.inner,
206211
s.as_c_str_ptr().cast(),
207212
s.len().try_into().unwrap(),
208213
),
209-
}) == ZEND_RESULT_CODE_SUCCESS
214+
}
210215
}
211216
}
212217

@@ -280,10 +285,8 @@ impl ZArray {
280285
}
281286

282287
#[inline]
283-
pub fn into_raw(mut self) -> *mut zend_array {
284-
let ptr = self.as_mut_ptr();
285-
forget(self);
286-
ptr
288+
pub fn into_raw(self) -> *mut zend_array {
289+
ManuallyDrop::new(self).as_mut_ptr()
287290
}
288291
}
289292

phper/src/values.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use phper_alloc::RefClone;
2626
use std::{
2727
convert::TryInto,
2828
marker::PhantomData,
29-
mem::{forget, transmute, zeroed, MaybeUninit},
29+
mem::{transmute, zeroed, MaybeUninit},
3030
os::raw::c_int,
3131
str,
3232
};
@@ -148,13 +148,6 @@ impl ZVal {
148148
self.inner
149149
}
150150

151-
#[inline]
152-
pub fn into_raw(mut self) -> *mut zval {
153-
let ptr = self.as_mut_ptr();
154-
forget(self);
155-
ptr
156-
}
157-
158151
pub fn get_type_info(&self) -> TypeInfo {
159152
let t = unsafe { phper_z_type_info_p(self.as_ptr()) };
160153
t.into()

0 commit comments

Comments
 (0)