lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

A bit of a solution to my own problem:

I can check if two states are compatible enough to "lua_xmove" using the "lua_topointer" method of both states. Given

     lua_State* leftL, lua_State* rightL

I can check if they both originate from the same place by checking the "void*" pointer value of their registries:

     const void* left_registry = lua_topointer(leftL, LUA_REGISTRYINDEX);
     const void* right_registry = lua_topointer(rightL, LUA_REGISTRYINDEX);

     return left_registry == right_registry;

This doesn't let me figure out if a "lua_State*" is the "main" one, but it does let me determine if they are disparate states (e.g., two completely different states originating from a "luaL_newstate()" call or similar). The only thing left to do is also add a check for if they're NULL or if they are equal beforehand, and that way I can tell if they are "lua_xmove"-able. I won't know about how "safe" they are: e.g., if someone blows up the thread from a coroutine and my lua_CFunction was called from that coroutine and I didn't get the main thread off the `lua_State*` handed to me, calls on C API functions with that state will crash horrifically (LuaJIT, strangely enough, does not suffer this issue).

Sorry for clogging the list with my question, when the solution was right there the whole time!


On Tue, Sep 12, 2017 at 9:03 PM, ThePhD <> wrote:
In my C program, I have multiple states. One of my functions check is if a lua reference struct I have (created with "luaL_ref") has the same state as another, and if not performs a "lua_xmove".

My problem becomes when my function is handed 2 independent "lua_State*"s that each came from their own "luaL_newstate()" (or similar) call. Is there anything about a specific "lua_State" I can use to find out if it's the top level state (not a thread)? Right now, what I'm doing is getting the main thread using a `LUA_RIDX_MAINTHREAD` call and seeing if it compares equal for both (then I know they came from the same state), but now I have to deal with Lua 5.1 and I don't have that capability.

I've seen the threads about storing the "main state" somewhere on that state: do I have any other options? Is there any way to tell if two "lua_State"s are disparate?