--- /dev/null
+--TEST--
+Generator::throw() where the generator throws a different exception
+--FILE--
+<?php
+
+function gen() {
+ try {
+ yield;
+ } catch (RuntimeException $e) {
+ echo 'Caught: ', $e, "\n\n";
+
+ throw new LogicException('new throw');
+ }
+}
+
+$gen = gen();
+var_dump($gen->throw(new RuntimeException('throw')));
+
+?>
+--EXPECTF--
+Caught: exception 'RuntimeException' with message 'throw' in %s:%d
+Stack trace:
+#0 {main}
+
+
+Fatal error: Uncaught exception 'LogicException' with message 'new throw' in %s:%d
+Stack trace:
+#0 [internal function]: gen()
+#1 %s(%d): Generator->throw(Object(RuntimeException))
+#2 {main}
+ thrown in %s on line %d
+
ZEND_API void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
{
- if (EG(exception)) {
- return;
- }
-
/* The generator is already closed, thus can't resume */
if (!generator->execute_data) {
return;
}
/* }}} */
-/* {{{ proto mixed Generator::send()
+/* {{{ proto mixed Generator::send(mixed $value)
* Sends a value to the generator */
ZEND_METHOD(Generator, send)
{
}
/* }}} */
+/* {{{ proto mixed Generator::throw(Exception $exception)
+ * Throws an exception into the generator */
+ZEND_METHOD(Generator, throw)
+{
+ zval *exception, *exception_copy;
+ zend_generator *generator;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception) == FAILURE) {
+ return;
+ }
+
+ ALLOC_ZVAL(exception_copy);
+ MAKE_COPY_ZVAL(&exception, exception_copy);
+
+ generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
+
+ if (generator->execute_data) {
+ /* Throw the exception in the context of the generator */
+ zend_execute_data *current_execute_data = EG(current_execute_data);
+ EG(current_execute_data) = generator->execute_data;
+
+ zend_throw_exception_object(exception_copy TSRMLS_CC);
+
+ EG(current_execute_data) = current_execute_data;
+
+ zend_generator_resume(generator TSRMLS_CC);
+
+ if (generator->value) {
+ RETURN_ZVAL(generator->value, 1, 0);
+ }
+ } else {
+ /* If the generator is already closed throw the exception in the
+ * current context */
+ zend_throw_exception_object(exception_copy TSRMLS_CC);
+ }
+}
+/* }}} */
+
/* {{{ proto void Generator::__wakeup()
* Throws an Exception as generators can't be serialized */
ZEND_METHOD(Generator, __wakeup)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
+ ZEND_ARG_INFO(0, exception)
+ZEND_END_ARG_INFO()
+
static const zend_function_entry generator_functions[] = {
ZEND_ME(Generator, rewind, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, valid, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, key, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, next, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, send, arginfo_generator_send, ZEND_ACC_PUBLIC)
+ ZEND_ME(Generator, throw, arginfo_generator_throw, ZEND_ACC_PUBLIC)
ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
ZEND_FE_END
};