3 Sep 15:28
Question regarding upValues in closures
From: Gangadhar NPK <npk.gangadhar <at> gmail.com>
Subject: Question regarding upValues in closures
Newsgroups: gmane.comp.lang.lua.general
Date: 2008-09-03 13:32:08 GMT
Subject: Question regarding upValues in closures
Newsgroups: gmane.comp.lang.lua.general
Date: 2008-09-03 13:32:08 GMT
All, I am trying to understand the implementation of Lua. I am refereing to this document for details[http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf]. I have a question regarding the implementation of upValues as done for function closures. Takking the example given in the document: function add(x) return function(y) return x+y end end add2=add(2) print(add2(5)) As explained further in the document, the reference to the variable x is stored in an upValue which can either point to the stack (when referenced from within the inner function) or can point to itself (when referenced from the creator of the closure). Consider the following flow of actions: 1.Take the creation of the closure add2 2.function add is called with the parameter 2 3.within the inner function of the add function, the reference to the parameter x is on the stack (x is actually 2 on the stack now) 4.An inner function is returned to the caller which contains a reference to the x on the stack. Of course, this reference to the x on the stack will no longer exist, as we have returned from the function call and hence the stack frame will not be valid(Continue reading)
RSS Feed