lua-users home
lua-l archive

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


Some more suggestions, make of them what you will.

------
Option 1
------
My understanding may be wrong, however, it sounds an awful lot like you're
running down the line of thinking that you have to include a "main" function
in your scripts.  In point of fact the script "chunk" can be used almost
exactly like a function itself (albeit with differences in how it can accept
parameters, which is to say that named parameters aren't available, only an
"arg table").  If this is at all acceptable to you then you don't have to
"find the function in the chunk" as you could just use the chunk/function
itself to index into a table of extra information, or to call directly.

------
Option 2
------
Another alternative is of course to have the script chunk return the end
function (and even a string name for it if you so desire).

Consider the following....

local function main(x, y, z) do
    return x*y*z
end

return main, "MultThree"


A script containing the above code would return its "main" function along
with a name for that function.  If you ran this script after loading it,
then you would conveniently have a function and a name for it.


------
Option 3
------
Similarly, as you mentioned closures you could code...

local Name = "MultThree"
local function Main(x,y,z) do
    return x*y*z
end

return main

...After which point Name would be available as an upvalue of the function
(OK, I'd have to double check that Lua doesn't optimize this particular
upvalue away given that it wasn't used in the function).


------
Option 4
------
Assuming that the functions within these scripts are declared globally
rather than locally, you could set a _newindex metamethod handler on the
global table before executing these scripts.  As globals are set, you can
make a note of with what script they were associated, what the name of the
global variable was, check the type of the value to see if it is a function,
(maybe add that to the function as an upvalue, store that off in a table,
whatever).


------
Option 5
------
Similar to option 4, but in the case that the functions are declared as
local, you could use the debug interface to enumerate the local variables of
the script chunk.  This you would probably want to do, <after> loading, but
<before> running.



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Brett Bibby
Sent: Friday, March 05, 2004 9:52 PM
To: Lua list
Subject: Re: function name


That may be possible, but the function is sometimes passed in a variable and
so it isn't hardcoded. We have a code preprocessor and it's theoretically
possible, but we would have to detect the difference between by value and by
name intentions, find the function the variable refers to and then replace
it with a string. Messy.  It looks like we should put the effort into
finding the function on the way into lua and then using that as a key into
the registry or something as this will perform very well at runtime.

Or, couldn't I just create a string of the function name as a closure to the
function?

Brett

----- Original Message -----
From: "Kevin Baca" <lualist@aidiastudios.com>
To: "'Lua list'" <lua@bazar2.conectiva.com.br>
Sent: Saturday, March 06, 2004 12:45 AM
Subject: RE: function name


> How about passing the name of the function instead?
>
> function Fooey()
>      SetSomeValueInTheCHost("Foo")
> end
>
> -Kevin
>
> >
> > Ashwin,
> > Thanks for your note.  I read the debug stuff before posting
> > but assumed it wasn't meant for runtime use. I guess I should
> > be more specific.  I need to retrieve a few thousand
> > functions names per second. Under the debug section it says
> > "The lua_getinfo function checks how the function was called
> > or whether it is the value of a global variable to find a
> > suitable name. If it cannot find a name, then name is set to
> > NULL."  So if I have:
> >
> > function Foo()
> >     -- some code here
> > end
> >
> > And then...
> >
> > function Fooey()
> >     SetSomeValueInTheCHost(Foo)
> > end
> >
> > And my C host has...
> >
> > int SetSomeValueInTheCHost(lua_State *luaStatePtr)
> > {
> >     // the first value on the lua stack is a function, how
> > can I get the name fast here?
> >     // candidates include getfuncname or getobjname?
> > }
> >
> > With the decalration as per above, would getfuncname or
> > getobjname retrieve anything useful? Is there a better/faster way?
> >
> > Thanks,
> > Brett
> >
> > ----- Original Message -----
> > From: "Ashwin Hirschi" <deery@operamail.com>
> > To: "Lua list" <lua@bazar2.conectiva.com.br>
> > Sent: Friday, March 05, 2004 10:16 AM
> > Subject: Re: function name
> >
> >
> > >
> > > > Is there anyway to get, find or resolve the string name
> > of a function on the stack?
> > >
> > > Have a look at Lua's "debug interface":
> > >
> > > http://www.lua.org/manual/5.0/manual.html#4
> > >
> > > Ashwin.
> > > --
> > > no signature is a signature
> >