Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:34075 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68110 invoked by uid 1010); 18 Dec 2007 04:44:44 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 68095 invoked from network); 18 Dec 2007 04:44:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2007 04:44:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=jeff@procata.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=jeff@procata.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain procata.com from 207.58.169.145 cause and error) X-PHP-List-Original-Sender: jeff@procata.com X-Host-Fingerprint: 207.58.169.145 vps.procata.net Linux 2.5 (sometimes 2.4) (4) Received: from [207.58.169.145] ([207.58.169.145:59196] helo=vps.procata.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 68/78-06889-B3057674 for ; Mon, 17 Dec 2007 23:44:44 -0500 Received: from c-76-112-12-49.hsd1.mi.comcast.net ([76.112.12.49] helo=[192.168.1.102]) by vps.procata.net with esmtp (Exim 4.63) (envelope-from ) id 1J4UJs-0008O2-OC; Mon, 17 Dec 2007 23:44:40 -0500 Cc: internals@lists.php.net Message-ID: <4FADC266-873E-4FD2-BEC8-28EA9D833297@procata.com> To: Larry Garfield In-Reply-To: <200712172130.08216.larry@garfieldtech.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v915) Date: Mon, 17 Dec 2007 23:44:37 -0500 References: <98b8086f0712150818n40056cedyf0aae7a5a08a27b7@mail.gmail.com> <476582E6.7020808@zend.com> <200712172130.08216.larry@garfieldtech.com> X-Mailer: Apple Mail (2.915) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vps.procata.net X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - procata.com Subject: Re: [PHP-DEV] Re: PATCH: anonymous functions in PHP From: jeff@procata.com (Jeff Moore) On Dec 17, 2007, at 10:30 PM, Larry Garfield wrote: > I'm assuming that making the function above GC-able would be a > herculean task > at this point, based on previous comments, but I do not actually > know myself. Hi Larry, Let me use a different example than yours. function getAdder($x) { return function ($y) { lexical $x; return $x + $y; } } $plusFive = getAdder(5); $plusTen = getAdder(10); echo $plusFive(4); // 9 echo $plusTen(7); // 17 If the closure definition (and thus getAddr) returns a string representing an anonymous function name, then there is no where to hold the enclosing variable $x and no way to know when to free it. Notice that in this example, we have two different enclosing contexts, one where $x is 5 and one where $x is 10. If we introduce a new variable type, a new kind of zval, then internally, that variable can hold: 1) A reference to the local variables of getAddr. (here $x) 2) A reference to $this if the closure is declared in a method 3) A reference to the opcodes of the compiled anonymous function. The opcodes for the closure would be compiled and squirreled away when the script is compiled. When getAddr is executed, a zval would be created that holds the reference to the enclosing locals and a reference to the proper opcodes. Note that $plusFive and $plusTen would hold two different zvals, one for each invocation of getAddr, but that both would refer to the same opcodes structure. When a codeblock zval is executed, such as at $plusFive(4), the enclosing context can be passed to the anonymous function function to make the lexical $x statement bind to the proper variable. When the zval in $plusFive reaches zero references, it can subsequently free its references to the local variables of getAddr. As far as I can see, no herculean garbage collection or reference counting is required. Adding a new kind of zval? Perhaps that's Herculean. I don't know. I hope I've gotten my zval terminology right. Best Regards, Jeff