From ef779d82507cb616a1da2354c7e76ee1c93ccc69 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 20:40:36 +0100 Subject: [PATCH 01/12] minimalist V8 -> PHP generator passing --- tests/generators_from_v8_basic.phpt | 43 +++++++++++ v8js_v8object_class.cc | 111 +++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 tests/generators_from_v8_basic.phpt diff --git a/tests/generators_from_v8_basic.phpt b/tests/generators_from_v8_basic.phpt new file mode 100644 index 0000000..575a8b0 --- /dev/null +++ b/tests/generators_from_v8_basic.phpt @@ -0,0 +1,43 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP +--SKIPIF-- + +--FILE-- +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=== diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index d3a2288..0bfccf1 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -2,12 +2,13 @@ +----------------------------------------------------------------------+ | 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 | +----------------------------------------------------------------------+ | Author: Jani Taskinen | | Author: Patrick Reilly | + | Author: Stefan Siegl | +----------------------------------------------------------------------+ */ @@ -32,6 +33,7 @@ extern "C" { /* {{{ Class Entries */ zend_class_entry *php_ce_v8object; zend_class_entry *php_ce_v8function; +zend_class_entry *php_ce_v8generator; /* }}} */ /* {{{ Object Handlers */ @@ -468,11 +470,92 @@ PHP_METHOD(V8Function, __wakeup) } /* }}} */ + +/* {{{ 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) +{ + RETURN_FALSE; +} +/* }}} */ + +/* {{{ scalar V8Generator::key() + */ +PHP_METHOD(V8Generator, key) +{ + RETURN_FALSE; +} +/* }}} */ + +/* {{{ void V8Generator::next() + */ +PHP_METHOD(V8Generator, next) +{ + RETURN_FALSE; +} +/* }}} */ + +/* {{{ void V8Generator::rewind() + */ +PHP_METHOD(V8Generator, rewind) +{ + RETURN_FALSE; +} +/* }}} */ + +/* {{{ boolean V8Generator::valid() + */ +PHP_METHOD(V8Generator, valid) +{ + RETURN_FALSE; +} +/* }}} */ + + void v8js_v8object_create(zval *res, v8::Handle value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ { v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0); - object_init_ex(res, value->IsFunction() ? php_ce_v8function : php_ce_v8object); + if(value->IsGeneratorObject()) { + object_init_ex(res, php_ce_v8generator); + } + else 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); c->v8obj.Reset(isolate, value); @@ -500,6 +583,21 @@ static const zend_function_entry v8js_v8function_methods[] = { /* {{{ */ }; /* }}} */ +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, NULL, ZEND_ACC_PUBLIC) + PHP_ME(V8Generator, key, NULL, ZEND_ACC_PUBLIC) + PHP_ME(V8Generator, next, NULL, ZEND_ACC_PUBLIC) + PHP_ME(V8Generator, rewind, NULL, ZEND_ACC_PUBLIC) + PHP_ME(V8Generator, valid, NULL, ZEND_ACC_PUBLIC) + + {NULL, NULL, NULL} +}; +/* }}} */ + PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ { @@ -517,6 +615,15 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ php_ce_v8function->ce_flags |= ZEND_ACC_FINAL; php_ce_v8function->create_object = v8js_v8object_new; + /* 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_v8object_new; + + zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator); + + /* V8 handlers */ memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); v8js_v8object_handlers.clone_obj = NULL; From 2c7a7ab87d54631a4861635ff913ac3cb53047e7 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 21:35:44 +0100 Subject: [PATCH 02/12] Implement PHP-side iteration of JS generator objects --- tests/generators_from_v8_001.phpt | 32 ++++++++++ v8js_v8.cc | 2 +- v8js_v8object_class.cc | 101 +++++++++++++++++++++++++++--- v8js_v8object_class.h | 19 +++++- 4 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 tests/generators_from_v8_001.phpt diff --git a/tests/generators_from_v8_001.phpt b/tests/generators_from_v8_001.phpt new file mode 100644 index 0000000..5d6cd2b --- /dev/null +++ b/tests/generators_from_v8_001.phpt @@ -0,0 +1,32 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (foreach) +--SKIPIF-- + +--FILE-- +executeString($js); + +foreach($gen as $a) { + var_dump($a); +} + +?> +===EOF=== +--EXPECTF-- +int(0) +int(1) +int(2) +int(3) +===EOF=== diff --git a/v8js_v8.cc b/v8js_v8.cc index 35d4617..633f38d 100644 --- a/v8js_v8.cc +++ b/v8js_v8.cc @@ -198,7 +198,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_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); } } diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index 0bfccf1..a4b58c0 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -38,6 +38,7 @@ zend_class_entry *php_ce_v8generator; /* {{{ Object Handlers */ static zend_object_handlers v8js_v8object_handlers; +static zend_object_handlers v8js_v8generator_handlers; /* }}} */ #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke" @@ -309,7 +310,6 @@ static int v8js_v8object_call_method(zend_string *method, zend_object *object, I } v8::Local *jsArgv = static_cast *>(alloca(sizeof(v8::Local) * argc)); - v8::Local js_retval; for (i = 0; i < argc; i++) { new(&jsArgv[i]) v8::Local; @@ -398,9 +398,6 @@ static zend_object *v8js_v8object_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */ c->std.handlers = &v8js_v8object_handlers; new(&c->v8obj) v8::Persistent(); - v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std); - v8js_v8object_handlers.free_obj = v8js_v8object_free_storage; - return &c->std; } /* }}} */ @@ -471,6 +468,78 @@ PHP_METHOD(V8Function, __wakeup) /* }}} */ +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_v8object *c; + c = (v8js_v8object *) ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce)); + + zend_object_std_init(&c->std, ce); + c->std.handlers = &v8js_v8object_handlers; + new(&c->v8obj) v8::Persistent(); + + return &c->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::Isolate *) > v8_call = [g](v8::Isolate *isolate) { + int i = 0; + + v8::Local method_name = V8JS_STR("next"); + v8::Local v8obj = v8::Local::New(isolate, g->v8obj.v8obj)->ToObject(); + v8::Local cb = v8::Local::Cast(v8obj->Get(method_name));; + + v8::Local result = cb->Call(v8obj, 0, NULL); + + if(!result->IsObject()) { + zend_throw_exception(php_ce_v8js_exception, + "V8Generator returned non-object on next()", 0); + return V8JS_NULL; + } + + v8::Local resultObj = result->ToObject(); + v8::Local val = resultObj->Get(V8JS_STR("value")); + v8::Local 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(); + } +} +/* }}} */ + + /* {{{ proto V8Generator::__construct() */ PHP_METHOD(V8Generator,__construct) @@ -505,7 +574,8 @@ PHP_METHOD(V8Generator, __wakeup) */ PHP_METHOD(V8Generator, current) { - RETURN_FALSE; + v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis()); + RETVAL_ZVAL(&g->value, 1, 0); } /* }}} */ @@ -521,7 +591,8 @@ PHP_METHOD(V8Generator, key) */ PHP_METHOD(V8Generator, next) { - RETURN_FALSE; + v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis()); + v8js_v8generator_next(g); } /* }}} */ @@ -537,7 +608,13 @@ PHP_METHOD(V8Generator, rewind) */ PHP_METHOD(V8Generator, valid) { - RETURN_FALSE; + v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis()); + + if(!g->primed) { + v8js_v8generator_next(g); + } + + RETVAL_BOOL(!g->done); } /* }}} */ @@ -619,7 +696,7 @@ PHP_MINIT_FUNCTION(v8js_v8object_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_v8object_new; + php_ce_v8generator->create_object = v8js_v8generator_new; zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator); @@ -638,6 +715,14 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ v8js_v8object_handlers.call_method = v8js_v8object_call_method; v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info; 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; + + /* V8Generator handlers */ + memcpy(&v8js_v8generator_handlers, &v8js_v8object_handlers, sizeof(zend_object_handlers)); + v8js_v8generator_handlers.offset = XtOffsetOf(struct v8js_v8generator, v8obj.std); + v8js_v8generator_handlers.free_obj = v8js_v8generator_free_storage; + return SUCCESS; } /* }}} */ diff --git a/v8js_v8object_class.h b/v8js_v8object_class.h index 31e8400..cc1da4d 100644 --- a/v8js_v8object_class.h +++ b/v8js_v8object_class.h @@ -24,8 +24,18 @@ struct v8js_v8object { }; /* }}} */ +/* {{{ Generator container */ +struct v8js_v8generator { + zval value; + bool primed; + bool done; + struct v8js_v8object v8obj; +}; +/* }}} */ + extern zend_class_entry *php_ce_v8object; extern zend_class_entry *php_ce_v8function; +extern zend_class_entry *php_ce_v8generator; /* Create PHP V8 object */ void v8js_v8object_create(zval *, v8::Handle, int, v8::Isolate * TSRMLS_DC); @@ -33,9 +43,16 @@ void v8js_v8object_create(zval *, v8::Handle, int, v8::Isolate * TSRM static inline v8js_v8object *v8js_v8object_fetch_object(zend_object *obj) { 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)); +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)); + + PHP_MINIT_FUNCTION(v8js_v8object_class); #endif /* V8JS_V8OBJECT_CLASS_H */ From e686603b89a5b80be6678e82bf8e49cda1ebb108 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 22:41:13 +0100 Subject: [PATCH 03/12] Implement direct iteration on JS generators --- tests/generators_from_v8_002.phpt | 57 ++++++++++++++++++++++++++++ v8js_v8object_class.cc | 62 +++++++++++++++++++++++-------- 2 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 tests/generators_from_v8_002.phpt diff --git a/tests/generators_from_v8_002.phpt b/tests/generators_from_v8_002.phpt new file mode 100644 index 0000000..7cdca3a --- /dev/null +++ b/tests/generators_from_v8_002.phpt @@ -0,0 +1,57 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (direct) +--SKIPIF-- + +--FILE-- +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=== diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index a4b58c0..27cdcdb 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -479,14 +479,14 @@ static void v8js_v8generator_free_storage(zend_object *object) /* {{{ */ static zend_object *v8js_v8generator_new(zend_class_entry *ce) /* {{{ */ { - v8js_v8object *c; - c = (v8js_v8object *) ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce)); + v8js_v8generator *c; + c = (v8js_v8generator *) ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce)); - zend_object_std_init(&c->std, ce); - c->std.handlers = &v8js_v8object_handlers; - new(&c->v8obj) v8::Persistent(); + zend_object_std_init(&c->v8obj.std, ce); + c->v8obj.std.handlers = &v8js_v8generator_handlers; + new(&c->v8obj.v8obj) v8::Persistent(); - return &c->std; + return &c->v8obj.std; } /* }}} */ @@ -539,6 +539,17 @@ static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */ } /* }}} */ +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() */ @@ -575,6 +586,11 @@ PHP_METHOD(V8Generator, __wakeup) 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); } /* }}} */ @@ -660,16 +676,31 @@ static const zend_function_entry v8js_v8function_methods[] = { /* {{{ */ }; /* }}} */ -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) +ZEND_BEGIN_ARG_INFO(arginfo_v8generator_current, 0) +ZEND_END_ARG_INFO() - PHP_ME(V8Generator, current, NULL, ZEND_ACC_PUBLIC) - PHP_ME(V8Generator, key, NULL, ZEND_ACC_PUBLIC) - PHP_ME(V8Generator, next, NULL, ZEND_ACC_PUBLIC) - PHP_ME(V8Generator, rewind, NULL, ZEND_ACC_PUBLIC) - PHP_ME(V8Generator, valid, NULL, ZEND_ACC_PUBLIC) +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} }; @@ -720,6 +751,7 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ /* 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; From da64b9f0553de79a6fa3bac537dd5dbb1cf87272 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 22:48:01 +0100 Subject: [PATCH 04/12] Handle rewind on primed V8Generator --- tests/generators_from_v8_003.phpt | 41 +++++++++++++++++++++++++++++++ v8js_v8object_class.cc | 8 ++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/generators_from_v8_003.phpt diff --git a/tests/generators_from_v8_003.phpt b/tests/generators_from_v8_003.phpt new file mode 100644 index 0000000..ac9e205 --- /dev/null +++ b/tests/generators_from_v8_003.phpt @@ -0,0 +1,41 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (rewind) +--SKIPIF-- + +--FILE-- +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 diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index 27cdcdb..d6867e7 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -616,6 +616,14 @@ PHP_METHOD(V8Generator, next) */ 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; } /* }}} */ From f97a25b69c2505f764b6b968b297415a50497188 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 22:57:50 +0100 Subject: [PATCH 05/12] Test & implement PHP-side instantiation of JS generators --- tests/generators_from_v8_004.phpt | 33 ++++++++++++++++++++++++++ tests/generators_from_v8_005.phpt | 39 +++++++++++++++++++++++++++++++ v8js_object_export.cc | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/generators_from_v8_004.phpt create mode 100644 tests/generators_from_v8_005.phpt diff --git a/tests/generators_from_v8_004.phpt b/tests/generators_from_v8_004.phpt new file mode 100644 index 0000000..10fc67f --- /dev/null +++ b/tests/generators_from_v8_004.phpt @@ -0,0 +1,33 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (instantiate in PHP + foreach) +--SKIPIF-- + +--FILE-- +executeString($js); +$gen = $TheGen(); + +foreach($gen as $a) { + var_dump($a); +} + +?> +===EOF=== +--EXPECTF-- +int(0) +int(1) +int(2) +int(3) +===EOF=== diff --git a/tests/generators_from_v8_005.phpt b/tests/generators_from_v8_005.phpt new file mode 100644 index 0000000..a1b55b8 --- /dev/null +++ b/tests/generators_from_v8_005.phpt @@ -0,0 +1,39 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (instantiate in PHP + iterate in JS) +--SKIPIF-- + +--FILE-- +executeString($js); +$gen = $TheGen(); + +$js = <<executeString($js); +$fn($gen); + +?> +===EOF=== +--EXPECTF-- +int(0) +int(1) +int(2) +int(3) +===EOF=== diff --git a/v8js_object_export.cc b/v8js_object_export.cc index 99a6d67..d7a4a68 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -938,7 +938,7 @@ v8::Handle v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRML } /* Special case, passing back object originating from JS to JS */ - if (ce == php_ce_v8function) { + if (ce == php_ce_v8function || ce == php_ce_v8generator) { v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(value); if(isolate != c->ctx->isolate) { From 6fa6f9316e11c9a17b2e9b36cc2a9b85783d01ec Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 23:00:45 +0100 Subject: [PATCH 06/12] Test yield from JS generators --- tests/generators_from_v8_006.phpt | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/generators_from_v8_006.phpt diff --git a/tests/generators_from_v8_006.phpt b/tests/generators_from_v8_006.phpt new file mode 100644 index 0000000..4a4abfd --- /dev/null +++ b/tests/generators_from_v8_006.phpt @@ -0,0 +1,41 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (yield from) +--SKIPIF-- + +--FILE-- +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=== From 479d14b5b046a2811e35dbec10b12768870a2be8 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 23:12:43 +0100 Subject: [PATCH 07/12] Handle exceptions thrown in JS generators well --- tests/generators_from_v8_007.phpt | 33 +++++++++++++++++++++++++++++++ v8js_v8object_class.cc | 5 +++++ 2 files changed, 38 insertions(+) create mode 100644 tests/generators_from_v8_007.phpt diff --git a/tests/generators_from_v8_007.phpt b/tests/generators_from_v8_007.phpt new file mode 100644 index 0000000..cc7db60 --- /dev/null +++ b/tests/generators_from_v8_007.phpt @@ -0,0 +1,33 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (throw JS) +--SKIPIF-- + +--FILE-- +executeString($js); + +foreach($gen as $a) { + var_dump($a); +} + +?> +===EOF=== +--EXPECTF-- +int(23) + +Fatal error: Uncaught V8JsScriptException: V8Js::compileString():3: Error: blar in %s +Stack trace: +#0 %s: V8Generator->next() +#1 {main} + thrown in %s diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index d6867e7..322f49a 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -510,6 +510,11 @@ static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */ v8::Local 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); From 46fba810af1941f2a0a4f7d897824eb2958fd527 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 23:15:13 +0100 Subject: [PATCH 08/12] Add test on PHP exceptions thrown by JS generators --- tests/generators_from_v8_008.phpt | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/generators_from_v8_008.phpt diff --git a/tests/generators_from_v8_008.phpt b/tests/generators_from_v8_008.phpt new file mode 100644 index 0000000..1989ceb --- /dev/null +++ b/tests/generators_from_v8_008.phpt @@ -0,0 +1,38 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (throw PHP) +--SKIPIF-- + +--FILE-- +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 From 798a87e481a72379c47e93749c782e3a02fc6a9e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 23:16:59 +0100 Subject: [PATCH 09/12] Add test on fatal errors within JS generators --- tests/generators_from_v8_009.phpt | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/generators_from_v8_009.phpt diff --git a/tests/generators_from_v8_009.phpt b/tests/generators_from_v8_009.phpt new file mode 100644 index 0000000..758d93b --- /dev/null +++ b/tests/generators_from_v8_009.phpt @@ -0,0 +1,32 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (fatal error) +--SKIPIF-- + +--FILE-- +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 From c500d0c21eec5c8e1942bec41c7515c380598496 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 23:19:31 +0100 Subject: [PATCH 10/12] Add test on property passing on generators --- tests/generators_from_v8_010.phpt | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/generators_from_v8_010.phpt diff --git a/tests/generators_from_v8_010.phpt b/tests/generators_from_v8_010.phpt new file mode 100644 index 0000000..6a2747f --- /dev/null +++ b/tests/generators_from_v8_010.phpt @@ -0,0 +1,32 @@ +--TEST-- +Test V8::executeString() : Generators V8 -> PHP (properties) +--SKIPIF-- + +--FILE-- +executeString($js); + +var_dump($gen->foo); +$gen->bar(); + +?> +===EOF=== +--EXPECT-- +int(23) +string(11) "Hello World" +===EOF=== From a4b4934361f7fb255c147de28523830e2ec119e8 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 8 Jan 2016 00:00:12 +0100 Subject: [PATCH 11/12] Relax EXPECTF, some V8 versions fail to provide the error location --- tests/generators_from_v8_007.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generators_from_v8_007.phpt b/tests/generators_from_v8_007.phpt index cc7db60..11b6eaf 100644 --- a/tests/generators_from_v8_007.phpt +++ b/tests/generators_from_v8_007.phpt @@ -26,7 +26,7 @@ foreach($gen as $a) { --EXPECTF-- int(23) -Fatal error: Uncaught V8JsScriptException: V8Js::compileString():3: Error: blar in %s +Fatal error: Uncaught V8JsScriptException: %s Error: blar in %s Stack trace: #0 %s: V8Generator->next() #1 {main} From 38b9c053a13000f0fd9d345b825e89562f3daedd Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 8 Jan 2016 00:12:06 +0100 Subject: [PATCH 12/12] Enable V8Generator support on V8 >= 3.30.0 --- php_v8js_macros.h | 4 ++++ tests/generators_from_v8_001.phpt | 8 +++++++- tests/generators_from_v8_002.phpt | 8 +++++++- tests/generators_from_v8_003.phpt | 8 +++++++- tests/generators_from_v8_004.phpt | 8 +++++++- tests/generators_from_v8_005.phpt | 8 +++++++- tests/generators_from_v8_006.phpt | 8 +++++++- tests/generators_from_v8_007.phpt | 8 +++++++- tests/generators_from_v8_008.phpt | 8 +++++++- tests/generators_from_v8_009.phpt | 8 +++++++- tests/generators_from_v8_010.phpt | 8 +++++++- tests/generators_from_v8_basic.phpt | 8 +++++++- v8js_object_export.cc | 6 +++++- v8js_v8object_class.cc | 20 ++++++++++++++++++-- v8js_v8object_class.h | 27 +++++++++++++++++---------- 15 files changed, 121 insertions(+), 24 deletions(-) diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 0fb42c4..2cb3dd8 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -61,6 +61,10 @@ extern "C" { #define V8JS_GET_CLASS_NAME(var, obj) \ 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 * declared as 'char *' instead of 'const char *' before PHP 5.4 */ #if ZEND_MODULE_API_NO >= 20100525 diff --git a/tests/generators_from_v8_001.phpt b/tests/generators_from_v8_001.phpt index 5d6cd2b..04b4306 100644 --- a/tests/generators_from_v8_001.phpt +++ b/tests/generators_from_v8_001.phpt @@ -1,7 +1,13 @@ --TEST-- Test V8::executeString() : Generators V8 -> PHP (foreach) --SKIPIF-- - + --FILE-- PHP (direct) --SKIPIF-- - + --FILE-- PHP (rewind) --SKIPIF-- - + --FILE-- PHP (instantiate in PHP + foreach) --SKIPIF-- - + --FILE-- PHP (instantiate in PHP + iterate in JS) --SKIPIF-- - + --FILE-- PHP (yield from) --SKIPIF-- - + --FILE-- PHP (throw JS) --SKIPIF-- - + --FILE-- PHP (throw PHP) --SKIPIF-- - + --FILE-- PHP (fatal error) --SKIPIF-- - + --FILE-- PHP (properties) --SKIPIF-- - + --FILE-- PHP --SKIPIF-- - + --FILE-- v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRML } /* Special case, passing back object originating from JS to JS */ - if (ce == php_ce_v8function || ce == php_ce_v8generator) { + if (ce == php_ce_v8function +#ifdef V8JS_V8GENERATOR_SUPPORT + || ce == php_ce_v8generator +#endif + ) { v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(value); if(isolate != c->ctx->isolate) { diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index 322f49a..ce9d068 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -33,12 +33,18 @@ extern "C" { /* {{{ Class Entries */ zend_class_entry *php_ce_v8object; zend_class_entry *php_ce_v8function; + +#ifdef V8JS_V8GENERATOR_SUPPORT zend_class_entry *php_ce_v8generator; +#endif /* }}} */ /* {{{ Object 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" @@ -468,6 +474,7 @@ 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); @@ -646,16 +653,20 @@ PHP_METHOD(V8Generator, valid) RETVAL_BOOL(!g->done); } /* }}} */ +#endif /* /V8JS_V8GENERATOR_SUPPORT */ void v8js_v8object_create(zval *res, v8::Handle value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */ { v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0); +#ifdef V8JS_V8GENERATOR_SUPPORT if(value->IsGeneratorObject()) { object_init_ex(res, php_ce_v8generator); } - else if(value->IsFunction()) { + else +#endif /* /V8JS_V8GENERATOR_SUPPORT */ + if(value->IsFunction()) { object_init_ex(res, php_ce_v8function); } else { @@ -689,6 +700,7 @@ static const zend_function_entry v8js_v8function_methods[] = { /* {{{ */ }; /* }}} */ +#ifdef V8JS_V8GENERATOR_SUPPORT ZEND_BEGIN_ARG_INFO(arginfo_v8generator_current, 0) ZEND_END_ARG_INFO() @@ -718,6 +730,7 @@ static const zend_function_entry v8js_v8generator_methods[] = { /* {{{ */ {NULL, NULL, NULL} }; /* }}} */ +#endif /* /V8JS_V8GENERATOR_SUPPORT */ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ @@ -736,6 +749,7 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ php_ce_v8function->ce_flags |= ZEND_ACC_FINAL; 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); @@ -743,6 +757,7 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ php_ce_v8generator->create_object = v8js_v8generator_new; zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator); +#endif /* /V8JS_V8GENERATOR_SUPPORT */ /* V8 handlers */ @@ -762,12 +777,13 @@ PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */ 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; } /* }}} */ diff --git a/v8js_v8object_class.h b/v8js_v8object_class.h index cc1da4d..1bfb6f4 100644 --- a/v8js_v8object_class.h +++ b/v8js_v8object_class.h @@ -24,18 +24,8 @@ struct v8js_v8object { }; /* }}} */ -/* {{{ Generator container */ -struct v8js_v8generator { - zval value; - bool primed; - bool done; - struct v8js_v8object v8obj; -}; -/* }}} */ - extern zend_class_entry *php_ce_v8object; extern zend_class_entry *php_ce_v8function; -extern zend_class_entry *php_ce_v8generator; /* Create PHP V8 object */ void v8js_v8object_create(zval *, v8::Handle, int, v8::Isolate * TSRMLS_DC); @@ -46,12 +36,29 @@ static inline v8js_v8object *v8js_v8object_fetch_object(zend_object *obj) { #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);