local a, b
function fA ()
print(a)
function fB ()
print(b)
end
end
How many upvalues does fA have, 1 or 2?
As mentioned earlier, in Lua 5.2, if the function uses global variables, there is also a hidden environment upvalue.
You may want to use luac -l -l to read the words generated for the code Section code.
What may confuse you is the definition of fB in the fA system. Recall that the function fB()print(b)end is just the sugar of fB = function()print(b)end. When you do this, it is obvious that b is being used in fA, and you are assigning a global variable fB. Therefore, you get 3 up values in Lua 5.2. (Using print also means that fA uses global variables.) If Use the native function fB… and delete print, you will see that fA uses 2 upvalues in 5.1 and 5.2.
Use a lua code block like this:
local a, b
function fA ()
print(a)
function fB ()
print(b)
end
end
How many upvalues does fA have, 1 or 2?
According to the definition, all external local variables used in the function are counted as upvalues.
As mentioned earlier, in Lua 5.2, if the function uses Global variables, there is also a hidden environment upvalue.
You may want to use luac -l -l to read the bytecode generated for the code.
What you are confused about is the definition of fB in the fA system. Recall that the function fB()print(b)end is just the sugar of fB = function()print(b)end. When you do this, it is clear that b is in fA Use, and you are assigning a global variable fB. Therefore, you get 3 up values in Lua 5.2. (Using print also means that fA uses global variables.) If you use the local function fB… and delete print, you will see Until fA uses 2 upvalues in 5.1 and 5.2.