Lua Operations common function learning 1

Source: Internet
Author: User

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

Data Interaction Between lua and C ++ is performed through the stack, and the data in the stack is located through the index value (the stack is like a container, and there must be labels for everything to be put in)
The top of the stack is-1, the bottom of the stack is 1, that is, the bottom of the stack is 1st. You can also say that positive numbers indicate the position (shift) relative to the bottom of the stack ), negative number indicates the position (shift) relative to the top of the stack );
(2) Calculation and clearing of elements in the stack:
1. Function lua_gettop ()
It is used to return the number of elements in the stack and also the index of the top element of the stack. Because the bottom of the stack is 1, the number of elements in the stack is what the top index of the stack is.
2. Function lua_settop ()
Function prototype: void lua_settop (lua_State * L, int index );
Set the stack top index of the stack to a specified value. For example, if a stack originally has eight elements and the call function sets the index to 7, the number of elements in the stack is set to 7, that is, deleting an element is the top element of the stack. This is a positive number, that is, it is set relative to the bottom element of the stack. If it is relative to the top element of the stack, a negative value is required; that is to say, if the index is set to-2 (index =-2), it is also equivalent to deleting the top element of the stack, set a macro:
# Define lua_pop (L, n) lua_settop (L,-(n)-1)
Here n is the first element relative to the top of the stack, mainly for understanding; the lua_settop (L,-(n)-1) it is represented by a negative number relative to the top displacement of the stack;
3. lua_pushvalue ()
Function prototype: void lua_pushvalue (lua_State * L, int index );
Pushes a copy of the element at the given valid index onto the stack.
In the following example, the initial stack status is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack:
Lua_pushvalue (L, 3) --> 10 20 30 40 50 30 *
Lua_pushvalue (L, 3) is the third element in the original stack, pressed 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 in the vacancy of the element above the index;
In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_remove (L,-3) --> 10 20 40 50 *

In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_settop (L,-3) --> 10 20 30 *
Lua_settop (L, 6) --> 10 20 30 nil *
5. lua_replace
Void lua_replace (lua_State * L, int index );
Lua_replace pushes the top element of the stack to the specified position without moving any element (so the value of the element at the specified position is replaced ).
In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_replace (L, 2) --> 10 50 30 40 * // replace 50 with the index position and remove the top element of the stack.

 

(3) usage of some functions when loading lua in C

Lua_getgobal ------ void lua_getglobal (lua_State * L, const char * name); press the global name value to the top of the stack.

Lua_is *** (lua_State * L, int index) checks whether the variable is of a certain type. index indicates the sequence of the variables. The top of the stack is-1.

Lua_to *** (lua_State * L, int index) gets the variables in the stack, converts them to a specified type, and returns the result.

Lua_close () destroys all objects in the specified Lua State and releases the dynamically allocated space used by the State.

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

Data Interaction Between lua and C ++ is performed through the stack, and the data in the stack is located through the index value (the stack is like a container, and there must be labels for everything to be put in)
The top of the stack is-1, the bottom of the stack is 1, that is, the bottom of the stack is 1st. You can also say that positive numbers indicate the position (shift) relative to the bottom of the stack ), negative number indicates the position (shift) relative to the top of the stack );
(2) Calculation and clearing of elements in the stack:
1. Function lua_gettop ()
It is used to return the number of elements in the stack and also the index of the top element of the stack. Because the bottom of the stack is 1, the number of elements in the stack is what the top index of the stack is.
2. Function lua_settop ()
Function prototype: void lua_settop (lua_State * L, int index );
Set the stack top index of the stack to a specified value. For example, if a stack originally has eight elements and the call function sets the index to 7, the number of elements in the stack is set to 7, that is, deleting an element is the top element of the stack. This is a positive number, that is, it is set relative to the bottom element of the stack. If it is relative to the top element of the stack, a negative value is required; that is to say, if the index is set to-2 (index =-2), it is also equivalent to deleting the top element of the stack, set a macro:
# Define lua_pop (L, n) lua_settop (L,-(n)-1)
Here n is the first element relative to the top of the stack, mainly for understanding; the lua_settop (L,-(n)-1) it is represented by a negative number relative to the top displacement of the stack;
3. lua_pushvalue ()
Function prototype: void lua_pushvalue (lua_State * L, int index );
Pushes a copy of the element at the given valid index onto the stack.
In the following example, the initial stack status is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack:
Lua_pushvalue (L, 3) --> 10 20 30 40 50 30 *
Lua_pushvalue (L, 3) is the third element in the original stack, pressed 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 in the vacancy of the element above the index;
In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_remove (L,-3) --> 10 20 40 50 *

In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_settop (L,-3) --> 10 20 30 *
Lua_settop (L, 6) --> 10 20 30 nil *
5. lua_replace
Void lua_replace (lua_State * L, int index );
Lua_replace pushes the top element of the stack to the specified position without moving any element (so the value of the element at the specified position is replaced ).
In the following example, the initial status of the stack is 10 20 30 40 50 * (from the bottom of the stack to the top of the stack, "*" is marked as the top of the stack, which has:
Lua_replace (L, 2) --> 10 50 30 40 * // replace 50 with the index position and remove the top element of the stack.

 

(3) usage of some functions when loading lua in C

Lua_getgobal ------ void lua_getglobal (lua_State * L, const char * name); press the global name value to the top of the stack.

Lua_is *** (lua_State * L, int index) checks whether the variable is of a certain type. index indicates the sequence of the variables. The top of the stack is-1.

Lua_to *** (lua_State * L, int index) gets the variables in the stack, converts them to a specified type, and returns the result.

Lua_close () destroys all objects in the specified Lua State and releases the dynamically allocated space used by the State.

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.