The previous section talked about some of the basic LUA applications, and below, I would like to highlight some of the concepts of the LUA stack, because this is really important and you will often use it. With Lua, the most important thing is to always know when the data in the stack is in what order. If you are familiar with this, you are already a master of Lua's use. When you initialize a stack, its bottom is 1, and the top of the stack is-1, say the image of some, you can think of the stack as a ring, there is a pointer to mark the current position, if-1, is the current top of the stack, if 2 is the current stack at the top of the previous parameter position. And so on Of course, you can also get it in sequence, note that for many of Lua's APIs, the subscript starts at 1. This is somewhat different from C + +. Moreover, in the subscript of the stack, the positive number represents the absolute bottom of the subscript, negative numbers are relative to the top of the stack relative address, this must have a clear concept, otherwise it is easy to see faint. (How data is stored in the stack, for example)
Note: In fact, from the bottom of the stack to the top of the index value increment .
Here's a look at some specific examples:
Lua_pushnumber (M_pstate, One); Lua_pushnumber (M_pstate, A);//Lua_gettop () This API tells you the current number of elements in the stackintNIn =lua_gettop (m_pstate)intNData1 = Lua_tonumber (M_pstate,1);//reads the elements in the first absolute coordinate of the bottom of a stackintNData2 = Lua_tonumber (M_pstate,2);//reads the elements in the second absolute coordinate of the bottom of the stackintNDATA3 = Lua_tonumber (M_pstate,-1);//reads the elements in the first relative coordinate of the top of a stackintNDATA4 = Lua_tonumber (M_pstate,-2);//reads the elements in the second relative coordinate of the top of the stackprintf ("[test]ndata1 =%d, NData2 =%d./n, nData3 =%d, nData4 =%d./n");
[Test]ndata1= One, NData2 = A, nData3 = A, nData4 = One
The above code is stored in the stack as:
Lua scripting and C + + interaction (ii)