lua-users home
lua-l archive

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


On Fri, 2004-10-29 at 12:27 +0800, Brett Bibby wrote:
> Now the stack has a function on it, but I want to get the string "f1234" 
> instead.  Is this possible?

In Lua, the code would look like:

target = Foo
fname = ""

for name,value in pairs(_G) do
  if string.find(name, "^f%d+$") then
    if value == target then
       fname = name
       break
    end
  end
end

print("The fNNN name is:", fname)

So just create an equivalent in C... *or*...

-- Once at start...

_FuncToName = {}

for name,value in pairs(_G) do
  if string.find(name, "^f%d+$") and type(value) == "function" then
    _FuncToName[value] = name
  end
end

Next you can do:

lua_pushstring(L, "_FuncToName");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pushstring(L, "Foo");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_gettable(L, -2)

Of course this is all pseudocode since I've not tested a single byte of
it :-)

D.

-- 
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 2BC8 4016 2068 7895