Posted by
| Nick Gammon
Australia (23,044 posts) bio
Forum Administrator |
Message
| Well, back to exposing each function individually. I thought, "why not let Lua do it for us?". So a quick bit of code later, and you can find all the functions that need to be made local, like this:
local script = [====[
--[[
<< put your function code here >>
]====]
local found = {}
local libs_used = {}
local funcs = {}
local libraries = {
"string",
"package",
"os",
"io",
"bc",
"progress",
"bit",
"rex",
"utils",
"table",
"math",
"debug",
"coroutine",
"lpeg",
"sqlite3",
"world",
} -- end of libraries
local tables = {
"trigger_flag",
"alias_flag",
"timer_flag",
"custom_colour",
"error_code",
"sendto",
} -- end of tables
for k, v in pairs (_G) do
if type (v) == "function" then
funcs [k] = true
end -- if
end -- for
for _, lib in ipairs (libraries) do
for k, v in pairs (_G [lib]) do
if type (v) == "function" then
funcs [lib .. "." .. k] = true
if lib == "world" then
funcs [k] = true
end -- if
end -- if
end -- for
end -- for each library
-- now do stuff like error_code, colours etc.
for _, lib in ipairs (tables) do
for k, v in pairs (_G [lib]) do
funcs [lib .. "." .. k] = true
end -- for
end -- for each table
-- scan our script for matching functions
for w in string.gmatch (script, "[%a%d._]+") do
if funcs [w] then
local lib, func = string.match (w, "^([%a%d_]+)%.([%a%d_]+)")
if lib then
libs_used [lib] = libs_used [lib] or {}
libs_used [lib] [func] = true
else
found [w] = true
end -- if
end -- if
end -- for
local tl = {}
for k in pairs (libs_used) do
table.insert (tl, k)
end -- for
table.sort (tl)
for _, lib in ipairs (tl) do
print ("local", lib, "= {")
for k in pairs (libs_used [lib]) do
print (" ", k, "=", lib .. "." .. k .. ",")
end -- for
print (" }")
end -- for
local t = {}
for k in pairs (found) do
table.insert (t, k)
end -- for
table.insert (t, "_G")
table.sort (t)
print ("local", table.concat (t, ", "), "=", table.concat (t, ", "))
Now the output from that in the case of my earlier PPI module was this:
local error_code = {
eOK = error_code.eOK,
}
local package = {
seeall = package.seeall,
}
local string = {
format = string.format,
}
local CallPlugin, ColourNote, GetPluginInfo, GetPluginName, GetPluginVariable, IsPluginInstalled, Note, SetVariable, _G, assert, check, getfenv, load, loadstring, module, print, require, setfenv, setmetatable, type, unpack = CallPlugin, ColourNote, GetPluginInfo, GetPluginName, GetPluginVariable, IsPluginInstalled, Note, SetVariable, _G, assert, check, getfenv, load, loadstring, module, print, require, setfenv, setmetatable, type, unpack
Now you follow that by:
-- hide all except non-local variables
module (...)
And you are all set! It isn't perfect, in particular I assumed my coding style where I don't put a space between things like: string . format
Also, it doesn't notice comments, so if a function appeared only in a comment, it would still be entered in the list.
However it is an interesting illustration of using Lua to parse some Lua source and pull out useful things. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|