lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 28-Oct-04, at 1:30 PM, Ben Sunshine-Hill wrote:

That code snippet wouldn't really impact lifetime-detection, since the
declaration of foo there would simply create an upvalue and
immediately close it (freeing up the stack space).

That's not quite accurate. The upvalue isn't closed until execution
passes out of the scope in which the upvalue was declared.

In my example, "a" can be referenced by its known stack index
anywhere within the scope of the "local a". Since the upvalue is
not closed until that scope is exited, reusing a's stack slot
(say, for the local "b" in my example) would change the value
which would eventually be used by function "foo".

I don't know anyway of demonstrating that (aside from moral
persuasion) other than by fabricating bytecode. But if you take
a look at the luac -l output for the following fragment:

  local a = 7
  function foo() return a end
  a = 42
  print(foo())

you will see that the assignment a=42 is not aware of the fact
that a is an upvalue, whereas the function foo is.

A more revealing example might be:

  local a = 7
  function foo(x)
    local t = a
    a = x
    return t
  end
  a = 42
  print(foo(105))
  print(a)

where you can see that within foo, the assignment is setting the upvalue,
whereas within the main function, the references are all from the stack.