diff --git a/php_v8js_macros.h b/php_v8js_macros.h index df64cab..ebc9a55 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 new file mode 100644 index 0000000..04b4306 --- /dev/null +++ b/tests/generators_from_v8_001.phpt @@ -0,0 +1,38 @@ +--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/tests/generators_from_v8_002.phpt b/tests/generators_from_v8_002.phpt new file mode 100644 index 0000000..7db6678 --- /dev/null +++ b/tests/generators_from_v8_002.phpt @@ -0,0 +1,63 @@ +--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/tests/generators_from_v8_003.phpt b/tests/generators_from_v8_003.phpt new file mode 100644 index 0000000..39ea9f8 --- /dev/null +++ b/tests/generators_from_v8_003.phpt @@ -0,0 +1,47 @@ +--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/tests/generators_from_v8_004.phpt b/tests/generators_from_v8_004.phpt new file mode 100644 index 0000000..4327e4f --- /dev/null +++ b/tests/generators_from_v8_004.phpt @@ -0,0 +1,39 @@ +--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..2d43093 --- /dev/null +++ b/tests/generators_from_v8_005.phpt @@ -0,0 +1,45 @@ +--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/tests/generators_from_v8_006.phpt b/tests/generators_from_v8_006.phpt new file mode 100644 index 0000000..9b93485 --- /dev/null +++ b/tests/generators_from_v8_006.phpt @@ -0,0 +1,47 @@ +--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=== diff --git a/tests/generators_from_v8_007.phpt b/tests/generators_from_v8_007.phpt new file mode 100644 index 0000000..ba89145 --- /dev/null +++ b/tests/generators_from_v8_007.phpt @@ -0,0 +1,39 @@ +--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: %s Error: blar in %s +Stack trace: +#0 %s: V8Generator->next() +#1 {main} + thrown in %s diff --git a/tests/generators_from_v8_008.phpt b/tests/generators_from_v8_008.phpt new file mode 100644 index 0000000..4448a47 --- /dev/null +++ b/tests/generators_from_v8_008.phpt @@ -0,0 +1,44 @@ +--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 diff --git a/tests/generators_from_v8_009.phpt b/tests/generators_from_v8_009.phpt new file mode 100644 index 0000000..ff707d7 --- /dev/null +++ b/tests/generators_from_v8_009.phpt @@ -0,0 +1,38 @@ +--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 diff --git a/tests/generators_from_v8_010.phpt b/tests/generators_from_v8_010.phpt new file mode 100644 index 0000000..872cbe0 --- /dev/null +++ b/tests/generators_from_v8_010.phpt @@ -0,0 +1,38 @@ +--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=== diff --git a/tests/generators_from_v8_basic.phpt b/tests/generators_from_v8_basic.phpt new file mode 100644 index 0000000..9476342 --- /dev/null +++ b/tests/generators_from_v8_basic.phpt @@ -0,0 +1,49 @@ +--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_object_export.cc b/v8js_object_export.cc index 99a6d67..c34e82b 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -938,7 +938,11 @@ 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 +#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_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 d3a2288..ce9d068 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,10 +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" @@ -307,7 +316,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; @@ -396,9 +404,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; } /* }}} */ @@ -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(); + + 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::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.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 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(); + } +} +/* }}} */ + +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 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); +#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); 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) /* {{{ */ { @@ -517,6 +749,17 @@ 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); + 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 handlers */ memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); 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.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; + +#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 31e8400..1bfb6f4 100644 --- a/v8js_v8object_class.h +++ b/v8js_v8object_class.h @@ -33,9 +33,33 @@ 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)); + +#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); #endif /* V8JS_V8OBJECT_CLASS_H */