debug.getupvalue

Returns the name and value of an upvalue

Prototype

name, value = debug.getupvalue (level, upvalue)

Description

Similar to debug.getlocal, returns the names and values of the upvalues for the nominated function. See also debug.setupvalue.

function newCounter ()
  local n = 0
  return function ()  -- anonymous function
    n = n + 1
    return n
    end -- function
end -- newCounter 

c = newCounter ()
c ()  -- count a couple of times
c ()

-- show upvalues in c

local i = 1

repeat
  name, val = debug.getupvalue (c, i)
  if name then
    print ("index", i, name, "=", val)
    i = i + 1
  end -- if
until not name

 -->
 
 index 1 n = 2
In this example the function c is a closure (with associated upvalue "n").

NOTE: Lua does not let you see or change upvalues for C functions (eg. string.gfind).

Lua functions

Topics