11-- tables
22local _G = _G
3- local string , io , os , table , math , package , debug , coroutine = string , io , os , table , math , package , debug , coroutine
3+ local string , io , debug , coroutine = string , io , debug , coroutine
44
55-- functions
6- local xpcall , tostring , print , unpack , require , getfenv , setmetatable = xpcall , tostring , print , unpack , require , getfenv , setmetatable
7- local next , assert , tonumber , rawequal , collectgarbage , getmetatable = next , assert , tonumber , rawequal , collectgarbage , getmetatable
8- local module , rawset , pcall , newproxy , type , select , gcinfo , pairs = module , rawset , pcall , newproxy , type , select , gcinfo , pairs
9- local rawget , loadstring , ipairs , dofile , setfenv , load , error , loadfile = rawget , loadstring , ipairs , dofile , setfenv , load , error , loadfile
6+ local tostring , print , require = tostring , print , require
7+ local next , assert = next , assert
8+ local pcall , type , pairs , ipairs = pcall , type , pairs , ipairs
9+ local error = error
1010
1111assert (debug , " debug table must be available at this point" )
1212
@@ -18,51 +18,75 @@ local table_concat = table.concat
1818local _M = {}
1919
2020-- this tables should be weak so the elements in them won't become uncollectable
21- local m_known_tables = {
22- [_G ] = " _G (global table)" ,
23- [string ] = " string module" ,
24- [io ] = " io module" ,
25- [os ] = " os module" ,
26- [table ] = " table module" ,
27- [math ] = " math module" ,
28- [package ] = " package table" ,
29- [debug ] = " debug table" ,
30- [coroutine ] = " coroutine table"
31- }
21+ local m_known_tables = { [_G ] = " _G (global table)" }
22+ local function add_known_module (name , desc )
23+ local ok , mod = pcall (require , name )
24+ if ok then
25+ m_known_tables [mod ] = desc
26+ end
27+ end
28+
29+ add_known_module (" string" , " string module" )
30+ add_known_module (" io" , " io module" )
31+ add_known_module (" os" , " os module" )
32+ add_known_module (" table" , " table module" )
33+ add_known_module (" math" , " math module" )
34+ add_known_module (" package" , " package module" )
35+ add_known_module (" debug" , " debug module" )
36+ add_known_module (" coroutine" , " coroutine module" )
37+
38+ -- lua5.2
39+ add_known_module (" bit32" , " bit32 module" )
40+ -- luajit
41+ add_known_module (" bit" , " bit module" )
42+ add_known_module (" jit" , " jit module" )
43+
44+
3245local m_user_known_tables = {}
3346
34- local m_known_functions = {
35- [xpcall ] = " xpcall" ,
36- [tostring ] = " tostring" ,
37- [print ] = " print" ,
38- [unpack ] = " unpack" ,
39- [require ] = " require" ,
40- [getfenv ] = " getfenv" ,
41- [setmetatable ] = " setmetatable" ,
42- [next ] = " next" ,
43- [assert ] = " assert" ,
44- [tonumber ] = " tonumber" ,
45- [rawequal ] = " rawequal" ,
46- [collectgarbage ] = " collectgarbage" ,
47- [getmetatable ] = " getmetatable" ,
48- [module ] = " module" ,
49- [rawset ] = " rawset" ,
50- [pcall ] = " pcall" ,
51- [newproxy ] = " newproxy" ,
52- [type ] = " type" ,
53- [select ] = " select" ,
54- [gcinfo ] = " gcinfo" ,
55- [pairs ] = " pairs" ,
56- [rawget ] = " rawget" ,
57- [loadstring ] = " loadstring" ,
58- [ipairs ] = " ipairs" ,
59- [dofile ] = " dofile" ,
60- [setfenv ] = " setfenv" ,
61- [load ] = " load" ,
62- [error ] = " error" ,
63- [loadfile ] = " loadfile" ,
47+ local m_known_functions = {}
48+ for _ , name in ipairs {
49+ -- Lua 5.2, 5.1
50+ " assert" ,
51+ " collectgarbage" ,
52+ " dofile" ,
53+ " error" ,
54+ " getmetatable" ,
55+ " ipairs" ,
56+ " load" ,
57+ " loadfile" ,
58+ " next" ,
59+ " pairs" ,
60+ " pcall" ,
61+ " print" ,
62+ " rawequal" ,
63+ " rawget" ,
64+ " rawlen" ,
65+ " rawset" ,
66+ " require" ,
67+ " select" ,
68+ " setmetatable" ,
69+ " tonumber" ,
70+ " tostring" ,
71+ " type" ,
72+ " xpcall" ,
73+
74+ -- Lua 5.1
75+ " gcinfo" ,
76+ " getfenv" ,
77+ " loadstring" ,
78+ " module" ,
79+ " newproxy" ,
80+ " setfenv" ,
81+ " unpack" ,
6482 -- TODO: add table.* etc functions
65- }
83+ } do
84+ if _G [name ] then
85+ m_known_functions [_G [name ]] = name
86+ end
87+ end
88+
89+
6690
6791local m_user_known_functions = {}
6892
0 commit comments