debug.getregistry介绍
debug.getregistry用于获取lua的注册表,是一个非常重要的数据结构,里面存放着lua运行时关键的信息。注册表里包含全局变量、已加载的库、主协程、文件句柄等相关信息。
debug.getregistry使用
我们用以下代码打印注册表中的内容,如下代码:
local t = debug.getregistry()
for k, v in pairs(t) do
print(k, v)
end
输出如下:
1 thread: 0x55c80abe52a8
2 table: 0x55c80abe5c50
_CLIBS table: 0x55c80abe6bc0
FILE* table: 0x55c80abe84b0
_LOADED table: 0x55c80abe6a60
_PRELOAD table: 0x55c80abe7c30
_IO_output file (0x7f92d61e45c0)
_IO_input file (0x7f92d61e38e0)
第2个元素其实就是全局变量表,当遍历它时就能看到各个全局变量的名字。
debug.getregistry总结
debug.getregistry用于获取lua的注册表,其中包含lua运行时的重要信息。