Permalink
Browse files

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.
  • Loading branch information...
1 parent ca59e54 commit 40b7533576b4f2ec4ba872d027179d92db71d844 @nikic nikic committed May 20, 2012
@@ -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
@@ -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
View
@@ -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;
}
/* }}} */

0 comments on commit 40b7533

Please sign in to comment.