debug.getupvalue
Lua function

debug.getupvalue

Summary

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).



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_debug.debug debug.debug (Enters interactive debugging)
LUA_debug.getfenv debug.getfenv (Returns the environment of an object)
LUA_debug.gethook debug.gethook (Returns the current hook settings)
LUA_debug.getinfo debug.getinfo (Returns a table with information about a function)
LUA_debug.getlocal debug.getlocal (Returns name and value of a local variable)
LUA_debug.getmetatable debug.getmetatable (Returns the metatable of the given object)
LUA_debug.getregistry debug.getregistry (Returns the registry table)
LUA_debug.setfenv debug.setfenv (Sets the environment of an object)
LUA_debug.sethook debug.sethook (Sets a debug hook function)
LUA_debug.setlocal debug.setlocal (Sets the value of the local variable)
LUA_debug.setmetatable debug.setmetatable (Sets the metatable for an object)
LUA_debug.setupvalue debug.setupvalue (Sets an upvalue for a function)
LUA_debug.traceback debug.traceback (Returns a string with a traceback of the stack call)

(Help topic: lua=debug.getupvalue)

DOC_contents Documentation contents page