1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 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@zend.com> |
17 | Marcus Boerger <helly@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* $Id$ */
22
23 #include "zend.h"
24 #include "zend_API.h"
25 #include "zend_closures.h"
26 #include "zend_exceptions.h"
27 #include "zend_interfaces.h"
28 #include "zend_objects.h"
29 #include "zend_objects_API.h"
30 #include "zend_globals.h"
31
32 #define ZEND_CLOSURE_PRINT_NAME "Closure object"
33
34 #define ZEND_CLOSURE_PROPERTY_ERROR() \
35 zend_throw_error(NULL, "Closure object cannot have properties")
36
37 /* reuse bit to mark "fake" closures (it wasn't used for functions before) */
38 #define ZEND_ACC_FAKE_CLOSURE ZEND_ACC_INTERFACE
39
40 typedef struct _zend_closure {
41 zend_object std;
42 zend_function func;
43 zval this_ptr;
44 zend_class_entry *called_scope;
45 void (*orig_internal_handler)(INTERNAL_FUNCTION_PARAMETERS);
46 } zend_closure;
47
48 /* non-static since it needs to be referenced */
49 ZEND_API zend_class_entry *zend_ce_closure;
50 static zend_object_handlers closure_handlers;
51
ZEND_METHOD(Closure,__invoke)52 ZEND_METHOD(Closure, __invoke) /* {{{ */
53 {
54 zend_function *func = EX(func);
55 zval *arguments = ZEND_CALL_ARG(execute_data, 1);
56
57 if (call_user_function_ex(CG(function_table), NULL, getThis(), return_value, ZEND_NUM_ARGS(), arguments, 1, NULL) == FAILURE) {
58 RETVAL_FALSE;
59 }
60
61 /* destruct the function also, then - we have allocated it in get_method */
62 zend_string_release(func->internal_function.function_name);
63 efree(func);
64 #if ZEND_DEBUG
65 execute_data->func = NULL;
66 #endif
67 }
68 /* }}} */
69
zend_valid_closure_binding(zend_closure * closure,zval * newthis,zend_class_entry * scope)70 static zend_bool zend_valid_closure_binding(
71 zend_closure *closure, zval *newthis, zend_class_entry *scope) /* {{{ */
72 {
73 zend_function *func = &closure->func;
74 zend_bool is_fake_closure = (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) != 0;
75 if (newthis) {
76 if (func->common.fn_flags & ZEND_ACC_STATIC) {
77 zend_error(E_WARNING, "Cannot bind an instance to a static closure");
78 return 0;
79 }
80
81 if (is_fake_closure && func->common.scope &&
82 !instanceof_function(Z_OBJCE_P(newthis), func->common.scope)) {
83 /* Binding incompatible $this to an internal method is not supported. */
84 zend_error(E_WARNING, "Cannot bind method %s::%s() to object of class %s",
85 ZSTR_VAL(func->common.scope->name),
86 ZSTR_VAL(func->common.function_name),
87 ZSTR_VAL(Z_OBJCE_P(newthis)->name));
88 return 0;
89 }
90 } else if (!(func->common.fn_flags & ZEND_ACC_STATIC) && func->common.scope
91 && func->type == ZEND_INTERNAL_FUNCTION) {
92 zend_error(E_WARNING, "Cannot unbind $this of internal method");
93 return 0;
94 }
95
96 if (scope && scope != func->common.scope && scope->type == ZEND_INTERNAL_CLASS) {
97 /* rebinding to internal class is not allowed */
98 zend_error(E_WARNING, "Cannot bind closure to scope of internal class %s",
99 ZSTR_VAL(scope->name));
100 return 0;
101 }
102
103 if (is_fake_closure && scope != func->common.scope) {
104 zend_error(E_WARNING, "Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()");
105 return 0;
106 }
107
108 return 1;
109 }
110 /* }}} */
111
112 /* {{{ proto mixed Closure::call(object to [, mixed parameter] [, mixed ...] )
113 Call closure, binding to a given object with its class as the scope */
ZEND_METHOD(Closure,call)114 ZEND_METHOD(Closure, call)
115 {
116 zval *zclosure, *newthis, closure_result;
117 zend_closure *closure;
118 zend_fcall_info fci;
119 zend_fcall_info_cache fci_cache;
120 zval *my_params;
121 int my_param_count = 0;
122 zend_function my_function;
123 zend_object *newobj;
124
125 if (zend_parse_parameters(ZEND_NUM_ARGS(), "o*", &newthis, &my_params, &my_param_count) == FAILURE) {
126 return;
127 }
128
129 zclosure = getThis();
130 closure = (zend_closure *) Z_OBJ_P(zclosure);
131
132 newobj = Z_OBJ_P(newthis);
133
134 if (!zend_valid_closure_binding(closure, newthis, Z_OBJCE_P(newthis))) {
135 return;
136 }
137
138 /* This should never happen as closures will always be callable */
139 if (zend_fcall_info_init(zclosure, 0, &fci, &fci_cache, NULL, NULL) != SUCCESS) {
140 ZEND_ASSERT(0);
141 }
142
143 fci.retval = &closure_result;
144 fci.params = my_params;
145 fci.param_count = my_param_count;
146 fci.object = fci_cache.object = newobj;
147 fci_cache.initialized = 1;
148 fci_cache.called_scope = Z_OBJCE_P(newthis);
149
150 if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
151 zval new_closure;
152 zend_create_closure(&new_closure, fci_cache.function_handler, Z_OBJCE_P(newthis), closure->called_scope, newthis);
153 closure = (zend_closure *) Z_OBJ(new_closure);
154 fci_cache.function_handler = &closure->func;
155 } else {
156 memcpy(&my_function, fci_cache.function_handler, fci_cache.function_handler->type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) : sizeof(zend_internal_function));
157 /* use scope of passed object */
158 my_function.common.scope = Z_OBJCE_P(newthis);
159 fci_cache.function_handler = &my_function;
160
161 /* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
162 if (ZEND_USER_CODE(my_function.type) && closure->func.common.scope != Z_OBJCE_P(newthis)) {
163 my_function.op_array.run_time_cache = emalloc(my_function.op_array.cache_size);
164 memset(my_function.op_array.run_time_cache, 0, my_function.op_array.cache_size);
165 }
166 }
167
168 if (zend_call_function(&fci, &fci_cache) == SUCCESS && Z_TYPE(closure_result) != IS_UNDEF) {
169 if (Z_ISREF(closure_result)) {
170 zend_unwrap_reference(&closure_result);
171 }
172 ZVAL_COPY_VALUE(return_value, &closure_result);
173 }
174
175 if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
176 /* copied upon generator creation */
177 --GC_REFCOUNT(&closure->std);
178 } else if (ZEND_USER_CODE(my_function.type) && closure->func.common.scope != Z_OBJCE_P(newthis)) {
179 efree(my_function.op_array.run_time_cache);
180 }
181 }
182 /* }}} */
183
184 /* {{{ proto Closure Closure::bind(callable old, object to [, mixed scope])
185 Create a closure from another one and bind to another object and scope */
ZEND_METHOD(Closure,bind)186 ZEND_METHOD(Closure, bind)
187 {
188 zval *newthis, *zclosure, *scope_arg = NULL;
189 zend_closure *closure;
190 zend_class_entry *ce, *called_scope;
191
192 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
193 return;
194 }
195
196 closure = (zend_closure *)Z_OBJ_P(zclosure);
197
198 if (scope_arg != NULL) { /* scope argument was given */
199 if (Z_TYPE_P(scope_arg) == IS_OBJECT) {
200 ce = Z_OBJCE_P(scope_arg);
201 } else if (Z_TYPE_P(scope_arg) == IS_NULL) {
202 ce = NULL;
203 } else {
204 zend_string *class_name = zval_get_string(scope_arg);
205 if (zend_string_equals_literal(class_name, "static")) {
206 ce = closure->func.common.scope;
207 } else if ((ce = zend_lookup_class_ex(class_name, NULL, 1)) == NULL) {
208 zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
209 zend_string_release(class_name);
210 RETURN_NULL();
211 }
212 zend_string_release(class_name);
213 }
214 } else { /* scope argument not given; do not change the scope by default */
215 ce = closure->func.common.scope;
216 }
217
218 if (!zend_valid_closure_binding(closure, newthis, ce)) {
219 return;
220 }
221
222 if (newthis) {
223 called_scope = Z_OBJCE_P(newthis);
224 } else {
225 called_scope = ce;
226 }
227
228 zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
229 }
230 /* }}} */
231
zend_closure_call_magic(INTERNAL_FUNCTION_PARAMETERS)232 static void zend_closure_call_magic(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ {
233 zend_fcall_info fci;
234 zend_fcall_info_cache fcc;
235 zval params[2];
236
237 memset(&fci, 0, sizeof(zend_fcall_info));
238 memset(&fcc, 0, sizeof(zend_fcall_info_cache));
239
240 fci.size = sizeof(zend_fcall_info);
241 fci.retval = return_value;
242
243 fcc.initialized = 1;
244 fcc.function_handler = (zend_function *) EX(func)->common.arg_info;
245 fci.params = params;
246 fci.param_count = 2;
247 ZVAL_STR(&fci.params[0], EX(func)->common.function_name);
248 array_init(&fci.params[1]);
249 zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]);
250
251 fci.object = Z_OBJ(EX(This));
252 fcc.object = Z_OBJ(EX(This));
253 fcc.calling_scope = zend_get_executed_scope();
254
255 zend_call_function(&fci, &fcc);
256
257 zval_ptr_dtor(&fci.params[0]);
258 zval_ptr_dtor(&fci.params[1]);
259 }
260 /* }}} */
261
zend_create_closure_from_callable(zval * return_value,zval * callable,char ** error)262 static int zend_create_closure_from_callable(zval *return_value, zval *callable, char **error) /* {{{ */ {
263 zend_fcall_info_cache fcc;
264 zend_function *mptr;
265 zval instance;
266 zend_internal_function call;
267
268 if (!zend_is_callable_ex(callable, NULL, 0, NULL, &fcc, error)) {
269 return FAILURE;
270 }
271
272 mptr = fcc.function_handler;
273 if (mptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
274 memset(&call, 0, sizeof(zend_internal_function));
275
276 call.type = ZEND_INTERNAL_FUNCTION;
277 call.handler = zend_closure_call_magic;
278 call.function_name = mptr->common.function_name;
279 call.arg_info = (zend_internal_arg_info *) mptr->common.prototype;
280 call.scope = mptr->common.scope;
281
282 zend_free_trampoline(mptr);
283 mptr = (zend_function *) &call;
284 }
285
286 if (fcc.object) {
287 ZVAL_OBJ(&instance, fcc.object);
288 zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, &instance);
289 } else {
290 zend_create_fake_closure(return_value, mptr, mptr->common.scope, fcc.called_scope, NULL);
291 }
292
293 return SUCCESS;
294 }
295 /* }}} */
296
297 /* {{{ proto Closure Closure::fromCallable(callable callable)
298 Create a closure from a callable using the current scope. */
ZEND_METHOD(Closure,fromCallable)299 ZEND_METHOD(Closure, fromCallable)
300 {
301 zval *callable;
302 int success;
303 char *error = NULL;
304
305 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
306 return;
307 }
308
309 if (Z_TYPE_P(callable) == IS_OBJECT && instanceof_function(Z_OBJCE_P(callable), zend_ce_closure)) {
310 /* It's already a closure */
311 RETURN_ZVAL(callable, 1, 0);
312 }
313
314 /* create closure as if it were called from parent scope */
315 EG(current_execute_data) = EX(prev_execute_data);
316 success = zend_create_closure_from_callable(return_value, callable, &error);
317 EG(current_execute_data) = execute_data;
318
319 if (success == FAILURE || error) {
320 if (error) {
321 zend_throw_exception_ex(zend_ce_type_error, 0, "Failed to create closure from callable: %s", error);
322 efree(error);
323 } else {
324 zend_throw_exception_ex(zend_ce_type_error, 0, "Failed to create closure from callable");
325 }
326 }
327 }
328 /* }}} */
329
zend_closure_get_constructor(zend_object * object)330 static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
331 {
332 zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
333 return NULL;
334 }
335 /* }}} */
336
zend_closure_compare_objects(zval * o1,zval * o2)337 static int zend_closure_compare_objects(zval *o1, zval *o2) /* {{{ */
338 {
339 return (Z_OBJ_P(o1) != Z_OBJ_P(o2));
340 }
341 /* }}} */
342
zend_get_closure_invoke_method(zend_object * object)343 ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
344 {
345 zend_closure *closure = (zend_closure *)object;
346 zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
347 const uint32_t keep_flags =
348 ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE;
349
350 invoke->common = closure->func.common;
351 /* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
352 * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
353 * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
354 * and we won't check arguments on internal function. We also set
355 * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
356 invoke->type = ZEND_INTERNAL_FUNCTION;
357 invoke->internal_function.fn_flags =
358 ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags);
359 if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
360 invoke->internal_function.fn_flags |=
361 ZEND_ACC_USER_ARG_INFO;
362 }
363 invoke->internal_function.handler = ZEND_MN(Closure___invoke);
364 invoke->internal_function.module = 0;
365 invoke->internal_function.scope = zend_ce_closure;
366 invoke->internal_function.function_name = CG(known_strings)[ZEND_STR_MAGIC_INVOKE];
367 return invoke;
368 }
369 /* }}} */
370
zend_get_closure_method_def(zval * obj)371 ZEND_API const zend_function *zend_get_closure_method_def(zval *obj) /* {{{ */
372 {
373 zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
374 return &closure->func;
375 }
376 /* }}} */
377
zend_get_closure_this_ptr(zval * obj)378 ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
379 {
380 zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
381 return &closure->this_ptr;
382 }
383 /* }}} */
384
zend_closure_get_method(zend_object ** object,zend_string * method,const zval * key)385 static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
386 {
387 if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
388 return zend_get_closure_invoke_method(*object);
389 }
390
391 return std_object_handlers.get_method(object, method, key);
392 }
393 /* }}} */
394
zend_closure_read_property(zval * object,zval * member,int type,void ** cache_slot,zval * rv)395 static zval *zend_closure_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
396 {
397 ZEND_CLOSURE_PROPERTY_ERROR();
398 return &EG(uninitialized_zval);
399 }
400 /* }}} */
401
zend_closure_write_property(zval * object,zval * member,zval * value,void ** cache_slot)402 static void zend_closure_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
403 {
404 ZEND_CLOSURE_PROPERTY_ERROR();
405 }
406 /* }}} */
407
zend_closure_get_property_ptr_ptr(zval * object,zval * member,int type,void ** cache_slot)408 static zval *zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
409 {
410 ZEND_CLOSURE_PROPERTY_ERROR();
411 return NULL;
412 }
413 /* }}} */
414
zend_closure_has_property(zval * object,zval * member,int has_set_exists,void ** cache_slot)415 static int zend_closure_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
416 {
417 if (has_set_exists != 2) {
418 ZEND_CLOSURE_PROPERTY_ERROR();
419 }
420 return 0;
421 }
422 /* }}} */
423
zend_closure_unset_property(zval * object,zval * member,void ** cache_slot)424 static void zend_closure_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
425 {
426 ZEND_CLOSURE_PROPERTY_ERROR();
427 }
428 /* }}} */
429
zend_closure_free_storage(zend_object * object)430 static void zend_closure_free_storage(zend_object *object) /* {{{ */
431 {
432 zend_closure *closure = (zend_closure *)object;
433
434 zend_object_std_dtor(&closure->std);
435
436 if (closure->func.type == ZEND_USER_FUNCTION) {
437 if (closure->func.op_array.fn_flags & ZEND_ACC_NO_RT_ARENA) {
438 efree(closure->func.op_array.run_time_cache);
439 closure->func.op_array.run_time_cache = NULL;
440 }
441 destroy_op_array(&closure->func.op_array);
442 }
443
444 if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
445 zval_ptr_dtor(&closure->this_ptr);
446 }
447 }
448 /* }}} */
449
zend_closure_new(zend_class_entry * class_type)450 static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
451 {
452 zend_closure *closure;
453
454 closure = emalloc(sizeof(zend_closure));
455 memset(closure, 0, sizeof(zend_closure));
456
457 zend_object_std_init(&closure->std, class_type);
458 closure->std.handlers = &closure_handlers;
459
460 return (zend_object*)closure;
461 }
462 /* }}} */
463
zend_closure_clone(zval * zobject)464 static zend_object *zend_closure_clone(zval *zobject) /* {{{ */
465 {
466 zend_closure *closure = (zend_closure *)Z_OBJ_P(zobject);
467 zval result;
468
469 zend_create_closure(&result, &closure->func,
470 closure->func.common.scope, closure->called_scope, &closure->this_ptr);
471 return Z_OBJ(result);
472 }
473 /* }}} */
474
zend_closure_get_closure(zval * obj,zend_class_entry ** ce_ptr,zend_function ** fptr_ptr,zend_object ** obj_ptr)475 int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
476 {
477 zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
478 *fptr_ptr = &closure->func;
479 *ce_ptr = closure->called_scope;
480
481 if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
482 *obj_ptr = Z_OBJ(closure->this_ptr);
483 } else {
484 *obj_ptr = NULL;
485 }
486
487 return SUCCESS;
488 }
489 /* }}} */
490
zend_closure_get_debug_info(zval * object,int * is_temp)491 static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp) /* {{{ */
492 {
493 zend_closure *closure = (zend_closure *)Z_OBJ_P(object);
494 zval val;
495 struct _zend_arg_info *arg_info = closure->func.common.arg_info;
496 HashTable *debug_info;
497 zend_bool zstr_args = (closure->func.type == ZEND_USER_FUNCTION) || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO);
498
499 *is_temp = 1;
500
501 ALLOC_HASHTABLE(debug_info);
502 zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0);
503
504 if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
505 HashTable *static_variables = closure->func.op_array.static_variables;
506 ZVAL_ARR(&val, zend_array_dup(static_variables));
507 zend_hash_update(debug_info, CG(known_strings)[ZEND_STR_STATIC], &val);
508 }
509
510 if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
511 Z_ADDREF(closure->this_ptr);
512 zend_hash_update(debug_info, CG(known_strings)[ZEND_STR_THIS], &closure->this_ptr);
513 }
514
515 if (arg_info &&
516 (closure->func.common.num_args ||
517 (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
518 uint32_t i, num_args, required = closure->func.common.required_num_args;
519
520 array_init(&val);
521
522 num_args = closure->func.common.num_args;
523 if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
524 num_args++;
525 }
526 for (i = 0; i < num_args; i++) {
527 zend_string *name;
528 zval info;
529 if (arg_info->name) {
530 if (zstr_args) {
531 name = zend_strpprintf(0, "%s$%s",
532 arg_info->pass_by_reference ? "&" : "",
533 ZSTR_VAL(arg_info->name));
534 } else {
535 name = zend_strpprintf(0, "%s$%s",
536 arg_info->pass_by_reference ? "&" : "",
537 ((zend_internal_arg_info*)arg_info)->name);
538 }
539 } else {
540 name = zend_strpprintf(0, "%s$param%d",
541 arg_info->pass_by_reference ? "&" : "",
542 i + 1);
543 }
544 ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
545 zend_hash_update(Z_ARRVAL(val), name, &info);
546 zend_string_release(name);
547 arg_info++;
548 }
549 zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
550 }
551
552 return debug_info;
553 }
554 /* }}} */
555
zend_closure_get_gc(zval * obj,zval ** table,int * n)556 static HashTable *zend_closure_get_gc(zval *obj, zval **table, int *n) /* {{{ */
557 {
558 zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
559
560 *table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
561 *n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
562 return (closure->func.type == ZEND_USER_FUNCTION) ?
563 closure->func.op_array.static_variables : NULL;
564 }
565 /* }}} */
566
567 /* {{{ proto Closure::__construct()
568 Private constructor preventing instantiation */
ZEND_METHOD(Closure,__construct)569 ZEND_COLD ZEND_METHOD(Closure, __construct)
570 {
571 zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
572 }
573 /* }}} */
574
575 ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bindto, 0, 0, 1)
576 ZEND_ARG_INFO(0, newthis)
577 ZEND_ARG_INFO(0, newscope)
578 ZEND_END_ARG_INFO()
579
580 ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
581 ZEND_ARG_INFO(0, closure)
582 ZEND_ARG_INFO(0, newthis)
583 ZEND_ARG_INFO(0, newscope)
584 ZEND_END_ARG_INFO()
585
586 ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_call, 0, 0, 1)
587 ZEND_ARG_INFO(0, newthis)
588 ZEND_ARG_VARIADIC_INFO(0, parameters)
589 ZEND_END_ARG_INFO()
590
591 ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_fromcallable, 0, 0, 1)
592 ZEND_ARG_INFO(0, callable)
593 ZEND_END_ARG_INFO()
594
595 static const zend_function_entry closure_functions[] = {
596 ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
597 ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
598 ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
599 ZEND_ME(Closure, call, arginfo_closure_call, ZEND_ACC_PUBLIC)
600 ZEND_ME(Closure, fromCallable, arginfo_closure_fromcallable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
601 ZEND_FE_END
602 };
603
zend_register_closure_ce(void)604 void zend_register_closure_ce(void) /* {{{ */
605 {
606 zend_class_entry ce;
607
608 INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
609 zend_ce_closure = zend_register_internal_class(&ce);
610 zend_ce_closure->ce_flags |= ZEND_ACC_FINAL;
611 zend_ce_closure->create_object = zend_closure_new;
612 zend_ce_closure->serialize = zend_class_serialize_deny;
613 zend_ce_closure->unserialize = zend_class_unserialize_deny;
614
615 memcpy(&closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
616 closure_handlers.free_obj = zend_closure_free_storage;
617 closure_handlers.get_constructor = zend_closure_get_constructor;
618 closure_handlers.get_method = zend_closure_get_method;
619 closure_handlers.write_property = zend_closure_write_property;
620 closure_handlers.read_property = zend_closure_read_property;
621 closure_handlers.get_property_ptr_ptr = zend_closure_get_property_ptr_ptr;
622 closure_handlers.has_property = zend_closure_has_property;
623 closure_handlers.unset_property = zend_closure_unset_property;
624 closure_handlers.compare_objects = zend_closure_compare_objects;
625 closure_handlers.clone_obj = zend_closure_clone;
626 closure_handlers.get_debug_info = zend_closure_get_debug_info;
627 closure_handlers.get_closure = zend_closure_get_closure;
628 closure_handlers.get_gc = zend_closure_get_gc;
629 }
630 /* }}} */
631
zend_closure_internal_handler(INTERNAL_FUNCTION_PARAMETERS)632 static void zend_closure_internal_handler(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
633 {
634 zend_closure *closure = (zend_closure*)EX(func)->common.prototype;
635 closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
636 OBJ_RELEASE((zend_object*)closure);
637 EX(func) = NULL;
638 }
639 /* }}} */
640
zend_create_closure(zval * res,zend_function * func,zend_class_entry * scope,zend_class_entry * called_scope,zval * this_ptr)641 ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
642 {
643 zend_closure *closure;
644
645 object_init_ex(res, zend_ce_closure);
646
647 closure = (zend_closure *)Z_OBJ_P(res);
648
649 if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
650 /* use dummy scope if we're binding an object without specifying a scope */
651 /* maybe it would be better to create one for this purpose */
652 scope = zend_ce_closure;
653 }
654
655 if (func->type == ZEND_USER_FUNCTION) {
656 memcpy(&closure->func, func, sizeof(zend_op_array));
657 closure->func.common.prototype = (zend_function*)closure;
658 closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
659 if (closure->func.op_array.static_variables) {
660 closure->func.op_array.static_variables =
661 zend_array_dup(closure->func.op_array.static_variables);
662 }
663
664 /* Runtime cache is scope-dependent, so we cannot reuse it if the scope changed */
665 if (!closure->func.op_array.run_time_cache
666 || func->common.scope != scope
667 || (func->common.fn_flags & ZEND_ACC_NO_RT_ARENA)
668 ) {
669 if (!func->op_array.run_time_cache && (func->common.fn_flags & ZEND_ACC_CLOSURE)) {
670 /* If a real closure is used for the first time, we create a shared runtime cache
671 * and remember which scope it is for. */
672 func->common.scope = scope;
673 func->op_array.run_time_cache = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
674 closure->func.op_array.run_time_cache = func->op_array.run_time_cache;
675 } else {
676 /* Otherwise, we use a non-shared runtime cache */
677 closure->func.op_array.run_time_cache = emalloc(func->op_array.cache_size);
678 closure->func.op_array.fn_flags |= ZEND_ACC_NO_RT_ARENA;
679 }
680 memset(closure->func.op_array.run_time_cache, 0, func->op_array.cache_size);
681 }
682 if (closure->func.op_array.refcount) {
683 (*closure->func.op_array.refcount)++;
684 }
685 } else {
686 memcpy(&closure->func, func, sizeof(zend_internal_function));
687 closure->func.common.prototype = (zend_function*)closure;
688 closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
689 /* wrap internal function handler to avoid memory leak */
690 if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
691 /* avoid infinity recursion, by taking handler from nested closure */
692 zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func));
693 ZEND_ASSERT(nested->std.ce == zend_ce_closure);
694 closure->orig_internal_handler = nested->orig_internal_handler;
695 } else {
696 closure->orig_internal_handler = closure->func.internal_function.handler;
697 }
698 closure->func.internal_function.handler = zend_closure_internal_handler;
699 if (!func->common.scope) {
700 /* if it's a free function, we won't set scope & this since they're meaningless */
701 this_ptr = NULL;
702 scope = NULL;
703 }
704 }
705
706 ZVAL_UNDEF(&closure->this_ptr);
707 /* Invariant:
708 * If the closure is unscoped or static, it has no bound object. */
709 closure->func.common.scope = scope;
710 closure->called_scope = called_scope;
711 if (scope) {
712 closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
713 if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
714 ZVAL_COPY(&closure->this_ptr, this_ptr);
715 }
716 }
717 }
718 /* }}} */
719
zend_create_fake_closure(zval * res,zend_function * func,zend_class_entry * scope,zend_class_entry * called_scope,zval * this_ptr)720 ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
721 {
722 zend_closure *closure;
723
724 zend_create_closure(res, func, scope, called_scope, this_ptr);
725
726 closure = (zend_closure *)Z_OBJ_P(res);
727 closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
728 }
729 /* }}} */
730
zend_closure_bind_var(zval * closure_zv,zend_string * var_name,zval * var)731 void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
732 {
733 zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
734 HashTable *static_variables = closure->func.op_array.static_variables;
735 zend_hash_update(static_variables, var_name, var);
736 }
737 /* }}} */
738
739 /*
740 * Local variables:
741 * tab-width: 4
742 * c-basic-offset: 4
743 * indent-tabs-mode: t
744 * End:
745 */
746