Skip to content

Instantly share code, notes, and snippets.

@SkyyySi
Created April 23, 2025 18:18
Show Gist options
  • Save SkyyySi/c8673e30adfea24a1f4ce896b64a8600 to your computer and use it in GitHub Desktop.
Save SkyyySi/c8673e30adfea24a1f4ce896b64a8600 to your computer and use it in GitHub Desktop.
#!/usr/bin/env lua
--- SPDX-License-Identifier: 0BSD
local setmetatable = setmetatable
local require = require
local pairs = pairs
local ipairs = ipairs
local type = type
local print = print
local string = string
local table = table
local package = package
local jit = jit
---@diagnostic disable-next-line: deprecated
if _VERSION == "Lua 5.1" and type(jit) == "table" then
require("string.buffer")
require("table.clear")
require("table.new")
_G.ffi = require("ffi")
end
local function is_identifier_name(key)
return type(key) == "string" and string.find(key, "^[a-zA-Z_][a-zA-Z0-9_]*$", 1) ~= nil
end
---@param tb table
---@param name string
---@param seen_tables { [table]: true? }
---@return string[] keys
local function get_keys(tb, name, seen_tables)
if seen_tables[tb] ~= nil or tb == package.loaded then
return {}
end
seen_tables[tb] = true
---@type string[]
local keys = {}
for key, value in pairs(tb) do
if not is_identifier_name(key) then
goto continue
end
local full_key = name .. "." .. key
keys[#keys+1] = full_key
if type(value) ~= "table" then
goto continue
end
for _, child_key in ipairs(get_keys(value, full_key, seen_tables)) do
keys[#keys+1] = child_key
end
::continue::
end
return keys
end
---@generic T
---@param tb T[]
local function remove_duplicates(tb)
local keep = {}
local discard = {}
for k, v in pairs(tb) do
if keep[k] == nil then
keep[k] = v
else
discard[k] = v
end
end
for k, _ in pairs(discard) do
tb[k] = nil
end
end
---@param tb table
---@param name string
---@return string sub_module
local function get_sub_modules(tb, name)
---@type { [table]: true? }
local seen_tables = setmetatable({}, {
__mode = "k",
})
local keys = get_keys(tb, name, seen_tables)
for k, v in ipairs(keys) do
keys[k] = string.sub(v, #name + 2)
end
table.sort(keys)
remove_duplicates(keys)
local result = ""
for i = 1, #keys do
result = result .. keys[i]
if i < #keys then
result = result .. "\n"
end
end
return result
end
print(get_sub_modules(_G, "_G"))

Zero-Clause BSD

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

_G
_VERSION
arg
assert
collectgarbage
coroutine
coroutine.close
coroutine.create
coroutine.isyieldable
coroutine.resume
coroutine.running
coroutine.status
coroutine.wrap
coroutine.yield
debug
debug.debug
debug.gethook
debug.getinfo
debug.getlocal
debug.getmetatable
debug.getregistry
debug.getupvalue
debug.getuservalue
debug.setcstacklimit
debug.sethook
debug.setlocal
debug.setmetatable
debug.setupvalue
debug.setuservalue
debug.traceback
debug.upvalueid
debug.upvaluejoin
dofile
error
getmetatable
io
io.close
io.flush
io.input
io.lines
io.open
io.output
io.popen
io.read
io.stderr
io.stdin
io.stdout
io.tmpfile
io.type
io.write
ipairs
load
loadfile
math
math.abs
math.acos
math.asin
math.atan
math.atan2
math.ceil
math.cos
math.cosh
math.deg
math.exp
math.floor
math.fmod
math.frexp
math.huge
math.ldexp
math.log
math.log10
math.max
math.maxinteger
math.min
math.mininteger
math.modf
math.pi
math.pow
math.rad
math.random
math.randomseed
math.sin
math.sinh
math.sqrt
math.tan
math.tanh
math.tointeger
math.type
math.ult
next
os
os.clock
os.date
os.difftime
os.execute
os.exit
os.getenv
os.remove
os.rename
os.setlocale
os.time
os.tmpname
package
package.config
package.cpath
package.loaded
package.loadlib
package.path
package.preload
package.searchers
package.searchpath
pairs
pcall
print
rawequal
rawget
rawlen
rawset
require
select
setmetatable
string
string.byte
string.char
string.dump
string.find
string.format
string.gmatch
string.gsub
string.len
string.lower
string.match
string.pack
string.packsize
string.rep
string.reverse
string.sub
string.unpack
string.upper
table
table.concat
table.insert
table.move
table.pack
table.remove
table.sort
table.unpack
tonumber
tostring
type
utf8
utf8.char
utf8.charpattern
utf8.codepoint
utf8.codes
utf8.len
utf8.offset
warn
xpcall
4a5,17
> bit
> bit.arshift
> bit.band
> bit.bnot
> bit.bor
> bit.bswap
> bit.bxor
> bit.lshift
> bit.rol
> bit.ror
> bit.rshift
> bit.tobit
> bit.tohex
7d19
< coroutine.close
16a29
> debug.getfenv
24c37
< debug.setcstacklimit
---
> debug.setfenv
34a48,70
> ffi
> ffi.C
> ffi.abi
> ffi.alignof
> ffi.arch
> ffi.cast
> ffi.cdef
> ffi.copy
> ffi.errno
> ffi.fill
> ffi.gc
> ffi.istype
> ffi.load
> ffi.metatype
> ffi.new
> ffi.offsetof
> ffi.os
> ffi.sizeof
> ffi.string
> ffi.typeinfo
> ffi.typeof
> gcinfo
> getfenv
51a88,100
> jit
> jit.arch
> jit.attach
> jit.flush
> jit.off
> jit.on
> jit.opt
> jit.opt.start
> jit.os
> jit.security
> jit.status
> jit.version
> jit.version_num
53a103
> loadstring
73d122
< math.maxinteger
75d123
< math.mininteger
87,89c135,136
< math.tointeger
< math.type
< math.ult
---
> module
> newproxy
106a154
> package.loaders
109a158
> package.preload.ffi
111a161
> package.seeall
120a171
> setfenv
133,134d183
< string.pack
< string.packsize
138d186
< string.unpack
140a189
> table.clear
141a191,193
> table.foreach
> table.foreachi
> table.getn
142a195
> table.maxn
143a197
> table.new
151,158c205
< utf8
< utf8.char
< utf8.charpattern
< utf8.codepoint
< utf8.codes
< utf8.len
< utf8.offset
< warn
---
> unpack
_G
_VERSION
arg
assert
bit
bit.arshift
bit.band
bit.bnot
bit.bor
bit.bswap
bit.bxor
bit.lshift
bit.rol
bit.ror
bit.rshift
bit.tobit
bit.tohex
collectgarbage
coroutine
coroutine.create
coroutine.isyieldable
coroutine.resume
coroutine.running
coroutine.status
coroutine.wrap
coroutine.yield
debug
debug.debug
debug.getfenv
debug.gethook
debug.getinfo
debug.getlocal
debug.getmetatable
debug.getregistry
debug.getupvalue
debug.getuservalue
debug.setfenv
debug.sethook
debug.setlocal
debug.setmetatable
debug.setupvalue
debug.setuservalue
debug.traceback
debug.upvalueid
debug.upvaluejoin
dofile
error
ffi
ffi.C
ffi.abi
ffi.alignof
ffi.arch
ffi.cast
ffi.cdef
ffi.copy
ffi.errno
ffi.fill
ffi.gc
ffi.istype
ffi.load
ffi.metatype
ffi.new
ffi.offsetof
ffi.os
ffi.sizeof
ffi.string
ffi.typeinfo
ffi.typeof
gcinfo
getfenv
getmetatable
io
io.close
io.flush
io.input
io.lines
io.open
io.output
io.popen
io.read
io.stderr
io.stdin
io.stdout
io.tmpfile
io.type
io.write
ipairs
jit
jit.arch
jit.attach
jit.flush
jit.off
jit.on
jit.opt
jit.opt.start
jit.os
jit.security
jit.status
jit.version
jit.version_num
load
loadfile
loadstring
math
math.abs
math.acos
math.asin
math.atan
math.atan2
math.ceil
math.cos
math.cosh
math.deg
math.exp
math.floor
math.fmod
math.frexp
math.huge
math.ldexp
math.log
math.log10
math.max
math.min
math.modf
math.pi
math.pow
math.rad
math.random
math.randomseed
math.sin
math.sinh
math.sqrt
math.tan
math.tanh
module
newproxy
next
os
os.clock
os.date
os.difftime
os.execute
os.exit
os.getenv
os.remove
os.rename
os.setlocale
os.time
os.tmpname
package
package.config
package.cpath
package.loaded
package.loaders
package.loadlib
package.path
package.preload
package.preload.ffi
package.searchers
package.searchpath
package.seeall
pairs
pcall
print
rawequal
rawget
rawlen
rawset
require
select
setfenv
setmetatable
string
string.byte
string.char
string.dump
string.find
string.format
string.gmatch
string.gsub
string.len
string.lower
string.match
string.rep
string.reverse
string.sub
string.upper
table
table.clear
table.concat
table.foreach
table.foreachi
table.getn
table.insert
table.maxn
table.move
table.new
table.pack
table.remove
table.sort
table.unpack
tonumber
tostring
type
unpack
xpcall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment