Note that there are some explanatory texts on larger screens.

plurals
  1. POLua: Get function source
    primarykey
    data
    text
    <p>I'm working on a system for serialization/deserialization, and I'm trying to get some really fancy stuff going with functions. My goal is to serialize objects containing functions in a human-readable and reversible manner (the serialized files will need processing after the loadfile() step). I need a way to get the actual source of a function, and it looks like I can't always do that with debug.getinfo(). </p> <p>I know that debug.getinfo() will give be the file and line where it was defined (or the source of the function, depending on its origin). Is there a way I can read the function text from that file? I'd be willing to use some kind of parser utilities to do so. Maybe there are Lua packages for parsing Lua code?</p> <p>Perhaps there's a way I can get loadfile() or require() to automatically retain function source somewhere?</p> <p>Yes, I know you can get all sorts of information out of debug.getinfo, but it was unable to deal with functions loaded through stdin...</p> <pre><code>uberblah@glade-m:~$ lua Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio &gt; a = function() print("hello, world!") end &gt; require("serpent") &gt; s = require("serpent") &gt; =s.block(debug.getinfo(a)) { currentline = -1, func = loadstring("LuaQ\000\000\000\000\000\000\000\000\000=stdin\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000A@\000\000@\000\000�\000\000\000\000\000\000\000\000\000\000\000print\000\000\000\000\000\000\000\000hello, world!\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000",'@serialized') --[[function: 0x2068a30]], lastlinedefined = 1, linedefined = 1, namewhat = "", nups = 0, short_src = "stdin", source = "=stdin", what = "Lua" } --[[table: 0x206cf80]] &gt; f = io.open("stdin", "r") &gt; =f nil </code></pre> <p>SOLUTION FOR SOURCE FROM STDIN... 1) Capture all STDIN, write it to a file 2) Load the information from that file, instead of from stdin 3) debug will track the function line numbers in that file</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload