In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the common functions in Lua". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what functions are commonly used in Lua.
Lua_getallocf
Lua_Alloc lua_getallocf (lua_State * L, void * * ud)
Returns the memory allocator function for a given state machine. If ud is not NULL, Lua places the pointer passed in when calling lua_newstate into * ud.
Lua_getfenv
Void lua_getfenv (lua_State * L, int index)
Press the environment table of the values at the index into the stack.
Lua_getfield
Void lua_getfield (lua_State * L, int index, const char * k)
Push the t [k] value onto the stack, where t is the value pointed to by the valid index index. In Lua, this function may trigger the meta-method corresponding to the "index" event (see §2.8).
Lua_getglobal
Void lua_getglobal (lua_State * L, const char * name)
Push the value of the global variable name onto the stack. This is defined by a macro:
# define lua_getglobal (LBERS) lua_getfield (L, LUA_GLOBALSINDEX, s)
Lua_getmetatable
Int lua_getmetatable (lua_State * L, int index)
Pushes the meta-table of the value pointed to by a given index onto the stack. If the index is invalid, or if the value does not have a meta-table, the function returns 0 and does not press anything on the stack.
Lua_gettable
Void lua_gettable (lua_State * L, int index)
Push the t [k] value into the stack, where t is the value pointed to by the valid index index, and k is the value placed at the top of the stack.
This function pops up the key on the stack (putting the result in the same place on the stack). In Lua, this function may trigger the meta-method corresponding to the "index" event (see §2.8).
Lua_gettop
Int lua_gettop (lua_State * L)
Returns the index of the top element of the stack. Because the index is numbered starting at 1, this result is equal to the number of elements on the stack (so returning 0 means the stack is empty).
Lua_insert
Void lua_insert (lua_State * L, int index)
Inserts the top element of the stack at the specified valid index and moves the elements above that index in turn. Do not call this function with a pseudo index, because the pseudo index does not really point to the location on the stack.
Lua_Integer
Typedef ptrdiff_t lua_Integer
This type is used for Lua API to receive integer values.
By default, this is defined as ptrdiff_t, which is usually the largest integer type that the machine can handle.
Lua_isboolean
Int lua_isboolean (lua_State * L, int index)
Returns 1 when the value type of the given index is boolean, otherwise 0.
Lua_iscfunction
Int lua_iscfunction (lua_State * L, int index)
Returns 1 when the value of a given index is a C function, otherwise 0.
Lua_isfunction
Int lua_isfunction (lua_State * L, int index)
Returns 1 when the value of a given index is a function (either the C or Lua function), otherwise 0.
Lua_islightuserdata
Int lua_islightuserdata (lua_State * L, int index)
Returns 1 when the value of a given index is a light userdata, otherwise 0.
Lua_isnil
Int lua_isnil (lua_State * L, int index)
Returns 1 when the value of the given index is nil, otherwise 0.
Lua_isnumber
Int lua_isnumber (lua_State * L, int index)
Returns 1 when the value of a given index is a number, or a string that can be converted to a number, otherwise 0.
Lua_isstring
Int lua_isstring (lua_State * L, int index)
Returns 1 when the value of a given index is a string or a number (numbers can always be converted to strings), otherwise 0 is returned.
Lua_istable
Int lua_istable (lua_State * L, int index)
Returns 1 when the value of a given index is a table, otherwise 0.
Lua_isthread
Int lua_isthread (lua_State * L, int index)
Returns 1 when the value of a given index is a thread, otherwise 0.
Lua_isuserdata
Int lua_isuserdata (lua_State * L, int index)
Returns 1 when the value of a given index is a userdata (whether full userdata or light userdata), otherwise 0.
Lua_lessthan
Int lua_lessthan (lua_State * L, int index1, int index2)
Returns 1 if the value at index index1 is less than the value at index index2; otherwise, returns 0. Its semantics follow the operators in Lua (that is, it is possible to call meta-methods). If any of the indexes are invalid, 0 is also returned.
Lua_load
Int lua_load (lua_State * L, lua_Reader reader, void * data, const char * chunkname)
Load a Lua chunk. If there are no errors, lua_load pushes a compiled chunk into the stack as a Lua function. Otherwise, press the error message. The return value of lua_load can be:
0: no error; LUA_ERRSYNTAX: syntax error encountered during precompilation; LUA_ERRMEM: memory allocation error.
This function just adds chunk; it doesn't run it.
Lua_load automatically detects whether the chunk is text or binary, and then loads accordingly (see program luac).
The lua_load function uses a user-supplied reader function to read the chunk (see lua_Reader). The data parameter is passed into the reader function.
The parameter chunkname can give chunk a name, which is used for error messages and debugging messages (see §3.8).
Lua_newstate
Lua_State * lua_newstate (lua_Alloc f, void * ud)
Create a new stand-alone state machine. Return NULL if it cannot be created (because of a memory problem). The parameter f is an allocator function; Lua will use this function to do all memory allocation operations in the state machine. The second parameter, ud, will be passed directly each time the allocator is called.
Lua_newtable
Void lua_newtable (lua_State * L)
Create an empty table and push it onto the stack. It is equivalent to lua_createtable (L, 0,0).
Lua_newthread
Lua_State * lua_newthread (lua_State * L)
Create a new thread, push it onto the stack, and return the lua_State pointer that maintains the thread. The new state machine returned by this function shares all the objects in the old state machine (such as some table), but it has a separate execution stack.
There are no explicit functions that can be used to close or destroy a thread. A thread, like other Lua objects, is one of the entries for garbage collection.
Lua_newuserdata
Void * lua_newuserdata (lua_State * L, size_t size)
This function allocates a block of memory of a specified size, pushes the address of the block of memory into the stack as a complete userdata, and returns that address.
Userdata represents the C value in Lua. The complete userdata represents a piece of memory. It is an object (like an object like table): you have to create it, it has its own meta-table, and it can be detected when it is recycled. A complete userdata is only equal to itself (under the primitive action of equals).
When Lua reclaims a complete userdata through the gc meta-method, Lua calls the meta-method and marks the userdata as terminated. When the userdata is collected again, the Lua frees up the associated memory.
Lua_next
Int lua_next (lua_State * L, int index)
Pop up a key (key) from the stack and push the key-value (healthy value) pair in the table specified by the index into the stack (specify the next (next) pair after the key). If there are no more elements in the table, lua_next returns 0 (nothing is pushed into the stack).
A typical traversal method is as follows:
/ * table at index't'/ lua_pushnil (L) / first key / while (lua_next (L, t)! = 0) {/ use 'key' (at index-2) and' value' (at index-1) / printf ("% s -% s\ n", lua_typename (L, lua_type (L,-2), lua_typename (L, lua_type (L,-1); / remove 'value' Keep 'key' for the next iteration * / lua_pop (L, 1);}
When traversing a table, do not call lua_tolstring directly to key unless you know that the key must be a string. Calling lua_tolstring may change the value of a given index location; this affects the next call to lua_next.
Lua_Number
Typedef double lua_Number
The type of number in the Lua. It is true that it is double, but you can modify it in luaconf.h.
By modifying the configuration file, you can change Lua to operate on other numeric types (such as float or long).
Lua_objlen
Size_t lua_objlen (lua_State * L, int index)
Returns the length of the value at the specified index. For string, that's the length of the string; for table, it's the result of the length operator ('#'); for userdata, it's the size of the memory block allocated to it; for other values, it's 0.
Lua_pcall
Lua_pcall (lua_State * L, int nargs, int nresults, int errfunc)
Call a function in protected mode.
Nargs and nresults have the same meaning as in lua_call. If no errors occur during the call, lua_pcall behaves exactly the same as lua_call. However, if an error occurs, lua_pcall catches it, pushes a single value (error message) onto the stack, and returns the error code. Like lua_call, lua_pcall always removes the function itself and its arguments from the stack.
If errfunc is 0, the error message returned at the top of the stack is exactly the same as the original error message. Otherwise, errfunc is treated as the index of the error handler on the stack. (in the current implementation, this index cannot be a pseudo index. When a run-time error occurs, this function is called and the argument is the error message The return value of the error handler is returned on the stack by lua_pcall as an error message.
In typical usage, error handlers are used to add more debugging information to the error message, such as stack trace information (stack traceback). After the lua_pcall returns, this information cannot be collected because the stack has been unwound.
The lua_pcall function returns 0 on a successful call, otherwise it returns one of the following error codes (defined in lua.h):
LUA_ERRRUN: runtime error. LUA_ERRMEM: memory allocation error. For this kind of error, Lua cannot call the error handler. LUA_ERRERR: an error that occurs while running an error handler. Lua_pop
Void lua_pop (lua_State * L, int n)
Pop n elements from the stack.
Lua_pushboolean
Void lua_pushboolean (lua_State * L, int b)
Push b into the stack as a boolean value.
Lua_pushcclosure
Void lua_pushcclosure (lua_State * L, lua_CFunction fn, int n)
Push a new C closure onto the stack.
When you create a C function, you can associate some values with it, so that you are creating a C closure (see §3.4); these values can then be accessed by the function whenever the function is called. In order to associate some values to a C function, the values first need to be pushed onto the stack (if there are multiple values, the first one). Next, call lua_pushcclosure to create the closure and press the C function on the stack. The parameter n tells the function how many values need to be associated with the function. Lua_pushcclosure also pops these values off the stack.
Lua_pushcfunction
Void lua_pushcfunction (lua_State * L, lua_CFunction f)
Push a C function onto the stack. This function takes a pointer to the C function and pushes a Lua value of type function onto the stack. When the value at the top of the stack is called, the corresponding C function is triggered.
Any function registered with Lua must follow the correct protocol to receive parameters and return values (see lua_CFunction).
Lua_pushcfunction appears as a macro definition:
# define lua_pushcfunction (Lmaine f) lua_pushcclosure (Lmaine fpen0)
Lua_pushfstring
Const char * lua_pushfstring (lua_State * L, const char * fmt, …)
Press a formatted string onto the stack and return a pointer to the string. It is similar to the C function sprintf, but with some important differences:
Feel that you need to allocate space for the result: the result is a Lua string, and Lua cares about its memory allocation (and frees up memory through garbage collection). This conversion is very limited. Flag, width, or specified precision is not supported. It only supports the following:'%'(insert a'%'),'% s'(insert a string with a zero Terminator with no length limit),'% f'(insert a lua_Number),'% p'(insert a pointer or a hexadecimal number),'% d' (insert an int),'% c' (insert an int as a character). At this point, I believe that you have a deeper understanding of "which functions are commonly used in Lua". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.