-- jave.lin - tiny_testing.lua - 测试打印 lua 的 _G
--[=[
local bit = require("bit")
--local band, bor, bxor, tohex = bit.band, bit.bor, bit.bxor, bit.tohex
--local brsh, blsh = bit.rshift, bit.lshift
local a = 1
local b = 3
local c = 2
local d = 16
print("a & b : " .. bit.band(a, b))
print("a | c : " .. bit.bor(a, c))
print("a ^ b : " .. bit.bxor(a, b))
print("tohex(d) : " .. bit.tohex(d))
print("1 << 3 = " .. bit.lshift(1, 3))
print("1 << 0 = " .. bit.lshift(1, 0))
print("3 << 0 = " .. bit.lshift(3, 0))
print("3 / 2 = " .. (3 / 2))
local tbl = {}
tbl._0xffffff = 99
print("tostring(tbl):" .. tostring(tbl))
print("tbl._0xffffff : " .. tbl._0xffffff)
]=]
this_is_global_var = "yep!i am global var!"
function GAO(tbl) -- get address of
if type(tbl) ~= "table" then
return "GAO(tbl), tbl is not a table"
end
-- return tostring(tbl):gsub("table: ", "", 1)
return string.sub(tostring(tbl), 8)
end
function RecordByGAO(recorder, address)
recorder["_" .. GAO(address)] = address
end
function GetByGAO(recorder, gao_str)
return recorder["_" .. gao_str]
end
function RemoveByGAO(recorder, gao_str)
recorder["_" .. gao_str] = nil
end
local tbl_1 = {}
local tbl_2 = {}
local tbl_3 = {}
local tbl_4 = {}
print("tostring(tbl_1) : " .. tostring(tbl_1))
print("tbl_1 address : " .. GAO(tbl_1))
print("tbl_2 address : " .. GAO(tbl_2))
print("tbl_3 address : " .. GAO(tbl_3))
print("tbl_4 address : " .. GAO(tbl_4))
local recorder = {}
RecordByGAO(recorder, tbl_3)
print("GetByGAO(recorder, GAO(tbl_1)) : " .. tostring(GetByGAO(recorder, GAO(tbl_1))))
print("GetByGAO(recorder, GAO(tbl_2)) : " .. tostring(GetByGAO(recorder, GAO(tbl_2))))
print("GetByGAO(recorder, GAO(tbl_3)) : " .. tostring(GetByGAO(recorder, GAO(tbl_3))))
print("GetByGAO(recorder, GAO(tbl_4)) : " .. tostring(GetByGAO(recorder, GAO(tbl_4))))
function dump(tbl, indent, indent_str, ar, distinct_only_inherit)
if type(tbl) ~= "table" then
return tostring(tbl)
end
ar = ar or {}
indent = indent or 1
indent_str = indent_str or " "
distinct_only_inherit = true
local str = "{\n"
local this_level_indent_str = string.rep(indent_str, indent)
RecordByGAO(ar, tbl)
for k, v in pairs(tbl) do
local type_str = type(v)
if type_str == "table" then
-- avoid infinit recur
if GetByGAO(ar, GAO(v)) == nil then
str = str .. this_level_indent_str .. "\"" .. k .. "\":" .. dump(v, indent + 1, indent_str, ar, distinct_only_inherit) .. ",\n"
if distinct_only_inherit then
RemoveByGAO(ar, GAO(v))
end
else
str = str .. this_level_indent_str .. "already print : " .. "\"" .. k .. "\":" .. tostring(v) .. ",\n"
end
elseif type_str == "number" or type_str == "function" then
str = str .. this_level_indent_str .. "\"" .. k .. "\":" .. tostring(v) .. ",\n"
elseif type_str == "string" then
str = str .. this_level_indent_str .. "\"" .. k .. "\":\"" .. tostring(v) .. "\",\n"
end
end
return str .. string.rep(indent_str, indent - 1) .. "}"
end
print("\"_G\":" .. dump(_G, 1, "-"))
--[=[
输出:
>lua -e "io.stdout:setvbuf 'no'" "tiny_testing.lua"
tostring(tbl_1) : table: 00C39750
tbl_1 address : 00C39750
tbl_2 address : 00C39688
tbl_3 address : 00C39778
tbl_4 address : 00C393B8
GetByGAO(recorder, GAO(tbl_1)) : nil
GetByGAO(recorder, GAO(tbl_2)) : nil
GetByGAO(recorder, GAO(tbl_3)) : table: 00C39778
GetByGAO(recorder, GAO(tbl_4)) : nil
"_G":{
-"string":{
--"sub":function: 00C3A940,
--"upper":function: 00C3AB80,
--"len":function: 00C39D88,
--"gfind":function: 00C39B68,
--"rep":function: 00C39C08,
--"find":function: 00C39D48,
--"match":function: 00C39B08,
--"char":function: 00C39C48,
--"dump":function: 00C39D28,
--"gmatch":function: 00C39B68,
--"reverse":function: 00C3ABA0,
--"byte":function: 00C39E48,
--"format":function: 00C39CC8,
--"gsub":function: 00C39E88,
--"lower":function: 00C39EA8,
-},
-"xpcall":function: 00C36EB0,
-"package":{
--"preload":{
--},
--"loadlib":function: 00C36E90,
--"loaded":{
---"string":{
----"sub":function: 00C3A940,
----"upper":function: 00C3AB80,
----"len":function: 00C39D88,
----"gfind":function: 00C39B68,
----"rep":function: 00C39C08,
----"find":function: 00C39D48,
----"match":function: 00C39B08,
----"char":function: 00C39C48,
----"dump":function: 00C39D28,
----"gmatch":function: 00C39B68,
----"reverse":function: 00C3ABA0,
----"byte":function: 00C39E48,
----"format":function: 00C39CC8,
----"gsub":function: 00C39E88,
----"lower":function: 00C39EA8,
---},
---"debug":{
----"getupvalue":function: 00C3B8F0,
----"debug":function: 00C3B630,
----"sethook":function: 00C3B6D0,
----"getmetatable":function: 00C3B6B0,
----"gethook":function: 00C3B730,
----"setmetatable":function: 00C3B750,
----"setlocal":function: 00C3B570,
----"traceback":function: 00C3B6F0,
----"setfenv":function: 00C3B770,
----"getinfo":function: 00C3B870,
----"setupvalue":function: 00C3B8B0,
----"getlocal":function: 00C3B670,
----"getregistry":function: 00C3B690,
----"getfenv":function: 00C3B650,
---},
---already print : "package":table: 00C37090,
---already print : "_G":table: 00C315D0,
---"io":{
----"lines":function: 00C37BC0,
----"write":function: 00C39F48,
----"close":function: 00C37D80,
----"flush":function: 00C37B80,
----"open":function: 00C39F28,
----"output":function: 00C3A108,
----"type":function: 00C3A228,
----"read":function: 00C3A128,
----"input":function: 00C37DC0,
----"popen":function: 00C39FE8,
----"tmpfile":function: 00C3A148,
---},
---"os":{
----"exit":function: 00C3A288,
----"setlocale":function: 00C39B88,
----"date":function: 00C39FA8,
----"getenv":function: 00C3A068,
----"difftime":function: 00C3A1C8,
----"remove":function: 00C3A088,
----"time":function: 00C39B28,
----"clock":function: 00C3A248,
----"tmpname":function: 00C39CA8,
----"rename":function: 00C39DC8,
----"execute":function: 00C3A008,
---},
---"table":{
----"setn":function: 00C37C20,
----"insert":function: 00C38000,
----"getn":function: 00C381E0,
----"foreachi":function: 00C37F40,
----"maxn":function: 00C37F80,
----"foreach":function: 00C381A0,
----"concat":function: 00C38220,
----"sort":function: 00C37D20,
----"remove":function: 00C37E60,
---},
---"math":{
----"log":function: 00C3B080,
----"max":function: 00C3AEC0,
----"acos":function: 00C3ABC0,
----"huge":1.#INF,
----"ldexp":function: 00C3ADE0,
----"pi":3.1415926535898,
----"cos":function: 00C3AA60,
----"tanh":function: 00C3AF40,
----"pow":function: 00C3AD20,
----"deg":function: 00C3A9C0,
----"tan":function: 00C3B590,
----"cosh":function: 00C3AA00,
----"sinh":function: 00C3AEE0,
----"random":function: 00C3ADC0,
----"randomseed":function: 00C3B040,
----"frexp":function: 00C3AFE0,
----"ceil":function: 00C3ABE0,
----"floor":function: 00C3ACA0,
----"rad":function: 00C3B000,
----"abs":function: 00C3AB40,
----"sqrt":function: 00C3AF20,
----"modf":function: 00C3AD80,
----"asin":function: 00C3AC00,
----"min":function: 00C3AFC0,
----"mod":function: 00C3A920,
----"fmod":function: 00C3A920,
----"log10":function: 00C3B0A0,
----"atan2":function: 00C3AAE0,
----"exp":function: 00C3AA40,
----"sin":function: 00C3AF60,
----"atan":function: 00C3AB00,
---},
---"coroutine":{
----"resume":function: 00C36C50,
----"yield":function: 00C36D10,
----"status":function: 00C36CD0,
----"wrap":function: 00C36CB0,
----"create":function: 00C36D50,
----"running":function: 00C36D70,
---},
--},
--"loaders":{
---"1":function: 00C38020,
---"2":function: 00C380E0,
---"4":function: 00C38120,
---"3":function: 00C38040,
--},
--"cpath":".\?.dll;.\?51.dll;F:\Program Files (x86)\Lua\5.1\?.dll;F:\Program Files (x86)\Lua\5.1\?51.dll;F:\Program Files (x86)\Lua\5.1\clibs\?.dll;F:\Program Files (x86)\Lua\5.1\clibs\?51.dll;F:\Program Files (x86)\Lua\5.1\loadall.dll;F:\Program Files (x86)\Lua\5.1\clibs\loadall.dll",
--"config":"\
;
?
!
-",
--"path":";.\?.lua;F:\Program Files (x86)\Lua\5.1\lua\?.lua;F:\Program Files (x86)\Lua\5.1\lua\?\init.lua;F:\Program Files (x86)\Lua\5.1\?.lua;F:\Program Files (x86)\Lua\5.1\?\init.lua;F:\Program Files (x86)\Lua\5.1\lua\?.luac",
--"seeall":function: 00C37FA0,
-},
-"tostring":function: 00C36E10,
-"print":function: 00C369F0,
-"os":{
--"exit":function: 00C3A288,
--"setlocale":function: 00C39B88,
--"date":function: 00C39FA8,
--"getenv":function: 00C3A068,
--"difftime":function: 00C3A1C8,
--"remove":function: 00C3A088,
--"time":function: 00C39B28,
--"clock":function: 00C3A248,
--"tmpname":function: 00C39CA8,
--"rename":function: 00C39DC8,
--"execute":function: 00C3A008,
-},
-"unpack":function: 00C36ED0,
-"require":function: 00C380A0,
-"getfenv":function: 00C368D0,
-"setmetatable":function: 00C36870,
-"next":function: 00C36A50,
-"assert":function: 00C36A70,
-"RemoveByGAO":function: 00A52EC8,
-"tonumber":function: 00C36EF0,
-"io":{
--"lines":function: 00C37BC0,
--"write":function: 00C39F48,
--"close":function: 00C37D80,
--"flush":function: 00C37B80,
--"open":function: 00C39F28,
--"output":function: 00C3A108,
--"type":function: 00C3A228,
--"read":function: 00C3A128,
--"input":function: 00C37DC0,
--"popen":function: 00C39FE8,
--"tmpfile":function: 00C3A148,
-},
-"rawequal":function: 00C36AF0,
-"collectgarbage":function: 00C36930,
-"arg":{
--"-2":"-e",
--"0":"tiny_testing.lua",
--"-3":"lua",
--"-1":"io.stdout:setvbuf 'no'",
-},
-"getmetatable":function: 00C368F0,
-"module":function: 00C38140,
-"this_is_global_var":"yep!i am global var!",
-"rawset":function: 00C368B0,
-"ipairs":function: 00C315F8,
-"GetByGAO":function: 00A52FA8,
-"RecordByGAO":function: 00A52CA8,
-"GAO":function: 00A52E28,
-"math":{
--"log":function: 00C3B080,
--"max":function: 00C3AEC0,
--"acos":function: 00C3ABC0,
--"huge":1.#INF,
--"ldexp":function: 00C3ADE0,
--"pi":3.1415926535898,
--"cos":function: 00C3AA60,
--"tanh":function: 00C3AF40,
--"pow":function: 00C3AD20,
--"deg":function: 00C3A9C0,
--"tan":function: 00C3B590,
--"cosh":function: 00C3AA00,
--"sinh":function: 00C3AEE0,
--"random":function: 00C3ADC0,
--"randomseed":function: 00C3B040,
--"frexp":function: 00C3AFE0,
--"ceil":function: 00C3ABE0,
--"floor":function: 00C3ACA0,
--"rad":function: 00C3B000,
--"abs":function: 00C3AB40,
--"sqrt":function: 00C3AF20,
--"modf":function: 00C3AD80,
--"asin":function: 00C3AC00,
--"min":function: 00C3AFC0,
--"mod":function: 00C3A920,
--"fmod":function: 00C3A920,
--"log10":function: 00C3B0A0,
--"atan2":function: 00C3AAE0,
--"exp":function: 00C3AA40,
--"sin":function: 00C3AF60,
--"atan":function: 00C3AB00,
-},
-"debug":{
--"getupvalue":function: 00C3B8F0,
--"debug":function: 00C3B630,
--"sethook":function: 00C3B6D0,
--"getmetatable":function: 00C3B6B0,
--"gethook":function: 00C3B730,
--"setmetatable":function: 00C3B750,
--"setlocal":function: 00C3B570,
--"traceback":function: 00C3B6F0,
--"setfenv":function: 00C3B770,
--"getinfo":function: 00C3B870,
--"setupvalue":function: 00C3B8B0,
--"getlocal":function: 00C3B670,
--"getregistry":function: 00C3B690,
--"getfenv":function: 00C3B650,
-},
-"pcall":function: 00C36950,
-"table":{
--"setn":function: 00C37C20,
--"insert":function: 00C38000,
--"getn":function: 00C381E0,
--"foreachi":function: 00C37F40,
--"maxn":function: 00C37F80,
--"foreach":function: 00C381A0,
--"concat":function: 00C38220,
--"sort":function: 00C37D20,
--"remove":function: 00C37E60,
-},
-"newproxy":function: 00C31708,
-"type":function: 00C36C10,
-"coroutine":{
--"resume":function: 00C36C50,
--"yield":function: 00C36D10,
--"status":function: 00C36CD0,
--"wrap":function: 00C36CB0,
--"create":function: 00C36D50,
--"running":function: 00C36D70,
-},
-already print : "_G":table: 00C315D0,
-"select":function: 00C36810,
-"gcinfo":function: 00C36B10,
-"pairs":function: 00C316D8,
-"rawget":function: 00C36830,
-"loadstring":function: 00C36A10,
-"dump":function: 00A52E88,
-"_VERSION":"Lua 5.1",
-"dofile":function: 00C36A30,
-"setfenv":function: 00C36890,
-"load":function: 00C36990,
-"error":function: 00C36B30,
-"loadfile":function: 00C36AB0,
}
>Exit code: 0
]=]
_G 下除了我们添加的成员外,还有很多我们见过的常用函数
总结:
- require 的模块都会再 _G 第一级子级下
- 我们定义的全局函数、变量也是在 _G 的第一级子级(搜索
this_is_global_var
就很容易发现)