@@ -5,7 +5,7 @@ use crate::{
5
5
arrays:: Array ,
6
6
errors:: { Throwable , TypeError } ,
7
7
functions:: ZendFunction ,
8
- objects:: Object ,
8
+ objects:: { Object , StatelessObject } ,
9
9
strings:: ZendString ,
10
10
sys:: * ,
11
11
types:: { get_type_by_const, Type } ,
@@ -80,16 +80,25 @@ pub struct Val {
80
80
}
81
81
82
82
impl Val {
83
- pub fn new < T : SetVal > ( t : T ) -> Self {
83
+ pub fn new ( t : impl SetVal ) -> Self {
84
84
let mut val = unsafe { zeroed :: < Val > ( ) } ;
85
- SetVal :: set_val ( t, & mut val) ;
85
+ unsafe {
86
+ SetVal :: set_val ( t, & mut val) ;
87
+ }
86
88
val
87
89
}
88
90
89
91
pub fn null ( ) -> Self {
90
92
Self :: new ( ( ) )
91
93
}
92
94
95
+ pub fn set ( & mut self , t : impl SetVal ) {
96
+ unsafe {
97
+ self . drop_value ( ) ;
98
+ SetVal :: set_val ( t, self ) ;
99
+ }
100
+ }
101
+
93
102
pub unsafe fn from_mut_ptr < ' a > ( ptr : * mut zval ) -> & ' a mut Self {
94
103
assert ! ( !ptr. is_null( ) , "ptr should not be null" ) ;
95
104
& mut * ( ptr as * mut Self )
@@ -207,22 +216,22 @@ impl Val {
207
216
}
208
217
}
209
218
210
- unsafe fn drop_me ( & mut self ) {
219
+ unsafe fn drop_value ( & mut self ) {
211
220
let t = self . get_type ( ) ;
212
221
if t. is_string ( ) {
213
- phper_zend_string_release ( self . inner . value . str ) ;
222
+ ZendString :: free ( self . inner . value . str as * mut ZendString ) ;
214
223
} else if t. is_array ( ) {
215
- zend_hash_destroy ( self . inner . value . arr ) ;
224
+ Array :: free ( self . inner . value . arr as * mut Array ) ;
216
225
} else if t. is_object ( ) {
217
- zend_objects_destroy_object ( self . inner . value . obj ) ;
226
+ Object :: free ( self . inner . value . obj as * mut StatelessObject ) ;
218
227
}
219
228
}
220
229
}
221
230
222
231
impl EAllocatable for Val {
223
232
fn free ( ptr : * mut Self ) {
224
233
unsafe {
225
- ptr. as_mut ( ) . unwrap ( ) . drop_me ( ) ;
234
+ ptr. as_mut ( ) . unwrap ( ) . drop_value ( ) ;
226
235
_efree ( ptr. cast ( ) ) ;
227
236
}
228
237
}
@@ -231,167 +240,153 @@ impl EAllocatable for Val {
231
240
impl Drop for Val {
232
241
fn drop ( & mut self ) {
233
242
unsafe {
234
- self . drop_me ( ) ;
243
+ self . drop_value ( ) ;
235
244
}
236
245
}
237
246
}
238
247
239
248
/// The trait for setting the value of [Val], mainly as the return value of
240
249
/// functions and methods, and initializer of [Val].
241
250
pub trait SetVal {
242
- fn set_val ( self , val : & mut Val ) ;
251
+ unsafe fn set_val ( self , val : & mut Val ) ;
243
252
}
244
253
245
254
impl SetVal for ( ) {
246
- fn set_val ( self , val : & mut Val ) {
255
+ unsafe fn set_val ( self , val : & mut Val ) {
247
256
val. set_type ( Type :: Null ) ;
248
257
}
249
258
}
250
259
251
260
impl SetVal for bool {
252
- fn set_val ( self , val : & mut Val ) {
261
+ unsafe fn set_val ( self , val : & mut Val ) {
253
262
val. set_type ( if self { Type :: True } else { Type :: False } ) ;
254
263
}
255
264
}
256
265
257
266
impl SetVal for i32 {
258
- fn set_val ( self , val : & mut Val ) {
267
+ unsafe fn set_val ( self , val : & mut Val ) {
259
268
SetVal :: set_val ( self as i64 , val)
260
269
}
261
270
}
262
271
263
272
impl SetVal for u32 {
264
- fn set_val ( self , val : & mut Val ) {
273
+ unsafe fn set_val ( self , val : & mut Val ) {
265
274
SetVal :: set_val ( self as i64 , val)
266
275
}
267
276
}
268
277
269
278
impl SetVal for i64 {
270
- fn set_val ( self , val : & mut Val ) {
279
+ unsafe fn set_val ( self , val : & mut Val ) {
271
280
val. set_type ( Type :: Long ) ;
272
- unsafe {
273
- ( * val. as_mut_ptr ( ) ) . value . lval = self ;
274
- }
281
+ ( * val. as_mut_ptr ( ) ) . value . lval = self ;
275
282
}
276
283
}
277
284
278
285
impl SetVal for f64 {
279
- fn set_val ( self , val : & mut Val ) {
286
+ unsafe fn set_val ( self , val : & mut Val ) {
280
287
val. set_type ( Type :: Double ) ;
281
- unsafe {
282
- ( * val. as_mut_ptr ( ) ) . value . dval = self ;
283
- }
288
+ ( * val. as_mut_ptr ( ) ) . value . dval = self ;
284
289
}
285
290
}
286
291
287
292
impl SetVal for & str {
288
- fn set_val ( self , val : & mut Val ) {
289
- unsafe {
290
- phper_zval_stringl ( val. as_mut_ptr ( ) , self . as_ptr ( ) . cast ( ) , self . len ( ) ) ;
291
- }
293
+ unsafe fn set_val ( self , val : & mut Val ) {
294
+ phper_zval_stringl ( val. as_mut_ptr ( ) , self . as_ptr ( ) . cast ( ) , self . len ( ) ) ;
292
295
}
293
296
}
294
297
295
298
impl SetVal for String {
296
- fn set_val ( self , val : & mut Val ) {
299
+ unsafe fn set_val ( self , val : & mut Val ) {
297
300
let s: & str = & self ;
298
301
SetVal :: set_val ( s, val)
299
302
}
300
303
}
301
304
302
305
impl SetVal for & [ u8 ] {
303
- fn set_val ( self , val : & mut Val ) {
304
- unsafe {
305
- // Because php string is binary safe, so can set `&[u8]` to php string.
306
- phper_zval_stringl ( val. as_mut_ptr ( ) , self . as_ptr ( ) . cast ( ) , self . len ( ) ) ;
307
- }
306
+ unsafe fn set_val ( self , val : & mut Val ) {
307
+ // Because php string is binary safe, so can set `&[u8]` to php string.
308
+ phper_zval_stringl ( val. as_mut_ptr ( ) , self . as_ptr ( ) . cast ( ) , self . len ( ) ) ;
308
309
}
309
310
}
310
311
311
312
impl SetVal for Vec < u8 > {
312
- fn set_val ( self , val : & mut Val ) {
313
+ unsafe fn set_val ( self , val : & mut Val ) {
313
314
let v: & [ u8 ] = & self ;
314
315
SetVal :: set_val ( v, val)
315
316
}
316
317
}
317
318
318
319
impl < T : SetVal > SetVal for Vec < T > {
319
- fn set_val ( self , val : & mut Val ) {
320
- unsafe {
321
- phper_array_init ( val. as_mut_ptr ( ) ) ;
322
- for ( k, v) in self . into_iter ( ) . enumerate ( ) {
323
- let v = EBox :: new ( Val :: new ( v) ) ;
324
- phper_zend_hash_index_update (
325
- ( * val. as_mut_ptr ( ) ) . value . arr ,
326
- k as u64 ,
327
- EBox :: into_raw ( v) . cast ( ) ,
328
- ) ;
329
- }
320
+ unsafe fn set_val ( self , val : & mut Val ) {
321
+ phper_array_init ( val. as_mut_ptr ( ) ) ;
322
+ for ( k, v) in self . into_iter ( ) . enumerate ( ) {
323
+ let v = EBox :: new ( Val :: new ( v) ) ;
324
+ phper_zend_hash_index_update (
325
+ ( * val. as_mut_ptr ( ) ) . value . arr ,
326
+ k as u64 ,
327
+ EBox :: into_raw ( v) . cast ( ) ,
328
+ ) ;
330
329
}
331
330
}
332
331
}
333
332
334
333
/// Setting the val to an array, Because of the feature of [std::collections::HashMap], the item
335
334
/// order of array is not guarantee.
336
335
impl < K : AsRef < str > , V : SetVal > SetVal for HashMap < K , V > {
337
- fn set_val ( self , val : & mut Val ) {
336
+ unsafe fn set_val ( self , val : & mut Val ) {
338
337
map_set_val ( self , val) ;
339
338
}
340
339
}
341
340
342
341
/// Setting the val to an array, which preserves item order.
343
342
impl < K : AsRef < str > , V : SetVal > SetVal for IndexMap < K , V > {
344
- fn set_val ( self , val : & mut Val ) {
343
+ unsafe fn set_val ( self , val : & mut Val ) {
345
344
map_set_val ( self , val) ;
346
345
}
347
346
}
348
347
349
348
impl < K : AsRef < str > , V : SetVal > SetVal for BTreeMap < K , V > {
350
- fn set_val ( self , val : & mut Val ) {
349
+ unsafe fn set_val ( self , val : & mut Val ) {
351
350
map_set_val ( self , val) ;
352
351
}
353
352
}
354
353
355
- fn map_set_val < K , V , I > ( iterator : I , val : & mut Val )
354
+ unsafe fn map_set_val < K , V , I > ( iterator : I , val : & mut Val )
356
355
where
357
356
I : IntoIterator < Item = ( K , V ) > ,
358
357
K : AsRef < str > ,
359
358
V : SetVal ,
360
359
{
361
- unsafe {
362
- phper_array_init ( val. as_mut_ptr ( ) ) ;
363
- for ( k, v) in iterator. into_iter ( ) {
364
- let k = k. as_ref ( ) ;
365
- let v = EBox :: new ( Val :: new ( v) ) ;
366
- phper_zend_hash_str_update (
367
- ( * val. as_mut_ptr ( ) ) . value . arr ,
368
- k. as_ptr ( ) . cast ( ) ,
369
- k. len ( ) ,
370
- EBox :: into_raw ( v) . cast ( ) ,
371
- ) ;
372
- }
360
+ phper_array_init ( val. as_mut_ptr ( ) ) ;
361
+ for ( k, v) in iterator. into_iter ( ) {
362
+ let k = k. as_ref ( ) ;
363
+ let v = EBox :: new ( Val :: new ( v) ) ;
364
+ phper_zend_hash_str_update (
365
+ ( * val. as_mut_ptr ( ) ) . value . arr ,
366
+ k. as_ptr ( ) . cast ( ) ,
367
+ k. len ( ) ,
368
+ EBox :: into_raw ( v) . cast ( ) ,
369
+ ) ;
373
370
}
374
371
}
375
372
376
373
impl SetVal for EBox < Array > {
377
- fn set_val ( self , val : & mut Val ) {
378
- unsafe {
379
- let arr = EBox :: into_raw ( self ) ;
380
- phper_zval_arr ( val. as_mut_ptr ( ) , arr. cast ( ) ) ;
381
- }
374
+ unsafe fn set_val ( self , val : & mut Val ) {
375
+ let arr = EBox :: into_raw ( self ) ;
376
+ phper_zval_arr ( val. as_mut_ptr ( ) , arr. cast ( ) ) ;
382
377
}
383
378
}
384
379
385
380
impl < T > SetVal for EBox < Object < T > > {
386
- fn set_val ( self , val : & mut Val ) {
381
+ unsafe fn set_val ( self , val : & mut Val ) {
387
382
let object = EBox :: into_raw ( self ) ;
388
383
val. inner . value . obj = object. cast ( ) ;
389
384
val. set_type ( Type :: ObjectEx ) ;
390
385
}
391
386
}
392
387
393
388
impl < T : SetVal > SetVal for Option < T > {
394
- fn set_val ( self , val : & mut Val ) {
389
+ unsafe fn set_val ( self , val : & mut Val ) {
395
390
match self {
396
391
Some ( t) => SetVal :: set_val ( t, val) ,
397
392
None => SetVal :: set_val ( ( ) , val) ,
@@ -400,10 +395,10 @@ impl<T: SetVal> SetVal for Option<T> {
400
395
}
401
396
402
397
impl < T : SetVal , E : Throwable > SetVal for Result < T , E > {
403
- fn set_val ( self , val : & mut Val ) {
398
+ unsafe fn set_val ( self , val : & mut Val ) {
404
399
match self {
405
400
Ok ( t) => t. set_val ( val) ,
406
- Err ( e) => unsafe {
401
+ Err ( e) => {
407
402
let class_entry = e. class_entry ( ) ;
408
403
let message = ensure_end_with_zero ( e. message ( ) ) ;
409
404
zend_throw_exception (
@@ -412,15 +407,13 @@ impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
412
407
e. code ( ) as i64 ,
413
408
) ;
414
409
SetVal :: set_val ( ( ) , val) ;
415
- } ,
410
+ }
416
411
}
417
412
}
418
413
}
419
414
420
415
impl SetVal for Val {
421
- fn set_val ( mut self , val : & mut Val ) {
422
- unsafe {
423
- phper_zval_copy ( val. as_mut_ptr ( ) , self . as_mut_ptr ( ) ) ;
424
- }
416
+ unsafe fn set_val ( mut self , val : & mut Val ) {
417
+ phper_zval_copy ( val. as_mut_ptr ( ) , self . as_mut_ptr ( ) ) ;
425
418
}
426
419
}
0 commit comments