In Lua 5.1, one can associate an environment table with a userdata. This lets us add "fields" to individual userdatas.
The obvious approach would be to create this environment table, possibly an empty one, right when you create the userdata. However, in my application there'll be many userdatas that won't necessarily need this environment table and they come and go out of existence rapidly. I don't want the overhead of creating so many temporary empty tables that won't be used.
So, instead, I thought of creating the environment table only when I detect that the userdata doesn't already have one created by me.
The problem is that in Lua 5.1 the default environment table isn't nil. It's supposedly the global table, _G (I wonder how on earth this is useful). So, supposedly, I'd test for a non-initialized userdata by doing:
/* 'index' is where my userdata is */
lua_getfenv(L, index);
initialized = lua_rawequal(L, index, LUA_GLOBALSINDEX);
Now, my question:
Do I need to use LUA_ENVIRONINDEX, or LUA_GLOBALSINDEX? Or do I need to do something else? Is this test going to work in absolutely any scenario?