ext-php-rs 0.17.0

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
//! Represents an object in PHP. Allows for overriding the internal object used
//! by classes, allowing users to store Rust data inside a PHP object.

use std::{
    cell::RefCell,
    fmt::Debug,
    mem,
    os::raw::c_char,
    ptr::{self, NonNull},
};

use crate::{
    boxed::{ZBox, ZBoxable},
    class::{PhpClass, RegisteredClass},
    convert::{FromZendObject, FromZval, IntoZval},
    error::{Error, Result},
    ffi::{
        ext_php_rs_zend_object_alloc, ext_php_rs_zend_object_release, object_properties_init,
        zend_object, zend_object_std_init, zend_objects_clone_members,
    },
    flags::DataType,
    types::{
        ZendObject, Zval,
        payload::{self, BorrowError, ClassRef, ClassRefMut},
    },
    zend::ClassEntry,
};

/// Representation of a Zend class object in memory.
#[repr(C)]
#[derive(Debug)]
pub struct ZendClassObject<T> {
    obj: RefCell<Option<T>>,
    /// The standard zend object.
    pub std: ZendObject,
}

impl<T: RegisteredClass> ZendClassObject<T> {
    /// Creates a new [`ZendClassObject`] of type `T`, where `T` is a
    /// [`RegisteredClass`] in PHP, storing the given value `val` inside the
    /// object.
    ///
    /// # Parameters
    ///
    /// * `val` - The value to store inside the object.
    ///
    /// # Panics
    ///
    /// Panics if memory was unable to be allocated for the new object.
    pub fn new(val: T) -> ZBox<Self> {
        Self::internal_new(Some(val), None)
    }

    /// Creates a new [`ZendClassObject`] of type `T`, with an uninitialized
    /// internal object.
    ///
    /// Until [`initialize`] is called, [`try_borrow`] and [`try_borrow_mut`]
    /// return [`BorrowError::Uninitialized`] and the [`Clone`]
    /// implementation panics.
    ///
    /// [`initialize`]: #method.initialize
    /// [`try_borrow`]: #method.try_borrow
    /// [`try_borrow_mut`]: #method.try_borrow_mut
    ///
    /// # Panics
    ///
    /// Panics if memory was unable to be allocated for the new object.
    pub fn new_uninit(ce: Option<&'static ClassEntry>) -> ZBox<Self> {
        Self::internal_new(None, ce)
    }

    /// Creates a new [`ZendClassObject`] of type `T` holding `val`, as an
    /// object of `ce`: `T`'s own class, or a PHP subclass of it.
    pub(crate) fn new_in(val: T, ce: Option<&'static ClassEntry>) -> ZBox<Self> {
        Self::internal_new(Some(val), ce)
    }

    #[expect(
        clippy::expect_used,
        reason = "the Zend allocator aborts the process on OOM, so the pointer is never null"
    )]
    fn internal_new(val: Option<T>, ce: Option<&'static ClassEntry>) -> ZBox<Self> {
        let size = mem::size_of::<ZendClassObject<T>>();
        let meta = T::get_metadata();
        let ce = ptr::from_ref(ce.unwrap_or_else(|| meta.ce())).cast_mut();
        let obj =
            unsafe { ext_php_rs_zend_object_alloc(size as _, ce).cast::<ZendClassObject<T>>() };
        let obj = unsafe {
            obj.as_mut()
                .expect("Failed to allocate for new Zend object")
        };

        unsafe { zend_object_std_init(&raw mut obj.std, ce) };
        unsafe { object_properties_init(&raw mut obj.std, ce) };

        // SAFETY: `obj` is non-null and well aligned as it is a reference.
        // As the data in `obj.obj` is uninitialized, we don't want to drop
        // the data, but directly override it.
        unsafe { ptr::write(&raw mut obj.obj, RefCell::new(val)) };

        obj.std.handlers = meta.handlers();
        unsafe { ZBox::from_raw(obj) }
    }

    /// Creates an object of the class of `source` that holds `val`, and copies
    /// the properties of `source` into it, like `zend_objects_clone_obj`.
    ///
    /// # Safety
    ///
    /// `source` points to a live object.
    pub(crate) unsafe fn new_clone(val: T, source: *mut ZendObject) -> ZBox<Self> {
        // SAFETY: the class entry of a live object outlives the object.
        let ce = unsafe { (*source).ce.as_ref() };
        let mut new = Self::internal_new(Some(val), ce);
        // SAFETY: `new` has the class of `source`, so both objects have the
        // same number of property slots.
        unsafe { zend_objects_clone_members(&raw mut new.std, source) };
        new
    }

    /// Initializes the class object with the value `val`.
    ///
    /// # Parameters
    ///
    /// * `val` - The value to initialize the object with.
    ///
    /// # Returns
    ///
    /// Returns the old value in an [`Option`] if the object had already been
    /// initialized, [`None`] otherwise.
    ///
    /// # Errors
    ///
    /// * [`BorrowError::Borrowed`] - The value is borrowed.
    pub fn initialize(&self, val: T) -> Result<Option<T>, BorrowError> {
        payload::replace(&self.obj, val, T::CLASS_NAME)
    }

    /// Borrows the Rust value of the object.
    ///
    /// # Errors
    ///
    /// * [`BorrowError::MutablyBorrowed`] - The value is borrowed mutably.
    /// * [`BorrowError::Uninitialized`] - The object has no Rust value.
    pub fn try_borrow(&self) -> Result<ClassRef<'_, T>, BorrowError> {
        payload::borrow(&self.obj, T::CLASS_NAME)
    }

    /// Borrows the Rust value of the object mutably.
    ///
    /// # Errors
    ///
    /// * [`BorrowError::Borrowed`] - The value is borrowed.
    /// * [`BorrowError::Uninitialized`] - The object has no Rust value.
    pub fn try_borrow_mut(&self) -> Result<ClassRefMut<'_, T>, BorrowError> {
        payload::borrow_mut(&self.obj, T::CLASS_NAME)
    }

    /// Returns the Rust value without a borrow guard, or [`None`] while a
    /// [`ClassRefMut`] lives or the object is not initialized.
    ///
    /// # Safety
    ///
    /// The value must not be borrowed mutably while the returned reference
    /// lives.
    pub(crate) unsafe fn unguarded(&self) -> Option<&T> {
        // SAFETY: the caller keeps the value free of mutable borrows while the
        // reference lives.
        unsafe { self.obj.try_borrow_unguarded() }
            .ok()
            .and_then(Option::as_ref)
    }

    /// Moves the Rust value out of the object, ignoring the borrow flag.
    ///
    /// # Safety
    ///
    /// No borrow of the value may be used again. The engine calls `free_obj`
    /// once the refcount is zero, so the only guards left are the ones a
    /// bailout skipped.
    pub(crate) unsafe fn take_unguarded(&self) -> Option<T> {
        // SAFETY: the caller makes sure that no borrow of the value is used
        // again, so the write through the cell pointer aliases nothing.
        unsafe { ptr::replace(self.obj.as_ptr(), None) }
    }

    /// Returns a reference to the [`ZendClassObject`] of a given zend
    /// object `obj`. Returns [`None`] if the given object is not of the
    /// type `T`.
    ///
    /// # Parameters
    ///
    /// * `obj` - The zend object to get the [`ZendClassObject`] for.
    ///
    /// # Panics
    ///
    /// * If the std offset over/underflows `isize`.
    #[must_use]
    pub fn from_zend_obj(std: &zend_object) -> Option<&Self> {
        // SAFETY: `std` is a live `zend_object` for the lifetime of the borrow,
        // and the resolved pointer is only ever read through a shared reference.
        unsafe { Self::resolve(ptr::from_ref(std), true)?.as_ref() }
    }

    /// Returns a reference to the [`ZendClassObject`] of a given zend object
    /// `obj`, even if the Rust object is not yet initialized. Returns
    /// [`None`] if the given object is not of the type `T`.
    ///
    /// # Parameters
    ///
    /// * `obj` - The zend object to get the [`ZendClassObject`] for.
    ///
    /// # Panics
    ///
    /// * If the std offset over/underflows `isize`.
    pub(crate) fn from_zend_obj_uninit(std: &zend_object) -> Option<&Self> {
        // SAFETY: `std` is a live `zend_object` for the lifetime of the borrow,
        // and the resolved pointer is only ever read through a shared reference.
        unsafe { Self::resolve(ptr::from_ref(std), false)?.as_ref() }
    }

    /// The [`ZendClassObject`] in `zval`, even if the Rust object is not
    /// initialized, so a borrow reports [`BorrowError::Uninitialized`].
    pub(crate) fn from_zval_uninit(zval: &Zval) -> Result<&Self> {
        zval.object()
            .and_then(Self::from_zend_obj_uninit)
            .ok_or_else(|| Error::ZvalConversion(zval.get_type()))
    }

    /// Resolves the [`ZendClassObject`] containing `std`, validating that the
    /// object was created by this type's `create_object` handler.
    ///
    /// # Safety
    ///
    /// `std` must be non-null and point to a live `zend_object` for the duration
    /// of the call.
    ///
    /// # Panics
    ///
    /// * If the std offset over/underflows `isize`.
    #[expect(
        clippy::expect_used,
        reason = "the offset is a compile-time struct offset and fits in isize"
    )]
    unsafe fn resolve(std: *const zend_object, require_initialized: bool) -> Option<*const Self> {
        // First, check if this object was created by our create_object handler.
        // We do this by comparing the handlers pointer. Objects created by PHP's
        // mock frameworks or subclasses won't have our custom handlers, and their
        // memory layout won't match ZendClassObject<T>.
        let expected_handlers = T::get_metadata().handlers();
        if !ptr::eq(unsafe { (*std).handlers }, expected_handlers) {
            return None;
        }

        let this = unsafe {
            let offset = isize::try_from(Self::std_offset()).expect("Offset overflow");
            std.cast::<c_char>().offset(0 - offset).cast::<Self>()
        };
        let this_ref = unsafe { this.as_ref()? };

        if !this_ref.std.instance_of(T::get_metadata().ce()) {
            return None;
        }

        // Check if the Rust object is initialized. Objects created via create_object
        // for subclasses that don't call parent::__construct() will have obj = None.
        // We must reject these to avoid panics when dereferencing.
        if require_initialized && !this_ref.is_initialized() {
            return None;
        }

        Some(this)
    }

    fn is_initialized(&self) -> bool {
        // SAFETY: the reference is dropped before this function returns. A
        // `ClassRefMut` only exists over an initialized value.
        unsafe { self.obj.try_borrow_unguarded() }.map_or(true, Option::is_some)
    }

    /// Returns a mutable reference to the underlying Zend object.
    pub fn get_mut_zend_obj(&mut self) -> &mut zend_object {
        &mut self.std
    }

    /// Returns the offset of the `std` property in the class object.
    pub(crate) fn std_offset() -> usize {
        unsafe {
            let null = NonNull::<Self>::dangling();
            let base = ptr::from_ref::<Self>(null.as_ref());
            let std = &raw const null.as_ref().std;

            (std as usize) - (base as usize)
        }
    }
}

impl<'a, T: RegisteredClass> FromZval<'a> for &'a ZendClassObject<T> {
    const TYPE: DataType = DataType::object(T::CLASS_NAME);

    fn from_zval(zval: &'a Zval) -> Option<Self> {
        Self::from_zend_object(zval.object()?).ok()
    }
}

impl<'a, T: RegisteredClass> FromZendObject<'a> for &'a ZendClassObject<T> {
    fn from_zend_object(obj: &'a ZendObject) -> Result<Self> {
        // TODO(david): replace with better error
        ZendClassObject::from_zend_obj(obj).ok_or(Error::InvalidScope)
    }
}

impl<'a, T: PhpClass> FromZval<'a> for ClassRef<'a, T> {
    const TYPE: DataType = DataType::object(T::CLASS_NAME);

    fn from_zval(zval: &'a Zval) -> Option<Self> {
        Self::try_from_zval(zval).ok()
    }

    fn try_from_zval(zval: &'a Zval) -> Result<Self> {
        Ok(ZendClassObject::<T>::from_zval_uninit(zval)?.try_borrow()?)
    }
}

impl<'a, T: PhpClass> FromZendObject<'a> for ClassRef<'a, T> {
    fn from_zend_object(obj: &'a ZendObject) -> Result<Self> {
        let obj = ZendClassObject::<T>::from_zend_obj(obj).ok_or(Error::InvalidScope)?;
        Ok(obj.try_borrow()?)
    }
}

impl<'a, T: PhpClass> FromZval<'a> for ClassRefMut<'a, T> {
    const TYPE: DataType = DataType::object(T::CLASS_NAME);

    fn from_zval(zval: &'a Zval) -> Option<Self> {
        Self::try_from_zval(zval).ok()
    }

    fn try_from_zval(zval: &'a Zval) -> Result<Self> {
        Ok(ZendClassObject::<T>::from_zval_uninit(zval)?.try_borrow_mut()?)
    }
}

impl<'a, T: PhpClass> FromZendObject<'a> for ClassRefMut<'a, T> {
    fn from_zend_object(obj: &'a ZendObject) -> Result<Self> {
        let obj = ZendClassObject::<T>::from_zend_obj(obj).ok_or(Error::InvalidScope)?;
        Ok(obj.try_borrow_mut()?)
    }
}

unsafe impl<T: RegisteredClass> ZBoxable for ZendClassObject<T> {
    fn free(&mut self) {
        // SAFETY: All constructors guarantee that `self` contains a valid pointer.
        // Further, all constructors guarantee that the `std` field of
        // `ZendClassObject` will be initialized.
        unsafe { ext_php_rs_zend_object_release(&raw mut self.std) }
    }
}

impl<T: RegisteredClass + Default> Default for ZBox<ZendClassObject<T>> {
    #[inline]
    fn default() -> Self {
        ZendClassObject::new(T::default())
    }
}

impl<T: RegisteredClass + Clone> Clone for ZBox<ZendClassObject<T>> {
    /// # Panics
    ///
    /// Panics with the [`BorrowError`] message if the value is borrowed
    /// mutably or not initialized.
    fn clone(&self) -> Self {
        let value = match self.try_borrow() {
            Ok(value) => T::clone(&value),
            Err(err) => panic!("{err}"),
        };
        // SAFETY: `self` is live, and the source guard is released.
        unsafe { ZendClassObject::new_clone(value, (&raw const self.std).cast_mut()) }
    }
}

impl<T: RegisteredClass> IntoZval for ZBox<ZendClassObject<T>> {
    const TYPE: DataType = DataType::object(T::CLASS_NAME);
    const NULLABLE: bool = false;

    fn set_zval(mut self, zv: &mut Zval, _: bool) -> Result<()> {
        // `set_object` adds a reference on insertion, so del_ref first to keep the
        // net refcount at 1. Matches `ZBox<ZendObject>::set_zval` in object.rs.
        self.std.del_ref();
        let obj = self.into_raw();
        // SAFETY: `into_raw` yields a valid, exclusively owned object whose
        // reference is transferred to the zval.
        zv.set_object(unsafe { &mut (*obj).std });
        Ok(())
    }
}

impl<T: RegisteredClass> IntoZval for &ZendClassObject<T> {
    const TYPE: DataType = DataType::object(T::CLASS_NAME);
    const NULLABLE: bool = false;

    #[inline]
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
        // SAFETY: `self.std` is a live object; the zval takes a new reference.
        unsafe { zv.set_object_ptr((&raw const self.std).cast_mut()) };
        Ok(())
    }
}