title: How to detect if a userdata has "environment table"?
body: <p>In Lua 5.1, one can associate an <em>environment</em> table with a userdata. This lets us add "fields" to individual userdatas.</p>
  
  <p>The obvious approach would be to create this environment table, possibly an empty one, <em>right when you create the userdata</em>. 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.</p>
  
  <p>So, instead, I thought of creating the environment table only when I detect that the userdata doesn't already have one created by me.</p>
  
  <p>The problem is that in Lua 5.1 the default environment table isn't <code>nil</code>. It's supposedly the global table, <code>_G</code> (I wonder how on earth this is useful). So, supposedly, I'd test for a non-initialized userdata by doing:</p>
  
  <pre class="lang-c prettyprint-override"><code>/* 'index' is where my userdata is */
  lua_getfenv(L, index);
  initialized = lua_rawequal(L, index, LUA_GLOBALSINDEX);
  </code></pre>
  
  <p>Now, my question:</p>
  
  <p>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 <strong>absolutely any</strong> scenario?</p>