Lua API Little Note 1

Source: Internet
Author: User

These things are encountered at ordinary times, feel that there is a certain value, so recorded, and later encountered similar problems can be consulted, at the same time to share the people can be easily needed, reproduced please specify from Ringofthec[[email protected]]

Planning to record some of the LUA_API may seem to be very clear in the LUA documentation, but I'm going to use my own way of documenting what I think is important, by agreeing on the format of the API description first.

Number. API role Brief

API function prototypes

API operating Instructions

Return value Description

Impact on the stack

Precautions

1. Build a new table

void Lua_createtable (lua_state *l, int narr, int nrec)
Create a new table and put it on top of the stack. Narr and Nrec Specify the number of pre-allocated elements for the table's array and hash sections, respectively
No return value
Stack height +1, stack top element is new table
#define LUA_NEWTABLE (L) lua_createtable (l, 0, 0) commonly used in this
2. Take the elements in the table
void Lua_getfield (lua_state *l, int index, const char *k)
Operation:   arr = Stack[index]    //arr must be a table
        Stack.push (Arr[k])
Take the element with the key k in the table, where the table is a table on the stack pointed to by index
No return value
Stack height +1, stack top element is (Stack[index]) [K]
Note that this action will trigger the __index meta-method
3. Assigning values to elements in a table
void Lua_setfield (lua_state *l, int index, const char *k)
Action:   arr = Stack[index]
        Arr[k] = Stack.top ()
        Stack.pop ()
Assigns a value to the element in the table with the key K (value is the top element of the stack), where the table is a table on the stack pointed to by index
No return value
Stack height-1, the popup is value
Note that this action will trigger the __newindex meta-method
4. Assigning Values to table elements and table elements
void Lua_gettable (lua_state *l, int index)

Operation: Ele = Stack[index]

Key = Stack.top ()

Stack.pop ()

Value = Ele[key]

Stack.push (value)

The corresponding table is assigned according to index; Take the top element of the stack as key, and pop the stack; Gets the value of the key in the table pressed into the top of the stack.

No return value

The stack height is constant, but a pop-up and press-in operation occurs, the key is popped, and the value is pressed in

Note that this action will trigger the __index meta-method

void Lua_settable (lua_state *l, int index)
Operation:   ele    = Stack[index]
        Value  = Stack.top ()
        Stack.pop ()
        Key    = Stack.top ()
        Stack.pop ()
        Ele[key] = value
The corresponding table is assigned according to index; Take the top element of the stack to do value, pop it; Then take the top element of the current stack to do key, also pop up; Then assign the element of the table key to key as value
No return value
Stack height-2, first popup value, second popup key
Note that this action will trigger the __newindex meta-method
5. Some operations on table [does not cause the original method]
void Lua_rawget (lua_state *l, int index)

Same as the lua_gettable operation

But does not trigger the corresponding meta-method

void Lua_rawgeti (lua_state *l, int index, int n)

Operation: Ele = Stack[index]

Value = Ele[n]

Stack.push (value)

No return value

Stack +1, the new element at the top of the stack is value

does not trigger the corresponding meta-method

Same as the lua_settable operation

But does not trigger the corresponding original method

Operation: Ele = Stack[index]

Value = Stack.top ()

Stack.pop ()

Ele[n] = value

No return value

Stack-1, the top of the stack pops the value

does not trigger the corresponding meta-method

6. Copy the elements on the stack and press into the stack

void Lua_pushvalue (lua_state *l, int index)

Action: value = Stack[index]

Stack.push (value)

No return value

Stack +1

7. Create a meta-table

int lual_newmetatable (lua_state *l, const char *tname)

Operation: 1. Find Tname in the registry, if already registered, return 0, no to continue, and flat stack

Lua_getfield (L, Lua_registryindex, Tname)

if (!lua_isnil (L,-1))

return 0;

Lua_pop (L, 1);

2. Create a table and register and return 1

Lua_newtable (L)

Lua_pushvalue (L,-1)

Lua_setfield (L, Lua_registryindex, Tname)

Return 1

has a return value
Stack +1, the top element of the stack is a new table registered in the registry
8. Create a C value
void *lua_newuserdata (Lua_state *l, size_t size)

This function allocates a block of memory specified by size and placed on top of the stack

The return value is the address of the newly allocated block

Stack +1, top of Stack is UserData

The UserData is used to represent values in C in Lua. A complete userdata has its own meta-table, and the __gc method of the meta-table can be called when it is garbage collected

9. Register the C function into Lua, but there is no such thing, only c closures in Lua

void Lua_pushcclosure (lua_state *l, lua_cfunction fn, int n)

Press A C closure on the stack

When a C function is created, it is possible to bind several values above it to form a closure. These binding values can be accessed at any time when the C function is called.

Binding method: First press the N value to bind to the stack, then call Lua_pushcclosure (L, FN, n) to form a C closure

No return value

Stack – (N-1), pops up n elements (and those bound values), pressing into a cclosure

#define LUA_PUSHCFUNCTION (L, F) lua_pushcclosure (L, F, 0)

#define Lua_register (l, N, F) (Lua_pushcfunction (L, F), Lua_setglobal (l, N))

No return value

Stack does not change

This is a more common use of n as a key in Lua to press into a 0 bound value of the cclosure.

Lua API Little Note 1

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.