4

I'm generating some (non-html) documentation for a Lua library that I developed. I will generate the documentation by hand, but I'd appreciate some kind of automation if possible (i.e. generating skeletons for each function so I can fill them in)

I'd like to know if there's a way for lua to know the names of the parameters that a function takes, from outside it.

For example, is there a way to do this in Lua?

function foo(x,y)
  ... -- any code here
end

print( something ... foo ... something)
-- expected output: "x", "y"

Thanks a lot.

kikito
  • 51,734
  • 32
  • 149
  • 189

5 Answers5

9

ok,here is the core code:

function getArgs(fun)
local args = {}
local hook = debug.gethook()

local argHook = function( ... )
    local info = debug.getinfo(3)
    if 'pcall' ~= info.name then return end

    for i = 1, math.huge do
        local name, value = debug.getlocal(2, i)
        if '(*temporary)' == name then
            debug.sethook(hook)
            error('')
            return
        end
        table.insert(args,name)
    end
end

debug.sethook(argHook, "c")
pcall(fun)

return args
end

and you can use like this:

print(getArgs(fun))
Mike Laren
  • 8,028
  • 17
  • 51
  • 70
abner
  • 91
  • 1
  • 1
4

Try my bytecode inspector library. In Lua 5.2 you'll be able to use debug.getlocal.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • I think I'll stick with lua's debug functionalities for now. But thanks for your answer. I think it can be useful for other people finding this question. – kikito Jun 23 '10 at 08:01
3

Take a look at debug.getinfo, but you probably need a parser for this task. I don't know of any way to fetch the parameters of a function from within Lua without actually running the function and inspecting its environment table (see debug.debug and debug.getlocal).

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
3
function GetArgs(func)
    local args = {}
    for i = 1, debug.getinfo(func).nparams, 1 do
        table.insert(args, debug.getlocal(func, i));
    end
    return args;
end

function a(bc, de, fg)
end

for k, v in pairs(GetArgs(a)) do
  print(k, v)
end

will print

1   bc
2   de
3   fg

Basically we use debug.getinfo to retrieve the nparams attribute (which gives us the information of how many parameters the function takes) and debug.getlocal to access the name of the parameters.

Tested and working with Lua 5.4

Sygmei
  • 467
  • 2
  • 10
1

Take a look at the luadoc utility. It is sort of like Doxygen, but for Lua. It is intended to allow the documentation to be written in-line with the source code, but it could certainly be used to produce a template of the documentation structure to be fleshed out separately. Of course, the template mechanism will leave you with a maintenance issue down the road...

RBerteig
  • 41,948
  • 7
  • 88
  • 128
  • My experiences with `luadoc` have been poor. I've instead been writing documentation as metadata within modules. For the poster's question, I think lhf's answer is definitive. – Norman Ramsey Jun 23 '10 at 01:52
  • Luadoc is, um, quirky for sure. Lua doesn't make it easy to get good documentation signatures for functions, and most of luadoc's quirks relate to that. I've had related problems with documentation for Lua libraries written in C where Doxygen is no help at all since all Lua-callable functions have the same opaque signature. – RBerteig Jun 23 '10 at 01:57
  • @Norman Ramsey: I'd like to know more about that "metatada in modules" you talk about. Do you have any sample/documentation? – kikito Jun 23 '10 at 08:02
  • 1
    @Norman Ramsey: I made a different question for this: http://stackoverflow.com/questions/3100062/lua-metadata-for-documentation – kikito Jun 23 '10 at 08:31