Special treatment for TValue in structs

  • From: Joshua Day <josh.day@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 2 Sep 2012 15:28:05 -0400

I think it would be handy to treat a TValue in a struct as a first-class Lua
value.  Maybe this isn't a high priority for most people, since it is already
possible to keep Lua values in a weak table and store indices in an ffi
struct, but it does seem like it would make a lot of code cleaner, faster,
and less error-prone.  Here's a simplistic example of what I'm imagining:

    ffi.cdef [[
        typedef struct {
             TValue a, b;
        } pair;
    ]]

    local t = {x = 9}
    local example = ffi.new("pair", t, "string")

    print (example.a.x, example.b) -- 9     string
    example.a, example.b = example, true
    print (example.a.b) -- true

Now, the GC wouldn't follow these links (a string literal is safe, since it's
referenced by the chunk), but that's par for the "no hand-holding" policy.
If you end up in limbo, that's on you.

(It would be great to be able to get the current lua_State or perform a Lua
API call of any function with the right signature, but it's not hard to write
something in C to do that, so I'm less eager to get that.)

--
Joshua

Other related posts: