lua-users home
lua-l archive

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


On Wed, Feb 17, 2010 at 12:05 AM, Christopher Eykamp <chris@eykamp.com> wrote:
> Is there a way from C++ to figure out if a function was called using "." as
> opposed to ":"?

In plain Lua, it might be done like:

function how(s, x) return debug.getinfo(1).namewhat == "method" and
":" or "." end
t = {how = how}
print(t.how()) --> .
print(t:how()) --> :

Translating the above into C shouldn't be too hard - just call
lua_getstack() and inspect namewhat. If namewhat is "method" or
"field", then : or . was used respectively. Other values for namewhat
do not help you (for example, if you tailcall t.how() or t:how() then
the call frame is lost, and namewhat will be ""). Note that this is a
debug operation and thus expensive, so only do it once you know the
first parameter is invalid.