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