Re: ffi.getmetatable

  • From: Karel Tuma <kat@xxxxxx>
  • To: luajit <luajit@xxxxxxxxxxxxx>
  • Date: Thu, 27 Nov 2014 21:14:38 +0100

Excerpts from Ronan Collobert's message of 2014-11-22 00:23:45 +0100:
> Hi,
> 
>   At this time there is no (easy) way with luajit+ffi to get the metatable of 
> a cdata/ctype. I understand the notion of metatable is ambiguous here, as the 
> metatable given by the user through ffi.metatype() is not the actual one on 
> the cdata (a global ffi-specific metatable that can be obtained by 
> debug.getmetatable).
> 
> Reading the mailing list, I saw several people encountered the same issue 
> (see for e.g. https://github.com/corsix/ffi-reflect and reflect.getmetatable).
> 
> Do you think it would be reasonable to have a ffi.getmetatable(ctype) 
> function, which returns the original “metatable” given by user through 
> ffi.metatype? (code in attachment) — If yes, I can work on the compiled 
> version. I sincerely feel this is missing in the current ffi lib.

> --- a/src/lib_ffi.c
> +++ b/src/lib_ffi.c

Dubious use cases aside, what about:

assert(ffi.miscmap[-tonumber(ffi.metatype("struct {int 
x;}",{test=333}))].test==333)

Single line change, and JIT tracer friendly:

https://github.com/katlogic/ljx/commit/af7f5e8726a1d1608a711344a8742cb3873fae1f

Other related posts: