LUA operates a common function to learn a

Source: Internet
Author: User

(1) Basic knowledge of the interaction between LUA and C + +:

Data interaction between LUA and C + + is done through the stack, and the data in the stack is positioned by the index value (the stack is like a container, where everything is labeled)
The top of the stack is-1, the bottom of the stack is 1, that is, the 1th into the stack at the bottom of the stack; it can be said that the positive number indicates the position (displacement) relative to the bottom of the stack, and the negative number indicates the position (displacement) relative to the top of the stack;
(2) Calculating and emptying the operation of elements in the stack:
1. function Lua_gettop ()
Used to return the number of elements in the stack, but also the index of the top element of the stack, because the bottom is 1, so how many elements in the stack, the top index is how much; hehe
2. function Lua_settop ()
Function prototype: void Lua_settop (lua_state *l, int index);
Used to set the stack top index to the specified value, for example, a stack originally has 8 elements, call the function set Index to 7, that is, the stack of elements set to 7, that is, delete an element, and is the top of the stack element; This is a positive number, which is relative to the stack bottom element set If it is relative to the top of the stack, a negative value is required, that is, if the index is set to -2 (index =-2), it is equivalent to deleting the top element of the stack; hehe, drawing a picture is very convenient; To illustrate the convenience, simply set a macro:
#define LUA_POP (L,n) lua_settop (l,-(n)-1)
Here n is relative to the top of the stack of elements, mainly for understanding; the Lua_settop (l,-(n)-1) is used to indicate a negative number relative to the top displacement of the stack;
3, Lua_pushvalue ()
Function prototype: void Lua_pushvalue (lua_state *l, int index);
English intent: Pushes a copy of the element at the given valid index onto the stack.
Here is an example where the initial state of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is identified as the top of the stack):
Lua_pushvalue (L, 3)--10 20 30 40 50 30*
Lua_pushvalue (l,3) is to get the third element in the original stack, pressing to the top of the stack;
4, Lua_remove ()
void Lua_remove (lua_state *l, int index);
Lua_remove deletes the element of the given index and fills the vacancy with the element above the index;
Here is an example of a stack with an initial state of 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" identified as the top of the stack, with:
Lua_remove (L,-3)--10 20 40 50*

Here is an example of a stack with an initial state of 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" identified as the top of the stack, with:
Lua_settop (L,-3)--10 20 30 *
Lua_settop (L, 6)--Ten nil Nil nil *
5, Lua_replace
void Lua_replace (lua_state* L, int index);
Lua_replace pushes the top element of the stack into the specified position without moving any elements (so that the value of the element at the specified position is replaced).
Here is an example of a stack with an initial state of 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" identified as the top of the stack, with:
Lua_replace (L, 2)--10 50 30 40 *//Replace 50 with the position of the index while removing the top element of the stack

(3) Use of some functions when loading Lua in C

Lua_getgobal------void Lua_getglobal (lua_state *l, const char *name), pressing the value of the global name to the top of the stack.

lua_is*** (lua_state *l,int Index) checks if a variable is a type, index indicates the order of the variable, and the stack top is-1.

lua_to*** (lua_state *l,int index) Gets the variable in the stack, then converts it to a specified type and returns.

Lua_close () Destroys all objects on the specified LUA state, releasing all the dynamically allocated space used by the.

LUA operates a common function to learn a

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.