lua-users home
lua-l archive

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


Asko Kauppi wrote:
> http://www.lua.org/manual/5.1/manual.html#lua_isfunction
> 
> 
> Joey Mukherjee kirjoitti 31.7.2008 kello 22:55:
> 
>> Is there a way from the C API to determine if a Lua function is
>> defined? 
>> 
>> If a Lua function is defined, I wish to call it, otherwise I want to
>> default to some other behavior, but it is not really an error.
>> 
>> How would I do something like this?  From lua_pcall, I get a return
>> value of 2 but is this defined somewhere?  Should I just check for
>> that value or is there a preferred constant?
>> 
>> Thanks,
>> Joey

This requires the function be on the stack and that you know the index,
though.

If you don't and it's a global function you can get it to the top of the
stack through lua_getglobal.
http://www.lua.org/manual/5.1/manual.html#lua_getglobal. If it's a local or
an upvalue and not presently on the stack you can still get to it but it
requires some iterating and being at the proper stack level (through
lua_getstack or from a debug hook function).