Last active
March 15, 2016 07:50
-
-
Save CheyiLin/4570e17ccae5af221c9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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