The Lua Tutorial (iii): The C language, the table example called Lua in _lua

Source: Internet
Author: User
Tags garbage collection lua

It's been half a month since I wrote a LUA article, and it's time to refresh your LUA state. This tutorial will introduce the LUA stack and basic stack operations, as well as how to read the LUA table in C + + code.

Understanding the LUA Stack

LUA uses a "virtual stack" to interact with a C + + program, and all of the LUA C APIs do this by manipulating the stack for data communication. This "virtual stack" of LUA addresses two major issues of communication between C + + programs and LUA programs:

1.Lua uses garbage collection, and C + + requires manual memory management.

2.Lua uses a dynamic type, while C + + uses a static type.

Because this stack is inside the LUA virtual machine, when a LUA variable is placed inside the stack, the virtual machine can know whether it is being used by the host program to determine if GC is used. In addition, Lua uses structs to encapsulate types like "lua_value" so that it can store any type of C. Thus, any type can be put into a slot of the stack when data is exchanged.

Since the stack is filo, when we operate the stack in Lua, each operation is at the top of the stack. The LUA C API has more control, and it has the flexibility to manipulate elements at any point in the stack.

Basic LUA Stack Operations

1. Press a value into the stack

Copy Code code as follows:

void Lua_pushnil (Lua_state *l);
void Lua_pushboolean (lua_state *l, int bool);
void Lua_pushnumber (Lua_state *l, Lua_number N);
void Lua_pushinteger (Lua_state *l, Lua_integer N);
void lua_pushunsigned (Lua_state *l, lua_unsigned N);
void Lua_pushlstring (lua_state *l, const char *s, size_t len);
void Lua_pushstring (lua_state *l, const char *s);

2. Query the elements inside the stack

Copy Code code as follows:

int lua_is* (lua_state * L, int index);

This can be boolean,nil,string,function and so on.

3. Get the value of the element at a given position within the stack

Copy Code code as follows:

XXX lua_toxxx (lua_state * L, int index);

This inside of XXX can be nil, Boolean, String,integer and so on.

4. Other stack operation

Copy Code code as follows:

Get the number of elements in the stack
int Lua_gettop (lua_state *l);
Sets the size of the stack to a specified value, while Lua_settop (l,0) empties the current stack
If the specified index is greater than the size of the previous stack, then the free space is nil filled
If index is less than the number of elements in the previous stack, the extra elements are discarded
void Lua_settop (lua_state *l, int index);
Push the elements of index in the stack into the stack
void Lua_pushvalue (lua_state *l, int index);
Remove the element in the stack where index is located
void Lua_remove (lua_state *l, int index);
The elements at the top of the stack are moved to the index
void Lua_insert (lua_state *l, int index);
POPs a value from the top of the stack and sets it to the given index
void Lua_replace (lua_state *l, int index);
Insert the element copy of the Fromidx into the TOIDX, which does not modify the elements of the FROMIDX
void Lua_copy (lua_state *l, int fromidx, int toidx);

Also, according to the book Programming in Lua, we can define a function stackdump to print the current stack:

Copy Code code as follows:

static void Stackdump (lua_state* L) {
cout<< "\nbegin dump LUA Stack" <<endl;
int i = 0;
int top = lua_gettop (L);
for (i = 1; I <= top; ++i) {
int t = lua_type (L, i);
Switch (t) {
Case lua_tstring:
{
printf ("'%s '", lua_tostring (L, i));
}
Break
Case Lua_tboolean:
{
printf (Lua_toboolean (L, i)? "True": "false");
}break;
Case Lua_tnumber:
{
printf ("%g", Lua_tonumber (L, i));
}
Break
Default
{
printf ("%s", Lua_typename (L, t));
}
Break
}
}
cout<< "\nend dump LUA Stack" <<endl;
}

C + + access to LUA table

Suppose we have a table in the Lua file:

Copy Code code as follows:

me = {name = "Zilongshanren", age = 27}

We can access its elements through the following C code:

Copy Code code as follows:

Get me this table from LUA and push it into the stack
Lua_getglobal (L, "Me");
if (!lua_istable (L,-1)) {
Cclog ("error! Me is not a table ");
}
Push a key:name into the stack
Lua_pushstring (L, "name");
Get the 2-bit table, then eject the top element of the stack, remove the Table[name value and press the stack
Lua_gettable (L,-2);
Name of the top of the output stack
Cclog ("name =%s", lua_tostring (L,-1));
Stackdump (L);
Bounce the top of the stack out.
Lua_pop (L, 1);
Press into another key:age.
Lua_pushstring (L, "age");
Take out the 2-bit table and push the value of the table[age into the stack
Lua_gettable (L,-2);
Stackdump (L);
Cclog ("Age =%td", Lua_tointeger (L,-1));

Lua5.1 also introduced a new approach:
Copy Code code as follows:

Lua_getfield (L,-1, "age");

It can replace

Copy Code code as follows:

Press into another key:age.
Lua_pushstring (L, "age");
Take out the 2-bit table and push the value of the table[age into the stack
Lua_gettable (L,-2);

In the next article, we'll explain how LUA calls functions inside C/A + +.

Related Article

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.