Re: Possible to store a reference to a lua table in an ffi struct?

  • From: Evan Wies <evan@xxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sat, 22 Mar 2014 17:50:36 -0400

On 3/22/14, 2:16 AM, demetri wrote:
One bit of elaboration: if you care you can make a C struct tableref {int uid;} and attach a metatype to it to give you some syntactic convenience: mystruct.member_tableref.foo will look up "foo" through the __index method of the tableref's metatype (i.e. looking up the table through uid2tbl and then getting tbl["foo"]).

Demetri
Using that same technique, you can store just the vector<string>-replacement table in the metatable. The downside of this approach is that you will need a different metatype/metatable for each instance (unless the intention is to share this vector<string> among among instances, i.e. a C++ static class member).

For example:
t = ffi.metatype( "struct { int a, b, c; }", { __index = { vstr = {} } })
foo = t()
foo.vstr[#foo.vstr+1] = "string1"

-Evan


Other related posts: