0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-12-22 12:51:52 +00:00

Merge pull request #220 from stesie/fluent-setters

Retain object identity on 'return $this'
This commit is contained in:
Stefan Siegl 2016-03-25 23:20:09 +01:00
commit 07c81e5a86
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,50 @@
--TEST--
Test V8::executeString() : return $this (aka fluent setters)
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
class Foo {
private $foo;
private $bar;
public function setFoo($value)
{
$this->foo = $value;
return $this;
}
public function setBar($value)
{
$this->bar = $value;
return $this;
}
}
$v8 = new V8Js();
$v8->theFoo = new Foo();
$v8->executeString(<<<EOJS
var a = PHP.theFoo.setFoo(23);
var b = a.setBar(42);
var_dump(PHP.theFoo === a);
var_dump(PHP.theFoo === b);
EOJS
);
var_dump($v8->theFoo);
?>
===EOF===
--EXPECTF--
bool(true)
bool(true)
object(Foo)#%d (2) {
["foo":"Foo":private]=>
int(23)
["bar":"Foo":private]=>
int(42)
}
===EOF===

View File

@ -156,6 +156,9 @@ failure:
} else {
v8js_terminate_execution(isolate);
}
} else if (retval_ptr == value) {
// special case: "return $this"
return_value = info.Holder();
} else if (retval_ptr != NULL) {
return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
}