[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: source for functions
- From: Stephan Herrmann <stephan@...>
- Date: 12 Dec 1997 11:04:06 +0100
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