title: Reading Lua Table from C++
body: <p>I have tried many alternatives for this simple thing but could not get it work. I want user to define a table from Lua in the 1st step:</p>
  
  <pre><code>a={["something"]=10} -- key=something, value=10
  </code></pre>
  
  <p>Then, in the second step the user will call from Lua a function designed in C++:</p>
  
  <pre><code>b=afunction(a) -- afunction will be designed in C++
  </code></pre>
  
  <p>The C++ code:</p>
  
  <pre><code>int lua_afunction(lua_State* L)
  {
  
     int nargs = lua_gettop(L);
     if(nargs&gt;1) throw "ERROR: Only 1 argument in the form of table must be supplied.";
     int type = lua_type(L, 1);
     if(type!=LUA_TTABLE) throw "ERROR: Argument must be a table";
     //Until here it works as expected
     lua_pushnil(L); //does not work with or without this line
     const char* key=lua_tostring(L,-2);
     double val=lua_tonumber(L,-1);
  
     return 0;
  }
  </code></pre>
  
  <p>As evidenced from the code <code>lua_type(L,1)</code> the bottom of the stack is the table itself. I was assuming on top of the table, the key will reside and on its top the value. So the height of the stack is 3 with idx=-1 the value, idx=-2 the key. However, it seems that I can neither read the key ("something") nor the value (10). Any ideas appreciated.</p>