Browse Source

Merge pull request #200 from stesie/generator-passing

Generator passing from JavaScript to PHP
Stefan Siegl 9 years ago
parent
commit
53b8ac759f

+ 4 - 0
php_v8js_macros.h

@@ -61,6 +61,10 @@ extern "C" {
 #define V8JS_GET_CLASS_NAME(var, obj) \
 #define V8JS_GET_CLASS_NAME(var, obj) \
 	v8::String::Utf8Value var(obj->GetConstructorName());
 	v8::String::Utf8Value var(obj->GetConstructorName());
 
 
+#if PHP_V8_API_VERSION >= 3030000
+#define V8JS_V8GENERATOR_SUPPORT 1
+#endif
+
 /* method signatures of zend_update_property and zend_read_property were
 /* method signatures of zend_update_property and zend_read_property were
  * declared as 'char *' instead of 'const char *' before PHP 5.4 */
  * declared as 'char *' instead of 'const char *' before PHP 5.4 */
 #if ZEND_MODULE_API_NO >= 20100525
 #if ZEND_MODULE_API_NO >= 20100525

+ 38 - 0
tests/generators_from_v8_001.phpt

@@ -0,0 +1,38 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (foreach)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(0)
+int(1)
+int(2)
+int(3)
+===EOF===

+ 63 - 0
tests/generators_from_v8_002.phpt

@@ -0,0 +1,63 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (direct)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+var_dump($gen->current());
+
+// JS generators don't have the key concept (-> just "false")
+var_dump($gen->key());
+
+// fetching multiple times shouldn't leak
+var_dump($gen->current());
+var_dump($gen->current());
+
+$gen->next(); // 1
+var_dump($gen->current());
+
+$gen->next(); // 2
+var_dump($gen->current());
+
+$gen->next(); // 3
+var_dump($gen->current());
+var_dump($gen->valid());
+
+$gen->next(); // undef
+var_dump($gen->current());
+var_dump($gen->valid());
+
+?>
+===EOF===
+--EXPECTF--
+int(0)
+bool(false)
+int(0)
+int(0)
+int(1)
+int(2)
+int(3)
+bool(true)
+NULL
+bool(false)
+===EOF===

+ 47 - 0
tests/generators_from_v8_003.phpt

@@ -0,0 +1,47 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (rewind)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(0)
+int(1)
+int(2)
+int(3)
+
+Fatal error: Uncaught V8JsException: V8Generator::rewind not supported by ES6 in %s
+Stack trace:
+#0 %s: V8Generator->rewind()
+#1 {main}
+  thrown in %s on line 20

+ 39 - 0
tests/generators_from_v8_004.phpt

@@ -0,0 +1,39 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (instantiate in PHP + foreach)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen;
+EOJS;
+
+$v8 = new V8Js();
+$TheGen = $v8->executeString($js);
+$gen = $TheGen();
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(0)
+int(1)
+int(2)
+int(3)
+===EOF===

+ 45 - 0
tests/generators_from_v8_005.phpt

@@ -0,0 +1,45 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (instantiate in PHP + iterate in JS)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen;
+EOJS;
+
+$v8 = new V8Js();
+$TheGen = $v8->executeString($js);
+$gen = $TheGen();
+
+$js = <<<EOJS
+(function(gen) {
+  for(var i of gen) {
+    var_dump(i);
+  }
+})
+EOJS;
+$fn = $v8->executeString($js);
+$fn($gen);
+
+?>
+===EOF===
+--EXPECTF--
+int(0)
+int(1)
+int(2)
+int(3)
+===EOF===

+ 47 - 0
tests/generators_from_v8_006.phpt

@@ -0,0 +1,47 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (yield from)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+function PhpGen() {
+    yield 23;
+
+    $js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen();
+EOJS;
+
+    $v8 = new V8Js();
+    $jsGen = $v8->executeString($js);
+
+    yield from $jsGen;
+}
+
+$gen = PhpGen();
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(23)
+int(0)
+int(1)
+int(2)
+int(3)
+===EOF===

+ 39 - 0
tests/generators_from_v8_007.phpt

@@ -0,0 +1,39 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (throw JS)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  yield 23;
+  throw new Error('blar');
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(23)
+
+Fatal error: Uncaught V8JsScriptException: %s Error: blar in %s
+Stack trace:
+#0 %s: V8Generator->next()
+#1 {main}
+  thrown in %s

+ 44 - 0
tests/generators_from_v8_008.phpt

@@ -0,0 +1,44 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (throw PHP)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  yield 23;
+  yield PHP.getValue();
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$v8->getValue = function() {
+    throw new \Exception('this shall not work');
+};
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(23)
+
+Fatal error: Uncaught Exception: this shall not work in %s
+Stack trace:
+#0 [internal function]: {closure}()
+#1 [internal function]: Closure->__invoke()
+#2 %s: V8Generator->next()
+#3 {main}
+  thrown in %s

+ 38 - 0
tests/generators_from_v8_009.phpt

@@ -0,0 +1,38 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (fatal error)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  yield 23;
+  yield PHP.getValue();
+}
+
+TheGen();
+EOJS;
+
+$v8 = new V8Js();
+$v8->getValue = function() {
+    trigger_error("you're gonna fail now", E_USER_ERROR);
+};
+$gen = $v8->executeString($js);
+
+foreach($gen as $a) {
+    var_dump($a);
+}
+
+?>
+===EOF===
+--EXPECTF--
+int(23)
+
+Fatal error: you're gonna fail now in %s

+ 38 - 0
tests/generators_from_v8_010.phpt

@@ -0,0 +1,38 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP (properties)
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  yield 23;
+  yield PHP.getValue();
+}
+
+var gen = TheGen();
+gen.foo = 23;
+gen.bar = function() { var_dump("Hello World"); };
+
+gen;
+EOJS;
+
+$v8 = new V8Js();
+$gen = $v8->executeString($js);
+
+var_dump($gen->foo);
+$gen->bar();
+
+?>
+===EOF===
+--EXPECT--
+int(23)
+string(11) "Hello World"
+===EOF===

+ 49 - 0
tests/generators_from_v8_basic.phpt

@@ -0,0 +1,49 @@
+--TEST--
+Test V8::executeString() : Generators V8 -> PHP
+--SKIPIF--
+<?php
+require_once(dirname(__FILE__) . '/skipif.inc');
+
+if (!class_exists('V8Generator')) {
+    die("skip Installed V8 version doesn't support generators");
+}
+?>
+--FILE--
+<?php
+
+$js = <<<EOJS
+function* TheGen() {
+  for(var i = 0; i < 4; i ++) {
+    yield i;
+  }
+}
+
+TheGen.theValue = 23;
+
+EOJS;
+
+$v8 = new V8Js();
+$v8->executeString($js);
+
+// just get the Generator itself -- it's just a normal JS function to us,
+// i.e. V8Js should create a V8Function object.
+$gen = $v8->executeString('(TheGen)');
+var_dump($gen);
+
+// now instantiate the Generator and pass that back -- should become a
+// V8Generator object that implements the Iterator interface
+$gen = $v8->executeString('(TheGen())');
+var_dump($gen);
+var_dump($gen instanceof Iterator);
+
+?>
+===EOF===
+--EXPECTF--
+object(V8Function)#%d (1) {
+  ["theValue"]=>
+  int(23)
+}
+object(V8Generator)#%d (0) {
+}
+bool(true)
+===EOF===

+ 5 - 1
v8js_object_export.cc

@@ -938,7 +938,11 @@ v8::Handle<v8::Value> v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRML
 	}
 	}
 
 
 	/* Special case, passing back object originating from JS to JS */
 	/* Special case, passing back object originating from JS to JS */
-	if (ce == php_ce_v8function) {
+	if (ce == php_ce_v8function
+#ifdef V8JS_V8GENERATOR_SUPPORT
+		|| ce == php_ce_v8generator
+#endif
+		) {
 		v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(value);
 		v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(value);
 
 
 		if(isolate != c->ctx->isolate) {
 		if(isolate != c->ctx->isolate) {

+ 1 - 1
v8js_v8.cc

@@ -198,7 +198,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
 		}
 		}
 
 
 		/* Convert V8 value to PHP value */
 		/* Convert V8 value to PHP value */
-		if (!result.IsEmpty()) {
+		if (return_value && !result.IsEmpty()) {
 			v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
 			v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
 		}
 		}
 	}
 	}

+ 259 - 6
v8js_v8object_class.cc

@@ -2,12 +2,13 @@
   +----------------------------------------------------------------------+
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   +----------------------------------------------------------------------+
-  | Copyright (c) 1997-2013 The PHP Group                                |
+  | Copyright (c) 1997-2016 The PHP Group                                |
   +----------------------------------------------------------------------+
   +----------------------------------------------------------------------+
   | http://www.opensource.org/licenses/mit-license.php  MIT License      |
   | http://www.opensource.org/licenses/mit-license.php  MIT License      |
   +----------------------------------------------------------------------+
   +----------------------------------------------------------------------+
   | Author: Jani Taskinen <[email protected]>                         |
   | Author: Jani Taskinen <[email protected]>                         |
   | Author: Patrick Reilly <[email protected]>                             |
   | Author: Patrick Reilly <[email protected]>                             |
+  | Author: Stefan Siegl <[email protected]>                                |
   +----------------------------------------------------------------------+
   +----------------------------------------------------------------------+
 */
 */
 
 
@@ -32,10 +33,18 @@ extern "C" {
 /* {{{ Class Entries */
 /* {{{ Class Entries */
 zend_class_entry *php_ce_v8object;
 zend_class_entry *php_ce_v8object;
 zend_class_entry *php_ce_v8function;
 zend_class_entry *php_ce_v8function;
+
+#ifdef V8JS_V8GENERATOR_SUPPORT
+zend_class_entry *php_ce_v8generator;
+#endif
 /* }}} */
 /* }}} */
 
 
 /* {{{ Object Handlers */
 /* {{{ Object Handlers */
 static zend_object_handlers v8js_v8object_handlers;
 static zend_object_handlers v8js_v8object_handlers;
+
+#ifdef V8JS_V8GENERATOR_SUPPORT
+static zend_object_handlers v8js_v8generator_handlers;
+#endif
 /* }}} */
 /* }}} */
 
 
 #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
 #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
@@ -307,7 +316,6 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I
 			}
 			}
 
 
 			v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
 			v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
-			v8::Local<v8::Value> js_retval;
 
 
 			for (i = 0; i < argc; i++) {
 			for (i = 0; i < argc; i++) {
 				new(&jsArgv[i]) v8::Local<v8::Value>;
 				new(&jsArgv[i]) v8::Local<v8::Value>;
@@ -396,9 +404,6 @@ static zend_object *v8js_v8object_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
 	c->std.handlers = &v8js_v8object_handlers;
 	c->std.handlers = &v8js_v8object_handlers;
 	new(&c->v8obj) v8::Persistent<v8::Value>();
 	new(&c->v8obj) v8::Persistent<v8::Value>();
 
 
-	v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
-	v8js_v8object_handlers.free_obj = v8js_v8object_free_storage;
-	
 	return &c->std;
 	return &c->std;
 }
 }
 /* }}} */
 /* }}} */
@@ -468,11 +473,206 @@ PHP_METHOD(V8Function, __wakeup)
 }
 }
 /* }}} */
 /* }}} */
 
 
+
+#ifdef V8JS_V8GENERATOR_SUPPORT
+static void v8js_v8generator_free_storage(zend_object *object) /* {{{ */
+{
+	v8js_v8generator *c = v8js_v8generator_fetch_object(object);
+	zval_dtor(&c->value);
+
+	v8js_v8object_free_storage(object);
+}
+/* }}} */
+
+static zend_object *v8js_v8generator_new(zend_class_entry *ce) /* {{{ */
+{
+	v8js_v8generator *c;
+	c = (v8js_v8generator *) ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce));
+
+	zend_object_std_init(&c->v8obj.std, ce);
+	c->v8obj.std.handlers = &v8js_v8generator_handlers;
+	new(&c->v8obj.v8obj) v8::Persistent<v8::Value>();
+
+	return &c->v8obj.std;
+}
+/* }}} */
+
+static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
+{
+	if (!g->v8obj.ctx) {
+		zend_throw_exception(php_ce_v8js_exception,
+			"Can't access V8Generator after V8Js instance is destroyed!", 0);
+		return;
+	}
+
+	/* std::function relies on its dtor to be executed, otherwise it leaks
+	 * some memory on bailout. */
+	{
+		std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [g](v8::Isolate *isolate) {
+			int i = 0;
+
+			v8::Local<v8::String> method_name = V8JS_STR("next");
+			v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject();
+			v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));;
+
+			v8::Local<v8::Value> result = cb->Call(v8obj, 0, NULL);
+
+			if(result.IsEmpty()) {
+				/* cb->Call probably threw (and already threw a zend exception), just return */
+				return V8JS_NULL;
+			}
+
+			if(!result->IsObject()) {
+				zend_throw_exception(php_ce_v8js_exception,
+					"V8Generator returned non-object on next()", 0);
+				return V8JS_NULL;
+			}
+
+			v8::Local<v8::Object> resultObj = result->ToObject();
+			v8::Local<v8::Value> val = resultObj->Get(V8JS_STR("value"));
+			v8::Local<v8::Value> done = resultObj->Get(V8JS_STR("done"));
+
+			zval_dtor(&g->value);
+			v8js_to_zval(val, &g->value, 0, isolate);
+
+			g->done = done->IsTrue();
+			g->primed = true;
+			return V8JS_NULL;
+		};
+
+		v8js_v8_call(g->v8obj.ctx, NULL, g->v8obj.flags, g->v8obj.ctx->time_limit, g->v8obj.ctx->memory_limit, v8_call);
+	}
+
+	if(V8JSG(fatal_error_abort)) {
+		/* Check for fatal error marker possibly set by v8js_error_handler; just
+		 * rethrow the error since we're now out of V8. */
+		zend_bailout();
+	}
+}
+/* }}} */
+
+static zend_function *v8js_v8generator_get_method(zend_object **object_ptr, zend_string *method, const zval *key) /* {{{ */
+{
+	zend_function *result = std_object_handlers.get_method(object_ptr, method, key);
+
+	if(!result) {
+		result = v8js_v8object_get_method(object_ptr, method, key);
+	}
+
+	return result;
+}
+/* }}} */
+
+/* {{{ proto V8Generator::__construct()
+ */
+PHP_METHOD(V8Generator,__construct)
+{
+	zend_throw_exception(php_ce_v8js_exception,
+		"Can't directly construct V8 objects!", 0 TSRMLS_CC);
+	RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ proto V8Generator::__sleep()
+ */
+PHP_METHOD(V8Generator, __sleep)
+{
+	zend_throw_exception(php_ce_v8js_exception,
+		"You cannot serialize or unserialize V8Generator instances", 0 TSRMLS_CC);
+	RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ proto V8Generator::__wakeup()
+ */
+PHP_METHOD(V8Generator, __wakeup)
+{
+	zend_throw_exception(php_ce_v8js_exception,
+		"You cannot serialize or unserialize V8Generator instances", 0 TSRMLS_CC);
+	RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ mixed V8Generator::current()
+ */
+PHP_METHOD(V8Generator, current)
+{
+	v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
+
+	if(!g->primed) {
+		v8js_v8generator_next(g);
+	}
+
+	RETVAL_ZVAL(&g->value, 1, 0);
+}
+/* }}} */
+
+/* {{{ scalar V8Generator::key()
+ */
+PHP_METHOD(V8Generator, key)
+{
+	RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ void V8Generator::next()
+ */
+PHP_METHOD(V8Generator, next)
+{
+	v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
+	v8js_v8generator_next(g);
+}
+/* }}} */
+
+/* {{{ void V8Generator::rewind()
+ */
+PHP_METHOD(V8Generator, rewind)
+{
+	v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
+
+	if(g->primed) {
+		zend_throw_exception(php_ce_v8js_exception,
+			"V8Generator::rewind not supported by ES6", 0 TSRMLS_CC);
+
+	}
+
+	RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ boolean V8Generator::valid()
+ */
+PHP_METHOD(V8Generator, valid)
+{
+	v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
+
+	if(!g->primed) {
+		v8js_v8generator_next(g);
+	}
+
+	RETVAL_BOOL(!g->done);
+}
+/* }}} */
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
+
+
 void v8js_v8object_create(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
 void v8js_v8object_create(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
 {
 {
 	v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
 	v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
 
 
-	object_init_ex(res, value->IsFunction() ? php_ce_v8function : php_ce_v8object);
+#ifdef V8JS_V8GENERATOR_SUPPORT
+	if(value->IsGeneratorObject()) {
+		object_init_ex(res, php_ce_v8generator);
+	}
+	else
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
+	if(value->IsFunction()) {
+		object_init_ex(res, php_ce_v8function);
+	}
+	else {
+		object_init_ex(res, php_ce_v8object);
+	}
+
 	v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(res);
 	v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(res);
 
 
 	c->v8obj.Reset(isolate, value);
 	c->v8obj.Reset(isolate, value);
@@ -500,6 +700,38 @@ static const zend_function_entry v8js_v8function_methods[] = { /* {{{ */
 };
 };
 /* }}} */
 /* }}} */
 
 
+#ifdef V8JS_V8GENERATOR_SUPPORT
+ZEND_BEGIN_ARG_INFO(arginfo_v8generator_current, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_v8generator_key, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_v8generator_next, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_v8generator_rewind, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_v8generator_valid, 0)
+ZEND_END_ARG_INFO()
+
+static const zend_function_entry v8js_v8generator_methods[] = { /* {{{ */
+	PHP_ME(V8Generator,	__construct,			NULL,							ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+	PHP_ME(V8Generator,	__sleep,				NULL,							ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
+	PHP_ME(V8Generator,	__wakeup,				NULL,							ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
+
+	PHP_ME(V8Generator,	current,				arginfo_v8generator_current,	ZEND_ACC_PUBLIC)
+	PHP_ME(V8Generator,	key,					arginfo_v8generator_key,		ZEND_ACC_PUBLIC)
+	PHP_ME(V8Generator,	next,					arginfo_v8generator_next,		ZEND_ACC_PUBLIC)
+	PHP_ME(V8Generator,	rewind,					arginfo_v8generator_rewind,		ZEND_ACC_PUBLIC)
+	PHP_ME(V8Generator,	valid,					arginfo_v8generator_valid,		ZEND_ACC_PUBLIC)
+
+	{NULL, NULL, NULL}
+};
+/* }}} */
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
+
 
 
 PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
 PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
 {
 {
@@ -517,6 +749,17 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
 	php_ce_v8function->ce_flags |= ZEND_ACC_FINAL;
 	php_ce_v8function->ce_flags |= ZEND_ACC_FINAL;
 	php_ce_v8function->create_object = v8js_v8object_new;
 	php_ce_v8function->create_object = v8js_v8object_new;
 
 
+#ifdef V8JS_V8GENERATOR_SUPPORT
+	/* V8Generator Class */
+	INIT_CLASS_ENTRY(ce, "V8Generator", v8js_v8generator_methods);
+	php_ce_v8generator = zend_register_internal_class(&ce TSRMLS_CC);
+	php_ce_v8generator->ce_flags |= ZEND_ACC_FINAL;
+	php_ce_v8generator->create_object = v8js_v8generator_new;
+
+	zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator);
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
+
+
 	/* V8<Object|Function> handlers */
 	/* V8<Object|Function> handlers */
 	memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 	memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 	v8js_v8object_handlers.clone_obj = NULL;
 	v8js_v8object_handlers.clone_obj = NULL;
@@ -531,6 +774,16 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
 	v8js_v8object_handlers.call_method = v8js_v8object_call_method;
 	v8js_v8object_handlers.call_method = v8js_v8object_call_method;
 	v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
 	v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
 	v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
 	v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
+	v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
+	v8js_v8object_handlers.free_obj = v8js_v8object_free_storage;
+
+#ifdef V8JS_V8GENERATOR_SUPPORT
+	/* V8Generator handlers */
+	memcpy(&v8js_v8generator_handlers, &v8js_v8object_handlers, sizeof(zend_object_handlers));
+	v8js_v8generator_handlers.get_method = v8js_v8generator_get_method;
+	v8js_v8generator_handlers.offset = XtOffsetOf(struct v8js_v8generator, v8obj.std);
+	v8js_v8generator_handlers.free_obj = v8js_v8generator_free_storage;
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
 
 
 	return SUCCESS;
 	return SUCCESS;
 } /* }}} */
 } /* }}} */

+ 25 - 1
v8js_v8object_class.h

@@ -33,9 +33,33 @@ void v8js_v8object_create(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRM
 static inline v8js_v8object *v8js_v8object_fetch_object(zend_object *obj) {
 static inline v8js_v8object *v8js_v8object_fetch_object(zend_object *obj) {
 	return (v8js_v8object *)((char *)obj - XtOffsetOf(struct v8js_v8object, std));
 	return (v8js_v8object *)((char *)obj - XtOffsetOf(struct v8js_v8object, std));
 }
 }
- 
+
 #define Z_V8JS_V8OBJECT_OBJ_P(zv) v8js_v8object_fetch_object(Z_OBJ_P(zv));
 #define Z_V8JS_V8OBJECT_OBJ_P(zv) v8js_v8object_fetch_object(Z_OBJ_P(zv));
 
 
+
+#ifdef V8JS_V8GENERATOR_SUPPORT
+
+/* {{{ Generator container */
+struct v8js_v8generator {
+	zval value;
+	bool primed;
+	bool done;
+	struct v8js_v8object v8obj;
+};
+/* }}} */
+
+extern zend_class_entry *php_ce_v8generator;
+
+
+static inline v8js_v8generator *v8js_v8generator_fetch_object(zend_object *obj) {
+	return (v8js_v8generator *)((char *)obj - XtOffsetOf(struct v8js_v8generator, v8obj.std));
+}
+
+#define Z_V8JS_V8GENERATOR_OBJ_P(zv) v8js_v8generator_fetch_object(Z_OBJ_P(zv));
+
+#endif  /* /V8JS_V8GENERATOR_SUPPORT */
+
+
 PHP_MINIT_FUNCTION(v8js_v8object_class);
 PHP_MINIT_FUNCTION(v8js_v8object_class);
 
 
 #endif /* V8JS_V8OBJECT_CLASS_H */
 #endif /* V8JS_V8OBJECT_CLASS_H */