Re: no custom metamethods for cdata

  • From: Richard Hundt <richardhundt@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 27 Mar 2014 21:35:27 +0100

On 2/28/14 5:34 PM, Evan Wies wrote:
>
> Generalized, this let's you attach Lua tables to cdata, which is kinda
> neat.  I'm not sure the performance impact of lots of cdata with their
> own metatables -- I tend to create one "class" and then effectively
> have static data members (in C++ parlance)...  But, you can do it:
>

Passing ctypes to `tonumber()` gives you their internal ID as a plain
ol' Lua number. I discovered this accidentally, so not sure if it's
documented and here to stay, but that makes it really easy to keep
metadata in an external table and pull it out with:

cdata_meta[tonumber(ffi.typeof(val))]

So a serializer could check if a ctype has registered a hook there
without the risk of raising an error due to an invalid member access.


Other related posts: