phper 0.10.2

The framework that allows us to write PHP extensions using pure and safe Rust whenever possible.
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright (c) 2022 PHPER Framework Team
// PHPER is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

//! The errors for crate and php.

use crate::{classes::ClassEntry, objects::ZObject, sys::*, types::TypeInfo, values::ZVal};
use derive_more::Constructor;
use phper_alloc::ToRefOwned;
use std::{
    convert::Infallible,
    error,
    ffi::FromBytesWithNulError,
    fmt::{self, Debug, Display},
    io,
    marker::PhantomData,
    mem::ManuallyDrop,
    ops::{Deref, DerefMut},
    result,
    str::Utf8Error,
    sync::atomic::{self, AtomicIsize},
};

/// Predefined interface `Throwable`.
#[inline]
pub fn throwable_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_throwable) }
}

/// Predefined class `Exception`.
#[inline]
pub fn exception_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_exception) }
}

/// Predefined class `Error`.
#[inline]
pub fn error_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_error) }
}

/// Predefined class `ErrorException`.
#[inline]
pub fn error_exception_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_error_exception) }
}

/// Predefined class `TypeError`.
#[inline]
pub fn type_error_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_type_error) }
}

/// Predefined class `ArgumentCountError` (>= PHP 7.1.0).
#[cfg(not(all(phper_major_version = "7", phper_minor_version = "0")))]
#[inline]
pub fn argument_count_error_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_argument_count_error) }
}

/// Predefined class `ArithmeticError`.
#[inline]
pub fn arithmetic_error_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_arithmetic_error) }
}

/// Predefined class `ParseError`.
#[inline]
pub fn parse_error_class<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_parse_error) }
}

/// Predefined class `DivisionByZeroError`.
#[inline]
pub fn division_by_zero_error<'a>() -> &'a ClassEntry {
    unsafe { ClassEntry::from_ptr(zend_ce_division_by_zero_error) }
}

/// PHP Throwable, can cause throwing an exception when setting to
/// [crate::values::ZVal].
pub trait Throwable: error::Error {
    /// Gets the class reference, implemented PHP `Throwable`.
    fn get_class(&self) -> &ClassEntry;

    /// Gets the exception code.
    #[inline]
    fn get_code(&self) -> Option<i64> {
        Some(0)
    }

    /// Gets the message.
    #[inline]
    fn get_message(&self) -> Option<String> {
        Some(self.to_string())
    }

    /// Create Exception object.
    ///
    /// By default, the Exception is instance of `get_class()`, the code is
    /// `get_code` and message is `get_message`;
    fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
        let mut object =
            ZObject::new(self.get_class(), []).map_err(|e| Box::new(e) as Box<dyn Throwable>)?;
        if let Some(code) = self.get_code() {
            object.set_property("code", code);
        }
        if let Some(message) = self.get_message() {
            object.set_property("message", message);
        }
        Ok(object)
    }
}

impl<T: Throwable> Throwable for Box<T> {
    fn get_class(&self) -> &ClassEntry {
        Throwable::get_class(self.deref())
    }

    fn get_code(&self) -> Option<i64> {
        Throwable::get_code(self.deref())
    }

    fn get_message(&self) -> Option<String> {
        Throwable::get_message(self.deref())
    }

    fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
        Throwable::to_object(self.deref_mut())
    }
}

impl Throwable for dyn error::Error {
    fn get_class(&self) -> &ClassEntry {
        error_exception_class()
    }
}

impl Throwable for Infallible {
    fn get_class(&self) -> &ClassEntry {
        match *self {}
    }

    fn get_code(&self) -> Option<i64> {
        match *self {}
    }

    fn get_message(&self) -> Option<String> {
        match *self {}
    }

    fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
        match *self {}
    }
}

/// Type of [Result]<T, [crate::Error]>.
///
/// [Result]: std::result::Result
pub type Result<T> = result::Result<T, self::Error>;

/// Crate level Error, which also can become an exception in php.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// The error type for I/O operations.
    #[error(transparent)]
    Io(#[from] io::Error),

    /// Errors which can occur when attempting to interpret a sequence of [`u8`]
    /// as a string.
    #[error(transparent)]
    Utf8(#[from] Utf8Error),

    /// An error indicating that a nul byte was not in the expected position.
    #[error(transparent)]
    FromBytesWithNul(#[from] FromBytesWithNulError),

    /// Owned boxed dynamic error.
    #[error(transparent)]
    Boxed(#[from] Box<dyn error::Error>),

    /// Owned exception object.
    #[error(transparent)]
    Throw(#[from] ThrowObject),

    /// Class not found, get the class by name failed, etc.
    #[error(transparent)]
    ClassNotFound(#[from] ClassNotFoundError),

    /// Throw when actual arguments count is not greater than expect in calling
    /// functions.
    #[error(transparent)]
    ArgumentCount(#[from] ArgumentCountError),

    /// Failed to initialize object.
    #[error(transparent)]
    InitializeObject(#[from] InitializeObjectError),

    /// Expect type is not the actual type.
    #[error(transparent)]
    ExpectType(#[from] ExpectTypeError),

    /// Failed when the object isn't implement PHP `Throwable`.
    #[error(transparent)]
    NotImplementThrowable(#[from] NotImplementThrowableError),
}

impl Error {
    /// Wrap the dynamic error into `Boxed`.
    pub fn boxed(e: impl Into<Box<dyn error::Error>> + 'static) -> Self {
        Self::Boxed(e.into())
    }

    /// Transfer [Throwable] item to exception object internally, and wrap it
    /// into `Throw`.
    pub fn throw(t: impl Throwable) -> Self {
        let obj = ThrowObject::from_throwable(t);
        Self::Throw(obj)
    }
}

impl Throwable for Error {
    #[inline]
    fn get_class(&self) -> &ClassEntry {
        match self {
            Error::Io(e) => Throwable::get_class(e as &dyn error::Error),
            Error::Utf8(e) => Throwable::get_class(e as &dyn error::Error),
            Error::FromBytesWithNul(e) => Throwable::get_class(e as &dyn error::Error),
            Error::Boxed(e) => Throwable::get_class(e.deref()),
            Error::Throw(e) => Throwable::get_class(e),
            Error::ClassNotFound(e) => Throwable::get_class(e),
            Error::ArgumentCount(e) => Throwable::get_class(e),
            Error::InitializeObject(e) => Throwable::get_class(e),
            Error::ExpectType(e) => Throwable::get_class(e),
            Error::NotImplementThrowable(e) => Throwable::get_class(e),
        }
    }

    #[inline]
    fn get_code(&self) -> Option<i64> {
        match self {
            Error::Io(e) => Throwable::get_code(e as &dyn error::Error),
            Error::Utf8(e) => Throwable::get_code(e as &dyn error::Error),
            Error::FromBytesWithNul(e) => Throwable::get_code(e as &dyn error::Error),
            Error::Boxed(e) => Throwable::get_code(e.deref()),
            Error::Throw(e) => Throwable::get_code(e),
            Error::ClassNotFound(e) => Throwable::get_code(e),
            Error::ArgumentCount(e) => Throwable::get_code(e),
            Error::InitializeObject(e) => Throwable::get_code(e),
            Error::ExpectType(e) => Throwable::get_code(e),
            Error::NotImplementThrowable(e) => Throwable::get_code(e),
        }
    }

    fn get_message(&self) -> Option<String> {
        match self {
            Error::Io(e) => Throwable::get_message(e as &dyn error::Error),
            Error::Utf8(e) => Throwable::get_message(e as &dyn error::Error),
            Error::FromBytesWithNul(e) => Throwable::get_message(e as &dyn error::Error),
            Error::Boxed(e) => Throwable::get_message(e.deref()),
            Error::Throw(e) => Throwable::get_message(e),
            Error::ClassNotFound(e) => Throwable::get_message(e),
            Error::ArgumentCount(e) => Throwable::get_message(e),
            Error::InitializeObject(e) => Throwable::get_message(e),
            Error::ExpectType(e) => Throwable::get_message(e),
            Error::NotImplementThrowable(e) => Throwable::get_message(e),
        }
    }

    fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
        match self {
            Error::Io(e) => Throwable::to_object(e as &mut dyn error::Error),
            Error::Utf8(e) => Throwable::to_object(e as &mut dyn error::Error),
            Error::FromBytesWithNul(e) => Throwable::to_object(e as &mut dyn error::Error),
            Error::Boxed(e) => Throwable::to_object(e.deref_mut()),
            Error::Throw(e) => Throwable::to_object(e),
            Error::ClassNotFound(e) => Throwable::to_object(e),
            Error::ArgumentCount(e) => Throwable::to_object(e),
            Error::InitializeObject(e) => Throwable::to_object(e),
            Error::ExpectType(e) => Throwable::to_object(e),
            Error::NotImplementThrowable(e) => Throwable::to_object(e),
        }
    }
}

/// Wrapper of Throwable object.
#[derive(Debug)]
pub struct ThrowObject(ZObject);

impl ThrowObject {
    /// Construct from Throwable object.
    ///
    /// Failed if the object is not instance of php `Throwable`.
    pub fn new(obj: ZObject) -> result::Result<Self, NotImplementThrowableError> {
        if !obj.get_class().is_instance_of(throwable_class()) {
            return Err(NotImplementThrowableError);
        }
        Ok(Self(obj))
    }

    /// Construct from Throwable.
    #[inline]
    pub fn from_throwable(mut t: impl Throwable) -> Self {
        Self::from_result(t.to_object())
    }

    /// Construct from dynamic Error.
    #[inline]
    pub fn from_error(mut e: impl error::Error + 'static) -> Self {
        let e = &mut e as &mut dyn error::Error;
        Self::from_result(Throwable::to_object(e))
    }

    fn from_result(mut result: result::Result<ZObject, Box<dyn Throwable>>) -> Self {
        let mut i = 0;

        let obj = loop {
            match result {
                Ok(o) => break o,
                Err(mut e) => {
                    if i > 50 {
                        panic!("recursion limit reached");
                    }
                    result = e.to_object();
                    i += 1;
                }
            }
        };

        Self::new(obj).unwrap()
    }

    /// Consumes the `ThrowObject`, returning the wrapped object.
    #[inline]
    pub fn into_inner(self) -> ZObject {
        self.0
    }

    fn inner_get_code(&self) -> i64 {
        self.0
            .get_property("code")
            .as_long()
            .expect("code isn't long")
    }

    fn inner_get_message(&self) -> String {
        self.0
            .get_property("message")
            .as_z_str()
            .expect("message isn't string")
            .to_str()
            .map(ToOwned::to_owned)
            .unwrap_or_default()
    }
}

impl Display for ThrowObject {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.inner_get_message(), f)
    }
}

impl error::Error for ThrowObject {}

impl Throwable for ThrowObject {
    #[inline]
    fn get_class(&self) -> &ClassEntry {
        self.0.get_class()
    }

    #[inline]
    fn get_code(&self) -> Option<i64> {
        Some(self.inner_get_code())
    }

    #[inline]
    fn get_message(&self) -> Option<String> {
        Some(self.inner_get_message())
    }

    #[inline]
    fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
        Ok(self.0.to_ref_owned())
    }
}

/// Expect type is not the actual type.
#[derive(Debug, thiserror::Error, Constructor)]
#[error("type error: must be of type {expect_type}, {actual_type} given")]
pub struct ExpectTypeError {
    expect_type: TypeInfo,
    actual_type: TypeInfo,
}

impl Throwable for ExpectTypeError {
    #[inline]
    fn get_class(&self) -> &ClassEntry {
        type_error_class()
    }
}

/// Class not found, get the class by name failed, etc.
#[derive(Debug, thiserror::Error, Constructor)]
#[error("Class '{class_name}' not found")]
pub struct ClassNotFoundError {
    class_name: String,
}

impl Throwable for ClassNotFoundError {
    fn get_class(&self) -> &ClassEntry {
        error_class()
    }
}

/// Throw when actual arguments count is not greater than expect in calling
/// functions.
#[derive(Debug, thiserror::Error, Constructor)]
#[error("{function_name}(): expects at least {expect_count} parameter(s), {given_count} given")]
pub struct ArgumentCountError {
    function_name: String,
    expect_count: usize,
    given_count: usize,
}

impl Throwable for ArgumentCountError {
    fn get_class(&self) -> &ClassEntry {
        #[cfg(not(all(phper_major_version = "7", phper_minor_version = "0")))]
        {
            argument_count_error_class()
        }

        #[cfg(all(phper_major_version = "7", phper_minor_version = "0"))]
        {
            type_error_class()
        }
    }
}

/// Failed to initialize object.
#[derive(Debug, thiserror::Error, Constructor)]
#[error("Cannot instantiate class {class_name}")]
pub struct InitializeObjectError {
    class_name: String,
}

impl Throwable for InitializeObjectError {
    fn get_class(&self) -> &ClassEntry {
        error_class()
    }
}

/// Failed when the object isn't implement PHP `Throwable`.
#[derive(Debug, thiserror::Error)]
#[error("Cannot throw objects that do not implement Throwable")]
pub struct NotImplementThrowableError;

impl Throwable for NotImplementThrowableError {
    fn get_class(&self) -> &ClassEntry {
        type_error_class()
    }
}

static EXCEPTION_GUARD_COUNT: AtomicIsize = AtomicIsize::new(0);

/// Guarder for preventing the thrown exception from being overwritten.
///
/// Normally, you don't need to use `ExceptionGuard`, unless before you call the
/// unsafe raw php function, like `call_user_function`.
///
/// Can be used nested.
pub struct ExceptionGuard(PhantomData<*mut ()>);

impl Default for ExceptionGuard {
    fn default() -> Self {
        EXCEPTION_GUARD_COUNT.fetch_add(1, atomic::Ordering::Acquire);
        unsafe {
            zend_exception_save();
        }
        Self(PhantomData)
    }
}

impl Drop for ExceptionGuard {
    fn drop(&mut self) {
        if EXCEPTION_GUARD_COUNT.fetch_sub(1, atomic::Ordering::Acquire) > 1 {
            return;
        }
        unsafe {
            zend_exception_restore();
        }
    }
}

/// # Safety
///
/// You should always return the `Result<ZVal, impl Throwable>` in the handler,
/// rather than use this function.
pub unsafe fn throw(e: impl Throwable) {
    let obj = ThrowObject::from_throwable(e).into_inner();
    let mut val = ManuallyDrop::new(ZVal::from(obj));
    zend_throw_exception_object(val.as_mut_ptr());
}