公司的产品中需要为用户提供一些lua 的API ,但是 又不希望一些api 被其他文件调到,比如说 我希望在用户写的第一个脚本里面注册一批api 但是 这些脚本API 又不希望被第二个脚本调用到。(在实际的产品中 这两个脚本分别是两个线程 共享一个 _G 表)
最终我们使用 lua的MetaTable 解决了这个问题
我们可以给 _G 表 设置一个 __index的 方法,当 _G 找不到这个函数的时候 会调用__index 的方法 去查找函数,我们可以在这个函数里面去实现我们想要实现的方法,进而控制函数的作用域。
关于 原表中提供的方法,大家可以参考
https://www.runoob.com/lua/lua-metatables.html
下面是 另外一位大佬对于 原表的介绍
metatable是被译作元表,Lua 中的每个值都可以用一个 metatable。
这个 metatable 就是一个原始的 Lua table ,它用来定义原始值在特定操作下的行为。
一个 metatable 可以控制一个对象做数学运算操作、比较操作、连接操作、取长度操作、取下标操作时的行为,metatable 中还可以定义一个函数,让 userdata 作垃圾收集时调用它。对于这些操作,Lua 都将其关联上一个被称作事件的指定键。当 Lua 需要对一个值发起这些操作中的一个时,它会去检查值中 metatable 中是否有对应事件。如果有的话,键名对应的值(元方法)将控制 Lua 怎样做这个操作。
metatable通过其包含的函数来给所挂接的table定义一些特殊的操作,包括:
__add: 定义所挂接table的加法操作
__mul: 定义乘法操作
__div: 定义除法操作
__sub: 定义减法操作
__unm: 定义负操作, 即: -table的含义
__tostring: 定义当table作为tostring()函式之参数被呼叫时的行为(例如: print(table)时将呼叫tostring(table)作为输出结果)
__concat: 定义连接操作(".."运算符)
__index: 定义当table中不存在的key值被试图获取时的行为
__newindex: 定义在table中产生新key值时的行为
————————————————
版权声明:本文为CSDN博主「lt3」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lts_cxl/article/details/27493125
最后 放上一个我自己写的 简单的demo
common.lua
function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
myMetatable = { }
function myMetatable.test()
print("this is a test function ...")
end
function myMetatable.__index(_, n)
print("this is __index function ...")
if(arg[0] == "1.lua") then
return myMetatable[n]
else
return nil
end
end
setmetatable(_G,myMetatable)
1.lua
dofile("/home/jeason/lua-5.4.3/script/common.lua")
test()
-- print_r(_G)
-- print_r(arg[0])
2.lua
dofile("/home/jeason/lua-5.4.3/script/common.lua")
test()
代码的运行结果如下
[jeason@centos7 script]$ pwd
/home/jeason/lua-5.4.3/script
[jeason@centos7 script]$ lua 1.lua
this is __index function ...
this is a test function ...
[jeason@centos7 script]$ lua 2.lua
this is __index function ...
lua: 2.lua:2: attempt to call global 'test' (a nil value)
stack traceback:
2.lua:2: in main chunk
[C]: ?
当然了在 实际的应用中 我们可以设置多种条件去决定要不要将某一个函数 对外注册出去,比如 你可以获取线程的id ,这样就可以限制一些函数在其他线程的使用