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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值