Commit:    40b7533576b4f2ec4ba872d027179d92db71d844
Author:    Nikita Popov <ni...@php.net>         Sun, 20 May 2012 14:19:16 +0200
Parents:   ca59e5464de78dae1f7c874a6bcc4f7ea2948b1d
Branches:  master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=40b7533576b4f2ec4ba872d027179d92db71d844

Log:
Add some boilerplate code for Generator class

The Generator class now uses a zend_generator struct, so it'll be able to
store additional info.

This commit also ensures that Generator cannot be directly instantiated
and extended. The error tests are now in a separate folder from the
(yet-to-come) functional tests.

Changed paths:
  A  Zend/tests/generators/errors/generator_extend_error.phpt
  A  Zend/tests/generators/errors/generator_instantiate_error.phpt
  A  Zend/tests/generators/errors/yield_in_normal_function_error.phpt
  A  Zend/tests/generators/errors/yield_outside_function_error.phpt
  D  Zend/tests/generators/yield_in_normal_function_error.phpt
  D  Zend/tests/generators/yield_outside_function_error.phpt
  M  Zend/zend_generators.c


Diff:
diff --git a/Zend/tests/generators/errors/generator_extend_error.phpt 
b/Zend/tests/generators/errors/generator_extend_error.phpt
new file mode 100644
index 0000000..550f16a
--- /dev/null
+++ b/Zend/tests/generators/errors/generator_extend_error.phpt
@@ -0,0 +1,10 @@
+--TEST--
+The Generator class cannot be extended
+--FILE--
+<?php
+
+class ExtendedGenerator extends Generator { }
+
+?>
+--EXPECTF--
+Fatal error: Class ExtendedGenerator may not inherit from final class 
(Generator) in %s on line %d
diff --git a/Zend/tests/generators/errors/generator_instantiate_error.phpt 
b/Zend/tests/generators/errors/generator_instantiate_error.phpt
new file mode 100644
index 0000000..f8941c0
--- /dev/null
+++ b/Zend/tests/generators/errors/generator_instantiate_error.phpt
@@ -0,0 +1,10 @@
+--TEST--
+It's not possible to directly instantiate the Generator class
+--FILE--
+<?php
+
+new Generator;
+
+?>
+--EXPECTF--
+Catchable fatal error: The "Generator" class is reserved for internal use and 
cannot be manually instantiated in %s on line %d
diff --git a/Zend/tests/generators/errors/yield_in_normal_function_error.phpt 
b/Zend/tests/generators/errors/yield_in_normal_function_error.phpt
new file mode 100644
index 0000000..802510d
--- /dev/null
+++ b/Zend/tests/generators/errors/yield_in_normal_function_error.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Yield cannot be used in normal (non-generator) functions
+--FILE--
+<?php
+
+function foo() {
+    yield "Test";
+}
+
+?>
+--EXPECTF--
+Fatal error: The "yield" statement can only be used inside a generator 
function in %s on line %d
diff --git a/Zend/tests/generators/errors/yield_outside_function_error.phpt 
b/Zend/tests/generators/errors/yield_outside_function_error.phpt
new file mode 100644
index 0000000..fd7169d
--- /dev/null
+++ b/Zend/tests/generators/errors/yield_outside_function_error.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Yield cannot be used outside of functions
+--FILE--
+<?php
+
+yield "Test";
+
+?>
+--EXPECTF--
+Fatal error: The "yield" statement can only be used inside a generator 
function in %s on line %d
diff --git a/Zend/tests/generators/yield_in_normal_function_error.phpt 
b/Zend/tests/generators/yield_in_normal_function_error.phpt
deleted file mode 100644
index 802510d..0000000
--- a/Zend/tests/generators/yield_in_normal_function_error.phpt
+++ /dev/null
@@ -1,12 +0,0 @@
---TEST--
-Yield cannot be used in normal (non-generator) functions
---FILE--
-<?php
-
-function foo() {
-    yield "Test";
-}
-
-?>
---EXPECTF--
-Fatal error: The "yield" statement can only be used inside a generator 
function in %s on line %d
diff --git a/Zend/tests/generators/yield_outside_function_error.phpt 
b/Zend/tests/generators/yield_outside_function_error.phpt
deleted file mode 100644
index fd7169d..0000000
--- a/Zend/tests/generators/yield_outside_function_error.phpt
+++ /dev/null
@@ -1,10 +0,0 @@
---TEST--
-Yield cannot be used outside of functions
---FILE--
-<?php
-
-yield "Test";
-
-?>
---EXPECTF--
-Fatal error: The "yield" statement can only be used inside a generator 
function in %s on line %d
diff --git a/Zend/zend_generators.c b/Zend/zend_generators.c
index 6f98dcb..21581db 100644
--- a/Zend/zend_generators.c
+++ b/Zend/zend_generators.c
@@ -23,6 +23,49 @@
 #include "zend_generators.h"
 
 ZEND_API zend_class_entry *zend_ce_generator;
+static zend_object_handlers zend_generator_handlers;
+
+typedef struct _zend_generator {
+       zend_object std;
+       /* nothing more for now */
+} zend_generator;
+
+static void zend_generator_free_storage(zend_generator *generator TSRMLS_DC) 
/* {{{ */
+{
+       zend_object_std_dtor(&generator->std TSRMLS_CC);
+
+       efree(generator);
+}
+/* }}} */
+
+static zend_object_value zend_generator_create(zend_class_entry *class_type 
TSRMLS_DC) /* {{{ */
+{
+       zend_generator *generator;
+       zend_object_value object;
+
+       generator = emalloc(sizeof(zend_generator));
+       memset(generator, 0, sizeof(zend_generator));
+
+       zend_object_std_init(&generator->std, class_type TSRMLS_CC);
+
+       object.handle = zend_objects_store_put(generator, NULL,
+               (zend_objects_free_object_storage_t) 
zend_generator_free_storage,
+               NULL /* no clone handler for now */
+               TSRMLS_CC
+       );
+       object.handlers = &zend_generator_handlers;
+
+       return object;
+}
+/* }}} */
+
+static zend_function *zend_generator_get_constructor(zval *object TSRMLS_DC) 
/* {{{ */
+{
+       zend_error(E_RECOVERABLE_ERROR, "The \"Generator\" class is reserved 
for internal use and cannot be manually instantiated");
+
+       return NULL;
+}
+/* }}} */
 
 static const zend_function_entry generator_functions[] = {
        ZEND_FE_END
@@ -35,6 +78,10 @@ void zend_register_generator_ce(TSRMLS_D) /* {{{ */
        INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
        zend_ce_generator = zend_register_internal_class(&ce TSRMLS_CC);
        zend_ce_generator->ce_flags |= ZEND_ACC_FINAL_CLASS;
+       zend_ce_generator->create_object = zend_generator_create;
+
+       memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), 
sizeof(zend_object_handlers));
+       zend_generator_handlers.get_constructor = 
zend_generator_get_constructor;
 }
 /* }}} */


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to