Skip to content

Instantly share code, notes, and snippets.

@CheyiLin
Last active March 15, 2016 07:50
Show Gist options
  • Save CheyiLin/4570e17ccae5af221c9b to your computer and use it in GitHub Desktop.
Save CheyiLin/4570e17ccae5af221c9b to your computer and use it in GitHub Desktop.
local print = print -- upvalue index 1
local a = 100 -- upvalue index 2
function func()
print("in func: ", a, b)
end
-- set global 'a', 'b' to function 'func'
setfenv(func, { a = 200, b = 101 })
func()
-- modify the upvalue 'a' of function 'func'
debug.setupvalue(func, 2, 10001)
func()
-- print the upvalue 'a' of function 'func'
print("upvalue: ", debug.getupvalue(func, 2))
-- print the local 'a' (modified)
print("a: ", a)
--[[
in func: 100 101
in func: 10001 101
upvalue: a 10001
a: 10001
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment