成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

解析Lua 5.1中關于API函數學習教程

移動開發 iOS
Lua腳本的編譯執行是相互獨立的,在不同的線程上執行。通過luaL_newstate()函數可以申請一個虛擬機,返回指針類型 lua_State。

Lua 5.1中關于API函數學習教程是本文要介紹的內容,主要是來學習API函數lua中的應用,具體內容來看本文詳解。

  1. lua_State* luaL_newstate() 

Lua腳本的編譯執行是相互獨立的,在不同的線程上執行。通過luaL_newstate()函數可以申請一個虛擬機,返回指針類型 lua_State。今后其他所有Lua Api函數的調用都需要此指針作為***參數,用來指定某個虛擬機。

  1. lua_State* L = luaL_newstate();  
  2. void  lua_close(lua_State *L) 

銷毀指定 Lua 狀態機中的所有對象(如果有垃圾收集相關的元方法的話,會調用它們),并且釋放狀態機中使用的所有動態內存。在一些平臺上,你可以不必調用這個函數,因為當宿主程序結束的時候,所有的資源就自然被釋放掉了。另一方面,長期運行的程序,比如一個后臺程序或是一個 web 服務器,當不再需要它們的時候就應該釋放掉相關狀態機。這樣可以避免狀態機擴張的過大。

  1. lua_close(L);  
  2. lua_State* lua_newthread(lua_State *L)  
  3. int   lua_gettop(lua_State *L) 

取得棧的高度

  1. for (int i = 0; i < 10; ++i)  
  2.       lua_pushnumber(L, i);  
  3. printf("%d", lua_gettop(L));  
  4. --> 10  
  5. void  lua_settop(lua_State *L, int idx) 

設置棧的高度,如果之前的棧頂比新設置的更高,那么高出來的元素會被丟棄,反之壓入nil來補足大小。

另外,Lua提供了一個宏,用來從棧中彈出n個元素:#define lua_pop(L, n) lua_settop(L, -(n)-1)將指定索引上值的副本壓入棧

  1. for (int i = 1; i <= 3; ++i)  
  2.       lua_pushnumber(i);  
  3. 棧中元素:(從下往上) 1 2 3  
  4. lua_pushvalue(L, 2)  
  5. 棧中元素:(從下往上) 1 2 3 2  
  6. void  lua_remove(lua_State *L, int idx) 

刪除指定索引上的元素,并將該位置之上的所有元素下移以補空缺

  1. for (int i = 1; i <= 3; ++i)  
  2.       lua_pushnumber(i);  
  3. 棧中元素:(從下往上) 1 2 3  
  4. lua_remove(L, 2)  
  5. 棧中元素:(從下往上) 1 3  
  6. void  lua_insert(lua_State *L, int idx) 

移指定位置上的所有元素以開辟一個空間槽的空間,然后將棧頂元素移到該位置

  1. for (int i = 1; i <= 5; ++i)  
  2.      lua_pushnumber(i);  
  3. 棧中元素:(從下往上) 1 2 3 4 5  
  4. lua_insert(L, 3)  
  5. 棧中元素:(從下往上) 1 2 5 4 3  
  6. void  lua_replace(lua_State *L, int idx) 

彈出棧頂的值,并將該值設置到指定索引上,但它不會移動任何東西

  1. for (int i = 1; i <= 5; ++i)  
  2.       lua_pushnumber(i);  
  3. 棧中元素:(從下往上) 1 2 3 4 5  
  4. lua_replace(L, 3)  
  5. 棧中元素:(從下往上) 1 2 5 4  
  6. int   lua_checkstack(lua_State *L, int sz) 

擴大棧的可用尺寸,棧的默認尺寸是20,此函數會確保堆棧上至少有 sz 個空位。如果不能把堆棧擴展到相應的尺寸,函數返回 false 。這個函數永遠不會縮小堆棧;如果堆棧已經比需要的大了,那么就放在那里不會產生變化。
lua_checkstack(L, 100)

  1. void  lua_xmove(lua_State* from, lua_State* to, int n)  
  2.  
  3. //access functions (stack -> C)  
  4. int   lua_isnumber(lua_State *L, int idx)  
  5. int   lua_isstring(lua_State *L, int idx)  
  6. int   lua_iscfunction(lua_State *L, int idx)  
  7. int   lua_isuserdata(lua_State *L, int idx)  
  8.  
  9. int lua_isnil(lua_State *L, int idx);  
  10. int lua_isboolean(lua_State *L, int idx);  
  11. int lua_istable(lua_State *L, int idx);  
  12. int lua_isfunction(lua_State *L, int idx);  
  13. int lua_islightuserdata (lua_State *L, int idx); 

上面四個函數都有一個同樣的原型int lua_is*(lua_State *L, int index),用來查詢某值是否能轉換成某個類型的值。對于任意數字,lua_isstring都返回真。

  1. lua_pushnumber(L, 994);  
  2. lua_pushstring(L, "hello,lua");  
  3. lua_isnumber(L, 1)-->true  
  4. lua_isnumber(L, 2)-->false  
  5. lua_isstring(L,1)-->true  
  6.  
  7. int   lua_type(lua_State *L, int idx) 

得到一個元素的類型,返回整型,返回值是如下列表之一:

  1. #define LUA_TNONE  (-1)  
  2.  
  3. #define LUA_TNIL  0  
  4. #define LUA_TBOOLEAN  1  
  5. #define LUA_TLIGHTUSERDATA 2  
  6. #define LUA_TNUMBER  3  
  7. #define LUA_TSTRING  4  
  8. #define LUA_TTABLE  5  
  9. #define LUA_TFUNCTION  6  
  10. #define LUA_TUSERDATA  7  
  11. #define LUA_TTHREAD  8  
  12. lua_pushnumber(L, 55);  
  13. lua_type(L, 1)-->LUA_TNUMBER  
  14. const char* lua_typename(lua_State *L, int tp) 

將一個類型編碼轉換成類型名

  1. lua_typename(L, 1)-->boolean  
  2. lua_typename(L, 3)-->number  
  3. int   lua_equal(lua_State *L, int idx1, int idx2) 

如果依照 Lua 中 == 操作符語義,索引 index1 和 index2 中的值相同的話,返回 1 。否則返回 0 。如果任何一個索引無效也會返回 0。

  1. lua_pushstring(L, "this");  
  2. lua_pushboolean(L, 1);  
  3. lua_pushboolean(L, 1);  
  4. lua_equal(L, -2, -3)  
  5. -->0  
  6. lua_equal(L, -1, -2)  
  7. -->1  
  8. lua_equal(L, -1, -10)  
  9. -->0  
  10.  
  11. int   lua_rawequal(lua_State *L, int idx1, int idx2)  
  12. int   lua_lessthan(lua_State *L, int idx1, int idx2)  
  13.  
  14. lua_Number lua_tonumber(lua_State *L, int idx)  
  15. lua_Integer lua_tointeger(lua_State *L, int idx)  
  16. int   lua_toboolean(lua_State *L, int idx)  
  17. const char* lua_tolstring(lua_State *L, int idx, size_t *len) 

以上四個函數都有一個原型lua_to*(lua_State *L, int idx),用于從棧中取一個值。如果指定的元素不具有正確的類型,調用這些函數也不會有問題,

在這種情況下,調用lua_toboolean,lua_tonumber,lua_tointeger會返回0,其它函數會返回NULL。通常不使用lua_is*函數,只需在調用它們之

后測試返回結果是否為NULL就可以了。

  1. lua_pushnumber(L, 100)  
  2. lua_tonumber(L, 1)-->100  
  3. lua_pushinteger(L, 200)  
  4. lua_tointeger(L, -1)-->200  
  5. lua_pushboolean(L, 0)  
  6. lua_toboolean(L, -1)-->false  
  7. lua_pushstring(L, "hello,lua")  
  8. lua_tolstring(L, -1, &len)-->hello,lua  

注:len是傳出參數,表示字符串的長度,如果想忽略此參數,傳入NULL

  1. size_t  lua_objlen(lua_State *L, int idx) 

返回值的長度,如果類型不正確,返回0

  1. lua_pushstring(L, "hello,lua")  
  2. lua_objlen(L, 1)-->9  
  3.  
  4. lua_CFunction lua_tocfunction(lua_State *L, int idx)  
  5. void*  lua_touserdata(lua_State *L, int idx)  
  6. lua_State* lua_tothread(lua_State *L, int idx)  
  7. const void* lua_topointer(lua_State *L, int idx)  
  8.  
  9. //push functions (C -> stack)  
  10. void  lua_pushnil(lua_State *L)  
  11. void  lua_pushnumber(lua_State *L, lua_Number n)  
  12. void  lua_pushinteger(lua_State *L, lua_Integer n)  
  13. void  lua_pushlstring(lua_State *L, const char* s, size_t l)  
  14. void  lua_pushstring(lua_State *L, const char *s)  
  15. const char* lua_pushvfstring(lua_State *L, const char *fmt, va_list argp)  
  16. const char* lua_pushfstring(lua_State *L, const char *fmt, ...)  
  17. void  lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)  
  18. void  lua_pushboolean(lua_State *L, void *b)  
  19. void  lua_pushlightuserdata(lua_State *L, void *p)  
  20. int   lua_pushthread(lua_State *L)  
  21. void  lua_gettable(lua_State *L, int idx)  
  22. void  lua_getfield(lua_State *L, int idx, const char *k) 

把 t[k] 值壓入堆棧,這里的 t 是指有效索引 index 指向的值。在 Lua 中,這個函數可能觸發對應 "index" 事件的元方法

  1. void  lua_rawget(lua_State *L, int idx)  
  2. void  lua_rawgeti(lua_State *L, int idx, int n)  
  3.  
  4. void  lua_createtable(lua_State *L, int narr, int nrec) 

創建一個新的空 table 壓入堆棧。這個新 table 將被預分配 narr 個元素的數組空間以及 nrec 個元素的非數組空間。當你明確知道表中需要多少個元素時,預分配就非常有用。如果你不知道,可以使用函數 lua_newtable。

舉例暫缺

  1. void*  lua_newuserdata(lua_State *L, size_t sz)  
  2. int   lua_getmetatable(lua_State *L, int objindex)  
  3.  
  4. void  lua_getfenv(lua_State *L, int idx) 

把索引處值的環境表壓入堆棧

  1. void  lua_settable(lua_State *L, int idx);  
  2. void  lua_setfield(lua_State *L, int idx, const char *k)  
  3. void  lua_rawset(lua_State *L, int idx)  
  4. void  lua_rawseti(lua_State *L, int idx, int n)  
  5. int   lua_setmetatable(lua_State *L, int objindex)  
  6. int   lua_setfenv(lua_State *L, int idx)  
  7.  
  8. //'load' and 'call' functions (load and run lua code)  
  9. void  lua_call(lua_State *L, int nargs, int nresults);  
  10. int   lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)  
  11.  
  12. int   lua_cpcall(lua_State *L, lua_CFunction func, void *ud) 

以保護模式調用 C 函數 func 。 func 只有能從堆棧上拿到一個參數,就是包含有 ud 的 light userdata。當有錯誤時, lua_cpcall 返回和 lua_pcall 相同的錯誤代碼,并在棧頂留下錯誤對象;否則它返回零,并不會修改堆棧。所有從 func 內返回的值都會被扔掉。

舉例暫缺

  1. int   lua_load(lua_State *L, lua_Reader reader, void *dt, const char *chunkname);  
  2.  
  3. int   lua_dump(lua_State *L, lua_Writer writer, void *data); 

把函數 dump 成二進制 chunk 。函數接收棧頂的 Lua 函數做參數,然后生成它的二進制 chunk 。若被 dump 出來的東西被再次加載,加載的結果就相當于原來的函數。當它在產生 chunk 的時候,lua_dump 通過調用函數 writer (參見 lua_Writer)來寫入數據,后面的 data 參數會被傳入 writer 。

***一次由寫入器 (writer) 返回值將作為這個函數的返回值返回; 0 表示沒有錯誤。

這個函數不會把 Lua 返回彈出堆棧。

舉例暫缺

  1. int   lua_yield(lua_State *L, int nresults)  
  2. int   lua_resume(lua_State *L, int narg)  
  3. int   lua_status(lua_State *L)  
  4. int   lua_gc(lua_State *L, int what, int data) 

控制垃圾收集器。 這個函數根據其參數 what 發起幾種不同的任務:

  1.  * LUA_GCSTOP: 停止垃圾收集器。   
  2.  * LUA_GCRESTART: 重啟垃圾收集器。   
  3.  * LUA_GCCOLLECT: 發起一次完整的垃圾收集循環。   
  4.  * LUA_GCCOUNT: 返回 Lua 使用的內存總量(以 K 字節為單位)。   
  5.  * LUA_GCCOUNTB: 返回當前內存使用量除以 1024 的余數。   
  6.  * LUA_GCSTEP: 發起一步增量垃圾收集。步數由 data 控制(越大的值意味著越多步),而其具體含義(具體數字表示了多少)并未標準化。
  7. 如果你想控制這個步數,必須實驗性的測試 data 的值。如果這一步結束了一個垃圾收集周期,返回返回 1 。   
  8.  * LUA_GCSETPAUSE: 把 data/100 設置為 garbage-collector pause 的新值。函數返回以前的值。   
  9.  * LUA_GCSETSTEPMUL: 把 arg/100 設置成 step multiplier 。函數返回以前的值。  
  10. int   lua_error(lua_State *L) 

產生一個 Lua 錯誤。錯誤信息(實際上可以是任何類型的 Lua 值)必須被置入棧頂。這個函數會做一次長跳轉,因此它不會再返回。(參見 luaL_error)。

  1. lua_pushstring(L, "one error");  
  2. lua_error(L);  
  3. printf("%s", "本行已經執行不到了");  
  4. int   lua_next(lua_State *L, int idx)  
  5. void  lua_concat(lua_State *L, int n) 

連接棧頂的 n 個值,然后將這些值出棧,并把結果放在棧頂。如果 n 為 1 ,結果就是一個字符串放在棧上(即,函數什么都不做);如果 n 為 0 ,結果是一個空串。 連接依照 Lua 中創建語義完成,如果嘗試把兩個不能連接的類型連接,程序會給出錯誤提示。

  1.  lua_pushstring(L, "this");  
  2.  lua_pushboolean(L, 1);  
  3.  lua_pushnumber(L, 9989);  
  4.  lua_pushnumber(L, 1111);  
  5.  lua_pushboolean(L, 0);  
  6.  lua_pushstring(L, "滿天都是小星星");  
  7.  lua_pushnumber(L, 1986);  
  8.  lua_pushstring(L, "onebyone");  
  9. -->'this' 'true' '9989' '1111' 'false' '滿天都是小星星' '1986' 'onebyone'  
  10. lua_concat(L, 3);  
  11. -->'this' 'true' '9989' '1111' 'false' '滿天都是小星星1986onebyone'  
  12.  
  13. lua_Alloc lua_getallocf(lua_State *L, void **ud) 

返回給定狀態機的內存分配器函數。如果 ud 不是 NULL ,Lua 把調用 lua_newstate 時傳入的那個指針放入 *ud 。

  1. void  lua_setallocf(lua_State *L, lua_Alloc f, void *ud)  
  2.  
  3. //Functions to be called by the debuger in specific events  
  4. int   lua_getstack(lua_State *L, int level, lua_Debug *ar)  
  5. int   lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)  
  6. const char* lua_getlocal(lua_State *L, const lua_Debug *ar, int n)  
  7. const char* lua_setlocal(lua_State *L, const lua_Debug *ar, int n)  
  8. const char* lua_getupvalue(lua_State *L, int funcindex, int n)  
  9. const char* lua_setupvalue(lua_State *L, int funcindex, int n)  
  10. int   lua_sethook(lua_State *L, lua_Hook func, int mask, int count);  
  11. lua_Hook lua_gethook(lua_State *L)  
  12. int   lua_gethookmask(lua_State *L)  
  13. int   lua_gethookcount(lua_State *L) 

小結:解析Lua 5.1中關于API函數學習教程的內容介紹完了希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 博客園
相關推薦

2011-08-23 16:37:05

Lua數學庫

2011-08-23 16:22:45

Lua 4.0函數

2011-08-23 17:33:08

LuaMetatable

2011-08-24 17:09:35

LUA閉包函數

2011-08-23 15:34:56

Lua模式 匹配

2011-08-24 14:14:13

LUA環境 配置

2011-08-23 13:15:37

LUAPackage

2011-08-23 17:06:03

2011-08-24 13:27:07

Lua 游戲C接口腳本

2011-08-23 16:59:16

C++LUA腳本LUA API

2011-08-24 15:34:44

MinGWLua環境配置

2011-08-24 15:42:38

LUA源代碼

2011-08-25 15:41:42

Lua源碼

2011-08-23 13:54:10

LUA全局變量

2011-08-23 16:14:27

Lua函數庫函數

2011-08-24 15:22:09

2011-08-23 15:57:21

Lua元表元方法

2011-08-24 11:08:09

Lua

2011-08-23 17:11:13

Lua事件C#

2011-08-25 16:38:06

EditPluslua
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 粉嫩国产精品一区二区在线观看 | 天天夜夜操 | 91国内产香蕉 | 日韩精品久久久久久 | 欧美 视频 | 久久精品国内 | 性色av香蕉一区二区 | 欧美色性| 一区二区三区国产 | 国产2区| 国产伦精品一区二区三区在线 | 欧美日韩一区二区电影 | 日韩电影一区二区三区 | 在线观看涩涩视频 | 午夜亚洲| 99视频久| 一区二区三区韩国 | 中文字字幕一区二区三区四区五区 | 亚洲欧美日韩在线 | 一区二区高清在线观看 | 色婷婷综合久久久中字幕精品久久 | 欧美色综合天天久久综合精品 | 日韩一区二区三区av | 国产资源一区二区三区 | 国产视频一视频二 | 四虎午夜剧场 | 欧美精品在线播放 | 日韩精品成人在线 | 精品视频在线播放 | 一区二区三区在线电影 | 国产特级毛片 | 中文字幕亚洲无线 | 国产xxxx搡xxxxx搡麻豆 | 97超碰人人 | www,黄色,com | 成人在线免费观看av | 亚洲伦理自拍 | 亚洲福利精品 | 日韩三极 | 国产一级片av | 精品国产乱码久久久久久牛牛 |