Re: ffi.getmetatable

  • From: Ronan Collobert <ronan@xxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 26 Nov 2014 13:48:37 -0800

Yes, thanks, I do know — but then you have to assume all cdata types have this 
field, or it will fail at query (and you probably do not want to add a 
metatable on simple cdata types).

Of course you can use a pcall(), but the JIT will bail out and return to 
interpreter mode, in case of failure… which is not an option for me.

Ronan.


> On Nov 26, 2014, at 1:26 PM, Peter Colberg <peter@xxxxxxxxxxx> wrote:
> 
> On Wed, Nov 26, 2014 at 11:51:07AM -0800, Ronan Collobert wrote:
>> More generally when one hides something in the metatable (a global
>> property of the “class” of a given object), right now one cannot
>> retrieve it easily. Once in while, the case occurs, someone
>> complains to me, and one has to play with the
>> tonumber(ffi.typeof())+additional table trick.
> 
> Did you know this feature of ctypes?
> 
>  local ffi = require("ffi")
>  local ctype = ffi.metatype("struct { int i; }", {
>    __index = {
>      class = "MyClass",
>      size = function(self)
>        return ffi.sizeof(self)
>      end,
>    }
>  })
> 
>  assert(ctype.class == "MyClass")
>  assert(ctype:size() == 4)
> 
>  local obj = ctype(123)
>  local objtype = ffi.typeof(obj)
>  assert(objtype.class == "MyClass")
>  assert(objtype:size() == 4)
> 
> That is the X to your Y :-).
> 
> Peter
> 

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Other related posts: