Lua and C + + interaction detailed summary _1_lua stack __c++

Source: Internet
Author: User

one, LUA stack

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

In simple terms, the main method of communication between Lua and C + + language is a ubiquitous virtual stack. The features of the stack are advanced later out.

In Lua, the LUA stack is a struct, but the way a stack index can be positive or negative, the difference is that a positive index 1 always represents the bottom of the stack, and a negative index-1 always represents the top of the stack. as shown in figure:


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

TValue Stack[max_stack_len]//Want to know the lstate.c stack_init function stored in the stack data types include values, strings, pointers, talbe, closures, and so on, the following is an example of a stack:

Executing the following code allows you to render the situation 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

The point here is that the type you're pressing has values, strings, tables, and closures [that appear to be different types of values in C], but in the end they are all consistent with the TValue data structure:), and the following diagram simply illustrates this data structure:


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

P--You can save a pointer, which is actually the light UserData structure N in Lua--all values exist here, but int, or float B--Boolean values exist here, notice, Lua_pushin Teger does not exist here, but exists N, B is only a Boolean GC--other things such as table, thread, closure, string that require memory management garbage collection exist here the GC is a pointer that can point to a type made by a union Body Gcobject definition, as can be seen from the diagram, there are string, UserData, closure, table, proto, Upvalue, thread

From the following diagram you can draw the following conclusions:

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

2. In Lua, string, table, closure, UserData, thread is the only pointer in the element on the stack, and they will be garbage collected at the end of the lifecycle.


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.