ext_php_rs/closure.rs
1//! Types and functions used for exporting Rust closures to PHP.
2
3use crate::{
4 args::{Arg, ArgParser},
5 builders::{ClassBuilder, FunctionBuilder},
6 class::{ClassEntryInfo, ClassMetadata, RegisteredClass},
7 convert::{FromZval, IntoZval},
8 describe::DocComments,
9 exception::PhpException,
10 flags::{DataType, MethodFlags},
11 types::Zval,
12 zend::ExecuteData,
13 zend_fastcall,
14};
15
16/// Class entry and handlers for Rust closures.
17static CLOSURE_META: ClassMetadata<Closure> = ClassMetadata::new(&[]);
18
19/// Wrapper around a Rust closure, which can be exported to PHP.
20///
21/// Closures can have up to 8 parameters, all must implement [`FromZval`], and
22/// can return anything that implements [`IntoZval`]. Closures must have a
23/// static lifetime, and therefore cannot modify any `self` references.
24///
25/// Internally, closures are implemented as a PHP class. A class `RustClosure`
26/// is registered with an `__invoke` method:
27///
28/// ```php
29/// <?php
30///
31/// class RustClosure {
32/// public function __invoke(...$args): mixed {
33/// // ...
34/// }
35/// }
36/// ```
37///
38/// The Rust closure is then double boxed, firstly as a `Box<dyn Fn(...) ->
39/// ...>` (depending on the signature of the closure) and then finally boxed as
40/// a `Box<dyn PhpClosure>`. This is a workaround, as `PhpClosure` is not
41/// generically implementable on types that implement `Fn(T, ...) -> Ret`. Make
42/// a suggestion issue if you have a better idea of implementing this!.
43///
44/// When the `__invoke` method is called from PHP, the `invoke` method is called
45/// on the `dyn PhpClosure`\ trait object, and from there everything is
46/// basically the same as a regular PHP function.
47pub struct Closure(Box<dyn PhpClosure>);
48
49unsafe impl Send for Closure {}
50unsafe impl Sync for Closure {}
51
52impl Closure {
53 /// Wraps a [`Fn`] or [`FnMut`] Rust closure into a type which can be
54 /// returned to PHP.
55 ///
56 /// The closure can accept up to 8 arguments which implement [`IntoZval`],
57 /// and can return any type which implements [`FromZval`]. The closure
58 /// must have a static lifetime, so cannot reference `self`.
59 ///
60 /// # Parameters
61 ///
62 /// * `func` - The closure to wrap. Should be boxed in the form `Box<dyn
63 /// Fn[Mut](...) -> ...>`.
64 ///
65 /// # Example
66 ///
67 /// ```rust,no_run
68 /// use ext_php_rs::closure::Closure;
69 ///
70 /// let closure = Closure::wrap(Box::new(|name| {
71 /// format!("Hello {}", name)
72 /// }) as Box<dyn Fn(String) -> String>);
73 /// ```
74 pub fn wrap<T>(func: T) -> Self
75 where
76 T: PhpClosure + 'static,
77 {
78 Self(Box::new(func) as Box<dyn PhpClosure>)
79 }
80
81 /// Wraps a [`FnOnce`] Rust closure into a type which can be returned to
82 /// PHP. If the closure is called more than once from PHP, an exception
83 /// is thrown.
84 ///
85 /// The closure can accept up to 8 arguments which implement [`IntoZval`],
86 /// and can return any type which implements [`FromZval`]. The closure
87 /// must have a static lifetime, so cannot reference `self`.
88 ///
89 /// # Parameters
90 ///
91 /// * `func` - The closure to wrap. Should be boxed in the form `Box<dyn
92 /// FnOnce(...) -> ...>`.
93 ///
94 /// # Example
95 ///
96 /// ```rust,no_run
97 /// use ext_php_rs::closure::Closure;
98 ///
99 /// let name: String = "Hello world".into();
100 /// let closure = Closure::wrap_once(Box::new(|| {
101 /// name
102 /// }) as Box<dyn FnOnce() -> String>);
103 /// ```
104 pub fn wrap_once<T>(func: T) -> Self
105 where
106 T: PhpOnceClosure + 'static,
107 {
108 func.into_closure()
109 }
110
111 /// Builds the class entry for [`Closure`], registering it with PHP. This
112 /// function should only be called once inside your module startup
113 /// function.
114 ///
115 /// If the class has already been built, this function returns early without
116 /// doing anything. This allows for safe repeated calls in test
117 /// environments.
118 ///
119 /// # Panics
120 ///
121 /// Panics if the `RustClosure` PHP class cannot be registered.
122 pub fn build() {
123 if CLOSURE_META.has_ce() {
124 return;
125 }
126
127 ClassBuilder::new("RustClosure")
128 .method(
129 FunctionBuilder::new("__invoke", Self::invoke)
130 .not_required()
131 .arg(Arg::new("args", DataType::Mixed).is_variadic())
132 .returns(DataType::Mixed, false, true),
133 MethodFlags::Public,
134 )
135 .object_override::<Self>()
136 .registration(|ce| CLOSURE_META.set_ce(ce))
137 .register()
138 .expect("Failed to build `RustClosure` PHP class.");
139 }
140
141 zend_fastcall! {
142 /// External function used by the Zend interpreter to call the closure.
143 extern "C" fn invoke(ex: &mut ExecuteData, ret: &mut Zval) {
144 let (parser, this) = ex.parser_method::<Self>();
145 let this = this.expect("Internal closure function called on non-closure class");
146
147 this.0.invoke(parser, ret);
148 }
149 }
150}
151
152impl RegisteredClass for Closure {
153 const CLASS_NAME: &'static str = "RustClosure";
154
155 const BUILDER_MODIFIER: Option<fn(ClassBuilder) -> ClassBuilder> = None;
156 const EXTENDS: Option<ClassEntryInfo> = None;
157 const IMPLEMENTS: &'static [ClassEntryInfo] = &[];
158
159 fn get_metadata() -> &'static ClassMetadata<Self> {
160 &CLOSURE_META
161 }
162
163 fn method_builders() -> Vec<(FunctionBuilder<'static>, MethodFlags)> {
164 unimplemented!()
165 }
166
167 fn constructor() -> Option<crate::class::ConstructorMeta<Self>> {
168 None
169 }
170
171 fn constants() -> &'static [(
172 &'static str,
173 &'static dyn crate::convert::IntoZvalDyn,
174 DocComments,
175 )] {
176 unimplemented!()
177 }
178}
179
180class_derives!(Closure);
181
182/// Implemented on types which can be used as PHP closures.
183///
184/// Types must implement the `invoke` function which will be called when the
185/// closure is called from PHP. Arguments must be parsed from the
186/// [`ExecuteData`] and the return value is returned through the [`Zval`].
187///
188/// This trait is automatically implemented on functions with up to 8
189/// parameters.
190#[allow(clippy::missing_safety_doc)]
191pub unsafe trait PhpClosure {
192 /// Invokes the closure.
193 fn invoke<'a>(&'a mut self, parser: ArgParser<'a, '_>, ret: &mut Zval);
194}
195
196/// Implemented on [`FnOnce`] types which can be used as PHP closures. See
197/// [`Closure`].
198///
199/// Internally, this trait should wrap the [`FnOnce`] closure inside a [`FnMut`]
200/// closure, and prevent the user from calling the closure more than once.
201pub trait PhpOnceClosure {
202 /// Converts the Rust [`FnOnce`] closure into a [`FnMut`] closure, and then
203 /// into a PHP closure.
204 fn into_closure(self) -> Closure;
205}
206
207unsafe impl<R> PhpClosure for Box<dyn Fn() -> R>
208where
209 R: IntoZval,
210{
211 fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
212 if let Err(e) = self().set_zval(ret, false) {
213 let _ = PhpException::default(format!("Failed to return closure result to PHP: {e}"))
214 .throw();
215 }
216 }
217}
218
219unsafe impl<R> PhpClosure for Box<dyn FnMut() -> R>
220where
221 R: IntoZval,
222{
223 fn invoke(&mut self, _: ArgParser, ret: &mut Zval) {
224 if let Err(e) = self().set_zval(ret, false) {
225 let _ = PhpException::default(format!("Failed to return closure result to PHP: {e}"))
226 .throw();
227 }
228 }
229}
230
231impl<R> PhpOnceClosure for Box<dyn FnOnce() -> R>
232where
233 R: IntoZval + 'static,
234{
235 fn into_closure(self) -> Closure {
236 let mut this = Some(self);
237
238 Closure::wrap(Box::new(move || {
239 let Some(this) = this.take() else {
240 let _ = PhpException::default(
241 "Attempted to call `FnOnce` closure more than once.".into(),
242 )
243 .throw();
244 return Option::<R>::None;
245 };
246
247 Some(this())
248 }) as Box<dyn FnMut() -> Option<R>>)
249 }
250}
251
252macro_rules! php_closure_impl {
253 ($($gen: ident),*) => {
254 php_closure_impl!(Fn; $($gen),*);
255 php_closure_impl!(FnMut; $($gen),*);
256
257 impl<$($gen),*, Ret> PhpOnceClosure for Box<dyn FnOnce($($gen),*) -> Ret>
258 where
259 $(for<'a> $gen: FromZval<'a> + 'static,)*
260 Ret: IntoZval + 'static,
261 {
262 fn into_closure(self) -> Closure {
263 let mut this = Some(self);
264
265 Closure::wrap(Box::new(move |$($gen),*| {
266 let Some(this) = this.take() else {
267 let _ = PhpException::default(
268 "Attempted to call `FnOnce` closure more than once.".into(),
269 )
270 .throw();
271 return Option::<Ret>::None;
272 };
273
274 Some(this($($gen),*))
275 }) as Box<dyn FnMut($($gen),*) -> Option<Ret>>)
276 }
277 }
278 };
279
280 ($fnty: ident; $($gen: ident),*) => {
281 unsafe impl<$($gen),*, Ret> PhpClosure for Box<dyn $fnty($($gen),*) -> Ret>
282 where
283 $(for<'a> $gen: FromZval<'a>,)*
284 Ret: IntoZval
285 {
286 fn invoke(&mut self, parser: ArgParser, ret: &mut Zval) {
287 $(
288 let mut $gen = Arg::new(stringify!($gen), $gen::TYPE);
289 )*
290
291 let parser = parser
292 $(.arg(&mut $gen))*
293 .parse();
294
295 if parser.is_err() {
296 return;
297 }
298
299 let result = self(
300 $(
301 match $gen.consume() {
302 Ok(val) => val,
303 _ => {
304 let _ = PhpException::default(concat!("Invalid parameter type for `", stringify!($gen), "`.").into()).throw();
305 return;
306 }
307 }
308 ),*
309 );
310
311 if let Err(e) = result.set_zval(ret, false) {
312 let _ = PhpException::default(format!("Failed to return closure result to PHP: {}", e)).throw();
313 }
314 }
315 }
316 };
317}
318
319php_closure_impl!(A);
320php_closure_impl!(A, B);
321php_closure_impl!(A, B, C);
322php_closure_impl!(A, B, C, D);
323php_closure_impl!(A, B, C, D, E);
324php_closure_impl!(A, B, C, D, E, F);
325php_closure_impl!(A, B, C, D, E, F, G);
326php_closure_impl!(A, B, C, D, E, F, G, H);