# \show for Lua commands

TeX (and all newer engines) has the primitive \show which helps me probe macros, and most of the time is enough to understand/remember the implementation (rather than having to go back to the source code).

Is there something similar for Lua commands? For instance, Heiko Oberdiek has written replacements for some pdfTeX primitives in LuaTeX, and I'd like to get more information about them using some hypothetical \showluatexcmd macro.

\input pdftexcmds.sty
\catcode@=11
\show\pdf@strcmp
%\showluatexcmd{oberdiek.pdftexcmds.strcmp}
\bye

-

I seriously doubt that there is something similar for Lua, but you can do the following to find out where the function is defined (to get to the source code).

\directlua{ require("showluatexcmd") }
\def\showluatexcmd#1{\directlua{showluatexcmd.show(#1)}}

\input pdftexcmds.sty
\catcode@=11
\show\pdf@strcmp
\showluatexcmd{oberdiek.pdftexcmds.strcmp}
\bye


and the file showluatexcmd.lua:

module(...,package.seeall)

function show( fun )
local debuginfo = { "source","linedefined","what" }
for _,v in ipairs(debuginfo) do
texio.write_nl(string.format("%20s=%q", v, debug.getinfo(fun)[v]))
end
end


One could extend this probably to directly load the function and display it. But in most cases this won't help as there are a lot of functions that are short and call other functions.

A sample output is

          source="@/opt/texlive2011/texmf-dist/scripts/oberdiek/pdftexcmds.lua"
linedefined="46"
what="Lua" )

-
there is also "lastlinedefined", so you could potentially print a portion of the source file .. –  Taco Hoekwater Feb 1 '12 at 8:36
I was hoping for something similar to Python's dis module. Actually, luac -l may help. Also, what does "what" mean in the debug info? –  Bruno Le Floch Feb 1 '12 at 14:10
@BrunoLeFloch a) what is Python's dis module? b) What describes where the function is defined: a "C" function or a "Lua" function (I remember seeing other things written there) –  topskip Feb 1 '12 at 14:29
@Patrick: sorry, the dis module is a disassembler: dis.dis(myfunction) will print (pseudo-)assembly code for myfunction. It seems luac -l does that too, but on compiled Lua files (see the well-hidden link in my previous comment), so I'm not sure whether it could be used for LuaTeX. –  Bruno Le Floch Feb 1 '12 at 14:39