10
10
11
11
//! Apis relate to [crate::sys::zend_array].
12
12
13
- use crate :: { strings:: ZStr , sys:: * , values:: ZVal } ;
13
+ use crate :: { alloc :: ToRefOwned , strings:: ZStr , sys:: * , values:: ZVal } ;
14
14
use derive_more:: From ;
15
- use phper_alloc:: ToRefOwned ;
16
15
use std:: {
17
16
borrow:: Borrow ,
18
17
convert:: TryInto ,
@@ -87,42 +86,46 @@ impl ZArr {
87
86
}
88
87
89
88
/// 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 ) {
91
90
let key = key. into ( ) ;
91
+ let val = value. as_mut_ptr ( ) ;
92
+
92
93
unsafe {
93
94
match key {
94
95
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 ) ;
96
97
}
97
98
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 ) ;
99
100
}
100
101
InsertKey :: Str ( s) => {
101
- phper_zend_hash_str_update (
102
+ phper_zend_symtable_str_update (
102
103
self . as_mut_ptr ( ) ,
103
104
s. as_ptr ( ) . cast ( ) ,
104
105
s. len ( ) . try_into ( ) . unwrap ( ) ,
105
- value . into_raw ( ) ,
106
+ val ,
106
107
) ;
107
108
}
108
109
InsertKey :: Bytes ( b) => {
109
- phper_zend_hash_str_update (
110
+ phper_zend_symtable_str_update (
110
111
self . as_mut_ptr ( ) ,
111
112
b. as_ptr ( ) . cast ( ) ,
112
113
b. len ( ) . try_into ( ) . unwrap ( ) ,
113
- value . into_raw ( ) ,
114
+ val ,
114
115
) ;
115
116
}
116
117
InsertKey :: ZStr ( s) => {
117
- phper_zend_hash_str_update (
118
+ phper_zend_symtable_str_update (
118
119
self . as_mut_ptr ( ) ,
119
120
s. as_c_str_ptr ( ) . cast ( ) ,
120
121
s. len ( ) . try_into ( ) . unwrap ( ) ,
121
- value . into_raw ( ) ,
122
+ val ,
122
123
) ;
123
124
}
124
125
}
125
126
}
127
+
128
+ forget ( value) ;
126
129
}
127
130
128
131
// Get item by key.
@@ -137,21 +140,22 @@ impl ZArr {
137
140
138
141
fn inner_get < ' a > ( & self , key : impl Into < Key < ' a > > ) -> Option < & ' a mut ZVal > {
139
142
let key = key. into ( ) ;
143
+ let ptr = self . as_ptr ( ) as * mut _ ;
140
144
unsafe {
141
145
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 ,
145
149
s. as_ptr ( ) . cast ( ) ,
146
150
s. len ( ) . try_into ( ) . unwrap ( ) ,
147
151
) ,
148
- Key :: Bytes ( b) => zend_hash_str_find (
149
- self . as_ptr ( ) ,
152
+ Key :: Bytes ( b) => phper_zend_symtable_str_find (
153
+ ptr ,
150
154
b. as_ptr ( ) . cast ( ) ,
151
155
b. len ( ) . try_into ( ) . unwrap ( ) ,
152
156
) ,
153
157
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 ( ) )
155
159
}
156
160
} ;
157
161
if value. is_null ( ) {
@@ -164,21 +168,22 @@ impl ZArr {
164
168
165
169
pub fn exists < ' a > ( & self , key : impl Into < Key < ' a > > ) -> bool {
166
170
let key = key. into ( ) ;
171
+ let ptr = self . as_ptr ( ) as * mut _ ;
167
172
unsafe {
168
173
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 ,
172
177
s. as_ptr ( ) . cast ( ) ,
173
178
s. len ( ) . try_into ( ) . unwrap ( ) ,
174
179
) ,
175
- Key :: Bytes ( b) => phper_zend_hash_str_exists (
176
- & self . inner ,
180
+ Key :: Bytes ( b) => phper_zend_symtable_str_exists (
181
+ ptr ,
177
182
b. as_ptr ( ) . cast ( ) ,
178
183
b. len ( ) . try_into ( ) . unwrap ( ) ,
179
184
) ,
180
- Key :: ZStr ( s) => phper_zend_hash_str_exists (
181
- & self . inner ,
185
+ Key :: ZStr ( s) => phper_zend_symtable_str_exists (
186
+ ptr ,
182
187
s. to_bytes ( ) . as_ptr ( ) . cast ( ) ,
183
188
s. len ( ) . try_into ( ) . unwrap ( ) ,
184
189
) ,
@@ -189,24 +194,24 @@ impl ZArr {
189
194
pub fn remove < ' a > ( & mut self , key : impl Into < Key < ' a > > ) -> bool {
190
195
let key = key. into ( ) ;
191
196
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 (
195
200
& mut self . inner ,
196
201
s. as_ptr ( ) . cast ( ) ,
197
202
s. len ( ) . try_into ( ) . unwrap ( ) ,
198
203
) ,
199
- Key :: Bytes ( b) => zend_hash_str_del (
204
+ Key :: Bytes ( b) => phper_zend_symtable_str_del (
200
205
& mut self . inner ,
201
206
b. as_ptr ( ) . cast ( ) ,
202
207
b. len ( ) . try_into ( ) . unwrap ( ) ,
203
208
) ,
204
- Key :: ZStr ( s) => zend_hash_str_del (
209
+ Key :: ZStr ( s) => phper_zend_symtable_str_del (
205
210
& mut self . inner ,
206
211
s. as_c_str_ptr ( ) . cast ( ) ,
207
212
s. len ( ) . try_into ( ) . unwrap ( ) ,
208
213
) ,
209
- } ) == ZEND_RESULT_CODE_SUCCESS
214
+ }
210
215
}
211
216
}
212
217
@@ -280,10 +285,8 @@ impl ZArray {
280
285
}
281
286
282
287
#[ 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 ( )
287
290
}
288
291
}
289
292
0 commit comments