/src/php-src/Zend/zend_closures.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Christian Seiler <chris_se@gmx.net> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | | Marcus Boerger <helly@php.net> | |
18 | | +----------------------------------------------------------------------+ |
19 | | */ |
20 | | |
21 | | #include "zend.h" |
22 | | #include "zend_API.h" |
23 | | #include "zend_closures.h" |
24 | | #include "zend_exceptions.h" |
25 | | #include "zend_interfaces.h" |
26 | | #include "zend_objects.h" |
27 | | #include "zend_objects_API.h" |
28 | | #include "zend_globals.h" |
29 | | #include "zend_closures_arginfo.h" |
30 | | |
31 | | typedef struct _zend_closure { |
32 | | zend_object std; |
33 | | zend_function func; |
34 | | zval this_ptr; |
35 | | zend_class_entry *called_scope; |
36 | | zif_handler orig_internal_handler; |
37 | | } zend_closure; |
38 | | |
39 | | /* non-static since it needs to be referenced */ |
40 | | ZEND_API zend_class_entry *zend_ce_closure; |
41 | | static zend_object_handlers closure_handlers; |
42 | | |
43 | | static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only); |
44 | | |
45 | | ZEND_METHOD(Closure, __invoke) /* {{{ */ |
46 | 214 | { |
47 | 214 | zend_function *func = EX(func); |
48 | 214 | zval *args; |
49 | 214 | uint32_t num_args; |
50 | 214 | HashTable *named_args; |
51 | | |
52 | 642 | ZEND_PARSE_PARAMETERS_START(0, -1) |
53 | 642 | Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args) |
54 | 642 | ZEND_PARSE_PARAMETERS_END(); |
55 | | |
56 | 214 | zend_fcall_info_cache fcc = { |
57 | 214 | .closure = Z_OBJ_P(ZEND_THIS), |
58 | 214 | }; |
59 | 214 | zend_closure_get_closure(Z_OBJ_P(ZEND_THIS), &fcc.calling_scope, &fcc.function_handler, &fcc.object, false); |
60 | 214 | fcc.called_scope = fcc.calling_scope; |
61 | 214 | zend_call_known_fcc(&fcc, return_value, num_args, args, named_args); |
62 | | |
63 | | /* destruct the function also, then - we have allocated it in get_method */ |
64 | 214 | zend_string_release_ex(func->internal_function.function_name, 0); |
65 | 214 | efree(func); |
66 | | |
67 | | /* Set the func pointer to NULL. Prior to PHP 8.3, this was only done for debug builds, |
68 | | * because debug builds check certain properties after the call and needed to know this |
69 | | * had been freed. |
70 | | * However, extensions can proxy zend_execute_internal, and it's a bit surprising to have |
71 | | * an invalid func pointer sitting on there, so this was changed in PHP 8.3. |
72 | | */ |
73 | 214 | execute_data->func = NULL; |
74 | 214 | } |
75 | | /* }}} */ |
76 | | |
77 | | static bool zend_valid_closure_binding( |
78 | | zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */ |
79 | 1.62k | { |
80 | 1.62k | zend_function *func = &closure->func; |
81 | 1.62k | bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0; |
82 | 1.62k | if (newthis) { |
83 | 1.40k | if (func->common.fn_flags & ZEND_ACC_STATIC) { |
84 | 41 | zend_error(E_WARNING, "Cannot bind an instance to a static closure, this will be an error in PHP 9"); |
85 | 41 | return false; |
86 | 41 | } |
87 | | |
88 | 1.36k | if (is_fake_closure && func->common.scope && |
89 | 75 | !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) { |
90 | | /* Binding incompatible $this to an internal method is not supported. */ |
91 | 11 | zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s, this will be an error in PHP 9", |
92 | 11 | ZSTR_VAL(func->common.scope->name), |
93 | 11 | ZSTR_VAL(func->common.function_name), |
94 | 11 | ZSTR_VAL(Z_OBJCE_P(newthis)->name)); |
95 | 11 | return false; |
96 | 11 | } |
97 | 1.36k | } else if (is_fake_closure && func->common.scope |
98 | 7 | && !(func->common.fn_flags & ZEND_ACC_STATIC)) { |
99 | 7 | zend_error(E_WARNING, "Cannot unbind $this of method, this will be an error in PHP 9"); |
100 | 7 | return false; |
101 | 213 | } else if (!is_fake_closure && !Z_ISUNDEF(closure->this_ptr) |
102 | 36 | && (func->common.fn_flags & ZEND_ACC_USES_THIS)) { |
103 | 31 | zend_error(E_WARNING, "Cannot unbind $this of closure using $this, this will be an error in PHP 9"); |
104 | 31 | return false; |
105 | 31 | } |
106 | | |
107 | 1.53k | if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) { |
108 | | /* rebinding to internal class is not allowed */ |
109 | 393 | zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s, this will be an error in PHP 9", |
110 | 393 | ZSTR_VAL(scope->name)); |
111 | 393 | return false; |
112 | 393 | } |
113 | | |
114 | 1.14k | if (is_fake_closure && scope != func->common.scope) { |
115 | 11 | if (func->common.scope == NULL) { |
116 | 6 | zend_error(E_WARNING, "Cannot rebind scope of closure created from function, this will be an error in PHP 9"); |
117 | 6 | } else { |
118 | 5 | zend_error(E_WARNING, "Cannot rebind scope of closure created from method, this will be an error in PHP 9"); |
119 | 5 | } |
120 | 11 | return false; |
121 | 11 | } |
122 | | |
123 | 1.13k | return true; |
124 | 1.14k | } |
125 | | /* }}} */ |
126 | | |
127 | | /* {{{ Call closure, binding to a given object with its class as the scope */ |
128 | | ZEND_METHOD(Closure, call) |
129 | 1.11k | { |
130 | 1.11k | zval *newthis, closure_result; |
131 | 1.11k | zend_closure *closure; |
132 | 1.11k | zend_fcall_info fci; |
133 | 1.11k | zend_fcall_info_cache fci_cache; |
134 | 1.11k | zend_object *newobj; |
135 | 1.11k | zend_class_entry *newclass; |
136 | | |
137 | 1.11k | fci.param_count = 0; |
138 | 1.11k | fci.params = NULL; |
139 | | |
140 | 3.33k | ZEND_PARSE_PARAMETERS_START(1, -1) |
141 | 4.44k | Z_PARAM_OBJECT(newthis) |
142 | 1.10k | Z_PARAM_VARIADIC_WITH_NAMED(fci.params, fci.param_count, fci.named_params) |
143 | 1.11k | ZEND_PARSE_PARAMETERS_END(); |
144 | | |
145 | 1.10k | closure = (zend_closure *) Z_OBJ_P(ZEND_THIS); |
146 | | |
147 | 1.10k | newobj = Z_OBJ_P(newthis); |
148 | 1.10k | newclass = newobj->ce; |
149 | | |
150 | 1.10k | if (!zend_valid_closure_binding(closure, newthis, newclass)) { |
151 | 405 | return; |
152 | 405 | } |
153 | | |
154 | 704 | fci_cache.called_scope = newclass; |
155 | 704 | fci_cache.object = fci.object = newobj; |
156 | | |
157 | 704 | fci.size = sizeof(fci); |
158 | 704 | ZVAL_OBJ(&fci.function_name, &closure->std); |
159 | 704 | ZVAL_UNDEF(&closure_result); |
160 | 704 | fci.retval = &closure_result; |
161 | | |
162 | 704 | if (closure->func.common.fn_flags & ZEND_ACC_GENERATOR) { |
163 | 6 | zval new_closure; |
164 | 6 | zend_create_closure(&new_closure, &closure->func, newclass, closure->called_scope, newthis); |
165 | 6 | closure = (zend_closure *) Z_OBJ(new_closure); |
166 | 6 | fci_cache.function_handler = &closure->func; |
167 | | |
168 | 6 | zend_call_function(&fci, &fci_cache); |
169 | | |
170 | | /* copied upon generator creation */ |
171 | 6 | GC_DELREF(&closure->std); |
172 | 698 | } else { |
173 | 698 | zend_closure *fake_closure; |
174 | 698 | zend_function *my_function; |
175 | | |
176 | 698 | fake_closure = emalloc(sizeof(zend_closure)); |
177 | 698 | memset(&fake_closure->std, 0, sizeof(fake_closure->std)); |
178 | 698 | fake_closure->std.gc.refcount = 1; |
179 | 698 | fake_closure->std.gc.u.type_info = GC_NULL; |
180 | 698 | ZVAL_UNDEF(&fake_closure->this_ptr); |
181 | 698 | fake_closure->called_scope = NULL; |
182 | 698 | my_function = &fake_closure->func; |
183 | 698 | if (ZEND_USER_CODE(closure->func.type)) { |
184 | 686 | memcpy(my_function, &closure->func, sizeof(zend_op_array)); |
185 | 686 | } else { |
186 | 12 | memcpy(my_function, &closure->func, sizeof(zend_internal_function)); |
187 | 12 | } |
188 | | /* use scope of passed object */ |
189 | 698 | my_function->common.scope = newclass; |
190 | 698 | if (closure->func.type == ZEND_INTERNAL_FUNCTION) { |
191 | 12 | my_function->internal_function.handler = closure->orig_internal_handler; |
192 | 12 | } |
193 | 698 | fci_cache.function_handler = my_function; |
194 | | |
195 | | /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */ |
196 | 698 | if (ZEND_USER_CODE(my_function->type) |
197 | 686 | && (closure->func.common.scope != newclass |
198 | 686 | || (closure->func.common.fn_flags & ZEND_ACC_HEAP_RT_CACHE))) { |
199 | 686 | void *ptr; |
200 | | |
201 | 686 | my_function->op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; |
202 | 686 | ptr = emalloc(my_function->op_array.cache_size); |
203 | 686 | ZEND_MAP_PTR_INIT(my_function->op_array.run_time_cache, ptr); |
204 | 686 | memset(ptr, 0, my_function->op_array.cache_size); |
205 | 686 | } |
206 | | |
207 | 698 | zend_call_function(&fci, &fci_cache); |
208 | | |
209 | 698 | if (ZEND_USER_CODE(my_function->type)) { |
210 | 686 | if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) { |
211 | 686 | efree(ZEND_MAP_PTR(my_function->op_array.run_time_cache)); |
212 | 686 | } |
213 | 686 | } |
214 | 698 | efree_size(fake_closure, sizeof(zend_closure)); |
215 | 698 | } |
216 | | |
217 | 704 | if (Z_TYPE(closure_result) != IS_UNDEF) { |
218 | 699 | if (Z_ISREF(closure_result)) { |
219 | 11 | zend_unwrap_reference(&closure_result); |
220 | 11 | } |
221 | 699 | ZVAL_COPY_VALUE(return_value, &closure_result); |
222 | 699 | } |
223 | 704 | } |
224 | | /* }}} */ |
225 | | |
226 | | static void do_closure_bind(zval *return_value, zval *zclosure, zval *newthis, zend_object *scope_obj, zend_string *scope_str) |
227 | 524 | { |
228 | 524 | zend_class_entry *ce, *called_scope; |
229 | 524 | zend_closure *closure = (zend_closure *) Z_OBJ_P(zclosure); |
230 | | |
231 | 524 | if (scope_obj) { |
232 | 26 | ce = scope_obj->ce; |
233 | 498 | } else if (scope_str) { |
234 | 411 | if (zend_string_equals(scope_str, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
235 | 208 | ce = closure->func.common.scope; |
236 | 208 | } else if ((ce = zend_lookup_class(scope_str)) == NULL) { |
237 | 7 | zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(scope_str)); |
238 | 7 | RETURN_NULL(); |
239 | 7 | } |
240 | 411 | } else { |
241 | 87 | ce = NULL; |
242 | 87 | } |
243 | | |
244 | 517 | if (!zend_valid_closure_binding(closure, newthis, ce)) { |
245 | 89 | return; |
246 | 89 | } |
247 | | |
248 | 428 | if (newthis) { |
249 | 246 | called_scope = Z_OBJCE_P(newthis); |
250 | 246 | } else { |
251 | 182 | called_scope = ce; |
252 | 182 | } |
253 | | |
254 | 428 | zend_create_closure(return_value, &closure->func, ce, called_scope, newthis); |
255 | 428 | } |
256 | | |
257 | | /* {{{ Create a closure from another one and bind to another object and scope */ |
258 | | ZEND_METHOD(Closure, bind) |
259 | 46 | { |
260 | 46 | zval *zclosure, *newthis; |
261 | 46 | zend_object *scope_obj = NULL; |
262 | 46 | zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC); |
263 | | |
264 | 138 | ZEND_PARSE_PARAMETERS_START(2, 3) |
265 | 184 | Z_PARAM_OBJECT_OF_CLASS(zclosure, zend_ce_closure) |
266 | 230 | Z_PARAM_OBJECT_OR_NULL(newthis) |
267 | 46 | Z_PARAM_OPTIONAL |
268 | 215 | Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str) |
269 | 215 | ZEND_PARSE_PARAMETERS_END(); |
270 | | |
271 | 46 | do_closure_bind(return_value, zclosure, newthis, scope_obj, scope_str); |
272 | 46 | } |
273 | | |
274 | | /* {{{ Create a closure from another one and bind to another object and scope */ |
275 | | ZEND_METHOD(Closure, bindTo) |
276 | 484 | { |
277 | 484 | zval *newthis; |
278 | 484 | zend_object *scope_obj = NULL; |
279 | 484 | zend_string *scope_str = ZSTR_KNOWN(ZEND_STR_STATIC); |
280 | | |
281 | 1.45k | ZEND_PARSE_PARAMETERS_START(1, 2) |
282 | 1.93k | Z_PARAM_OBJECT_OR_NULL(newthis) |
283 | 483 | Z_PARAM_OPTIONAL |
284 | 1.87k | Z_PARAM_OBJ_OR_STR_OR_NULL(scope_obj, scope_str) |
285 | 1.87k | ZEND_PARSE_PARAMETERS_END(); |
286 | | |
287 | 478 | do_closure_bind(return_value, ZEND_THIS, newthis, scope_obj, scope_str); |
288 | 478 | } |
289 | | |
290 | 271 | static ZEND_NAMED_FUNCTION(zend_closure_call_magic) /* {{{ */ { |
291 | 271 | zend_fcall_info fci; |
292 | 271 | zend_fcall_info_cache fcc; |
293 | 271 | zval params[2]; |
294 | | |
295 | 271 | memset(&fci, 0, sizeof(zend_fcall_info)); |
296 | 271 | memset(&fcc, 0, sizeof(zend_fcall_info_cache)); |
297 | | |
298 | 271 | fci.size = sizeof(zend_fcall_info); |
299 | 271 | fci.retval = return_value; |
300 | | |
301 | 271 | fcc.function_handler = (EX(func)->internal_function.fn_flags & ZEND_ACC_STATIC) ? |
302 | 223 | EX(func)->internal_function.scope->__callstatic : EX(func)->internal_function.scope->__call; |
303 | 271 | fci.named_params = NULL; |
304 | 271 | fci.params = params; |
305 | 271 | fci.param_count = 2; |
306 | 271 | ZVAL_STR(&fci.params[0], EX(func)->common.function_name); |
307 | 271 | if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) { |
308 | 29 | zend_string *name; |
309 | 29 | zval *named_param_zval; |
310 | 29 | array_init_size(&fci.params[1], ZEND_NUM_ARGS() + zend_hash_num_elements(EX(extra_named_params))); |
311 | | /* Avoid conversion from packed to mixed later. */ |
312 | 29 | zend_hash_real_init_mixed(Z_ARRVAL(fci.params[1])); |
313 | 29 | zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]); |
314 | 174 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EX(extra_named_params), name, named_param_zval) { |
315 | 174 | Z_TRY_ADDREF_P(named_param_zval); |
316 | 174 | zend_hash_add_new(Z_ARRVAL(fci.params[1]), name, named_param_zval); |
317 | 174 | } ZEND_HASH_FOREACH_END(); |
318 | 242 | } else if (ZEND_NUM_ARGS()) { |
319 | 164 | array_init_size(&fci.params[1], ZEND_NUM_ARGS()); |
320 | 164 | zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]); |
321 | 164 | } else { |
322 | 78 | ZVAL_EMPTY_ARRAY(&fci.params[1]); |
323 | 78 | } |
324 | | |
325 | 271 | fcc.object = fci.object = Z_OBJ_P(ZEND_THIS); |
326 | 271 | fcc.called_scope = zend_get_called_scope(EG(current_execute_data)); |
327 | | |
328 | 271 | zend_call_function(&fci, &fcc); |
329 | | |
330 | 271 | zval_ptr_dtor(&fci.params[1]); |
331 | 271 | } |
332 | | /* }}} */ |
333 | | |
334 | 699 | static zend_result zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ { |
335 | 699 | zend_fcall_info_cache fcc; |
336 | 699 | zend_function *mptr; |
337 | 699 | zval instance; |
338 | 699 | zend_internal_function call; |
339 | | |
340 | 699 | if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) { |
341 | 106 | return FAILURE; |
342 | 106 | } |
343 | | |
344 | 593 | mptr = fcc.function_handler; |
345 | 593 | if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) { |
346 | | /* For Closure::fromCallable([$closure, "__invoke"]) return $closure. */ |
347 | 126 | if (fcc.object && fcc.object->ce == zend_ce_closure |
348 | 48 | && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) { |
349 | 48 | RETVAL_OBJ_COPY(fcc.object); |
350 | 48 | zend_free_trampoline(mptr); |
351 | 48 | return SUCCESS; |
352 | 48 | } |
353 | | |
354 | 78 | if (!mptr->common.scope) { |
355 | 0 | return FAILURE; |
356 | 0 | } |
357 | 78 | if (mptr->common.fn_flags & ZEND_ACC_STATIC) { |
358 | 5 | if (!mptr->common.scope->__callstatic) { |
359 | 0 | return FAILURE; |
360 | 0 | } |
361 | 73 | } else { |
362 | 73 | if (!mptr->common.scope->__call) { |
363 | 0 | return FAILURE; |
364 | 0 | } |
365 | 73 | } |
366 | | |
367 | 78 | memset(&call, 0, sizeof(zend_internal_function)); |
368 | 78 | call.type = ZEND_INTERNAL_FUNCTION; |
369 | 78 | call.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED); |
370 | 78 | call.handler = zend_closure_call_magic; |
371 | 78 | call.function_name = mptr->common.function_name; |
372 | 78 | call.scope = mptr->common.scope; |
373 | 78 | call.doc_comment = NULL; |
374 | 78 | call.attributes = mptr->common.attributes; |
375 | | |
376 | 78 | zend_free_trampoline(mptr); |
377 | 78 | mptr = (zend_function *) &call; |
378 | 78 | } |
379 | | |
380 | 545 | if (fcc.object) { |
381 | 316 | ZVAL_OBJ(&instance, fcc.object); |
382 | 316 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance); |
383 | 316 | } else { |
384 | 229 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL); |
385 | 229 | } |
386 | | |
387 | 545 | if (&mptr->internal_function == &call) { |
388 | 78 | zend_string_release(mptr->common.function_name); |
389 | 78 | } |
390 | | |
391 | 545 | return SUCCESS; |
392 | 593 | } |
393 | | /* }}} */ |
394 | | |
395 | | /* {{{ Create a closure from a callable using the current scope. */ |
396 | | ZEND_METHOD(Closure, fromCallable) |
397 | 704 | { |
398 | 704 | zval *callable; |
399 | 704 | char *error = NULL; |
400 | | |
401 | 2.11k | ZEND_PARSE_PARAMETERS_START(1, 1) |
402 | 2.81k | Z_PARAM_ZVAL(callable) |
403 | 2.81k | ZEND_PARSE_PARAMETERS_END(); |
404 | | |
405 | 704 | if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) { |
406 | | /* It's already a closure */ |
407 | 5 | RETURN_COPY(callable); |
408 | 5 | } |
409 | | |
410 | 699 | if (zend_create_closure_from_callable(return_value, callable, &error) == FAILURE) { |
411 | 106 | if (error) { |
412 | 106 | zend_type_error("Failed to create closure from callable: %s", error); |
413 | 106 | efree(error); |
414 | 106 | } else { |
415 | 0 | zend_type_error("Failed to create closure from callable"); |
416 | 0 | } |
417 | 106 | } |
418 | 699 | } |
419 | | /* }}} */ |
420 | | |
421 | | ZEND_METHOD(Closure, getCurrent) |
422 | 63 | { |
423 | 63 | ZEND_PARSE_PARAMETERS_NONE(); |
424 | | |
425 | 63 | zend_execute_data *prev_ex = EX(prev_execute_data); |
426 | | |
427 | 63 | if (!prev_ex |
428 | 63 | || !prev_ex->func |
429 | 63 | || (prev_ex->func->common.fn_flags & (ZEND_ACC_CLOSURE|ZEND_ACC_FAKE_CLOSURE)) != ZEND_ACC_CLOSURE) { |
430 | 10 | zend_throw_error(NULL, "Current function is not a closure"); |
431 | 10 | RETURN_THROWS(); |
432 | 10 | } |
433 | | |
434 | 53 | zend_object *obj = ZEND_CLOSURE_OBJECT(prev_ex->func); |
435 | 53 | RETURN_OBJ_COPY(obj); |
436 | 53 | } |
437 | | |
438 | | static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */ |
439 | 11 | { |
440 | 11 | zend_throw_error(NULL, "Instantiation of class Closure is not allowed"); |
441 | 11 | return NULL; |
442 | 11 | } |
443 | | /* }}} */ |
444 | | |
445 | | /* int return due to Object Handler API */ |
446 | | static int zend_closure_compare(zval *o1, zval *o2) /* {{{ */ |
447 | 192 | { |
448 | 192 | ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2); |
449 | | |
450 | 192 | zend_closure *lhs = (zend_closure*) Z_OBJ_P(o1); |
451 | 192 | zend_closure *rhs = (zend_closure*) Z_OBJ_P(o2); |
452 | | |
453 | 192 | if (!((lhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE) && (rhs->func.common.fn_flags & ZEND_ACC_FAKE_CLOSURE))) { |
454 | 0 | return ZEND_UNCOMPARABLE; |
455 | 0 | } |
456 | | |
457 | 192 | if (Z_TYPE(lhs->this_ptr) != Z_TYPE(rhs->this_ptr)) { |
458 | 3 | return ZEND_UNCOMPARABLE; |
459 | 3 | } |
460 | | |
461 | 189 | if (Z_TYPE(lhs->this_ptr) == IS_OBJECT && Z_OBJ(lhs->this_ptr) != Z_OBJ(rhs->this_ptr)) { |
462 | 41 | return ZEND_UNCOMPARABLE; |
463 | 41 | } |
464 | | |
465 | 148 | if (lhs->called_scope != rhs->called_scope) { |
466 | 17 | return ZEND_UNCOMPARABLE; |
467 | 17 | } |
468 | | |
469 | 131 | if (lhs->func.type != rhs->func.type) { |
470 | 0 | return ZEND_UNCOMPARABLE; |
471 | 0 | } |
472 | | |
473 | 131 | if (lhs->func.common.scope != rhs->func.common.scope) { |
474 | 0 | return ZEND_UNCOMPARABLE; |
475 | 0 | } |
476 | | |
477 | 131 | if (!zend_string_equals(lhs->func.common.function_name, rhs->func.common.function_name)) { |
478 | 53 | return ZEND_UNCOMPARABLE; |
479 | 53 | } |
480 | | |
481 | 78 | return 0; |
482 | 131 | } |
483 | | /* }}} */ |
484 | | |
485 | | ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */ |
486 | 420 | { |
487 | 420 | zend_closure *closure = (zend_closure *)object; |
488 | 420 | zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function)); |
489 | 420 | const uint32_t keep_flags = |
490 | 420 | ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE | ZEND_ACC_DEPRECATED; |
491 | | |
492 | 420 | invoke->common = closure->func.common; |
493 | | /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the |
494 | | * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*). |
495 | | * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set, |
496 | | * and we won't check arguments on internal function. We also set |
497 | | * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */ |
498 | 420 | invoke->type = ZEND_INTERNAL_FUNCTION; |
499 | 420 | invoke->internal_function.fn_flags = |
500 | 420 | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags); |
501 | 420 | if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) { |
502 | 341 | invoke->internal_function.fn_flags |= |
503 | 341 | ZEND_ACC_USER_ARG_INFO; |
504 | 341 | } |
505 | 420 | invoke->internal_function.handler = ZEND_MN(Closure___invoke); |
506 | 420 | invoke->internal_function.doc_comment = NULL; |
507 | 420 | invoke->internal_function.module = 0; |
508 | 420 | invoke->internal_function.scope = zend_ce_closure; |
509 | 420 | invoke->internal_function.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE); |
510 | 420 | return invoke; |
511 | 420 | } |
512 | | /* }}} */ |
513 | | |
514 | | ZEND_API const zend_function *zend_get_closure_method_def(zend_object *obj) /* {{{ */ |
515 | 562 | { |
516 | 562 | zend_closure *closure = (zend_closure *) obj; |
517 | 562 | return &closure->func; |
518 | 562 | } |
519 | | /* }}} */ |
520 | | |
521 | | ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */ |
522 | 10 | { |
523 | 10 | zend_closure *closure = (zend_closure *)Z_OBJ_P(obj); |
524 | 10 | return &closure->this_ptr; |
525 | 10 | } |
526 | | /* }}} */ |
527 | | |
528 | | static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */ |
529 | 1.77k | { |
530 | 1.77k | if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) { |
531 | 388 | return zend_get_closure_invoke_method(*object); |
532 | 388 | } |
533 | | |
534 | 1.38k | return zend_std_get_method(object, method, key); |
535 | 1.77k | } |
536 | | /* }}} */ |
537 | | |
538 | | static void zend_closure_free_storage(zend_object *object) /* {{{ */ |
539 | 13.0k | { |
540 | 13.0k | zend_closure *closure = (zend_closure *)object; |
541 | | |
542 | 13.0k | zend_object_std_dtor(&closure->std); |
543 | | |
544 | 13.0k | if (closure->func.type == ZEND_USER_FUNCTION) { |
545 | | /* We don't own the static variables of fake closures. */ |
546 | 12.3k | if (!(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) { |
547 | 11.7k | zend_destroy_static_vars(&closure->func.op_array); |
548 | 11.7k | } |
549 | 12.3k | destroy_op_array(&closure->func.op_array); |
550 | 12.3k | } else if (closure->func.type == ZEND_INTERNAL_FUNCTION) { |
551 | 717 | zend_string_release(closure->func.common.function_name); |
552 | 717 | } |
553 | | |
554 | 13.0k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
555 | 2.73k | zval_ptr_dtor(&closure->this_ptr); |
556 | 2.73k | } |
557 | 13.0k | } |
558 | | /* }}} */ |
559 | | |
560 | | static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */ |
561 | 13.0k | { |
562 | 13.0k | zend_closure *closure; |
563 | | |
564 | 13.0k | closure = emalloc(sizeof(zend_closure)); |
565 | 13.0k | memset(closure, 0, sizeof(zend_closure)); |
566 | | |
567 | 13.0k | zend_object_std_init(&closure->std, class_type); |
568 | | |
569 | 13.0k | return (zend_object*)closure; |
570 | 13.0k | } |
571 | | /* }}} */ |
572 | | |
573 | | static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */ |
574 | 23 | { |
575 | 23 | zend_closure *closure = (zend_closure *)zobject; |
576 | 23 | zval result; |
577 | | |
578 | 23 | zend_create_closure(&result, &closure->func, |
579 | 23 | closure->func.common.scope, closure->called_scope, &closure->this_ptr); |
580 | 23 | return Z_OBJ(result); |
581 | 23 | } |
582 | | /* }}} */ |
583 | | |
584 | | static zend_result zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */ |
585 | 15.1k | { |
586 | 15.1k | zend_closure *closure = (zend_closure*)obj; |
587 | | |
588 | 15.1k | *fptr_ptr = &closure->func; |
589 | 15.1k | *ce_ptr = closure->called_scope; |
590 | | |
591 | 15.1k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
592 | 1.45k | *obj_ptr = Z_OBJ(closure->this_ptr); |
593 | 13.7k | } else { |
594 | 13.7k | *obj_ptr = NULL; |
595 | 13.7k | } |
596 | | |
597 | 15.1k | return SUCCESS; |
598 | 15.1k | } |
599 | | /* }}} */ |
600 | | |
601 | | /* *is_temp is int due to Object Handler API */ |
602 | | static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */ |
603 | 1.08k | { |
604 | 1.08k | zend_closure *closure = (zend_closure *)object; |
605 | 1.08k | zval val; |
606 | 1.08k | struct _zend_arg_info *arg_info = closure->func.common.arg_info; |
607 | 1.08k | HashTable *debug_info; |
608 | 1.08k | bool zstr_args = (closure->func.type == ZEND_USER_FUNCTION) || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO); |
609 | | |
610 | 1.08k | *is_temp = 1; |
611 | | |
612 | 1.08k | debug_info = zend_new_array(8); |
613 | | |
614 | 1.08k | if (closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE) { |
615 | 201 | if (closure->func.common.scope) { |
616 | 45 | zend_string *class_name = closure->func.common.scope->name; |
617 | 45 | zend_string *func_name = closure->func.common.function_name; |
618 | 45 | zend_string *combined = zend_string_concat3( |
619 | 45 | ZSTR_VAL(class_name), ZSTR_LEN(class_name), |
620 | 45 | "::", strlen("::"), |
621 | 45 | ZSTR_VAL(func_name), ZSTR_LEN(func_name) |
622 | 45 | ); |
623 | 45 | ZVAL_STR(&val, combined); |
624 | 156 | } else { |
625 | 156 | ZVAL_STR_COPY(&val, closure->func.common.function_name); |
626 | 156 | } |
627 | 201 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FUNCTION), &val); |
628 | 887 | } else { |
629 | 887 | ZVAL_STR_COPY(&val, closure->func.common.function_name); |
630 | 887 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_NAME), &val); |
631 | | |
632 | 887 | ZVAL_STR_COPY(&val, closure->func.op_array.filename); |
633 | 887 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_FILE), &val); |
634 | | |
635 | 887 | ZVAL_LONG(&val, closure->func.op_array.line_start); |
636 | 887 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_LINE), &val); |
637 | 887 | } |
638 | | |
639 | 1.08k | if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) { |
640 | 135 | zval *var; |
641 | 135 | zend_string *key; |
642 | 135 | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
643 | | |
644 | 135 | array_init(&val); |
645 | | |
646 | 586 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(static_variables, key, var) { |
647 | 586 | zval copy; |
648 | | |
649 | 586 | if (Z_ISREF_P(var) && Z_REFCOUNT_P(var) == 1) { |
650 | 31 | var = Z_REFVAL_P(var); |
651 | 31 | } |
652 | 586 | ZVAL_COPY(©, var); |
653 | | |
654 | 586 | zend_hash_add_new(Z_ARRVAL(val), key, ©); |
655 | 586 | } ZEND_HASH_FOREACH_END(); |
656 | | |
657 | 135 | if (zend_hash_num_elements(Z_ARRVAL(val))) { |
658 | 109 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_STATIC), &val); |
659 | 109 | } else { |
660 | 26 | zval_ptr_dtor(&val); |
661 | 26 | } |
662 | 135 | } |
663 | | |
664 | 1.08k | if (Z_TYPE(closure->this_ptr) != IS_UNDEF) { |
665 | 615 | Z_ADDREF(closure->this_ptr); |
666 | 615 | zend_hash_update(debug_info, ZSTR_KNOWN(ZEND_STR_THIS), &closure->this_ptr); |
667 | 615 | } |
668 | | |
669 | 1.08k | if (arg_info && |
670 | 194 | (closure->func.common.num_args || |
671 | 189 | (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) { |
672 | 189 | uint32_t i, num_args, required = closure->func.common.required_num_args; |
673 | | |
674 | 189 | array_init(&val); |
675 | | |
676 | 189 | num_args = closure->func.common.num_args; |
677 | 189 | if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) { |
678 | 21 | num_args++; |
679 | 21 | } |
680 | 399 | for (i = 0; i < num_args; i++) { |
681 | 210 | zend_string *name; |
682 | 210 | zval info; |
683 | 210 | ZEND_ASSERT(arg_info->name && "Argument should have name"); |
684 | 210 | if (zstr_args) { |
685 | 66 | name = zend_strpprintf(0, "%s$%s", |
686 | 66 | ZEND_ARG_SEND_MODE(arg_info) ? "&" : "", |
687 | 66 | ZSTR_VAL(arg_info->name)); |
688 | 144 | } else { |
689 | 144 | name = zend_strpprintf(0, "%s$%s", |
690 | 144 | ZEND_ARG_SEND_MODE(arg_info) ? "&" : "", |
691 | 144 | ((zend_internal_arg_info*)arg_info)->name); |
692 | 144 | } |
693 | 210 | ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>")); |
694 | 210 | zend_hash_update(Z_ARRVAL(val), name, &info); |
695 | 210 | zend_string_release_ex(name, 0); |
696 | 210 | arg_info++; |
697 | 210 | } |
698 | 189 | zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val); |
699 | 189 | } |
700 | | |
701 | 1.08k | return debug_info; |
702 | 1.08k | } |
703 | | /* }}} */ |
704 | | |
705 | | static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */ |
706 | 12.9k | { |
707 | 12.9k | zend_closure *closure = (zend_closure *)obj; |
708 | | |
709 | 12.9k | *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL; |
710 | 12.9k | *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0; |
711 | | /* Fake closures don't own the static variables they reference. */ |
712 | 12.9k | return (closure->func.type == ZEND_USER_FUNCTION |
713 | 12.7k | && !(closure->func.op_array.fn_flags & ZEND_ACC_FAKE_CLOSURE)) ? |
714 | 12.9k | ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr) : NULL; |
715 | 12.9k | } |
716 | | /* }}} */ |
717 | | |
718 | | /* {{{ Private constructor preventing instantiation */ |
719 | | ZEND_COLD ZEND_METHOD(Closure, __construct) |
720 | 0 | { |
721 | 0 | zend_throw_error(NULL, "Instantiation of class Closure is not allowed"); |
722 | 0 | } |
723 | | /* }}} */ |
724 | | |
725 | | void zend_register_closure_ce(void) /* {{{ */ |
726 | 16 | { |
727 | 16 | zend_ce_closure = register_class_Closure(); |
728 | 16 | zend_ce_closure->create_object = zend_closure_new; |
729 | 16 | zend_ce_closure->default_object_handlers = &closure_handlers; |
730 | | |
731 | 16 | memcpy(&closure_handlers, &std_object_handlers, sizeof(zend_object_handlers)); |
732 | 16 | closure_handlers.free_obj = zend_closure_free_storage; |
733 | 16 | closure_handlers.get_constructor = zend_closure_get_constructor; |
734 | 16 | closure_handlers.get_method = zend_closure_get_method; |
735 | 16 | closure_handlers.compare = zend_closure_compare; |
736 | 16 | closure_handlers.clone_obj = zend_closure_clone; |
737 | 16 | closure_handlers.get_debug_info = zend_closure_get_debug_info; |
738 | 16 | closure_handlers.get_closure = zend_closure_get_closure; |
739 | 16 | closure_handlers.get_gc = zend_closure_get_gc; |
740 | 16 | } |
741 | | /* }}} */ |
742 | | |
743 | | static ZEND_NAMED_FUNCTION(zend_closure_internal_handler) /* {{{ */ |
744 | 476 | { |
745 | 476 | zend_closure *closure = (zend_closure*)ZEND_CLOSURE_OBJECT(EX(func)); |
746 | 476 | closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); |
747 | | // Assign to EX(this) so that it is released after observer checks etc. |
748 | 476 | ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_RELEASE_THIS); |
749 | 476 | Z_OBJ(EX(This)) = &closure->std; |
750 | 476 | } |
751 | | /* }}} */ |
752 | | |
753 | | static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr, bool is_fake) /* {{{ */ |
754 | 13.0k | { |
755 | 13.0k | zend_closure *closure; |
756 | 13.0k | void *ptr; |
757 | | |
758 | 13.0k | object_init_ex(res, zend_ce_closure); |
759 | | |
760 | 13.0k | closure = (zend_closure *)Z_OBJ_P(res); |
761 | | |
762 | 13.0k | if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) { |
763 | | /* use dummy scope if we're binding an object without specifying a scope */ |
764 | | /* maybe it would be better to create one for this purpose */ |
765 | 74 | scope = zend_ce_closure; |
766 | 74 | } |
767 | | |
768 | 13.0k | if (func->type == ZEND_USER_FUNCTION) { |
769 | 12.3k | memcpy(&closure->func, func, sizeof(zend_op_array)); |
770 | 12.3k | closure->func.common.fn_flags |= ZEND_ACC_CLOSURE; |
771 | 12.3k | closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE; |
772 | | |
773 | 12.3k | zend_string_addref(closure->func.op_array.function_name); |
774 | 12.3k | if (closure->func.op_array.refcount) { |
775 | 577 | (*closure->func.op_array.refcount)++; |
776 | 577 | } |
777 | | |
778 | | /* For fake closures, we want to reuse the static variables of the original function. */ |
779 | 12.3k | HashTable *ht = ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr); |
780 | 12.3k | if (!is_fake) { |
781 | 11.7k | if (!ht) { |
782 | 11.6k | ht = closure->func.op_array.static_variables; |
783 | 11.6k | } |
784 | 11.7k | ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr, |
785 | 11.7k | ht ? zend_array_dup(ht) : NULL); |
786 | 11.7k | } else if (func->op_array.static_variables) { |
787 | 94 | if (!ht) { |
788 | 45 | ht = zend_array_dup(func->op_array.static_variables); |
789 | 45 | ZEND_MAP_PTR_SET(func->op_array.static_variables_ptr, ht); |
790 | 45 | } |
791 | 94 | ZEND_MAP_PTR_INIT(closure->func.op_array.static_variables_ptr, ht); |
792 | 94 | } |
793 | | |
794 | | /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */ |
795 | 12.3k | ptr = ZEND_MAP_PTR_GET(func->op_array.run_time_cache); |
796 | 12.3k | if (!ptr |
797 | 2.18k | || func->common.scope != scope |
798 | 1.92k | || (func->common.fn_flags & ZEND_ACC_HEAP_RT_CACHE) |
799 | 12.3k | ) { |
800 | 10.5k | if (!ptr |
801 | 10.1k | && (func->common.fn_flags & ZEND_ACC_CLOSURE) |
802 | 9.90k | && (func->common.scope == scope || |
803 | 7.48k | !(func->common.fn_flags & ZEND_ACC_IMMUTABLE))) { |
804 | | /* If a real closure is used for the first time, we create a shared runtime cache |
805 | | * and remember which scope it is for. */ |
806 | 7.48k | if (func->common.scope != scope) { |
807 | 8 | func->common.scope = scope; |
808 | 8 | } |
809 | 7.48k | ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size); |
810 | 7.48k | ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr); |
811 | 7.48k | closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE; |
812 | 7.48k | } else { |
813 | | /* Otherwise, we use a non-shared runtime cache */ |
814 | 3.04k | ptr = emalloc(func->op_array.cache_size); |
815 | 3.04k | closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE; |
816 | 3.04k | } |
817 | 10.5k | memset(ptr, 0, func->op_array.cache_size); |
818 | 10.5k | } |
819 | 12.3k | ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr); |
820 | 12.3k | } else { |
821 | 717 | memcpy(&closure->func, func, sizeof(zend_internal_function)); |
822 | 717 | closure->func.common.fn_flags |= ZEND_ACC_CLOSURE; |
823 | | /* wrap internal function handler to avoid memory leak */ |
824 | 717 | if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) { |
825 | | /* avoid infinity recursion, by taking handler from nested closure */ |
826 | 35 | zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func)); |
827 | 35 | ZEND_ASSERT(nested->std.ce == zend_ce_closure); |
828 | 35 | closure->orig_internal_handler = nested->orig_internal_handler; |
829 | 682 | } else { |
830 | 682 | closure->orig_internal_handler = closure->func.internal_function.handler; |
831 | 682 | } |
832 | 717 | closure->func.internal_function.handler = zend_closure_internal_handler; |
833 | 717 | zend_string_addref(closure->func.op_array.function_name); |
834 | 717 | if (!func->common.scope) { |
835 | | /* if it's a free function, we won't set scope & this since they're meaningless */ |
836 | 361 | this_ptr = NULL; |
837 | 361 | scope = NULL; |
838 | 361 | } |
839 | 717 | } |
840 | | |
841 | 13.0k | ZVAL_UNDEF(&closure->this_ptr); |
842 | | /* Invariant: |
843 | | * If the closure is unscoped or static, it has no bound object. */ |
844 | 13.0k | closure->func.common.scope = scope; |
845 | 13.0k | closure->called_scope = called_scope; |
846 | 13.0k | if (scope) { |
847 | 3.52k | closure->func.common.fn_flags |= ZEND_ACC_PUBLIC; |
848 | 3.52k | if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) { |
849 | 2.73k | ZVAL_OBJ_COPY(&closure->this_ptr, Z_OBJ_P(this_ptr)); |
850 | 2.73k | } |
851 | 3.52k | } |
852 | 13.0k | } |
853 | | /* }}} */ |
854 | | |
855 | | ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) |
856 | 11.7k | { |
857 | 11.7k | zend_create_closure_ex(res, func, scope, called_scope, this_ptr, |
858 | 11.7k | /* is_fake */ (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0); |
859 | 11.7k | } |
860 | | |
861 | | ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */ |
862 | 1.30k | { |
863 | 1.30k | zend_closure *closure; |
864 | | |
865 | 1.30k | zend_create_closure_ex(res, func, scope, called_scope, this_ptr, /* is_fake */ true); |
866 | | |
867 | 1.30k | closure = (zend_closure *)Z_OBJ_P(res); |
868 | 1.30k | closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE; |
869 | 1.30k | if (Z_TYPE(closure->this_ptr) != IS_OBJECT) { |
870 | 827 | GC_ADD_FLAGS(&closure->std, GC_NOT_COLLECTABLE); |
871 | 827 | } |
872 | 1.30k | } |
873 | | /* }}} */ |
874 | | |
875 | | /* __call and __callStatic name the arguments "$arguments" in the docs. */ |
876 | | static zend_internal_arg_info trampoline_arg_info[] = {ZEND_ARG_VARIADIC_TYPE_INFO(false, arguments, IS_MIXED, false)}; |
877 | | |
878 | 543 | void zend_closure_from_frame(zval *return_value, zend_execute_data *call) { /* {{{ */ |
879 | 543 | zval instance; |
880 | 543 | zend_internal_function trampoline; |
881 | 543 | zend_function *mptr = call->func; |
882 | | |
883 | 543 | if (ZEND_CALL_INFO(call) & ZEND_CALL_CLOSURE) { |
884 | 7 | RETURN_OBJ(ZEND_CLOSURE_OBJECT(mptr)); |
885 | 7 | } |
886 | | |
887 | 536 | if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) { |
888 | 138 | if ((ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) && |
889 | 90 | (Z_OBJCE(call->This) == zend_ce_closure) |
890 | 11 | && zend_string_equals(mptr->common.function_name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))) { |
891 | 11 | zend_free_trampoline(mptr); |
892 | 11 | RETURN_OBJ_COPY(Z_OBJ(call->This)); |
893 | 11 | } |
894 | | |
895 | 127 | memset(&trampoline, 0, sizeof(zend_internal_function)); |
896 | 127 | trampoline.type = ZEND_INTERNAL_FUNCTION; |
897 | 127 | trampoline.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_VARIADIC | ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_DEPRECATED); |
898 | 127 | trampoline.handler = zend_closure_call_magic; |
899 | 127 | trampoline.function_name = mptr->common.function_name; |
900 | 127 | trampoline.scope = mptr->common.scope; |
901 | 127 | trampoline.doc_comment = NULL; |
902 | 127 | if (trampoline.fn_flags & ZEND_ACC_VARIADIC) { |
903 | 127 | trampoline.arg_info = trampoline_arg_info; |
904 | 127 | } |
905 | 127 | trampoline.attributes = mptr->common.attributes; |
906 | | |
907 | 127 | zend_free_trampoline(mptr); |
908 | 127 | mptr = (zend_function *) &trampoline; |
909 | 127 | } |
910 | | |
911 | 525 | if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_THIS) { |
912 | 141 | ZVAL_OBJ(&instance, Z_OBJ(call->This)); |
913 | | |
914 | 141 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_OBJCE(instance), &instance); |
915 | 384 | } else { |
916 | 384 | zend_create_fake_closure(return_value, mptr, mptr->common.scope, Z_CE(call->This), NULL); |
917 | 384 | } |
918 | | |
919 | 525 | if (&mptr->internal_function == &trampoline) { |
920 | 127 | zend_string_release(mptr->common.function_name); |
921 | 127 | } |
922 | 525 | } /* }}} */ |
923 | | |
924 | | void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */ |
925 | 0 | { |
926 | 0 | zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv); |
927 | 0 | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
928 | 0 | zend_hash_update(static_variables, var_name, var); |
929 | 0 | } |
930 | | /* }}} */ |
931 | | |
932 | | void zend_closure_bind_var_ex(zval *closure_zv, uint32_t offset, zval *val) /* {{{ */ |
933 | 5.66k | { |
934 | 5.66k | zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv); |
935 | 5.66k | HashTable *static_variables = ZEND_MAP_PTR_GET(closure->func.op_array.static_variables_ptr); |
936 | 5.66k | zval *var = (zval*)((char*)static_variables->arData + offset); |
937 | 5.66k | zval_ptr_dtor(var); |
938 | 5.66k | ZVAL_COPY_VALUE(var, val); |
939 | 5.66k | } |
940 | | /* }}} */ |