1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2016 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
40typedef 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 */
49ZEND_API zend_class_entry *zend_ce_closure;
50static zend_object_handlers closure_handlers;
51
52ZEND_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
70static 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 */
114ZEND_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		ZVAL_COPY_VALUE(return_value, &closure_result);
170	}
171
172	if (fci_cache.function_handler->common.fn_flags & ZEND_ACC_GENERATOR) {
173		/* copied upon generator creation */
174		--GC_REFCOUNT(&closure->std);
175	} else if (ZEND_USER_CODE(my_function.type) && closure->func.common.scope != Z_OBJCE_P(newthis)) {
176		efree(my_function.op_array.run_time_cache);
177	}
178}
179/* }}} */
180
181/* {{{ proto Closure Closure::bind(callable old, object to [, mixed scope])
182   Create a closure from another one and bind to another object and scope */
183ZEND_METHOD(Closure, bind)
184{
185	zval *newthis, *zclosure, *scope_arg = NULL;
186	zend_closure *closure, *new_closure;
187	zend_class_entry *ce, *called_scope;
188
189	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oo!|z", &zclosure, zend_ce_closure, &newthis, &scope_arg) == FAILURE) {
190		return;
191	}
192
193	closure = (zend_closure *)Z_OBJ_P(zclosure);
194
195	if (scope_arg != NULL) { /* scope argument was given */
196		if (Z_TYPE_P(scope_arg) == IS_OBJECT) {
197			ce = Z_OBJCE_P(scope_arg);
198		} else if (Z_TYPE_P(scope_arg) == IS_NULL) {
199			ce = NULL;
200		} else {
201			zend_string *class_name = zval_get_string(scope_arg);
202			if (zend_string_equals_literal(class_name, "static")) {
203				ce = closure->func.common.scope;
204			} else if ((ce = zend_lookup_class_ex(class_name, NULL, 1)) == NULL) {
205				zend_error(E_WARNING, "Class '%s' not found", ZSTR_VAL(class_name));
206				zend_string_release(class_name);
207				RETURN_NULL();
208			}
209			zend_string_release(class_name);
210		}
211	} else { /* scope argument not given; do not change the scope by default */
212		ce = closure->func.common.scope;
213	}
214
215	if (!zend_valid_closure_binding(closure, newthis, ce)) {
216		return;
217	}
218
219	if (newthis) {
220		called_scope = Z_OBJCE_P(newthis);
221	} else {
222		called_scope = ce;
223	}
224
225	zend_create_closure(return_value, &closure->func, ce, called_scope, newthis);
226	new_closure = (zend_closure *) Z_OBJ_P(return_value);
227
228	/* Runtime cache relies on bound scope to be immutable, hence we need a separate rt cache in case scope changed */
229	if (ZEND_USER_CODE(closure->func.type) && (closure->func.common.scope != new_closure->func.common.scope || (closure->func.op_array.fn_flags & ZEND_ACC_NO_RT_ARENA))) {
230		new_closure->func.op_array.run_time_cache = emalloc(new_closure->func.op_array.cache_size);
231		memset(new_closure->func.op_array.run_time_cache, 0, new_closure->func.op_array.cache_size);
232
233		new_closure->func.op_array.fn_flags |= ZEND_ACC_NO_RT_ARENA;
234	}
235}
236/* }}} */
237
238static ZEND_COLD zend_function *zend_closure_get_constructor(zend_object *object) /* {{{ */
239{
240	zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
241	return NULL;
242}
243/* }}} */
244
245static int zend_closure_compare_objects(zval *o1, zval *o2) /* {{{ */
246{
247	return (Z_OBJ_P(o1) != Z_OBJ_P(o2));
248}
249/* }}} */
250
251ZEND_API zend_function *zend_get_closure_invoke_method(zend_object *object) /* {{{ */
252{
253	zend_closure *closure = (zend_closure *)object;
254	zend_function *invoke = (zend_function*)emalloc(sizeof(zend_function));
255	const uint32_t keep_flags =
256		ZEND_ACC_RETURN_REFERENCE | ZEND_ACC_VARIADIC | ZEND_ACC_HAS_RETURN_TYPE;
257
258	invoke->common = closure->func.common;
259	/* We return ZEND_INTERNAL_FUNCTION, but arg_info representation is the
260	 * same as for ZEND_USER_FUNCTION (uses zend_string* instead of char*).
261	 * This is not a problem, because ZEND_ACC_HAS_TYPE_HINTS is never set,
262	 * and we won't check arguments on internal function. We also set
263	 * ZEND_ACC_USER_ARG_INFO flag to prevent invalid usage by Reflection */
264	invoke->type = ZEND_INTERNAL_FUNCTION;
265	invoke->internal_function.fn_flags =
266		ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER | (closure->func.common.fn_flags & keep_flags);
267	if (closure->func.type != ZEND_INTERNAL_FUNCTION || (closure->func.common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
268		invoke->internal_function.fn_flags |=
269			ZEND_ACC_USER_ARG_INFO;
270	}
271	invoke->internal_function.handler = ZEND_MN(Closure___invoke);
272	invoke->internal_function.module = 0;
273	invoke->internal_function.scope = zend_ce_closure;
274	invoke->internal_function.function_name = CG(known_strings)[ZEND_STR_MAGIC_INVOKE];
275	return invoke;
276}
277/* }}} */
278
279ZEND_API const zend_function *zend_get_closure_method_def(zval *obj) /* {{{ */
280{
281	zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
282	return &closure->func;
283}
284/* }}} */
285
286ZEND_API zval* zend_get_closure_this_ptr(zval *obj) /* {{{ */
287{
288	zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
289	return &closure->this_ptr;
290}
291/* }}} */
292
293static zend_function *zend_closure_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
294{
295	if (zend_string_equals_literal_ci(method, ZEND_INVOKE_FUNC_NAME)) {
296		return zend_get_closure_invoke_method(*object);
297	}
298
299	return std_object_handlers.get_method(object, method, key);
300}
301/* }}} */
302
303static zval *zend_closure_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
304{
305	ZEND_CLOSURE_PROPERTY_ERROR();
306	return &EG(uninitialized_zval);
307}
308/* }}} */
309
310static void zend_closure_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
311{
312	ZEND_CLOSURE_PROPERTY_ERROR();
313}
314/* }}} */
315
316static zval *zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
317{
318	ZEND_CLOSURE_PROPERTY_ERROR();
319	return NULL;
320}
321/* }}} */
322
323static int zend_closure_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
324{
325	if (has_set_exists != 2) {
326		ZEND_CLOSURE_PROPERTY_ERROR();
327	}
328	return 0;
329}
330/* }}} */
331
332static void zend_closure_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
333{
334	ZEND_CLOSURE_PROPERTY_ERROR();
335}
336/* }}} */
337
338static void zend_closure_free_storage(zend_object *object) /* {{{ */
339{
340	zend_closure *closure = (zend_closure *)object;
341
342	zend_object_std_dtor(&closure->std);
343
344	if (closure->func.type == ZEND_USER_FUNCTION) {
345		if (closure->func.op_array.fn_flags & ZEND_ACC_NO_RT_ARENA) {
346			efree(closure->func.op_array.run_time_cache);
347			closure->func.op_array.run_time_cache = NULL;
348		}
349		destroy_op_array(&closure->func.op_array);
350	}
351
352	if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
353		zval_ptr_dtor(&closure->this_ptr);
354	}
355}
356/* }}} */
357
358static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
359{
360	zend_closure *closure;
361
362	closure = emalloc(sizeof(zend_closure));
363	memset(closure, 0, sizeof(zend_closure));
364
365	zend_object_std_init(&closure->std, class_type);
366	closure->std.handlers = &closure_handlers;
367
368	return (zend_object*)closure;
369}
370/* }}} */
371
372static zend_object *zend_closure_clone(zval *zobject) /* {{{ */
373{
374	zend_closure *closure = (zend_closure *)Z_OBJ_P(zobject);
375	zval result;
376
377	zend_create_closure(&result, &closure->func,
378		closure->func.common.scope, closure->called_scope, &closure->this_ptr);
379	return Z_OBJ(result);
380}
381/* }}} */
382
383int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
384{
385	zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
386	*fptr_ptr = &closure->func;
387	*ce_ptr = closure->called_scope;
388
389	if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
390		*obj_ptr = Z_OBJ(closure->this_ptr);
391	} else {
392		*obj_ptr = NULL;
393	}
394
395	return SUCCESS;
396}
397/* }}} */
398
399static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp) /* {{{ */
400{
401	zend_closure *closure = (zend_closure *)Z_OBJ_P(object);
402	zval val;
403	struct _zend_arg_info *arg_info = closure->func.common.arg_info;
404	HashTable *debug_info;
405
406	*is_temp = 1;
407
408	ALLOC_HASHTABLE(debug_info);
409	zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0);
410
411	if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
412		HashTable *static_variables = closure->func.op_array.static_variables;
413		ZVAL_ARR(&val, zend_array_dup(static_variables));
414		zend_hash_update(debug_info, CG(known_strings)[ZEND_STR_STATIC], &val);
415	}
416
417	if (Z_TYPE(closure->this_ptr) != IS_UNDEF) {
418		Z_ADDREF(closure->this_ptr);
419		zend_hash_update(debug_info, CG(known_strings)[ZEND_STR_THIS], &closure->this_ptr);
420	}
421
422	if (arg_info &&
423		(closure->func.common.num_args ||
424		 (closure->func.common.fn_flags & ZEND_ACC_VARIADIC))) {
425		uint32_t i, num_args, required = closure->func.common.required_num_args;
426
427		array_init(&val);
428
429		num_args = closure->func.common.num_args;
430		if (closure->func.common.fn_flags & ZEND_ACC_VARIADIC) {
431			num_args++;
432		}
433		for (i = 0; i < num_args; i++) {
434			zend_string *name;
435			zval info;
436			if (arg_info->name) {
437				name = zend_strpprintf(0, "%s$%s",
438						arg_info->pass_by_reference ? "&" : "",
439						ZSTR_VAL(arg_info->name));
440			} else {
441				name = zend_strpprintf(0, "%s$param%d",
442						arg_info->pass_by_reference ? "&" : "",
443						i + 1);
444			}
445			ZVAL_NEW_STR(&info, zend_strpprintf(0, "%s", i >= required ? "<optional>" : "<required>"));
446			zend_hash_update(Z_ARRVAL(val), name, &info);
447			zend_string_release(name);
448			arg_info++;
449		}
450		zend_hash_str_update(debug_info, "parameter", sizeof("parameter")-1, &val);
451	}
452
453	return debug_info;
454}
455/* }}} */
456
457static HashTable *zend_closure_get_gc(zval *obj, zval **table, int *n) /* {{{ */
458{
459	zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
460
461	*table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
462	*n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;
463	return (closure->func.type == ZEND_USER_FUNCTION) ?
464		closure->func.op_array.static_variables : NULL;
465}
466/* }}} */
467
468/* {{{ proto Closure::__construct()
469   Private constructor preventing instantiation */
470ZEND_COLD ZEND_METHOD(Closure, __construct)
471{
472	zend_throw_error(NULL, "Instantiation of 'Closure' is not allowed");
473}
474/* }}} */
475
476ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bindto, 0, 0, 1)
477	ZEND_ARG_INFO(0, newthis)
478	ZEND_ARG_INFO(0, newscope)
479ZEND_END_ARG_INFO()
480
481ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
482	ZEND_ARG_INFO(0, closure)
483	ZEND_ARG_INFO(0, newthis)
484	ZEND_ARG_INFO(0, newscope)
485ZEND_END_ARG_INFO()
486
487ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_call, 0, 0, 1)
488	ZEND_ARG_INFO(0, newthis)
489	ZEND_ARG_VARIADIC_INFO(0, parameters)
490ZEND_END_ARG_INFO()
491
492static const zend_function_entry closure_functions[] = {
493	ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
494	ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
495	ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
496	ZEND_ME(Closure, call, arginfo_closure_call, ZEND_ACC_PUBLIC)
497	ZEND_FE_END
498};
499
500void zend_register_closure_ce(void) /* {{{ */
501{
502	zend_class_entry ce;
503
504	INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
505	zend_ce_closure = zend_register_internal_class(&ce);
506	zend_ce_closure->ce_flags |= ZEND_ACC_FINAL;
507	zend_ce_closure->create_object = zend_closure_new;
508	zend_ce_closure->serialize = zend_class_serialize_deny;
509	zend_ce_closure->unserialize = zend_class_unserialize_deny;
510
511	memcpy(&closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
512	closure_handlers.free_obj = zend_closure_free_storage;
513	closure_handlers.get_constructor = zend_closure_get_constructor;
514	closure_handlers.get_method = zend_closure_get_method;
515	closure_handlers.write_property = zend_closure_write_property;
516	closure_handlers.read_property = zend_closure_read_property;
517	closure_handlers.get_property_ptr_ptr = zend_closure_get_property_ptr_ptr;
518	closure_handlers.has_property = zend_closure_has_property;
519	closure_handlers.unset_property = zend_closure_unset_property;
520	closure_handlers.compare_objects = zend_closure_compare_objects;
521	closure_handlers.clone_obj = zend_closure_clone;
522	closure_handlers.get_debug_info = zend_closure_get_debug_info;
523	closure_handlers.get_closure = zend_closure_get_closure;
524	closure_handlers.get_gc = zend_closure_get_gc;
525}
526/* }}} */
527
528static void zend_closure_internal_handler(INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
529{
530	zend_closure *closure = (zend_closure*)EX(func)->common.prototype;
531	closure->orig_internal_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
532	OBJ_RELEASE((zend_object*)closure);
533	EX(func) = NULL;
534}
535/* }}} */
536
537ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
538{
539	zend_closure *closure;
540
541	object_init_ex(res, zend_ce_closure);
542
543	closure = (zend_closure *)Z_OBJ_P(res);
544
545	if ((scope == NULL) && this_ptr && (Z_TYPE_P(this_ptr) != IS_UNDEF)) {
546		/* use dummy scope if we're binding an object without specifying a scope */
547		/* maybe it would be better to create one for this purpose */
548		scope = zend_ce_closure;
549	}
550
551	if (func->type == ZEND_USER_FUNCTION) {
552		memcpy(&closure->func, func, sizeof(zend_op_array));
553		closure->func.common.prototype = (zend_function*)closure;
554		closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
555		if (closure->func.op_array.static_variables) {
556			closure->func.op_array.static_variables =
557				zend_array_dup(closure->func.op_array.static_variables);
558		}
559		if (UNEXPECTED(!closure->func.op_array.run_time_cache)) {
560			closure->func.op_array.run_time_cache = func->op_array.run_time_cache = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
561			memset(func->op_array.run_time_cache, 0, func->op_array.cache_size);
562		}
563		if (closure->func.op_array.refcount) {
564			(*closure->func.op_array.refcount)++;
565		}
566	} else {
567		memcpy(&closure->func, func, sizeof(zend_internal_function));
568		closure->func.common.prototype = (zend_function*)closure;
569		closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
570		/* wrap internal function handler to avoid memory leak */
571		if (UNEXPECTED(closure->func.internal_function.handler == zend_closure_internal_handler)) {
572			/* avoid infinity recursion, by taking handler from nested closure */
573			zend_closure *nested = (zend_closure*)((char*)func - XtOffsetOf(zend_closure, func));
574			ZEND_ASSERT(nested->std.ce == zend_ce_closure);
575			closure->orig_internal_handler = nested->orig_internal_handler;
576		} else {
577			closure->orig_internal_handler = closure->func.internal_function.handler;
578		}
579		closure->func.internal_function.handler = zend_closure_internal_handler;
580		if (!func->common.scope) {
581			/* if it's a free function, we won't set scope & this since they're meaningless */
582			this_ptr = NULL;
583			scope = NULL;
584		}
585	}
586
587	ZVAL_UNDEF(&closure->this_ptr);
588	/* Invariant:
589	 * If the closure is unscoped or static, it has no bound object. */
590	closure->func.common.scope = scope;
591	closure->called_scope = called_scope;
592	if (scope) {
593		closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
594		if (this_ptr && Z_TYPE_P(this_ptr) == IS_OBJECT && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
595			ZVAL_COPY(&closure->this_ptr, this_ptr);
596		}
597	}
598}
599/* }}} */
600
601ZEND_API void zend_create_fake_closure(zval *res, zend_function *func, zend_class_entry *scope, zend_class_entry *called_scope, zval *this_ptr) /* {{{ */
602{
603	zend_closure *closure;
604
605	zend_create_closure(res, func, scope, called_scope, this_ptr);
606
607	closure = (zend_closure *)Z_OBJ_P(res);
608	closure->func.common.fn_flags |= ZEND_ACC_FAKE_CLOSURE;
609}
610/* }}} */
611
612void zend_closure_bind_var(zval *closure_zv, zend_string *var_name, zval *var) /* {{{ */
613{
614	zend_closure *closure = (zend_closure *) Z_OBJ_P(closure_zv);
615	HashTable *static_variables = closure->func.op_array.static_variables;
616	zend_hash_update(static_variables, var_name, var);
617}
618/* }}} */
619
620/*
621 * Local variables:
622 * tab-width: 4
623 * c-basic-offset: 4
624 * indent-tabs-mode: t
625 * End:
626 */
627