ext-php-rs 0.3.2

Bindings for the Zend API to build PHP extensions natively in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Represents an array in PHP. As all arrays in PHP are associative arrays, they are represented
//! by hash tables.

use std::{
    collections::HashMap,
    convert::{TryFrom, TryInto},
    ffi::CString,
    fmt::Debug,
    marker::PhantomData,
    u64,
};

use crate::{
    bindings::{
        HashTable, _Bucket, _zend_new_array, zend_array_destroy, zend_array_dup, zend_hash_clean,
        zend_hash_index_del, zend_hash_index_find, zend_hash_index_update,
        zend_hash_next_index_insert, zend_hash_str_del, zend_hash_str_find, zend_hash_str_update,
        HT_MIN_SIZE,
    },
    errors::{Error, Result},
    php::enums::DataType,
};

use super::{
    string::ZendString,
    zval::{IntoZval, Zval},
};

/// Result type returned after attempting to insert an element into a hash table.
#[derive(Debug)]
pub enum HashTableInsertResult<'a> {
    /// The element was inserted into the hash table successfully.
    Ok,
    /// The element was inserted into the hash table successfully, over-writing an existing element.
    OkWithOverwrite(&'a Zval),
}

/// A PHP array, which internally is a hash table.
pub struct ZendHashTable<'a> {
    ptr: *mut HashTable,
    free: bool,
    phantom: PhantomData<&'a HashTable>,
}

impl<'a> ZendHashTable<'a> {
    /// Creates a new, empty, PHP associative array.
    pub fn new() -> Self {
        Self::with_capacity(HT_MIN_SIZE)
    }

    /// Creates a new, empty, PHP associative array with an initial size.
    ///
    /// # Parameters
    ///
    /// * `size` - The size to initialize the array with.
    pub fn with_capacity(size: u32) -> Self {
        // SAFETY: PHP allocater handles the creation of the
        // array.
        let ptr = unsafe { _zend_new_array(size) };
        Self {
            ptr,
            free: true,
            phantom: PhantomData,
        }
    }

    /// Creates a new hash table wrapper.
    /// This _will not_ be freed when it goes out of scope in Rust.
    ///
    /// # Parameters
    ///
    /// * `ptr` - The pointer of the actual hash table.
    /// * `free` - Whether the pointer should be freed when the resulting [`ZendHashTable`]
    /// goes out of scope.
    ///
    /// # Safety
    ///
    /// As a raw pointer is given this function is unsafe, you must ensure that the pointer is valid when calling
    /// the function. A simple null check is done but this is not sufficient in most cases.
    pub unsafe fn from_ptr(ptr: *mut HashTable, free: bool) -> Result<Self> {
        if ptr.is_null() {
            return Err(Error::InvalidPointer);
        }

        Ok(Self {
            ptr,
            free,
            phantom: PhantomData,
        })
    }

    /// Returns the current number of elements in the array.
    pub fn len(&self) -> usize {
        unsafe { *self.ptr }.nNumOfElements as usize
    }

    /// Returns whether the hash table is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Clears the hash table, removing all values.
    pub fn clear(&mut self) {
        unsafe { zend_hash_clean(self.ptr) }
    }

    /// Attempts to retrieve a value from the hash table with a string key.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to search for in the hash table.
    ///
    /// # Returns
    ///
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash table.
    /// * `None` - No value at the given position was found.
    pub fn get(&self, key: &str) -> Option<&Zval> {
        let str = CString::new(key).ok()?;
        unsafe { zend_hash_str_find(self.ptr, str.as_ptr(), key.len() as _).as_ref() }
    }

    /// Attempts to retrieve a value from the hash table with an index.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to search for in the hash table.
    ///
    /// # Returns
    ///
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash table.
    /// * `None` - No value at the given position was found.
    pub fn get_index(&self, key: u64) -> Option<&Zval> {
        unsafe { zend_hash_index_find(self.ptr, key).as_ref() }
    }

    /// Attempts to remove a value from the hash table with a string key.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to remove from the hash table.
    ///
    /// # Returns
    ///
    /// * `Some(())` - Key was successfully removed.
    /// * `None` - No key was removed, did not exist.
    pub fn remove<K>(&self, key: &str) -> Option<()> {
        let result = unsafe {
            zend_hash_str_del(self.ptr, CString::new(key).ok()?.as_ptr(), key.len() as _)
        };

        if result < 0 {
            None
        } else {
            Some(())
        }
    }

    /// Attempts to remove a value from the hash table with a string key.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to remove from the hash table.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Key was successfully removed.
    /// * `None` - No key was removed, did not exist.
    pub fn remove_index(&self, key: u64) -> Option<()> {
        let result = unsafe { zend_hash_index_del(self.ptr, key) };

        if result < 0 {
            None
        } else {
            Some(())
        }
    }

    /// Attempts to insert an item into the hash table, or update if the key already exists.
    /// Returns a result containing a [`HashTableInsertResult`], which will indicate a successful
    /// insert, with the insert result variants either containing the overwritten value or nothing.
    ///
    /// # Parameters
    ///
    /// * `key` - The key to insert the value at in the hash table.
    /// * `value` - The value to insert into the hash table.
    pub fn insert<V>(&mut self, key: &str, val: V) -> Result<HashTableInsertResult>
    where
        V: IntoZval,
    {
        let mut val = val.into_zval(false)?;
        let existing_ptr = unsafe {
            zend_hash_str_update(
                self.ptr,
                CString::new(key)?.as_ptr(),
                key.len() as u64,
                &mut val,
            )
        };
        val.release();

        // Should we be claiming this Zval into rust?
        // I'm not sure if the PHP GC will collect this.

        // SAFETY: The `zend_hash_str_update` function will either return a valid pointer or a null pointer.
        // In the latter case, `as_ref()` will return `None`.
        Ok(match unsafe { existing_ptr.as_ref() } {
            Some(ptr) => HashTableInsertResult::OkWithOverwrite(ptr),
            None => HashTableInsertResult::Ok,
        })
    }

    /// Inserts an item into the hash table at a specified index,
    /// or updates if the key already exists.
    ///
    /// # Parameters
    ///
    /// * `key` - The index at which the value should be inserted.
    /// * `val` - The value to insert into the hash table.
    ///
    /// # Returns
    ///
    /// * `Some(&Zval)` - The existing value in the hash table that was overriden.
    /// * `None` - The element was inserted.
    pub fn insert_at_index<V>(&mut self, key: u64, val: V) -> Result<HashTableInsertResult>
    where
        V: IntoZval,
    {
        let mut val = val.into_zval(false)?;
        let existing_ptr = unsafe { zend_hash_index_update(self.ptr, key, &mut val) };
        val.release();

        // SAFETY: The `zend_hash_str_update` function will either return a valid pointer or a null pointer.
        // In the latter case, `as_ref()` will return `None`.
        Ok(match unsafe { existing_ptr.as_ref() } {
            Some(ptr) => HashTableInsertResult::OkWithOverwrite(ptr),
            None => HashTableInsertResult::Ok,
        })
    }

    /// Pushes an item onto the end of the hash table. Returns a result containing nothing if the
    /// element was sucessfully inserted.
    ///
    /// # Parameters
    ///
    /// * `val` - The value to insert into the hash table.
    pub fn push<V>(&mut self, val: V) -> Result<()>
    where
        V: IntoZval,
    {
        let mut val = val.into_zval(false)?;
        unsafe { zend_hash_next_index_insert(self.ptr, &mut val) };
        val.release();

        Ok(())
    }

    /// Returns an iterator over the hash table.
    #[inline]
    pub fn iter(&self) -> Iter<'_> {
        Iter::new(self)
    }

    /// Converts the hash table into a raw pointer to be passed to Zend.
    pub(crate) fn into_ptr(mut self) -> *mut HashTable {
        self.free = false;
        self.ptr
    }
}

impl<'a> Debug for ZendHashTable<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_map()
            .entries(
                self.iter()
                    .map(|(k, k2, v)| (k2.unwrap_or_else(|| k.to_string()), v)),
            )
            .finish()
    }
}

impl<'a> Default for ZendHashTable<'a> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> Drop for ZendHashTable<'a> {
    fn drop(&mut self) {
        if self.free {
            unsafe { zend_array_destroy(self.ptr) };
        }
    }
}

impl<'a> Clone for ZendHashTable<'a> {
    fn clone(&self) -> Self {
        // SAFETY: If this fails then `emalloc` failed - we are doomed anyway?
        // `from_ptr()` checks if the ptr is null.
        unsafe {
            let ptr = zend_array_dup(self.ptr);
            Self::from_ptr(ptr, true).expect("ZendHashTable cloning failed when duplicating array.")
        }
    }
}

impl<'a> IntoIterator for ZendHashTable<'a> {
    type Item = (u64, Option<String>, &'a Zval);
    type IntoIter = IntoIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter::new(self)
    }
}

macro_rules! build_iter {
    ($name: ident, $ht: ty) => {
        pub struct $name<'a> {
            ht: $ht,
            pos: *mut _Bucket,
            end: *mut _Bucket,
        }

        impl<'a> $name<'a> {
            pub fn new(ht: $ht) -> Self {
                let ptr = unsafe { *ht.ptr };
                let pos = ptr.arData;
                let end = unsafe { ptr.arData.offset(ptr.nNumUsed as isize) };
                Self { ht, pos, end }
            }
        }

        impl<'a> Iterator for $name<'a> {
            type Item = (u64, Option<String>, &'a Zval);

            fn next(&mut self) -> Option<Self::Item> {
                // iterator complete
                if self.pos == self.end {
                    return None;
                }

                let result = if let Some(val) = unsafe { self.pos.as_ref() } {
                    // SAFETY: We can ensure safety further by checking if it is null before
                    // converting it to a reference (val.key.as_ref() returns None if ptr == null)
                    let str_key = unsafe { ZendString::from_ptr(val.key, false) }
                        .and_then(|s| s.try_into())
                        .ok();

                    Some((val.h, str_key, &val.val))
                } else {
                    None
                };

                self.pos = unsafe { self.pos.offset(1) };
                result
            }

            fn count(self) -> usize
            where
                Self: Sized,
            {
                unsafe { *self.ht.ptr }.nNumOfElements as usize
            }
        }
    };
}

build_iter!(Iter, &'a ZendHashTable<'a>);
build_iter!(IntoIter, ZendHashTable<'a>);

impl<'a, V> TryFrom<ZendHashTable<'a>> for HashMap<String, V>
where
    V: TryFrom<&'a Zval>,
{
    type Error = Error;

    fn try_from(zht: ZendHashTable<'a>) -> Result<Self> {
        let mut hm = HashMap::with_capacity(zht.len());

        for (idx, key, val) in zht.into_iter() {
            hm.insert(
                key.unwrap_or_else(|| idx.to_string()),
                val.try_into()
                    .map_err(|_| Error::ZvalConversion(DataType::Null))?,
            );
        }

        Ok(hm)
    }
}

impl<'a, K, V> TryFrom<HashMap<K, V>> for ZendHashTable<'a>
where
    K: AsRef<str>,
    V: IntoZval,
{
    type Error = Error;

    fn try_from(hm: HashMap<K, V>) -> Result<Self> {
        let mut ht =
            ZendHashTable::with_capacity(hm.len().try_into().map_err(|_| Error::IntegerOverflow)?);

        for (k, v) in hm.into_iter() {
            ht.insert(k.as_ref(), v)?;
        }

        Ok(ht)
    }
}

/// Implementation converting a Rust HashTable into a ZendHashTable.
impl<'a, 'b, K, V> TryFrom<&'a HashMap<K, V>> for ZendHashTable<'b>
where
    K: AsRef<str>,
    V: IntoZval + Clone,
{
    type Error = Error;

    fn try_from(hm: &'a HashMap<K, V>) -> Result<Self> {
        let mut ht =
            ZendHashTable::with_capacity(hm.len().try_into().map_err(|_| Error::IntegerOverflow)?);

        for (k, v) in hm.iter() {
            ht.insert(k.as_ref(), v.clone())?;
        }

        Ok(ht)
    }
}

/// Implementation for converting a reference to `ZendHashTable` into a `Vec` of given type.
/// Will return an error type if one of the values inside the array cannot be converted into
/// a type `T`.
impl<'a, V> TryFrom<ZendHashTable<'a>> for Vec<V>
where
    V: TryFrom<&'a Zval>,
{
    type Error = Error;

    fn try_from(ht: ZendHashTable<'a>) -> Result<Self> {
        ht.into_iter()
            .map(|(_, _, v)| {
                v.try_into()
                    .map_err(|_| Error::ZvalConversion(DataType::Null))
            })
            .collect::<Result<Vec<_>>>()
    }
}

impl<'a, V> TryFrom<Vec<V>> for ZendHashTable<'a>
where
    V: IntoZval,
{
    type Error = Error;

    fn try_from(vec: Vec<V>) -> Result<Self> {
        let mut ht =
            ZendHashTable::with_capacity(vec.len().try_into().map_err(|_| Error::IntegerOverflow)?);

        for val in vec.into_iter() {
            ht.push(val)?;
        }

        Ok(ht)
    }
}

impl<'a, V> TryFrom<&Vec<V>> for ZendHashTable<'a>
where
    V: IntoZval + Clone,
{
    type Error = Error;

    fn try_from(vec: &Vec<V>) -> Result<Self> {
        let mut ht =
            ZendHashTable::with_capacity(vec.len().try_into().map_err(|_| Error::IntegerOverflow)?);

        for val in vec.iter() {
            ht.push(val.clone())?;
        }

        Ok(ht)
    }
}