C + + Gets the table data from the stack on the LUA stack.
Copy Code code as follows:
Map<string,string> traverse_table (lua_state *l, int index)
{
Map<string,string> data;
Lua_pushnil (L);
Now the stack: -1 => nil; Index => Table
index = index-1;
while (Lua_next (L, index))
{
Now stack: -1 => value; -2 => Key; Index => Table
Copying a key to the top of the stack and then doing lua_tostring on it will not change the original key value.
Lua_pushvalue (L,-2);
Now the stack: -1 => key; -2 => value; -3 => Key; Index => Table
Const char* key = Lua_tostring (L,-1);
Const char* value = lua_tostring (L,-2);
Data[key]=value;
Eject the value and copy key, leaving the original key as the next Lua_next parameter
Lua_pop (L, 2);
Now the stack: -1 => key; Index => Table
}
Now stack: Index => table (the last Lua_next returns 0 when it has already popped the last key left)
So the stack has been restored to its state when it enters this function.
return data;
}