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