lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm looking for a way to get the source string of a function from Lua, but I'd rather not have to parse it myself.
 
Using the debug library, I see that I could get the source filename, and line numbers of where the function was defined...But I was hoping there was a way for me to ask the debug library to give me exactly the string that was used to define the function, and nothing more.  This would only be for named functions.
 
Example:
 
-- bob.lua
--
return {
    name = "Bob",
    _OnClick_ = function(self) print("You clicked on "..self.name) end,
}
-- EOF
 
I want to do this:
 
local bob = dofile("bob.lua")
 
print(debug.getinfo(bob.OnClick).string)
 
>> function(self) print("You clicked on "..self.name) end
 
Since this exact functionality does not seem to exist in the debug library, I'm looking at adding it, but I wanted to ask here if someone already has something like this, or a parser that can get this information.
 
This is for an object editor, where the user can load, edit , and save the code associated with objects....Edit, and Save are trivial.  Loading is what I'm asking about.  Worst case is I could store the function as a string, but I'd rather not.
 
Brian