lua-users home
lua-l archive

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


Hi!

Am 18.02.2010 10:48, schrieb Christopher Eykamp:
> In the example I gave, yes, your test would work.  But
> bot:setAngleToOtherBot(bot2) would fail that test.

I used the attached module to log calls to functions and methods in
userdata. You could do something similar to throw meaningful error
messages, but you might want to disable this when you release and/or at
least add some caching...

> 
> On 2/16/2010 4:11 PM, Wesley Smith wrote:
>>> Is there a way from C++ to figure out if a function was called using
>>> "." as
>>> opposed to ":"?
>>>      
>> What you should be asking instead is how to tell if the object is the
>> first argument in C++ since bot.setAngle(bot, angle) is equivalent to
>> bot:setAngle(angle).  If bot is a userdata that you grab the pointer
>> to, then the check should be trivial.
>>
>> wes
>>    
> 


Philipp

return function( object )
  local proxy = {}
  setmetatable( proxy, {
    __index = function( _, key )
      local real_value = object[ key ]
      if type( real_value ) == "function" then
        return function( self, ... )
          if self == proxy then
            -- method call
            self  = object
            print( key, ":", "self", ... )
          else
            -- function call
            print( key, ":", self, ... )
          end
          return real_value( self, ... )
        end
      else
        return real_value
      end
    end
  } )
  return proxy
end