Simple Lua API Annotation

Source: Internet
Author: User

The entire state of the Lua interpreter, such as global variables and stacks, is stored in an object with the structure type lua_state dynamically allocated. the pointer to this object must be passed as the first parameter to all connected database APIs. before calling all the API functions, you must use lua_open to generate a state.

 lua_State* lua_open(void)

Call lua_close to release a state generated through lua_open:

void lua_close (lua_State *L)

Destroys all objects in the given lua_state and releases the dynamic memory occupied by the State.

Lua uses the virtual stack mechanism and C Programs to transmit values to each other. All elements in the stack can be considered as a Lua value, such as nil, number, and string.

Int lua_gettop (lua_state * l) // gets the index of the top element of the stack. Because the index starts counting from 1, the returned value of lua_gettop is the number of elements in the stack.

Expand the size of the stack to accommodate the top + extra elements.

int lua_checkstack (lua_State *L, ine extra)

When Lua calls C, it can ensure that at least lua_minstack stack locations are available.

Set the stack top index of the stack to a specified value.

void lua_settop(lua_State* L, int index)

Deletes n elements starting from the top of the stack.

#define lua_pop(L,n) lua_settop(L,-(n)-1)

Copy the value of the specified index on the stack to the top of the stack.

void lua_pushvalue(lua_State* L, int index)

Delete the element of the given index and move the corresponding element

void lua_remove(lua_State* L, int index)

Inserts corresponding elements at the given index and overflows the top element of the stack.

void lua_insert(lua_State* L, int index)

Use the top element of the stack to replace the elements at the specified index on the stack.

void lua_replace(lua_State* L, int index)

You can use the following functions to check the data type of stack elements:

Int lua_type (lua_state * l, int index) // return the value type of the stack element int lua_isnil (lua_state * l, int index) int lua_isboolean (lua_state * l, int index) int lua_isnumber (lua_state * l, int index) int lua_isstring (lua_state * l, int index) int lua_istable (lua_state * l, int index) int lua_isfunction (lua_state * l, int index) int lua_iscfunction (lua_state * l, int index) int lua_isuserdata (lua_state * l, int index) int lua_islightuserdata (lua_state * l, int index)

A function that compares the sizes of two values in a stack.

int lua_equal   (lua_State *L, int index1, int index2)int lua_rawequal(lua_State *L, int index1, int index2)int lua_lessthan(lua_State *L, int index1, int index2)

 
Converts the value in the stack to a specified C-type related function.

int           lua_toboolean  (lua_State *L, int index)lua_Number    lua_tonumber   (lua_State *L, int index)const char*   lua_tostring   (lua_State *L, int index)size_t        lua_strlen     (lua_State *L, int index)lua_CFunction lua_tocfunction(lua_State *L, int index)void         *lua_touserdata (lua_State *L, int index)lua_State    *lua_tothread   (lua_State *L, int index)void         *lua_topointer  (lua_State *L, int index)

The following APIs can push C values into the stack

void lua_pushboolean      (lua_State *L, int b)void lua_pushnumber       (lua_State *L, lua_Number n)void lua_pushlstring      (lua_State *L, const char *s, size_t len)void lua_pushstring       (lua_State *L, const char *s)void lua_pushnil          (lua_State *L)void lua_pushcfunction    (lua_State *L, lua_CFunction f)void lua_pushlightuserdata(lua_State *L, void *p)

Format a string

const char *lua_pushfstring (lua_State *L, const char *fmt, ...)const char *lua_pushvfstring(lua_State *L, const char *fmt, va_list argp)

Void lua_getfield (lua_state * l, int index, const char * k) // obtain the key-K element of the table specified by the index on the stack and press it to the top of the stack.

Void lua_setfield (lua_state * l, int index, const char * k) // set the value of the element whose key is K on the table specified by the index on the stack to the value of the element at the top of the stack.

Void lua_gettable (lua_state * l, int index) // obtain the elements on the table specified by the index on the stack whose key is the value of the element at the top of the stack and press it to the top of the stack.

Void lua_settable (lua_state * l, int index) // set the element value of the second element value starting from the top of the stack to the top of the stack when the key on the table specified by the index on the stack is

Void lua_rawgeti (lua_state * l, int index, int N) // obtain the element whose table subscript is n specified by the index on the stack and press it to the top of the stack.

Local tablea = {1, 2, 4, 5}; size_t nnum = lua_objlen (L, index ); // The index variable is the index of the tablea element on the stack for (unsigned int IX = 1; ix <= nnum; ++ IX) {lua_rawgeti (L, index, IX ); int value = lua_tointeger (L,-1); lua_pop (L, 1 );}

Local tableb = {x = "X", y = "Y"} lua_getfiled (L, index, "x ") // The index variable is the index const char * x = lua_tostring (L,-1) lua_pop (L, 1); lua_getfiled (L, index, "Y"); const char * Y = lua_tostring (L,-1); lua_pop (L, 1 );

Lua registry is a global table that can only be accessed by C code. Generally, it can be used to save the data that needs to be shared in several modules. lua registry is always located on a pseudo index value lua_registryindex.

Int ref = lual_ref (L, lua_registryindex) // store the top element of the stack to Lua registry and return the index in Lua registry.

Lua_rawgeti (L, lua_registryindex, ref) // obtain the element with the index ref on the Lua registry and press it to the top of the stack.

Lual_unref (L, lua_registyrindex, ref) // when you do not need to retain elements in the Lua registry, input the index ref to remove the elements from the Lua registry to avoid resource leakage

Maintain stack size

int top = lua_gettop(L);lua_settop(L, top); 

The elements that can be accommodated on the Lua Stack are limited and occupy resources. Generally, the stack size is maintained by calls of pair lua_gettop and lua_settop, ensure that the local objects allocated by the operations in the stack are not retained on the stack, especially when the Lua call is called to execute the Lua function and returns but ignores the return value.

Call the Lua Function

XL_LRT_API long __stdcall XLLRT_LuaCall(lua_State* luaState,int args,int results,const wchar_t* contextInfo);

Call the Lua function. ARGs specifies the number of input parameters. The top of the stack is the last parameter. The elements on the stack end are the second-to-last parameter until the first parameter, the first parameter is the function object to be called. after the call is complete, all elements of the function object that are extremely large will pop up from the stack. If results is specified as lua_multret, all return values of the function object will be pushed to the stack; otherwise, all return values will be pushed to the results; the contextinfo string specifies the context information, which is identified in the callback when a script error occurs in the called Lua function. in bolts, use the xllrt_luacall call of the xlluaruntime module to replace the Lua _ * call class function. for example, Lua
Function func, prototype: String, integer func (integer, string), called in C: (the index variable is the index of the func function object on the stack, and L is the lua_state pointer)

Int Top = lua_gettop (l); lua_pushvalue (L, index); // (the index variable is the index lua_pushinteger (L, 1) of the func function object on the stack; lua_pushstring (L, "test"); long result = xllrt_luacall (l, 2, 2, l "for test"); If (result = 0) {const char * ret1 = lua_tostring (L, -2); int ret2 = lua_tointeger (L,-1);} lua_settop (L, top );

It is equivalent to calling func ret1, ret2 = func (1, "test") in Lua "). note that the returned value of xllrt_luacall indicates that the called Lua function is correctly executed when the value is 0. Otherwise, the Lua function is interrupted and a script error occurs, at this time, there is no return value after the func function is executed on the Lua stack. You cannot take the return value for the operation, or continue to execute any operation dependent on the successful execution of func.

The lua_getglobal () function is used to push the global variables in the Lua script file into the stack. This function has two parameters. The first parameter is the Lua state machine (used to store data ),
The second parameter is the name identifier of the global variable you want to obtain.

void lua_setglobal (lua_State *L, const char *name);  #define lua_setglobal(L,s)  lua_setfield(L, LUA_GLOBALSINDEX, s)

A value pops up from the stack and is set to the global variable name. It is defined by a macro.

Void lua_newtable (lua_state * l); "=" lua_createtable (L,) Create an empty table and press it into the stack

void lua_settable (lua_State *L, int index);

Perform an operation equivalent to T [k] = V. Here T is the value at a given valid index, and V is the value at the top of the stack, K is the value at the top of the stack. this function will pop up the keys and values from the stack.

void lua_rawseti(lua_State *L, int index, int n)

Does the equivalent of T [N] = V, where T is the value at the given valid index and V is the value at the top of the stack. this function pops the value from the stack. the assignment is raw; that is, it does not invoke metamethods.

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.