At work, I encountered the need for Lua to interact with C++, and found that there are many knowledge points that I missed before, so I will add them one by one.
Since it was written during off-duty hours, the code example is not posted, and I will add it when I have time
Table of contents
1. The meaning of lua's virtual stack subscript
2.1 void lua_settable (lua_State *L, int index);
2.2 void lua_gettable (lua_State *L, int index);
3 lua_setfield, lua_getfield analysis
3.1 void lua_setfield(lua_State *L, int index, string key)
4 lua_pcall(lua_State *L,int nargs,int nresults,int errfunc)
7. C++ and Lua pass userdata parameters
1. The meaning of lua's virtual stack subscript
Lua implements a virtual stack, and exchange data with C through this virtual stack. Accessing this virtual stack also uses subscripts. Integers 1, 2, 3, 4 represent counting from the bottom of the stack, negative numbers -1, -2, -3, -4 represent counting from the top of the stack
2、lua_settable、lua_gettable解析
2.1 void lua_settable (lua_State *L, int index);
Do an operation equivalent to t[k] = v, where t is the value at a given effective index index, v refers to the value at the top of the stack, and k is the value below the top of the stack. This function pops both the key and the value from the stack.
2.2 void lua_gettable (lua_State *L, int index);
Use the top of the stack as the key to get the value in the table of the index index.
Generally, the key should be placed on the top of the virtual stack before use. Use with lua_pushstring, lua_pushnumber, etc.
3 lua_setfield, lua_getfield analysis
3.1 void lua_setfield(lua_State *L, int index, string key)
Do an operation equivalent to t[k] = v, where t is the value at a given effective index index, v is the value at the top of the stack, and k is the parameter key
Equivalent to
lua_pushstring(L, "key");
lua_pushstring(L, "value");
Llua_settable(L, -2)
3.2 lua_getfield to be added
void lua_getfield(lua_State *L, int index, string key)
Get the value of t[key], t is the table whose index is index in the virtual stack, named t
4 lua_pcall(lua_State *L,int nargs,int nresults,int errfunc)
nargs 参数个数
nresults 返回值个数
errFunc 错误处理函数,0表示无,表示错误处理函数在栈中的索引
After using lua_pcall, if there is a return value, it will save the return value to the top of the stack
5、lua_rawgeti、lua_rawset
lua_rawgeti
* Obtain the corresponding subscript element from the table
void lua_rawgeti(lua_State *L, int index, int key)
index indicates the position of the table in the stack
key indicates the position of the element in the table
The result is stored on top of the stack
lua_rawset
* Put the value in the corresponding subscript position of the table
void lua_rawseti(lua_State *L, int index, int key)
index indicates the position of the table in the stack
key indicates the position of the element in the table
The difference between lua_rawgeti and lua_getfield
Presumably through the parameters of the two also know the difference. lua_getfield is a string key, lua_rawgeti is a numeric key
6 Lua returns table value
How to traverse when c++ calls lua function and the return value is table?
Reference Blog: Lua Study Notes 1_Isaf's Blog - CSDN Blog
lua_pushnill(L);
while(lua_next(L, -2))
{
//这时值在-1(栈顶)处,key在-2处。
lua_pop(L, 1);//把栈顶的值移出栈,让key成为栈顶以便继续遍历
}
Here we focus on lua_next. Its operation is like this, first judge the value of the previous key (this value is placed on the top of the stack, if it is nil, it means that the value of the first element in the table is currently fetched), and then calculate the current key, at this time First pop the top of the stack out, push the new key into the stack, and finally push the value corresponding to the new key into the stack. In this way, the top of the stack is the value of the first traversed element in the table. After using this value, we need to pop this value out of the stack, so that the key is on the top of the stack to continue traversing. When the next key value cannot be calculated based on the previous key value, lua_next returns 0, ending the loop
7. C++ and Lua pass userdata parameters
lua returns userdata:
https://www.javaroad.cn/questions/325604
c pass userdata to lua
lua_pushlightuserdata
void lua_pushlightuserdata (lua_State *L, void *p)