5

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.

5 Answers 5

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)) 
Sign up to request clarification or add additional context in comments.

Comments

4

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

1 Comment

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.
4
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

Comments

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).

3 Comments

Thanks. Are those supposed to be links to somewhere?
Yes, they are links to the appropriate sections of the online Lua reference manual.
I'll try with debug.getinfo. Thanks!
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...

4 Comments

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.
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.
@Norman Ramsey: I'd like to know more about that "metatada in modules" you talk about. Do you have any sample/documentation?
@Norman Ramsey: I made a different question for this: stackoverflow.com/questions/3100062/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.