Re: Special treatment for TValue in structs

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 02 Sep 2012 15:25:01 -0700

But now you are mixing internals with externals.

My understanding is that luajit vm makes everything possible not to expose it's internals to any code that it's running. This allows the VM to make certain assumptions, and not
verify, fix itself on every possible corner (if that is ever possible).

Now if you want to somehow deal with TValue, and the luajit API, then you should start
a second luajit vm instance from your code, and run there.

e.g.

1. luajitvm1 (No one can touch this guy here)
2. boot.lua (started by luajitvm1, this can touch luajitvm2 State, through the legal API's)
3. luajitvm2 (started by boot.lua as an ffi)

This way you can have your little "boot.lua" process create a second luajit (luajitvm2), and then using the API's construct what is allowed. I'm not sure even then that what you
want is possible, but at least you have to go to this step.

But maybe I'm getting this wrong, and not understanding you.

On 9/2/2012 12:28 PM, Joshua Day wrote:
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: