当前位置: 首页 > article >正文

Lua Global环境

Lua的全局变量实际上是用environment这个表来存储所有的全局变量,其优点就是简化了lua的内部实现,而且还能像其他类型的表一样去操作这个全局表。

_G

Lua将environment本身存储在全局变量_G中,其中_G._G = _G。如若感兴趣,可以直接用pairs函数打印当前的全局变量名:

A = {1,2,3}

function testMySelf()
end

for n in pairs(_G) do print(n) end
--[[
assert
dofile
...
testMySelf
utf8
A
package
...
pairs
_G
...
]]--

_G的用法其实和直接赋值全局变量差不多,如下两句性质完全一样的代码所示:

_G["a"] = _G["var1"]
a = var1

getfield/setfield

手动写一个getfield函数,用以通过字符串查找相应的域,比如a.b.c.d的值是多少。实际上是从_G开始循环向域内部遍历:

function getfield (f) 
    local v = _G -- start with the table of globals 
    for w in string.gmatch(f, "([%w_]+)") do
     v = v[w]
    end
    return v
end

A = {["a"] = 1}
print(getfield("A.a")) --1

setfield也是一样的道理,只不过需要判断是否是最后一个".",需要独立处理最后一个域并进行赋值,比如A.a = 1:

function setfield (f, v) 
    local t = _G -- start with the table of globals 
    for w, d in string.gmatch(f, "([%w_]+)(.?)") do
        if d == "." then -- not last field? 
            t[w] = t[w] or {} -- create table if absent 
            t = t[w] -- get the table 
        else -- last field 
            t[w] = v -- do the assignment 
        end
    end
end

setfield("A.a",2)
print(A.a) --2

声明全局变量

如若控制全局变量的声明和访问,则直接控制_G的newindex和index即可,但是需要注意一点的是如果在newindex中明确禁止了全局变量的声明操作,那么任何直接赋值都是有问题的,如以下代码:

setmetatable(_G, { 
    __newindex = function (_, n)
        print("attempt to write to undeclared variable "..n)
   end,
    __index = function (_, n)
        print("attempt to read undeclared variable "..n)
   end,
})
A = {} --attempt to write to undeclared variable A

这时候只能通过rawget/rawset来直接进行访问操作。比如声明一个入口函数,所有全局变量的声明都必须经此函数初始化;判断这个域是否存在,也需要用rawget函数直接访问:

function declare (name, initval) 
 rawset(_G, name, initval or false) 
end

...
if rawget(_G, var) == nil then
-- 'var' is undeclared 
 ... 
end

当然需要注意的是上述处理中初始化全局变量如果是nil赋值则默认赋值为false了。

如若不需要这个默认赋值,希望代码支持类似a = nil的声明,那么也可以维护一个注册表并放入newindex元方法中判断,当然这也是官方文档给的一个解决方案(不过对本人来说觉得有点鸡肋,毕竟类似a = nil的代码是可以避免的):

local declaredNames = {}
function declare (name, initval)
    rawset(_G, name, initval)
    declaredNames[name] = true
end
setmetatable(_G, {
    __newindex = function (t, n, v)
    if not declaredNames[n] then
        error("attempt to write to undeclared var. "..n, 2)
    else
        print("newindex")
        rawset(t, n, v) -- do the actual set 
    end
    end,
    __index = function (_, n)
        if not declaredNames[n] then
            error("attempt to read undeclared var. "..n, 2)
        else
            return nil
        end
    end,
})

declare("A",nil)
A = {} --newindex


http://www.kler.cn/news/234013.html

相关文章:

  • 时间序列预测——BiGRU模型
  • 应急响应-挖矿木马-常规处置方法
  • notepad++成功安装后默认显示英文怎么设置中文界面?
  • 突破编程_C++_面试(基础知识(10))
  • 学习笔记——ENM模拟
  • 微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
  • 零基础学编程从入门到精通,系统化的编程视频教程上线,中文编程开发语言工具构件之缩放控制面板构件用法
  • Centos 7系统安装proftpd-1.3.8过程
  • 图像的旋转不变特性及应用
  • React18原理: Fiber架构下的单线程CPU调度策略
  • 代码随想录-背包问题
  • vue3 之 商城项目—详情页
  • 双非本科准备秋招(21.1)—— 力扣二叉搜索树
  • C++ //练习 5.4 说明下列例子的含义,如果存在问题,试着修改它。
  • 随机MM引流源码PHP开源版
  • RabbitMQ-1.介绍与安装
  • django中实现观察者模式
  • Python面试题7-12
  • Linux无交互自动安装miniconda3
  • 004集—二调数据库标注分子分母模式及统计净面积——arcgis
  • MybatisPlus Wrapper构造器(查询篇)
  • 【Java面试】数据类型常见面试题
  • elasticsearch下载及可视化工具下载使用
  • 二十、K8S-1-权限管理RBAC详解
  • MacOS 查AirPods 电量技巧:可实现低电量提醒、自动弹窗
  • Open CASCADE学习|环形弹簧建模
  • 蓝桥杯基础知识7 vector
  • 【开源】JAVA+Vue.js实现计算机机房作业管理系统
  • 【MySQL】字符串函数的学习
  • Python自动化办公实战案例分享