[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Did they mean "." or ":"?
- From: Peter Cawley <lua@...>
- Date: Wed, 17 Feb 2010 00:18:14 +0000
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.