lua-users home
lua-l archive

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


Steve Dekorte <steve@inquisit.com> writes:

> Is there any way to ask Lua for the source code of a function?
> If not, what's the best way to add this feature?
Of course, there is a way, not builtin, but rather through -- guess -- 
tag-methods. Something like:

FunctionTag=newtag("table")

function callFunction (m, ...)
    call (m.func, arg)
end

settagmethod(FunctionTag, "function", callFunction)

function compileFunction (name)
    local descriptor=getglobal(name)
    dostring(descriptor.source)
    descriptor.func=getglobal(name)
    setglobal(name, descriptor)
    settag(descriptor, FunctionTag)
end

-- now you need a table wrapping every function:
myPrint={source="function myPrint (a) print(\"myprint says:\",a) end"}
compileFunction("myPrint")

-- call and print the function:
myPrint("Here we are")
print(myPrint.source)

Of course definition of functions is not straight-forward, but within 
a programming environment this might be wrapped at the user-interface, right?

As question remains: should we be able to access the byte code from within
lua for dumping/undumping etc.?

Does this in some way or other meet your requirements?

Cheers
Stephan