Lua and C + + interaction detailed summary _1_lua stack

Source: Internet
Author: User

One, Lua stacks

To understand Lua and C + + interactions, first understand the LUA stack.

To put it simply, the main method of communication between Lua and C + + languages is a ubiquitous virtual stack. The stack is characterized by advanced post-out.

In Lua, the LUA stack is a struct, and the stack index is either positive or negative, the difference being that positive index 1 always represents the bottom of the stack, and negative index 1 always represents the top of the stack.


The LUA stack is similar to the following definition, which was created when the lua_state was created:

TValue Stack[max_stack_len]///For information on LSTATE.C's stack_init function The data types that are stored on the stack include numeric values, strings, pointers, talbe, closures, and so on, here is an example of a stack:

Executing the following code allows you to render the picture on your LUA stack.

    Lua_pushcclosure (L, func, 0)//create and press a closure    lua_createtable (l, 0, 0)        //new and press into a table    Lua_pushnumber (L, 343)      Press into a number    lua_pushstring (L, "mystr")   //press into a string

Here to illustrate, you press into the type has values, strings, tables and closures [In C appears to be different types of values], but the final is unified with TValue this data structure to save:), the following is a simple illustration of this data structure:


The TValue structure corresponds to all data types in Lua, is a {value, type} structure, which is the implementation of a dynamic type in Lua, which binds values and types together , uses TT to record the type of value, and value is a union structure, defined by value, You can see that this Union has four domains, which first explains the simple

    • P--A pointer can be stored, which is actually the light userdata structure in LUA
    • N--all values exist here, but int, or float
    • B--The Boolean value exists here, note that Lua_pushinteger is not present here, but exists in N, and b is stored in Boolean
    • GC--Other types such as table, thread, closure, and string require memory management garbage collection exist here
    • A GC is a pointer to a type defined by the Union Gcobject, as can be seen, with string, UserData, closure, table, proto, Upvalue, thread

The following figure can be drawn from the following conclusions:

1. Lua, number, Boolean, nil, light userdata four types of values are directly present on the stack element, regardless of garbage collection.

2. Lua, string, table, closure, UserData, thread exist only pointers in the elements of the stack, and they will be garbage collected after the end of the life cycle.




Lua and C + + interaction detailed summary _1_lua stack

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.