title: Why is Lua returning an empty table in this code?
body: <p>edit:
  As I commented before, my problem is I can't create a table in C which uses strings as keys. I made a quick test program which demonstrates the problem I'm having:</p>
  
  <p>Here is the C++ part of it:</p>
  
  <pre><code>#include &lt;lua.hpp&gt;
  #include &lt;String.h&gt;
  
  BString
  LuaTypeToString(lua_State *L, int index, int type)
  {
      BString out;
      switch (type)
      {
          case LUA_TSTRING:
          {
              out &lt;&lt; "'" &lt;&lt; lua_tostring(L, index) &lt;&lt; "'";
              break;
          }
          case LUA_TBOOLEAN:
          {
              out &lt;&lt; (lua_toboolean(L, index) ? "true" : "false");
              break;
          }
          case LUA_TNUMBER:
          {
              out &lt;&lt; (float)lua_tonumber(L, index);
              break;
          }
          default:
          {
              out &lt;&lt; lua_typename(L, type);
              break;
          }
      }
      return out;
  }
  
  
  void
  DumpLuaTable(lua_State *L, int tableIndex)
  {
      lua_pushnil(L);
      printf("\t{ ");
      while (lua_next(L, tableIndex) != 0)
      {
          BString keyString = lua_tostring(L, -2);
  
          BString valueString;
          int type = lua_type(L, -1);
          if (type == LUA_TTABLE)
              DumpLuaTable(L, lua_gettop(L));
          else
              valueString = LuaTypeToString(L, -1, type);
  
          printf("%s=%s,",
              keyString.String(),
              valueString.String());
          lua_pop(L, 1);
  
          if (lua_isnumber(L, -1))
          {
              lua_pop(L, 1);
              break;
          }
      }
      printf(" }");
  }
  
  
  void
  DumpLuaStack(lua_State *L)
  {
      printf("DumpLuaStack:\n");
      int top = lua_gettop(L);
      for (int i = 1; i &lt;= top; i++)
      {
          int type = lua_type(L, i);
          if (type == LUA_TTABLE)
              DumpLuaTable(L, i);
          else
              printf("\t%s  ", LuaTypeToString(L, i, type).String());
      }
      printf("\n");
  }
  
  
  
  static
  int
  ReturnTable(lua_State *L)
  {
      lua_newtable(L);
      lua_pushnumber(L, 3.14);
      lua_pushstring(L, "1");
      lua_settable(L, -3);
  
      DumpLuaStack(L);
  
      return 1;
  }
  
  
  int
  main(void)
  {
      lua_State *L = luaL_newstate();
      luaL_openlibs(L);
  
      lua_pushcfunction(L, ReturnTable);
      lua_setglobal(L, "ReturnTable");
  
      int status = luaL_dofile(L, "luatest.lua");
      if (status)
          printf("Status = %d: %s\n", status, lua_tostring(L, -1));
  
      lua_close(L);
  
      return status;
  }
  </code></pre>
  
  <p>And the Lua part of it:</p>
  
  <pre><code>function DumpTable(table)
      print("LuaDumpTable")
      local out = "\t"
      if #table &gt; 0 then
          for i, v in ipairs(table) do
            out = out .. i .. "=" .. v .. " "
          end
          print(out)
      else
          print("\tEmpty table")
      end
  end
  
  value = ReturnTable()
  if (type(value) == "table") then
      DumpTable(value)
  else
      print(type(value))
  end
  </code></pre>
  
  <p>The code as it is works without problem -- the Lua script prints out what you would expect. Change the line which pushes 3.14 to the stack to, say, <code>lua_pushstring(L, "foo");</code> and all I get is an empty table on the Lua side. Any ideas what I'm doing wrong here?</p>