lua常用函数

void lua_setglobal (lua_State *L, const char *name);
Pops a value from the stack and sets it as the new value of global name.


void lua_register (lua_State *L,
                   const char *name,
                   lua_CFunction f);
Sets the C function f as the new value of global name.




void lua_settable (lua_State *L, int index);
Does the equivalent to t[k] = v, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top. 


This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event (see §2.8). 




void lua_gettable (lua_State *L, int index);
Pushes onto the stack the value t[k], where t is the value at the given valid index and k is the value at the top of the stack. 


This function pops the key from the stack (putting the resulting value in its place). As in Lua, this function may trigger a metamethod for the "index" event (see §2.8). 


void lua_pop (lua_State *L, int n);
Pops n elements from the stack. 




lua_pushboolean
[-0, +1, -] 


void lua_pushboolean (lua_State *L, int b);
Pushes a boolean value with value b onto the stack. 




--------------------------------------------------------------------------------


lua_pushcclosure
[-n, +1, m] 


void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
Pushes a new C closure onto the stack. 


When a C function is created, it is possible to associate some values with it, thus creating a C closure (see §3.4); these values are then accessible to the function whenever it is called. To associate values with a C function, first these values should be pushed onto the stack (when there are multiple values, the first value is pushed first). Then lua_pushcclosure is called to create and push the C function onto the stack, with the argument n telling how many values should be associated with the function. lua_pushcclosure also pops these values from the stack. 


The maximum value for n is 255. 




--------------------------------------------------------------------------------


lua_pushcfunction
[-0, +1, m] 


void lua_pushcfunction (lua_State *L, lua_CFunction f);
Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function. 


Any function to be registered in Lua must follow the correct protocol to receive its parameters and return its results (see lua_CFunction). 


lua_pushcfunction is defined as a macro: 


     #define lua_pushcfunction(L,f)  lua_pushcclosure(L,f,0)




--------------------------------------------------------------------------------


lua_pushfstring
[-0, +1, m] 


const char *lua_pushfstring (lua_State *L, const char *fmt, ...);
Pushes onto the stack a formatted string and returns a pointer to this string. It is similar to the C function sprintf, but has some important differences: 


You do not have to allocate space for the result: the result is a Lua string and Lua takes care of memory allocation (and deallocation, through garbage collection). 
The conversion specifiers are quite restricted. There are no flags, widths, or precisions. The conversion specifiers can only be '%%' (inserts a '%' in the string), '%s' (inserts a zero-terminated string, with no size restrictions), '%f' (inserts a lua_Number), '%p' (inserts a pointer as a hexadecimal numeral), '%d' (inserts an int), and '%c' (inserts an int as a character). 


--------------------------------------------------------------------------------


lua_pushinteger
[-0, +1, -] 


void lua_pushinteger (lua_State *L, lua_Integer n);
Pushes a number with value n onto the stack. 




--------------------------------------------------------------------------------


lua_pushlightuserdata
[-0, +1, -] 


void lua_pushlightuserdata (lua_State *L, void *p);
Pushes a light userdata onto the stack. 


Userdata represent C values in Lua. A light userdata represents a pointer. It is a value (like a number): you do not create it, it has no individual metatable, and it is not collected (as it was never created). A light userdata is equal to "any" light userdata with the same C address. 




--------------------------------------------------------------------------------


lua_pushliteral
[-0, +1, m] 


void lua_pushliteral (lua_State *L, const char *s);
This macro is equivalent to lua_pushlstring, but can be used only when s is a literal string. In these cases, it automatically provides the string length. 




--------------------------------------------------------------------------------


lua_pushlstring
[-0, +1, m] 


void lua_pushlstring (lua_State *L, const char *s, size_t len);
Pushes the string pointed to by s with size len onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string can contain embedded zeros. 




--------------------------------------------------------------------------------


lua_pushnil
[-0, +1, -] 


void lua_pushnil (lua_State *L);
Pushes a nil value onto the stack. 




--------------------------------------------------------------------------------


lua_pushnumber
[-0, +1, -] 


void lua_pushnumber (lua_State *L, lua_Number n);
Pushes a number with value n onto the stack. 




--------------------------------------------------------------------------------


lua_pushstring
[-0, +1, m] 


void lua_pushstring (lua_State *L, const char *s);
Pushes the zero-terminated string pointed to by s onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string cannot contain embedded zeros; it is assumed to end at the first zero. 




--------------------------------------------------------------------------------


lua_pushthread
[-0, +1, -] 


int lua_pushthread (lua_State *L);
Pushes the thread represented by L onto the stack. Returns 1 if this thread is the main thread of its state. 




--------------------------------------------------------------------------------


lua_pushvalue
[-0, +1, -] 


void lua_pushvalue (lua_State *L, int index);
Pushes a copy of the element at the given valid index onto the stack. 




--------------------------------------------------------------------------------


lua_pushvfstring
[-0, +1, m] 


const char *lua_pushvfstring (lua_State *L,
                              const char *fmt,
                              va_list argp);
Equivalent to lua_pushfstring, except that it receives a va_list instead of a variable number of arguments. 




lua_toboolean
[-0, +0, -] 


int lua_toboolean (lua_State *L, int index);
Converts the Lua value at the given acceptable index to a C boolean value (0 or 1). Like all tests in Lua, lua_toboolean returns 1 for any Lua value different from false and nil; otherwise it returns 0. It also returns 0 when called with a non-valid index. (If you want to accept only actual boolean values, use lua_isboolean to test the value's type.) 




--------------------------------------------------------------------------------


lua_tocfunction
[-0, +0, -] 


lua_CFunction lua_tocfunction (lua_State *L, int index);
Converts a value at the given acceptable index to a C function. That value must be a C function; otherwise, returns NULL. 




--------------------------------------------------------------------------------


lua_tointeger
[-0, +0, -] 


lua_Integer lua_tointeger (lua_State *L, int index);
Converts the Lua value at the given acceptable index to the signed integral type lua_Integer. The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tointeger returns 0. 


If the number is not an integer, it is truncated in some non-specified way. 




--------------------------------------------------------------------------------


lua_tolstring
[-0, +0, m] 


const char *lua_tolstring (lua_State *L, int index, size_t *len);
Converts the Lua value at the given acceptable index to a C string. If len is not NULL, it also sets *len with the string length. The Lua value must be a string or a number; otherwise, the function returns NULL. If the value is a number, then lua_tolstring also changes the actual value in the stack to a string. (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.) 


lua_tolstring returns a fully aligned pointer to a string inside the Lua state. This string always has a zero ('\0') after its last character (as in C), but can contain other zeros in its body. Because Lua has garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid after the corresponding value is removed from the stack. 




--------------------------------------------------------------------------------


lua_tonumber
[-0, +0, -] 


lua_Number lua_tonumber (lua_State *L, int index);
Converts the Lua value at the given acceptable index to the C type lua_Number (see lua_Number). The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tonumber returns 0. 




--------------------------------------------------------------------------------


lua_topointer
[-0, +0, -] 


const void *lua_topointer (lua_State *L, int index);
Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value. 


Typically this function is used only for debug information. 




--------------------------------------------------------------------------------


lua_tostring
[-0, +0, m] 


const char *lua_tostring (lua_State *L, int index);
Equivalent to lua_tolstring with len equal to NULL. 




--------------------------------------------------------------------------------


lua_tothread
[-0, +0, -] 


lua_State *lua_tothread (lua_State *L, int index);
Converts the value at the given acceptable index to a Lua thread (represented as lua_State*). This value must be a thread; otherwise, the function returns NULL. 




--------------------------------------------------------------------------------


lua_touserdata
[-0, +0, -] 


void *lua_touserdata (lua_State *L, int index);
If the value at the given acceptable index is a full userdata, returns its block address. If the value is a light userdata, returns its pointer. Otherwise, returns NULL. 




--------------------------------------------------------------------------------


lua_isboolean
[-0, +0, -] 


int lua_isboolean (lua_State *L, int index);
Returns 1 if the value at the given acceptable index has type boolean, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_iscfunction
[-0, +0, -] 


int lua_iscfunction (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a C function, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isfunction
[-0, +0, -] 


int lua_isfunction (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a function (either C or Lua), and 0 otherwise. 




--------------------------------------------------------------------------------


lua_islightuserdata
[-0, +0, -] 


int lua_islightuserdata (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a light userdata, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isnil
[-0, +0, -] 


int lua_isnil (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is nil, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isnone
[-0, +0, -] 


int lua_isnone (lua_State *L, int index);
Returns 1 if the given acceptable index is not valid (that is, it refers to an element outside the current stack), and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isnoneornil
[-0, +0, -] 


int lua_isnoneornil (lua_State *L, int index);
Returns 1 if the given acceptable index is not valid (that is, it refers to an element outside the current stack) or if the value at this index is nil, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isnumber
[-0, +0, -] 


int lua_isnumber (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a number or a string convertible to a number, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isstring
[-0, +0, -] 


int lua_isstring (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a string or a number (which is always convertible to a string), and 0 otherwise. 




--------------------------------------------------------------------------------


lua_istable
[-0, +0, -] 


int lua_istable (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a table, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isthread
[-0, +0, -] 


int lua_isthread (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a thread, and 0 otherwise. 




--------------------------------------------------------------------------------


lua_isuserdata
[-0, +0, -] 


int lua_isuserdata (lua_State *L, int index);
Returns 1 if the value at the given acceptable index is a userdata (either full or light), and 0 otherwise. 




int lua_checkstack (lua_State *L, int extra);
Ensures that there are at least extra free stack slots in the stack. It returns false if it cannot grow the stack to that size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged. 




Function lua_open was replaced by lua_newstate to allow the user to set a memory-allocation function. You can use luaL_newstate from the standard library to create a state with a standard allocation function (based on realloc). 

int lua_gettop (lua_State *L);
Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack). 

void lua_settop (lua_State *L, int index);
Accepts any acceptable index, or 0, and sets the stack top to this index. If the new top is larger than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed. 


int luaL_loadbuffer (lua_State *L,
                     const char *buff,
                     size_t sz,
                     const char *name);
Loads a buffer as a Lua chunk. This function uses lua_load to load the chunk in the buffer pointed to by buff with size sz. 


This function returns the same results as lua_load. name is the chunk name, used for debug information and error messages. 


int lua_load (lua_State *L,
              lua_Reader reader,
              void *data,
              const char *chunkname);
Loads a Lua chunk. If there are no errors, lua_load pushes the compiled chunk as a Lua function on top of the stack. Otherwise, it pushes an error message. The return values of lua_load are: 


0: no errors; 
LUA_ERRSYNTAX: syntax error during pre-compilation; 
LUA_ERRMEM: memory allocation error. 
This function only loads a chunk; it does not run it. 


lua_load automatically detects whether the chunk is text or binary, and loads it accordingly (see program luac). 


The lua_load function uses a user-supplied reader function to read the chunk (see lua_Reader). The data argument is an opaque value passed to the reader function. 


The chunkname argument gives a name to the chunk, which is used for error messages and in debug information (see §3.8). 


int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
Calls a function in protected mode. 


Both nargs and nresults have the same meaning as in lua_call. If there are no errors during the call, lua_pcall behaves exactly like lua_call. However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like lua_call, lua_pcall always removes the function and its arguments from the stack. 


If errfunc is 0, then the error message returned on the stack is exactly the original error message. Otherwise, errfunc is the stack index of an error handler function. (In the current implementation, this index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by lua_pcall. 


Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall, since by then the stack has unwound. 


The lua_pcall function returns 0 in case of success or one of the following error codes (defined in lua.h): 


LUA_ERRRUN: a runtime error. 
LUA_ERRMEM: memory allocation error. For such errors, Lua does not call the error handler function. 
LUA_ERRERR: error while running the error handler function. 
void lua_getglobal (lua_State *L, const char *name);
Pushes onto the stack the value of the global name
void lua_newtable (lua_State *L);
Creates a new empty table and pushes it onto the stack. It is equivalent to lua_createtable(L, 0, 0). 


void lua_createtable (lua_State *L, int narr, int nrec);
Creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for narr array elements and nrec non-array elements. This pre-allocation is useful when you know exactly how many elements the table will have. Otherwise you can use the function lua_newtable. 


void lua_close (lua_State *L);
Destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by this state. On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs, such as a daemon or a web server, might need to release states as soon as they are not needed, to avoid growing too large. 



Metatablesint luaL_dofile (lua_State *L, const char *filename);
Loads and runs the given file. It is defined as the following macro: 


     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))


It returns 0 if there are no errors or 1 in case of errors. 


luaL_loadbuffer
[-0, +1, m] 


int luaL_loadbuffer (lua_State *L,
                     const char *buff,
                     size_t sz,
                     const char *name);
Loads a buffer as a Lua chunk. This function uses lua_load to load the chunk in the buffer pointed to by buff with size sz. 


This function returns the same results as lua_load. name is the chunk name, used for debug information and error messages. 




--------------------------------------------------------------------------------


luaL_loadfile
[-0, +1, m] 


int luaL_loadfile (lua_State *L, const char *filename);
Loads a file as a Lua chunk. This function uses lua_load to load the chunk in the file named filename. If filename is NULL, then it loads from the standard input. The first line in the file is ignored if it starts with a #. 


This function returns the same results as lua_load, but it has an extra error code LUA_ERRFILE if it cannot open/read the file. 


As lua_load, this function only loads the chunk; it does not run it. 




--------------------------------------------------------------------------------


luaL_loadstring
[-0, +1, m] 


int luaL_loadstring (lua_State *L, const char *s);
Loads a string as a Lua chunk. This function uses lua_load to load the chunk in the zero-terminated string s. 


This function returns the same results as lua_load. 


Also as lua_load, this function only loads the chunk; it does not run it. 

 

转载于:https://my.oschina.net/u/3485339/blog/900350

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值