I'm not sure if what I'm going to say it's correct, but from the
manual "The environment of the running C function is always at
pseudo-index LUA_ENVIRONINDEX" I think that the lua_environindex is
only accessible when your function gets called from lua, and not when
you are creating a new state. For example:
int test(lua_State *L) {
lua_newtable(L);
lua_replace(L, LUA_ENVIRONINDEX);
return 0;
}
int main(int argc, char* argv[])
{
lua_State * L = luaL_newstate();
luaL_openlibs(L);
luaL_dostring(L, "print(_VERSION)"); // Lua 5.1
lua_pushcfunction(L,test);
lua_setglobal(L, "test");
luaL_dostring(L, "test()");
lua_close(L);
return 0;
}