Paralife
Paralife

Reputation: 6236

In Lua, is there a function that given a function, it returns its name as a string?

Sorry if this is too obvious, but I am a total newcomer to lua, and I can't find it in the reference.

Is there a NAME_OF_FUNCTION function in Lua, that given a function gives me its name so that I can index a table with it? Reason I want this is that I want to do something like this:

local M = {}

local function export(...)
   for x in ...
     M[NAME_OF_FUNCTION(x)] = x
   end
end

local function fun1(...)
...
end

local function fun2(...)
...
end

.
.
.

export(fun1, fun2, ...)

return M

Upvotes: 5

Views: 492

Answers (4)

MattJ
MattJ

Reputation: 7924

Technically this is possible, here's an implementation of the export() function:

function export(...)
        local env = getfenv(2);
        local funcs = {...};
        for i=1, select("#", ...) do
                local func = funcs[i];
                for local_index = 1, math.huge do
                        local local_name, local_value = debug.getlocal(2, local_index);
                        if not local_name then
                                break;
                        end
                        if local_value == func then
                                env[local_name] = local_value;
                                break;
                        end
                end
        end
        return env;
end

It uses the debug API, would require some changes for Lua 5.2, and finally I don't necessarily endorse it as a good way to write modules, I'm just answering the question quite literally.

Upvotes: 1

jpjacobs
jpjacobs

Reputation: 9549

There simply is no such function. I guess there is no such function, as functions are first class citizens. So a function is just a value like any other, referenced to by variable. Hence the NAME_OF_FUNCTION function wouldn't be very useful, as the same function can have many variable pointing to it, or none.

You could emulate one for global functions, or functions in a table by looping through the table (arbitrary or _G), checking if the value equals x. If so you have found the function name.

a=function() print"fun a" end
b=function() print"fun b" end
t={
   a=a,
   c=b
}
function NameOfFunctionIn(fun,t) --returns the name of a function pointed to by fun in table t
   for k,v in pairs(t) do
       if v==fun then return k end
   end
end
print(NameOfFunctionIn(a,t)) -- prints a, in t
print(NameOfFunctionIn(b,t)) -- prints c
print(NameOfFunctionIn(b,_G)) -- prints b, because b in the global table is b. Kind of a NOOP here really.

Another approach would be to wrap functions in a table, and have a metatable set up that calls the function, like this:

fun1={
    fun=function(self,...)
        print("Hello from "..self.name)
        print("Arguments received:")
        for k,v in pairs{...} do print(k,v) end
    end,
    name="fun1"
}
fun_mt={
    __call=function(t,...)
        t.fun(t,...)
    end,
    __tostring=function(t)
        return t.name
    end
}
setmetatable(fun1,fun_mt)
fun1('foo')
print(fun1) -- or print(tostring(fun1))

This will be a bit slower than using bare functions because of the metatable lookup. And it will not prevent anyone from changing the name of the function in the state, changing the name of the function in the table containing it, changing the function, etc etc, so it's not tamper proof. You could also strip the tables of just by indexing like fun1.fun which might be good if you export it as a module, but you loose the naming and other tricks you could put into the metatable.

Upvotes: 6

fortran
fortran

Reputation: 76057

If I am not wrong (and I probably will, because I actually never programmed in Lua, just read a bunch of papers and articles), internally there is already a table with function names (like locals and globals in Python), so you should be able to perform a reverse-lookup to see what key matches a function reference.

Anyway, just speculating.

But the fact is that looking at your code, you already know the name of the functions, so you are free to construct the table. If you want to be less error prone, it would be easier to use the name of the function to get the function reference (with eval or something like that) than the other way around.

Upvotes: 0

Hybrid
Hybrid

Reputation: 396

Try this:

http://pgl.yoyo.org/luai/i/tostring

tostring( x ) should hopefully be what you are looking for

Upvotes: 0

Related Questions