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: Nikita Popov <nikic@php.net>                                |
16   |          Bob Weinand <bobwei9@hotmail.com>                           |
17   +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22#include "zend.h"
23#include "zend_API.h"
24#include "zend_interfaces.h"
25#include "zend_exceptions.h"
26#include "zend_generators.h"
27
28ZEND_API zend_class_entry *zend_ce_generator;
29ZEND_API zend_class_entry *zend_ce_ClosedGeneratorException;
30static zend_object_handlers zend_generator_handlers;
31
32static zend_object *zend_generator_create(zend_class_entry *class_type);
33
34static void zend_generator_cleanup_unfinished_execution(zend_generator *generator) /* {{{ */
35{
36	zend_execute_data *execute_data = generator->execute_data;
37
38	if (generator->send_target) {
39		Z_TRY_DELREF_P(generator->send_target);
40		generator->send_target = NULL;
41	}
42
43	if (execute_data->opline != execute_data->func->op_array.opcodes) {
44		/* -1 required because we want the last run opcode, not the next to-be-run one. */
45		uint32_t op_num = execute_data->opline - execute_data->func->op_array.opcodes - 1;
46
47		/* There may be calls to zend_vm_stack_free_call_frame(), which modifies the VM stack
48		 * globals, so need to load/restore those. */
49		zend_vm_stack original_stack = EG(vm_stack);
50		original_stack->top = EG(vm_stack_top);
51		EG(vm_stack_top) = generator->stack->top;
52		EG(vm_stack_end) = generator->stack->end;
53		EG(vm_stack) = generator->stack;
54
55		zend_cleanup_unfinished_execution(execute_data, op_num, 0);
56
57		generator->stack = EG(vm_stack);
58		generator->stack->top = EG(vm_stack_top);
59		EG(vm_stack_top) = original_stack->top;
60		EG(vm_stack_end) = original_stack->end;
61		EG(vm_stack) = original_stack;
62	}
63}
64/* }}} */
65
66ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution) /* {{{ */
67{
68	if (UNEXPECTED(Z_TYPE(generator->values) != IS_UNDEF)) {
69		zval_ptr_dtor(&generator->values);
70		ZVAL_UNDEF(&generator->values);
71	}
72
73	if (EXPECTED(generator->execute_data)) {
74		zend_execute_data *execute_data = generator->execute_data;
75
76		if (execute_data->symbol_table) {
77			zend_clean_and_cache_symbol_table(execute_data->symbol_table);
78		}
79		/* always free the CV's, in the symtable are only not-free'd IS_INDIRECT's */
80		zend_free_compiled_variables(execute_data);
81
82		if (Z_OBJ(execute_data->This)) {
83			OBJ_RELEASE(Z_OBJ(execute_data->This));
84		}
85
86		/* A fatal error / die occurred during the generator execution.
87		 * Trying to clean up the stack may not be safe in this case. */
88		if (UNEXPECTED(CG(unclean_shutdown))) {
89			generator->execute_data = NULL;
90			return;
91		}
92
93		zend_vm_stack_free_extra_args(generator->execute_data);
94
95		/* Some cleanups are only necessary if the generator was closed
96		 * before it could finish execution (reach a return statement). */
97		if (UNEXPECTED(!finished_execution)) {
98			zend_generator_cleanup_unfinished_execution(generator);
99		}
100
101		/* Free closure object */
102		if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
103			OBJ_RELEASE((zend_object *) EX(func)->common.prototype);
104		}
105
106		efree(generator->stack);
107		generator->execute_data = NULL;
108	}
109}
110/* }}} */
111
112static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
113{
114	zend_generator *generator = (zend_generator*) object;
115	zend_execute_data *ex = generator->execute_data;
116	uint32_t op_num, finally_op_num, finally_op_end;
117	int i;
118
119	if (EXPECTED(!ex) || EXPECTED(!(ex->func->op_array.fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK))) {
120		return;
121	}
122
123	/* -1 required because we want the last run opcode, not the
124	 * next to-be-run one. */
125	op_num = ex->opline - ex->func->op_array.opcodes - 1;
126
127	/* Find next finally block */
128	finally_op_num = 0;
129	finally_op_end = 0;
130	for (i = 0; i < ex->func->op_array.last_try_catch; i++) {
131		zend_try_catch_element *try_catch = &ex->func->op_array.try_catch_array[i];
132
133		if (op_num < try_catch->try_op) {
134			break;
135		}
136
137		if (op_num < try_catch->finally_op) {
138			finally_op_num = try_catch->finally_op;
139			finally_op_end = try_catch->finally_end;
140		}
141	}
142
143	/* If a finally block was found we jump directly to it and
144	 * resume the generator. */
145	if (finally_op_num) {
146		zval *fast_call = ZEND_CALL_VAR(ex, ex->func->op_array.opcodes[finally_op_end].op1.var);
147
148		Z_OBJ_P(fast_call) = EG(exception);
149		EG(exception) = NULL;
150		fast_call->u2.lineno = (uint32_t)-1;
151
152		ex->opline = &ex->func->op_array.opcodes[finally_op_num];
153		generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
154		zend_generator_resume(generator);
155	}
156}
157/* }}} */
158
159static zend_generator *zend_generator_get_child(zend_generator_node *node, zend_generator *leaf);
160
161static void zend_generator_free_storage(zend_object *object) /* {{{ */
162{
163	zend_generator *generator = (zend_generator*) object;
164
165	zend_generator_close(generator, 0);
166
167	/* we can't immediately free them in zend_generator_close() else yield from won't be able to fetch it */
168	zval_ptr_dtor(&generator->value);
169	zval_ptr_dtor(&generator->key);
170
171	if (EXPECTED(!Z_ISUNDEF(generator->retval))) {
172		zval_ptr_dtor(&generator->retval);
173	}
174
175	if (UNEXPECTED(generator->node.children > 4)) {
176		zend_hash_destroy(&generator->node.child.ht);
177	}
178
179	zend_object_std_dtor(&generator->std);
180
181	if (generator->iterator) {
182		zend_iterator_dtor(generator->iterator);
183	}
184
185	if (EXPECTED(generator->node.children == 0)) {
186		zend_generator *root = generator->node.ptr.root, *next;
187		while (UNEXPECTED(root != generator)) {
188			next = zend_generator_get_child(&root->node, generator);
189			OBJ_RELEASE(&root->std);
190			root = next;
191		}
192	}
193}
194/* }}} */
195
196static zend_object *zend_generator_create(zend_class_entry *class_type) /* {{{ */
197{
198	zend_generator *generator;
199
200	generator = emalloc(sizeof(zend_generator));
201	memset(generator, 0, sizeof(zend_generator));
202
203	/* The key will be incremented on first use, so it'll start at 0 */
204	generator->largest_used_integer_key = -1;
205
206	ZVAL_UNDEF(&generator->retval);
207	ZVAL_UNDEF(&generator->values);
208
209	/* By default we have a tree of only one node */
210	generator->node.parent = NULL;
211	generator->node.children = 0;
212	generator->node.ptr.root = generator;
213
214	zend_object_std_init(&generator->std, class_type);
215	generator->std.handlers = &zend_generator_handlers;
216
217	return (zend_object*)generator;
218}
219/* }}} */
220
221/* Requires globals EG(scope), EG(This) and EG(current_execute_data). */
222ZEND_API void zend_generator_create_zval(zend_execute_data *call, zend_op_array *op_array, zval *return_value) /* {{{ */
223{
224	zend_generator *generator;
225	zend_execute_data *current_execute_data;
226	zend_execute_data *execute_data;
227	zend_vm_stack current_stack = EG(vm_stack);
228
229	current_stack->top = EG(vm_stack_top);
230
231	/* Create new execution context. We have to back up and restore  EG(current_execute_data) here. */
232	current_execute_data = EG(current_execute_data);
233	execute_data = zend_create_generator_execute_data(call, op_array, return_value);
234	EG(current_execute_data) = current_execute_data;
235
236	object_init_ex(return_value, zend_ce_generator);
237
238	if (Z_OBJ(call->This)) {
239		Z_ADDREF(call->This);
240	}
241
242	/* Save execution context in generator object. */
243	generator = (zend_generator *) Z_OBJ_P(return_value);
244	generator->execute_data = execute_data;
245	generator->stack = EG(vm_stack);
246	generator->stack->top = EG(vm_stack_top);
247	EG(vm_stack_top) = current_stack->top;
248	EG(vm_stack_end) = current_stack->end;
249	EG(vm_stack) = current_stack;
250
251	/* EX(return_value) keeps pointer to zend_object (not a real zval) */
252	execute_data->return_value = (zval*)generator;
253
254	memset(&generator->execute_fake, 0, sizeof(zend_execute_data));
255	Z_OBJ(generator->execute_fake.This) = (zend_object *) generator;
256}
257/* }}} */
258
259static ZEND_COLD zend_function *zend_generator_get_constructor(zend_object *object) /* {{{ */
260{
261	zend_throw_error(NULL, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
262
263	return NULL;
264}
265/* }}} */
266
267ZEND_API zend_execute_data *zend_generator_check_placeholder_frame(zend_execute_data *ptr)
268{
269	if (!ptr->func && Z_OBJ(ptr->This)) {
270		if (Z_OBJCE(ptr->This) == zend_ce_generator) {
271			zend_generator *generator = (zend_generator *) Z_OBJ(ptr->This);
272			zend_generator *root = (generator->node.children < 1 ? generator : generator->node.ptr.leaf)->node.ptr.root;
273			zend_execute_data *prev = ptr->prev_execute_data;
274			if (generator->node.parent != root) {
275				do {
276					generator->execute_data->prev_execute_data = prev;
277					prev = generator->execute_data;
278					generator = generator->node.parent;
279				} while (generator->node.parent != root);
280			}
281			generator->execute_data->prev_execute_data = prev;
282			ptr = generator->execute_data;
283		}
284	}
285	return ptr;
286}
287
288static void zend_generator_throw_exception(zend_generator *generator, zval *exception)
289{
290	/* if we don't stop an array/iterator yield from, the exception will only reach the generator after the values were all iterated over */
291	if (UNEXPECTED(Z_TYPE(generator->values) != IS_UNDEF)) {
292		zval_ptr_dtor(&generator->values);
293		ZVAL_UNDEF(&generator->values);
294	}
295
296	/* Throw the exception in the context of the generator. Decrementing the opline
297	 * to pretend the exception happened during the YIELD opcode. */
298	zend_execute_data *original_execute_data = EG(current_execute_data);
299	EG(current_execute_data) = generator->execute_data;
300	generator->execute_data->opline--;
301	if (exception) {
302		zend_throw_exception_object(exception);
303	} else {
304		zend_throw_exception_internal(NULL);
305	}
306	generator->execute_data->opline++;
307	EG(current_execute_data) = original_execute_data;
308}
309
310static zend_generator *zend_generator_get_child(zend_generator_node *node, zend_generator *leaf)
311{
312	switch (node->children) {
313		case 0:
314			return NULL;
315		case 1:
316			return node->child.array[0].child;
317
318#define ZEND_GEN_GET_CHILD(x) \
319			if (node->child.array[x].leaf == leaf) { \
320				return node->child.array[x].child; \
321			}
322		case 4:
323			ZEND_GEN_GET_CHILD(3)
324		case 3:
325			ZEND_GEN_GET_CHILD(2)
326		case 2:
327			ZEND_GEN_GET_CHILD(1)
328			ZEND_GEN_GET_CHILD(0)
329			ZEND_ASSERT(0); // we never should have no matching child
330	}
331
332	return zend_hash_index_find_ptr(&node->child.ht, (zend_ulong) leaf);
333}
334
335static zend_generator_node *zend_generator_search_multi_children_node(zend_generator_node *node)
336{
337	while (node->children == 1) {
338		node = &node->child.array[0].child->node;
339	}
340	return node->children > 1 ? node : NULL;
341}
342
343static void zend_generator_add_single_child(zend_generator_node *node, zend_generator *child, zend_generator *leaf)
344{
345	if (node->children < 4) {
346		node->child.array[node->children].leaf = leaf;
347		node->child.array[node->children].child = child;
348	} else if (node->children > 4) {
349		zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) leaf, child);
350	} else {
351		struct {
352			zend_generator *leaf;
353			zend_generator *child;
354		} array[4];
355		int i;
356
357		memcpy(&array, &node->child.array, sizeof(array));
358		zend_hash_init(&node->child.ht, 5, sigh, NULL, 0);
359		for (i = 0; i < 4; i++) {
360			zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) array[i].leaf, array[i].child);
361		}
362		zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) leaf, child);
363	}
364
365	node->children++;
366}
367
368static void zend_generator_merge_child_nodes(zend_generator_node *dest, zend_generator_node *src, zend_generator *child)
369{
370	if (src->children <= 4) {
371		int i = src->children;
372		while (i--) {
373			zend_generator_add_single_child(dest, child, src->child.array[i].leaf);
374		}
375	} else {
376		zend_ulong leaf;
377		ZEND_HASH_FOREACH_NUM_KEY(&src->child.ht, leaf) {
378			zend_generator_add_single_child(dest, child, (zend_generator *) leaf);
379		} ZEND_HASH_FOREACH_END();
380	}
381}
382
383/* Make attention so that the root of each subtree of the Generators tree is referenced once per leaf */
384static void zend_generator_add_child(zend_generator *generator, zend_generator *child)
385{
386	zend_generator *leaf = child->node.children ? child->node.ptr.leaf : child;
387	zend_generator_node *multi_children_node;
388	zend_bool was_leaf = generator->node.children == 0;
389
390	if (was_leaf) {
391		zend_generator *next = generator->node.parent;
392		leaf->node.ptr.root = generator->node.ptr.root;
393		++GC_REFCOUNT(&generator->std); /* we need to increment the generator refcount here as it became integrated into the tree (no leaf), but we must not increment the refcount of the *whole* path in tree */
394		generator->node.ptr.leaf = leaf;
395
396		while (next) {
397			if (next->node.children > 1) {
398				if (next->node.children > 4) {
399					zend_generator *child = zend_hash_index_find_ptr(&next->node.child.ht, (zend_ulong) generator);
400					zend_hash_index_del(&next->node.child.ht, (zend_ulong) generator);
401					zend_hash_index_add_ptr(&next->node.child.ht, (zend_ulong) leaf, child);
402				} else {
403					switch (next->node.children) {
404#define ZEND_GEN_UPDATE_CHILD(x) \
405							if (next->node.child.array[x].leaf == generator) { \
406								next->node.child.array[x].leaf = leaf; \
407								break; \
408							}
409						case 4:
410							ZEND_GEN_UPDATE_CHILD(3)
411						case 3:
412							ZEND_GEN_UPDATE_CHILD(2)
413						case 2:
414							ZEND_GEN_UPDATE_CHILD(1)
415							ZEND_GEN_UPDATE_CHILD(0)
416							ZEND_ASSERT(0); // we never should have no matching child
417					}
418				}
419			}
420
421			next->node.ptr.leaf = leaf;
422			next = next->node.parent;
423		}
424
425		zend_generator_add_single_child(&generator->node, child, leaf);
426	} else if (generator->node.children == 1) {
427		multi_children_node = zend_generator_search_multi_children_node(&generator->node);
428		if (multi_children_node) {
429			generator->node.children = 0;
430			zend_generator_merge_child_nodes(&generator->node, multi_children_node, generator->node.child.array[0].child);
431		}
432	}
433
434	if (!was_leaf) {
435		multi_children_node = zend_generator_search_multi_children_node(&child->node);
436	} else {
437		multi_children_node = (zend_generator_node *) 0x1;
438	}
439
440	{
441		zend_generator *parent = generator->node.parent, *cur = generator;
442
443		if (multi_children_node > (zend_generator_node *) 0x1) {
444			zend_generator_merge_child_nodes(&generator->node, multi_children_node, child);
445		} else {
446			zend_generator_add_single_child(&generator->node, child, leaf);
447		}
448		while (parent) {
449			if (parent->node.children > 1) {
450				if (multi_children_node == (zend_generator_node *) 0x1) {
451					multi_children_node = zend_generator_search_multi_children_node(&child->node);
452				}
453				if (multi_children_node) {
454					zend_generator_merge_child_nodes(&parent->node, multi_children_node, cur);
455				} else {
456					zend_generator_add_single_child(&parent->node, cur, leaf);
457				}
458			}
459			cur = parent;
460			parent = parent->node.parent;
461		}
462	}
463}
464
465void zend_generator_yield_from(zend_generator *generator, zend_generator *from)
466{
467	zend_generator_add_child(from, generator);
468
469	generator->node.parent = from;
470	zend_generator_get_current(generator);
471	--GC_REFCOUNT(&from->std);
472}
473
474ZEND_API zend_generator *zend_generator_update_current(zend_generator *generator, zend_generator *leaf)
475{
476	zend_generator *old_root, *root = leaf->node.ptr.root;
477
478	/* generator at the root had stopped */
479	if (root != generator) {
480		old_root = root;
481		root = zend_generator_get_child(&root->node, leaf);
482	} else {
483		old_root = NULL;
484	}
485
486	while (!root->execute_data && root != generator) {
487		OBJ_RELEASE(&old_root->std);
488		old_root = root;
489
490		root = zend_generator_get_child(&root->node, leaf);
491	}
492
493	if (root->node.parent) {
494		if (root->node.parent->execute_data == NULL) {
495			if (EXPECTED(EG(exception) == NULL)) {
496				zend_op *yield_from = (zend_op *) root->execute_data->opline - 1;
497
498				if (yield_from->opcode == ZEND_YIELD_FROM) {
499					if (Z_ISUNDEF(root->node.parent->retval)) {
500						/* Throw the exception in the context of the generator */
501						zend_execute_data *original_execute_data = EG(current_execute_data);
502						EG(current_execute_data) = root->execute_data;
503
504						if (root == generator) {
505							root->execute_data->prev_execute_data = original_execute_data;
506						} else {
507							root->execute_data->prev_execute_data = &generator->execute_fake;
508							generator->execute_fake.prev_execute_data = original_execute_data;
509						}
510
511						zend_throw_exception(zend_ce_ClosedGeneratorException, "Generator yielded from aborted, no return value available", 0);
512
513						EG(current_execute_data) = original_execute_data;
514					} else {
515						zval_ptr_dtor(&root->value);
516						ZVAL_COPY(&root->value, &root->node.parent->value);
517						ZVAL_COPY(ZEND_CALL_VAR(root->execute_data, yield_from->result.var), &root->node.parent->retval);
518					}
519				}
520			}
521
522			root->node.parent = NULL;
523		} else {
524			do {
525				root = root->node.parent;
526				++GC_REFCOUNT(&root->std);
527			} while (root->node.parent);
528		}
529	}
530
531	if (old_root) {
532		OBJ_RELEASE(&old_root->std);
533	}
534
535	return leaf->node.ptr.root = root;
536}
537
538static int zend_generator_get_next_delegated_value(zend_generator *generator) /* {{{ */
539{
540	zval *value;
541	if (Z_TYPE(generator->values) == IS_ARRAY) {
542		HashTable *ht = Z_ARR(generator->values);
543		HashPosition pos = Z_FE_POS(generator->values);
544
545		Bucket *p;
546		do {
547			if (UNEXPECTED(pos >= ht->nNumUsed)) {
548				/* Reached end of array */
549				goto failure;
550			}
551
552			p = &ht->arData[pos];
553			value = &p->val;
554			if (Z_TYPE_P(value) == IS_INDIRECT) {
555				value = Z_INDIRECT_P(value);
556			}
557			pos++;
558		} while (Z_ISUNDEF_P(value));
559
560		zval_ptr_dtor(&generator->value);
561		ZVAL_COPY(&generator->value, value);
562
563		zval_ptr_dtor(&generator->key);
564		if (p->key) {
565			ZVAL_STR_COPY(&generator->key, p->key);
566		} else {
567			ZVAL_LONG(&generator->key, p->h);
568		}
569
570		Z_FE_POS(generator->values) = pos;
571	} else {
572		zend_object_iterator *iter = (zend_object_iterator *) Z_OBJ(generator->values);
573
574		if (iter->index++ > 0) {
575			iter->funcs->move_forward(iter);
576			if (UNEXPECTED(EG(exception) != NULL)) {
577				goto exception;
578			}
579		}
580
581		if (iter->funcs->valid(iter) == FAILURE) {
582			/* reached end of iteration */
583			goto failure;
584		}
585
586		value = iter->funcs->get_current_data(iter);
587		if (UNEXPECTED(EG(exception) != NULL)) {
588			goto exception;
589		} else if (UNEXPECTED(!value)) {
590			goto failure;
591		}
592
593		zval_ptr_dtor(&generator->value);
594		ZVAL_COPY(&generator->value, value);
595
596		zval_ptr_dtor(&generator->key);
597		if (iter->funcs->get_current_key) {
598			iter->funcs->get_current_key(iter, &generator->key);
599			if (UNEXPECTED(EG(exception) != NULL)) {
600				ZVAL_UNDEF(&generator->key);
601				goto exception;
602			}
603		} else {
604			ZVAL_LONG(&generator->key, iter->index);
605		}
606	}
607	return SUCCESS;
608
609exception: {
610		zend_execute_data *ex = EG(current_execute_data);
611		EG(current_execute_data) = generator->execute_data;
612		zend_throw_exception_internal(NULL);
613		EG(current_execute_data) = ex;
614	}
615
616failure:
617	zval_ptr_dtor(&generator->values);
618	ZVAL_UNDEF(&generator->values);
619	return FAILURE;
620}
621/* }}} */
622
623ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
624{
625	zend_generator *generator;
626
627	/* The generator is already closed, thus can't resume */
628	if (UNEXPECTED(!orig_generator->execute_data)) {
629		return;
630	}
631
632	generator = zend_generator_get_current(orig_generator);
633
634try_again:
635	if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
636		zend_throw_error(NULL, "Cannot resume an already running generator");
637		return;
638	}
639
640	if (UNEXPECTED((orig_generator->flags & ZEND_GENERATOR_DO_INIT) != 0 && !Z_ISUNDEF(generator->value))) {
641		/* We must not advance Generator if we yield from a Generator being currently run */
642		return;
643	}
644
645	if (UNEXPECTED(!Z_ISUNDEF(generator->values))) {
646		if (EXPECTED(zend_generator_get_next_delegated_value(generator) == SUCCESS)) {
647			return;
648		}
649		/* If there are no more deletegated values, resume the generator
650		 * after the "yield from" expression. */
651	}
652
653	/* Drop the AT_FIRST_YIELD flag */
654	orig_generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
655
656	{
657		/* Backup executor globals */
658		zend_execute_data *original_execute_data = EG(current_execute_data);
659		zend_class_entry *original_scope = EG(scope);
660		zend_vm_stack original_stack = EG(vm_stack);
661		original_stack->top = EG(vm_stack_top);
662
663		/* Set executor globals */
664		EG(current_execute_data) = generator->execute_data;
665		EG(scope) = generator->execute_data->func->common.scope;
666		EG(vm_stack_top) = generator->stack->top;
667		EG(vm_stack_end) = generator->stack->end;
668		EG(vm_stack) = generator->stack;
669
670		/* We want the backtrace to look as if the generator function was
671		 * called from whatever method we are current running (e.g. next()).
672		 * So we have to link generator call frame with caller call frame. */
673		if (generator == orig_generator) {
674			generator->execute_data->prev_execute_data = original_execute_data;
675		} else {
676			/* We need some execute_data placeholder in stacktrace to be replaced
677			 * by the real stack trace when needed */
678			generator->execute_data->prev_execute_data = &orig_generator->execute_fake;
679			orig_generator->execute_fake.prev_execute_data = original_execute_data;
680		}
681
682		/* Resume execution */
683		generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
684		zend_execute_ex(generator->execute_data);
685		generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
686
687		/* Unlink generator call_frame from the caller and backup vm_stack_top */
688		if (EXPECTED(generator->execute_data)) {
689			generator->stack = EG(vm_stack);
690			generator->stack->top = EG(vm_stack_top);
691		}
692
693		/* Restore executor globals */
694		EG(current_execute_data) = original_execute_data;
695		EG(scope) = original_scope;
696		EG(vm_stack_top) = original_stack->top;
697		EG(vm_stack_end) = original_stack->end;
698		EG(vm_stack) = original_stack;
699
700		/* If an exception was thrown in the generator we have to internally
701		 * rethrow it in the parent scope.
702		 * In case we did yield from, the Exception must be rethrown into
703		 * its calling frame (see above in if (check_yield_from). */
704		if (UNEXPECTED(EG(exception) != NULL)) {
705			if (generator == orig_generator) {
706				zend_generator_close(generator, 0);
707				zend_throw_exception_internal(NULL);
708			} else {
709				generator = zend_generator_get_current(orig_generator);
710				zend_generator_throw_exception(generator, NULL);
711				goto try_again;
712			}
713		}
714
715		/* yield from was used, try another resume. */
716		if (UNEXPECTED((generator != orig_generator && !Z_ISUNDEF(generator->retval)) || (generator->execute_data && (generator->execute_data->opline - 1)->opcode == ZEND_YIELD_FROM))) {
717			generator = zend_generator_get_current(orig_generator);
718			goto try_again;
719		}
720	}
721}
722/* }}} */
723
724static void inline zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
725{
726	if (UNEXPECTED(Z_TYPE(generator->value) == IS_UNDEF) && EXPECTED(generator->execute_data) && EXPECTED(generator->node.parent == NULL)) {
727		generator->flags |= ZEND_GENERATOR_DO_INIT;
728		zend_generator_resume(generator);
729		generator->flags &= ~ZEND_GENERATOR_DO_INIT;
730		generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
731	}
732}
733/* }}} */
734
735static void inline zend_generator_rewind(zend_generator *generator) /* {{{ */
736{
737	zend_generator_ensure_initialized(generator);
738
739	if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
740		zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0);
741	}
742}
743/* }}} */
744
745/* {{{ proto void Generator::rewind()
746 * Rewind the generator */
747ZEND_METHOD(Generator, rewind)
748{
749	zend_generator *generator;
750
751	if (zend_parse_parameters_none() == FAILURE) {
752		return;
753	}
754
755	generator = (zend_generator *) Z_OBJ_P(getThis());
756
757	zend_generator_rewind(generator);
758}
759/* }}} */
760
761/* {{{ proto bool Generator::valid()
762 * Check whether the generator is valid */
763ZEND_METHOD(Generator, valid)
764{
765	zend_generator *generator;
766
767	if (zend_parse_parameters_none() == FAILURE) {
768		return;
769	}
770
771	generator = (zend_generator *) Z_OBJ_P(getThis());
772
773	zend_generator_ensure_initialized(generator);
774
775	zend_generator_get_current(generator);
776
777	RETURN_BOOL(EXPECTED(generator->execute_data != NULL));
778}
779/* }}} */
780
781/* {{{ proto mixed Generator::current()
782 * Get the current value */
783ZEND_METHOD(Generator, current)
784{
785	zend_generator *generator, *root;
786
787	if (zend_parse_parameters_none() == FAILURE) {
788		return;
789	}
790
791	generator = (zend_generator *) Z_OBJ_P(getThis());
792
793	zend_generator_ensure_initialized(generator);
794
795	root = zend_generator_get_current(generator);
796	if (EXPECTED(generator->execute_data != NULL && Z_TYPE(root->value) != IS_UNDEF)) {
797		zval *value = &root->value;
798
799		ZVAL_DEREF(value);
800		ZVAL_COPY(return_value, value);
801	}
802}
803/* }}} */
804
805/* {{{ proto mixed Generator::key()
806 * Get the current key */
807ZEND_METHOD(Generator, key)
808{
809	zend_generator *generator, *root;
810
811	if (zend_parse_parameters_none() == FAILURE) {
812		return;
813	}
814
815	generator = (zend_generator *) Z_OBJ_P(getThis());
816
817	zend_generator_ensure_initialized(generator);
818
819	root = zend_generator_get_current(generator);
820	if (EXPECTED(generator->execute_data != NULL && Z_TYPE(root->key) != IS_UNDEF)) {
821		zval *key = &root->key;
822
823		ZVAL_DEREF(key);
824		ZVAL_COPY(return_value, key);
825	}
826}
827/* }}} */
828
829/* {{{ proto void Generator::next()
830 * Advances the generator */
831ZEND_METHOD(Generator, next)
832{
833	zend_generator *generator;
834
835	if (zend_parse_parameters_none() == FAILURE) {
836		return;
837	}
838
839	generator = (zend_generator *) Z_OBJ_P(getThis());
840
841	zend_generator_ensure_initialized(generator);
842
843	zend_generator_resume(generator);
844}
845/* }}} */
846
847/* {{{ proto mixed Generator::send(mixed value)
848 * Sends a value to the generator */
849ZEND_METHOD(Generator, send)
850{
851	zval *value;
852	zend_generator *generator, *root;
853
854#ifndef FAST_ZPP
855	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
856		return;
857	}
858#else
859	ZEND_PARSE_PARAMETERS_START(1, 1)
860		Z_PARAM_ZVAL(value)
861	ZEND_PARSE_PARAMETERS_END();
862#endif
863
864	generator = (zend_generator *) Z_OBJ_P(getThis());
865
866	zend_generator_ensure_initialized(generator);
867
868	/* The generator is already closed, thus can't send anything */
869	if (UNEXPECTED(!generator->execute_data)) {
870		return;
871	}
872
873	root = zend_generator_get_current(generator);
874	/* Put sent value in the target VAR slot, if it is used */
875	if (root->send_target) {
876		Z_TRY_DELREF_P(root->send_target);
877		ZVAL_COPY(root->send_target, value);
878	}
879
880	zend_generator_resume(generator);
881
882	root = zend_generator_get_current(generator);
883	if (EXPECTED(generator->execute_data)) {
884		zval *value = &root->value;
885
886		ZVAL_DEREF(value);
887		ZVAL_COPY(return_value, value);
888	}
889}
890/* }}} */
891
892/* {{{ proto mixed Generator::throw(Exception exception)
893 * Throws an exception into the generator */
894ZEND_METHOD(Generator, throw)
895{
896	zval *exception, exception_copy;
897	zend_generator *generator;
898
899#ifndef FAST_ZPP
900	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception) == FAILURE) {
901		return;
902	}
903#else
904	ZEND_PARSE_PARAMETERS_START(1, 1)
905		Z_PARAM_ZVAL(exception)
906	ZEND_PARSE_PARAMETERS_END();
907#endif
908
909	ZVAL_DUP(&exception_copy, exception);
910
911	generator = (zend_generator *) Z_OBJ_P(getThis());
912
913	zend_generator_ensure_initialized(generator);
914
915	if (generator->execute_data) {
916		zend_generator *root = zend_generator_get_current(generator);
917
918		zend_generator_throw_exception(root, &exception_copy);
919
920		zend_generator_resume(generator);
921
922		root = zend_generator_get_current(generator);
923		if (generator->execute_data) {
924			zval *value = &root->value;
925
926			ZVAL_DEREF(value);
927			ZVAL_COPY(return_value, value);
928		}
929	} else {
930		/* If the generator is already closed throw the exception in the
931		 * current context */
932		zend_throw_exception_object(&exception_copy);
933	}
934}
935/* }}} */
936
937/* {{{ proto mixed Generator::getReturn()
938 * Retrieves the return value of the generator */
939ZEND_METHOD(Generator, getReturn)
940{
941	zend_generator *generator;
942
943	if (zend_parse_parameters_none() == FAILURE) {
944		return;
945	}
946
947	generator = (zend_generator *) Z_OBJ_P(getThis());
948
949	zend_generator_ensure_initialized(generator);
950	if (UNEXPECTED(EG(exception))) {
951		return;
952	}
953
954	if (Z_ISUNDEF(generator->retval)) {
955		/* Generator hasn't returned yet -> error! */
956		zend_throw_exception(NULL,
957			"Cannot get return value of a generator that hasn't returned", 0);
958		return;
959	}
960
961	ZVAL_COPY(return_value, &generator->retval);
962}
963/* }}} */
964
965/* {{{ proto void Generator::__wakeup()
966 * Throws an Exception as generators can't be serialized */
967ZEND_METHOD(Generator, __wakeup)
968{
969	/* Just specifying the zend_class_unserialize_deny handler is not enough,
970	 * because it is only invoked for C unserialization. For O the error has
971	 * to be thrown in __wakeup. */
972
973	if (zend_parse_parameters_none() == FAILURE) {
974		return;
975	}
976
977	zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0);
978}
979/* }}} */
980
981/* get_iterator implementation */
982
983static void zend_generator_iterator_dtor(zend_object_iterator *iterator) /* {{{ */
984{
985	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
986	generator->iterator = NULL;
987	zval_ptr_dtor(&iterator->data);
988	zend_iterator_dtor(iterator);
989}
990/* }}} */
991
992static int zend_generator_iterator_valid(zend_object_iterator *iterator) /* {{{ */
993{
994	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
995
996	zend_generator_ensure_initialized(generator);
997
998	zend_generator_get_current(generator);
999
1000	return generator->execute_data ? SUCCESS : FAILURE;
1001}
1002/* }}} */
1003
1004static zval *zend_generator_iterator_get_data(zend_object_iterator *iterator) /* {{{ */
1005{
1006	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
1007
1008	zend_generator_ensure_initialized(generator);
1009
1010	root = zend_generator_get_current(generator);
1011
1012	return &root->value;
1013}
1014/* }}} */
1015
1016static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key) /* {{{ */
1017{
1018	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
1019
1020	zend_generator_ensure_initialized(generator);
1021
1022	root = zend_generator_get_current(generator);
1023
1024	if (EXPECTED(Z_TYPE(root->key) != IS_UNDEF)) {
1025		zval *zv = &root->key;
1026
1027		ZVAL_DEREF(zv);
1028		ZVAL_COPY(key, zv);
1029	} else {
1030		ZVAL_NULL(key);
1031	}
1032}
1033/* }}} */
1034
1035static void zend_generator_iterator_move_forward(zend_object_iterator *iterator) /* {{{ */
1036{
1037	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
1038
1039	zend_generator_ensure_initialized(generator);
1040
1041	zend_generator_resume(generator);
1042}
1043/* }}} */
1044
1045static void zend_generator_iterator_rewind(zend_object_iterator *iterator) /* {{{ */
1046{
1047	zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
1048
1049	zend_generator_rewind(generator);
1050}
1051/* }}} */
1052
1053static zend_object_iterator_funcs zend_generator_iterator_functions = {
1054	zend_generator_iterator_dtor,
1055	zend_generator_iterator_valid,
1056	zend_generator_iterator_get_data,
1057	zend_generator_iterator_get_key,
1058	zend_generator_iterator_move_forward,
1059	zend_generator_iterator_rewind
1060};
1061
1062zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1063{
1064	zend_object_iterator *iterator;
1065	zend_generator *generator = (zend_generator*)Z_OBJ_P(object);
1066
1067	if (!generator->execute_data) {
1068		zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0);
1069		return NULL;
1070	}
1071
1072	if (UNEXPECTED(by_ref) && !(generator->execute_data->func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
1073		zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0);
1074		return NULL;
1075	}
1076
1077	iterator = generator->iterator = emalloc(sizeof(zend_object_iterator));
1078
1079	zend_iterator_init(iterator);
1080
1081	iterator->funcs = &zend_generator_iterator_functions;
1082	ZVAL_COPY(&iterator->data, object);
1083
1084	return iterator;
1085}
1086/* }}} */
1087
1088ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
1089ZEND_END_ARG_INFO()
1090
1091ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
1092	ZEND_ARG_INFO(0, value)
1093ZEND_END_ARG_INFO()
1094
1095ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
1096	ZEND_ARG_INFO(0, exception)
1097ZEND_END_ARG_INFO()
1098
1099static const zend_function_entry generator_functions[] = {
1100	ZEND_ME(Generator, rewind,   arginfo_generator_void, ZEND_ACC_PUBLIC)
1101	ZEND_ME(Generator, valid,    arginfo_generator_void, ZEND_ACC_PUBLIC)
1102	ZEND_ME(Generator, current,  arginfo_generator_void, ZEND_ACC_PUBLIC)
1103	ZEND_ME(Generator, key,      arginfo_generator_void, ZEND_ACC_PUBLIC)
1104	ZEND_ME(Generator, next,     arginfo_generator_void, ZEND_ACC_PUBLIC)
1105	ZEND_ME(Generator, send,     arginfo_generator_send, ZEND_ACC_PUBLIC)
1106	ZEND_ME(Generator, throw,    arginfo_generator_throw, ZEND_ACC_PUBLIC)
1107	ZEND_ME(Generator, getReturn,arginfo_generator_void, ZEND_ACC_PUBLIC)
1108	ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
1109	ZEND_FE_END
1110};
1111
1112void zend_register_generator_ce(void) /* {{{ */
1113{
1114	zend_class_entry ce;
1115
1116	INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
1117	zend_ce_generator = zend_register_internal_class(&ce);
1118	zend_ce_generator->ce_flags |= ZEND_ACC_FINAL;
1119	zend_ce_generator->create_object = zend_generator_create;
1120	zend_ce_generator->serialize = zend_class_serialize_deny;
1121	zend_ce_generator->unserialize = zend_class_unserialize_deny;
1122
1123	/* get_iterator has to be assigned *after* implementing the inferface */
1124	zend_class_implements(zend_ce_generator, 1, zend_ce_iterator);
1125	zend_ce_generator->get_iterator = zend_generator_get_iterator;
1126	zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
1127
1128	memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1129	zend_generator_handlers.free_obj = zend_generator_free_storage;
1130	zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
1131	zend_generator_handlers.clone_obj = NULL;
1132	zend_generator_handlers.get_constructor = zend_generator_get_constructor;
1133
1134	INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", NULL);
1135	zend_ce_ClosedGeneratorException = zend_register_internal_class_ex(&ce, zend_ce_exception);
1136}
1137/* }}} */
1138
1139/*
1140 * Local variables:
1141 * tab-width: 4
1142 * c-basic-offset: 4
1143 * indent-tabs-mode: t
1144 * End:
1145 */
1146