lua變量研究

--[[
In Lua, Global variables are accessible via the _G
table ...
]]
function tellme()
for k,v in pairs(_G) do
print("[Global key]", k, "[value]", v) -- v若是是function或者table,後面的是內存地址嗎?
end
end


starter = "Prawn Cocktail"
main = "Jellied Eel"
desert = "Pineapple"


tellme()




--  It will return a table of locals from the calling scope
function locals()
  local variables = {}
  local idx = 1
  while true do
    local ln, lv = debug.getlocal(2, idx)
    if ln ~= nil then
      variables[ln] = lv
    else
      break
    end
    idx = 1 + idx
  end
  return variables -- 這裏返回的是一個local變量,怎麼理解?
end


local a = 2


local function func()
end


for x, v in pairs(locals()) do
print(x, v)
end


-- Upvalues are local variables from outer scopes, that are used in the current function. They are neither in _G nor in locals()
function upvalues()
  local variables = {}
  local idx = 1
  local func = debug.getinfo(2, "f").func
  while true do
    local ln, lv = debug.getupvalue(func, idx)
    if ln ~= nil then
      variables[ln] = lv
    else
      break
    end
    idx = 1 + idx
  end
  return variables
end


function f()
local b = a; -- a被稱爲upvalue
for x,v in pairs(upvalues()) do
print(x,v)
end
end;


f()


-- Use debug.getlocal
print('--------------------')
local foobar = 1


local i = 1
repeat
    local k, v = debug.getlocal(1, i)
    if k then
        print(k, v)
        i = i + 1
    end
until nil == k




function testLocal(a, b) -- 函數參數是local變量
local i = 1
repeat
local k, v = debug.getlocal(1, i)
if k then
print(k, v)
i = i + 1
end
until nil == k
end


print('testLocal')
testLocal()


function testReturn001()
local o = o or {}


local i = 1
repeat
local k, v = debug.getlocal(1, i)
if k then
print(k, v)
i = i + 1
end
until nil == k


return o
end


print('testReturn001')
t1 = testReturn001()
print(t1)


print('ss')
local i = 1
repeat
local k, v = debug.getlocal(1, i)
if k then
print(k, v)
i = i + 1
end
until nil == k


--[[Static Variables
--The "static" keyword does not exist in Lua; therefore, it's not possible to create a static variable. Static variables are useful in that they are local to the function, but retain their value with subsequent calls to said function. You can simulate this functionality by encapsulating your function definition in a do...end block with the local variable declaration inside the do...end block and before the function definition. Like so:
do
    local myStaticFrame
    function(...)
        if not myStaticFrame then
            -- Don't create the frame until the function is called once and don't make it again.
            myStaticFrame = CreateFrame('Frame')
        end
        -- Do stuff to myStaticFrame
    end
end
--This could be useful in OnUpdate scripts that must use a frame/table each time it's executed, which is horribly inefficient.
--]]


-- in general it is easier to access local variables than global variables, often times, programmers will create local copies of globals they retrieve. This can make code much easier to understand and run:
local global_n = _G["n"]; -- Make local copy of global,疑問:這裏改變local變量,會改變相應的global變量嗎?


--[[
upvalue:嵌套函數的外部函數的局部變量
function func(a) <== 這個函數返回值是一個函數
return function ()
    a = a + 1    <== 這裏能夠訪問外部函數func的局部變量a,這個變量a就是upvalue
    return a
end
end


func返回一個匿名函數,可用變量接取之。該匿名函數有一個upvalue a(有點像C函數的static變量),初值爲首次調用func時的參數


閉包:一個匿名函數加上其可訪問的upvalue
c = func(1) <== c如今指向一個擁有upvalue a = 1的匿名函數,c也被稱做一個閉包
c()          <== 返回2
c()          <== 返回3
c2 = func(1) <== c2如今指向另一個擁有upvalue a = 1的匿名函數,c2也被稱做一個閉包
c2()         <== 返回2
--]]


print('test')
-- test.lua
function conv(data)
local r_table = {}
print("conv: "..tostring(r_table).." ("..type(r_table)..")")
print(r_table)
print(_G['r_table'])
return r_table
end
data_table = conv(bytes)
print("main: "..tostring(data_table).." ("..type(data_table)..")")
print(data_table)
print(_G['data_table'])

-- end of test.lua閉包

相關文章
相關標籤/搜索