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