From d44592910d9e400317ba81ea280e1bc12c928ee5 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 25 Mar 2016 19:15:25 +0100 Subject: [PATCH] Retain object indentity on 'return $this' --- tests/return_this_basic.phpt | 50 ++++++++++++++++++++++++++++++++++++ v8js_object_export.cc | 3 +++ 2 files changed, 53 insertions(+) create mode 100644 tests/return_this_basic.phpt diff --git a/tests/return_this_basic.phpt b/tests/return_this_basic.phpt new file mode 100644 index 0000000..127d9ea --- /dev/null +++ b/tests/return_this_basic.phpt @@ -0,0 +1,50 @@ +--TEST-- +Test V8::executeString() : return $this (aka fluent setters) +--SKIPIF-- + +--FILE-- +foo = $value; + return $this; + } + + public function setBar($value) + { + $this->bar = $value; + return $this; + } +} + +$v8 = new V8Js(); +$v8->theFoo = new Foo(); + +$v8->executeString(<<theFoo); + +?> +===EOF=== +--EXPECTF-- +bool(true) +bool(true) +object(Foo)#%d (2) { + ["foo":"Foo":private]=> + int(23) + ["bar":"Foo":private]=> + int(42) +} +===EOF=== diff --git a/v8js_object_export.cc b/v8js_object_export.cc index 34ab680..aea7438 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -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); }