Type Definition ext_php_rs::zend::ExecuteData

source ·
pub type ExecuteData = zend_execute_data;
Expand description

Execute data passed when a function is called from PHP.

This generally contains things related to the call, including but not limited to:

  • Arguments
  • $this object reference
  • Reference to return value
  • Previous execute data

Implementations§

Returns an ArgParser pre-loaded with the arguments contained inside self.

Example
use ext_php_rs::{types::Zval, zend::ExecuteData, args::Arg, flags::DataType};

#[no_mangle]
pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    let mut a = Arg::new("a", DataType::Long);

    // The `parse_args!()` macro can be used for this.
    let parser = ex.parser()
        .arg(&mut a)
        .parse();

    if parser.is_err() {
        return;
    }

    dbg!(a);
}

Returns an ArgParser pre-loaded with the arguments contained inside self.

A reference to $this is also returned in an Option, which resolves to None if this function is not called inside a method.

Example
use ext_php_rs::{types::Zval, zend::ExecuteData, args::Arg, flags::DataType};

#[no_mangle]
pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    let mut a = Arg::new("a", DataType::Long);

    let (parser, this) = ex.parser_object();
    let parser = parser
        .arg(&mut a)
        .parse();

    if parser.is_err() {
        return;
    }

    dbg!(a, this);
}
Examples found in repository?
src/zend/ex.rs (line 46)
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
    pub fn parser(&mut self) -> ArgParser<'_, '_> {
        self.parser_object().0
    }

    /// Returns an [`ArgParser`] pre-loaded with the arguments contained inside
    /// `self`.
    ///
    /// A reference to `$this` is also returned in an [`Option`], which resolves
    /// to [`None`] if this function is not called inside a method.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ext_php_rs::{types::Zval, zend::ExecuteData, args::Arg, flags::DataType};
    ///
    /// #[no_mangle]
    /// pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    ///     let mut a = Arg::new("a", DataType::Long);
    ///
    ///     let (parser, this) = ex.parser_object();
    ///     let parser = parser
    ///         .arg(&mut a)
    ///         .parse();
    ///
    ///     if parser.is_err() {
    ///         return;
    ///     }
    ///
    ///     dbg!(a, this);
    /// }
    /// ```
    pub fn parser_object(&mut self) -> (ArgParser<'_, '_>, Option<&mut ZendObject>) {
        // SAFETY: All fields of the `u2` union are the same type.
        let n_args = unsafe { self.This.u2.num_args };
        let mut args = vec![];

        for i in 0..n_args {
            // SAFETY: Function definition ensures arg lifetime doesn't exceed execution
            // data lifetime.
            let arg = unsafe { self.zend_call_arg(i as usize) };
            args.push(arg);
        }

        let obj = self.This.object_mut();

        (ArgParser::new(args), obj)
    }

    /// Returns an [`ArgParser`] pre-loaded with the arguments contained inside
    /// `self`.
    ///
    /// A reference to `$this` is also returned in an [`Option`], which resolves
    /// to [`None`] if this function is not called inside a method.
    ///
    /// This function differs from [`parse_object`] in the fact that it returns
    /// a reference to a [`ZendClassObject`], which is an object that
    /// contains an arbitrary Rust type at the start of the object. The
    /// object will also resolve to [`None`] if the function is called
    /// inside a method that does not belong to an object with type `T`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ext_php_rs::{types::Zval, zend::ExecuteData, args::Arg, flags::DataType, prelude::*};
    ///
    /// #[php_class]
    /// #[derive(Debug)]
    /// struct Example;
    ///
    /// #[no_mangle]
    /// pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    ///     let mut a = Arg::new("a", DataType::Long);
    ///
    ///     let (parser, this) = ex.parser_method::<Example>();
    ///     let parser = parser
    ///         .arg(&mut a)
    ///         .parse();
    ///
    ///     if parser.is_err() {
    ///         return;
    ///     }
    ///
    ///     dbg!(a, this);
    /// }
    ///
    /// #[php_module]
    /// pub fn module(module: ModuleBuilder) -> ModuleBuilder {
    ///     module
    /// }
    /// ```
    ///
    /// [`parse_object`]: #method.parse_object
    pub fn parser_method<T: RegisteredClass>(
        &mut self,
    ) -> (ArgParser<'_, '_>, Option<&mut ZendClassObject<T>>) {
        let (parser, obj) = self.parser_object();
        (
            parser,
            obj.and_then(|obj| ZendClassObject::from_zend_obj_mut(obj)),
        )
    }

Returns an ArgParser pre-loaded with the arguments contained inside self.

A reference to $this is also returned in an Option, which resolves to None if this function is not called inside a method.

This function differs from parse_object in the fact that it returns a reference to a ZendClassObject, which is an object that contains an arbitrary Rust type at the start of the object. The object will also resolve to None if the function is called inside a method that does not belong to an object with type T.

Example
use ext_php_rs::{types::Zval, zend::ExecuteData, args::Arg, flags::DataType, prelude::*};

#[php_class]
#[derive(Debug)]
struct Example;

#[no_mangle]
pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    let mut a = Arg::new("a", DataType::Long);

    let (parser, this) = ex.parser_method::<Example>();
    let parser = parser
        .arg(&mut a)
        .parse();

    if parser.is_err() {
        return;
    }

    dbg!(a, this);
}

#[php_module]
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
    module
}
Examples found in repository?
src/closure.rs (line 144)
143
144
145
146
147
148
        extern "C" fn invoke(ex: &mut ExecuteData, ret: &mut Zval) {
            let (parser, this) = ex.parser_method::<Self>();
            let this = this.expect("Internal closure function called on non-closure class");

            this.0.invoke(parser, ret)
        }

Attempts to retrieve a reference to the underlying class object of the Zend object.

Returns a ZendClassObject if the execution data contained a valid object of type T, otherwise returns None.

Example
use ext_php_rs::{types::Zval, zend::ExecuteData, prelude::*};

#[php_class]
#[derive(Debug)]
struct Example;

#[no_mangle]
pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    let this = ex.get_object::<Example>();
    dbg!(this);
}

#[php_module]
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
    module
}
Examples found in repository?
src/builders/class.rs (line 192)
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
            extern fn constructor<T: RegisteredClass>(ex: &mut ExecuteData, _: &mut Zval) {
                let ConstructorMeta { constructor, .. } = match T::CONSTRUCTOR {
                    Some(c) => c,
                    None => {
                        PhpException::default("You cannot instantiate this class from PHP.".into())
                            .throw()
                            .expect("Failed to throw exception when constructing class");
                        return;
                    }
                };

                let this = match constructor(ex) {
                    ConstructorResult::Ok(this) => this,
                    ConstructorResult::Exception(e) => {
                        e.throw()
                            .expect("Failed to throw exception while constructing class");
                        return;
                    }
                    ConstructorResult::ArgError => return,
                };
                let this_obj = match ex.get_object::<T>() {
                    Some(obj) => obj,
                    None => {
                        PhpException::default("Failed to retrieve reference to `this` object.".into())
                            .throw()
                            .expect("Failed to throw exception while constructing class");
                        return;
                    }
                };
                this_obj.initialize(this);
            }

Attempts to retrieve the ‘this’ object, which can be used in class methods to retrieve the underlying Zend object.

Example
use ext_php_rs::{types::Zval, zend::ExecuteData};

#[no_mangle]
pub extern "C" fn example_fn(ex: &mut ExecuteData, retval: &mut Zval) {
    let this = ex.get_self();
    dbg!(this);
}
Examples found in repository?
src/zend/ex.rs (line 174)
173
174
175
    pub fn get_object<T: RegisteredClass>(&mut self) -> Option<&mut ZendClassObject<T>> {
        ZendClassObject::from_zend_obj_mut(self.get_self()?)
    }