lua-users home
lua-l archive

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


Hello,

I'm trying to write a helper function for getting function name with
function pointer.
«AH! Recursion with _ENV!» i thought.
Like all simple and good idea, it was not so simple and not so good.

For an unknown reason, it will work with functions directly stored
in _ENV, but not with subtables.

get_fx_name(get_fx_name) -- works
get_fx_name(math.cos) -- thrash stack

Do you have any idea why ? Thank you.


function _get_fx_name(fx, t)
  for k,v in pairs(t) do
    if type(v) == "table" and k ~= "_G" then
      _get_fx_name(fx, v)
    end
    if fx == v then
      return k
    end
  end
end

function get_fx_name(fx)
  local name
  name = _get_fx_name(fx, _ENV)
  name = name or "unknown function"
  return name
end