0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-11-09 15:18:41 +00:00

Add more tests on exported (PHP) Generator objects

This commit is contained in:
Stefan Siegl 2016-01-09 13:19:23 +01:00
parent a18ae9e853
commit dbe29d7e96
4 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,30 @@
--TEST--
Test V8::executeString() : Generators PHP -> V8 (instantite in JS)
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$js = <<<EOJS
for(var i of PHP.Gen()) {
var_dump(i);
}
EOJS;
$v8 = new V8Js();
$v8->Gen = function() {
for($i = 0; $i < 4; $i ++) {
yield $i;
}
};
$v8->executeString($js);
?>
===EOF===
--EXPECTF--
int(0)
int(1)
int(2)
int(3)
===EOF===

View File

@ -0,0 +1,28 @@
--TEST--
Test V8::executeString() : Generators PHP -> V8 (instantiate in JS, iterate in PHP)
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$v8 = new V8Js();
$v8->Gen = function() {
for($i = 0; $i < 4; $i ++) {
yield $i;
}
};
$gen = $v8->executeString('(PHP.Gen())');
foreach($gen as $i) {
var_dump($i);
}
?>
===EOF===
--EXPECTF--
int(0)
int(1)
int(2)
int(3)
===EOF===

View File

@ -0,0 +1,47 @@
--TEST--
Test V8::executeString() : Generators PHP -> V8 (instantiate in JS, iterate in PHP)
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
$v8 = new V8Js();
$v8->Gen = function() {
for($i = 0; $i < 6; $i ++) {
yield $i;
}
};
$JS = <<<EOJS
var g = PHP.Gen();
var_dump(g.next());
var_dump(g.next());
(g);
EOJS;
$gen = $v8->executeString($JS);
foreach($gen as $i) {
var_dump($i);
}
?>
===EOF===
--EXPECTF--
object(Object)#%d (2) {
["value"] =>
int(0)
["done"] =>
bool(false)
}
object(Object)#%d (2) {
["value"] =>
int(1)
["done"] =>
bool(false)
}
int(2)
int(3)
int(4)
int(5)
===EOF===

View File

@ -0,0 +1,48 @@
--TEST--
Test V8::executeString() : Generators PHP -> V8 (yield from)
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
function FibonacciGenerator()
{
$i = 0;
$j = 1;
for(;;) {
yield $j;
list($i, $j) = array($j, $i + $j);
}
}
$v8 = new V8Js();
$v8->fibs = FibonacciGenerator();
$JS = <<<EOJS
function* prefixer() {
yield* arguments;
yield* PHP.fibs;
}
var gen = prefixer(23, 42);
for(var i = 0; i < 10; i ++) {
var_dump(gen.next().value);
}
EOJS;
$v8->executeString($JS);
?>
===EOF===
--EXPECT--
int(23)
int(42)
int(1)
int(1)
int(2)
int(3)
int(5)
int(8)
int(13)
int(21)
===EOF===