LuaAnd CData ExchangeThe problem solution is the content to be introduced in this article, mainly to understand and learn aboutLUAAnd C ++Data ExchangeFor more information, see this article.
Data Exchange
1. The Lua and C Programs exchange data through a stack: struct lua_State
2. The stack sequence number can be counted from the top and bottom of the stack, and from the bottom of the stack, the bottom of the stack is 1, increasing toward the top of the stack. From the top of the stack, the top of the stack is-1, and the direction toward the bottom of the stack is decreasing. It is generally counted from the top of the stack. The default stack size is 20. You can use lua_checkstack to modify the stack. Use lua_gettop to obtain the number of elements in the stack. It does not mean that there is an integer element at the top of the stack. Instead, it calculates the positive index of the top element of the stack in the stack, which is equivalent to the number of elements.
3. The stack used by Lua to call the C function is temporary and destroyed after the call is completed.
4. How to obtainLuaParameters in the script
1) If you know the name of a global variable in the Lua script, you can use void lua_getglobal (lua_State * L, const char * name ). This function places the value of the Lua variable referred to by name on the top of the stack.
2) If you want to obtain the parameters used by Lua to call the function in the C function:
1. First Use lua_gettop to check the number of parameters
2. Use the lua_is... class function to detect the parameter type and handle errors.
3. Use the lua_to... class function to convert the parameter to number or string. (For Lua, there are only two simple types)
Lua_tonumber returns double
Lua_tostring returns char *
4. Use lua_remove to delete elements from the stack
5. Continue to obtain the next element. Because lua_remove is called every time, the index used for each call of lua_tonumber is fixed to-1, that is, the top of the stack.
6. If lua_istable is set up, it indicates that the top of the stack is a table. Note that the table cannot be retrieved, and only the elements in the table can be retrieved one by one.
First, press the element name to the top of the stack: lua_pushstring (L, "I"); then you can call it with lua_gettable, and the value will be placed on the top of the stack. At the same time, the name of the pressed element is displayed. In the above method, we can obtain this value. Remember to use lua_remove. If one element of a table is also a table, repeat it. When all the elements of a table are finished, remember that the table is still in the stack and delete it with lua_remove.
7. If you want to obtain an array (the so-called array is actually the table where the key is the sequence of numbers starting from 1 and the value type is the same), you can use lua_next to traverse the array:
First, press lua_pushnil into a null value, and then
- While (lua_next (L,-2 )! = 0)
- {
- If (lua_isnumber (L,-1) // you can determine the element type, which may be a string.
- {
- Arrf. add (float) lua_tonumber (L,-1); // obtain the element value
-
- Lua_remove (L,-1 );
- }
- }
- Lua_remove (L,-1); // Delete NIL5. how to return data from C to Lua script
Use the lua_push... class function to push data into the stack and return n; To tell Lua to return several values. Lua supports multiple return values, such as x, y = Test (). Lua extracts data from the stack based on n.
If you want to return a table:
- Lua_newtable (L); // create a table and place it on the top of the stack.
-
- Lua_pushstring (L, "mydata"); // press the key
- Lua_pushnumber (L, 66); // press value
- Lua_settable (L,-3); // the key and value are displayed and set to the table.
-
- Lua_pushstring (L, "subdata"); // press the key
- Lua_newtable (L); // press value, which is also a table
- Lua_pushstring (L, "mydata"); // press the key of the subtable
- Lua_pushnumber (L, 53); // value
- Lua_settable (L,-3); // the key and value are displayed and set to subtable.
- Lua_settable (L,-3); // at this time, the location of the parent table is-3. The key, value (subtable) is displayed and set it to the table.
-
- Lua_pushstring (L, "mydata2"); // same as above
- Lua_pushnumber (L, 77 );
- Lua_settable (L,-3 );
Return 1; // a table is now in the stack. All the other tables are popped up.
If you want to return an array, use the following code: (pay attention to the trick comment. I am waiting for the official explanation. After verification, this problem only occurs when the dll method is called in windows. Normal WinCE)
- lua_pushstring(L,"arri");
- lua_newtable(L);
- {
- //a trick:otherwise the lua engine will crash. This element is invisible in Lua script
- lua_pushnumber(L,-1);
- lua_rawseti(L,-2,0);
- for(int i = 0; i < arri.size();i++)
- {
- lua_pushnumber(L,arri[i]);
- lua_rawseti(L,-2,i+1);
- }
- }
Lua_settable (L,-3 );LuaAs follows:
- For I, v in ipairs (data. arri) do
- Print (v)
- End
-
- Or
-
- For I = 1, table. getn (data. arri) do
- Print (data. arri [I])
- End
Only the array can do this. The Record consisting of name and value cannot, and table. getn is only valid for the array.
8. Because of the high similarity of the above Code, it is easy to automatically generate the code. For example, according to a struct definition in C:
- typedef enum
- {
- BR_9600,
- BR_4800,
- } BaudRate;
-
- typedef struct flag
- {
- int onoff;
- int j;
- long l;
- double d;
- char* name;
- BaudRate rate;
- }flag;
The following code is automatically generated:
- bool DataToLua(flag data,lua_State *L)
- {
- lua_newtable(L);
- lua_pushstring(L,"onoff");
- lua_pushnumber(L,(double)data.onoff);
- lua_settable(L,-3);
- lua_pushstring(L,"j");
- lua_pushnumber(L,(double)data.j);
- lua_settable(L,-3);
- lua_pushstring(L,"l");
- lua_pushnumber(L,(double)data.l);
- lua_settable(L,-3);
- lua_pushstring(L,"d");
- lua_pushnumber(L,(double)data.d);
- lua_settable(L,-3);
- lua_pushstring(L,"name");
- lua_pushstring(L,data.name.c_str());
- lua_settable(L,-3);
- lua_pushstring(L,"rate");
- lua_pushnumber(L,(double)(int)data.rate);
- lua_settable(L,-3);
- return true;
- }
LuaToData is similar.
If you use the object-oriented method to encapsulate the flag, it is more convenient to convert the DataToLua into a flag class method.
Summary:LuaAnd CData ExchangeThe content of the problem solution has been introduced. I hope you can learn this article to help you!