Lua scripting and C + + interaction (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.