Lua 語言的簡單介紹
Lua 是一個小巧的腳本語言。作者是巴西人。該語言的設計目的是爲了嵌入應用程序中,從而爲應用程序提供靈活的擴展和定製功能。它的主頁是 www.lua.org。
Lua最著名的應用是在暴雪公司的網絡遊戲WOW中。
Lua腳本可以很容易的被C/C++代碼調用,也可以反過來調用C/C++的函數,這使得Lua在應用程序中可以被廣泛應用。不僅僅作爲擴展腳本,也可以作爲普通的配置文件,代替XML,Ini等文件格式,並且更容易理解和維護。
Lua由標準C編寫而成,代碼簡潔優美,幾乎在所有操作系統和平臺上都可以編譯,運行。一個完整的Lua解釋器不過200k,在目前所有腳本引擎中,Lua的速度是最快的。這一切 都決定了Lua是作爲嵌入式腳本的最佳選擇。
Lua 有一個同時進行的JIT項目,提供在特定平臺上的即時編譯功能,這將給Lua帶來更加優秀的性能。請訪問 http://luajit.luaforge.net/ 來了解這個項目。
和Python等腳本不同,Lua並沒有提供強大的庫,這是由它的定位決定的。所以Lua不適合作爲開發獨立應用程序的語言。不過Lua還是具備了比如數學運算和字符串處理等基本的功能。
Lua 目前的最新版本是 5.1.
Lua只有一種數據類型,table. 實際上就是hash表。它用這個來模擬數組,鏈表等等。 在語法上,Lua支持如下形式:
data = {} --定義一個table data.i = 1 data.name = "jason" data.package = {1,2,2,3,56,7} data.others = {} data.others.a = 1 data.others.b = 1.1
這使得Lua具有了跟C的struct類似的形式,非常便於設計C函數的參數,用一個table就可以傳入很複雜的參數。
-
Lua和C程序通過一個堆棧交換數據: struct lua_State
-
堆棧的序號可以從棧頂和棧底計數,從棧底計數,則棧底是1,向棧頂方向遞增。從棧頂計數,則棧頂是-1,向棧底方向遞減。一般都用從棧頂計數的方式。堆棧的默認大小是20,可以用lua_checkstack修改.用lua_gettop則可以獲得棧裏的元素數目。並不是說在棧頂有一個整形元素。而是計算了一下棧頂元素在棧裏的正index,相當於元素數目。
-
Lua 調用C函數用的堆棧是臨時的,調用結束之後就被銷燬了。
-
如何從堆棧中獲取從Lua腳本中的參數
-
如果知道Lua腳本中某個全局變量的名字,可以用void lua_getglobal (lua_State *L, const char *name) 。這個函數會將name所指Lua變量的值放在棧頂.
-
如果是在C 函數中要獲取Lua調用函數使用的參數:
-
首先用lua_gettop檢查參數數量
-
用lua_is...類函數檢測參數的類型,做好錯誤處理
-
用lua_to...類函數將參數轉換爲number或者string.(對Lua來說,只有這兩種簡單類型)
lua_tonumber返回的是double
lua_tostring返回的是char*
-
用lua_remove從棧中刪除掉元素
-
繼續獲取下一個元素. 因爲每次都調用lua_remove,所以每次調用lua_tonumber,使用的index都將固定是-1,即棧頂。
-
如果lua_istable成立,那麼說明棧頂是一個table.注意table是不能取出來的,只能把table裏的元素一個個取出來。
首先把元素的名字壓入棧頂: lua_pushstring(L,"i"); 然後就可以用lua_gettable調用,值會放在棧頂。同時剛纔壓入的元素名字被彈出。 用上面的辦法,可以把這個值取出來。記得也應該lua_remove。 如果table的某一個元素也是table,重複即可。 當table的所有元素都取完了,記住這個table本身還在堆棧裏,要用lua_remove把它刪除。
-
如果要獲取的是一個數組(所謂數組,其實就是key是從1開始的數字序列的table,並且值類型相同),用lua_next可以遍歷這個數組:
首先lua_pushnil,壓入一個空值,然後
while (lua_next(L, -2) != 0) { if(lua_isnumber(L,-1)) //判斷元素類型,也可能是string { arrf.add((float)lua_tonumber(L, -1));//獲取元素的值 lua_remove(L,-1); } } lua_remove(L,-1);//刪除NIL
-
-
-
如何從C返回數據給Lua腳本
用lua_push...類函數壓入數據到堆棧中,並用return n;來告訴Lua返回了幾個返回值。 Lua是天生支持多個返回值的,如 x,y = Test()。 Lua會根據n從棧裏取相應的數據。
如果要返回一個table:
lua_newtable(L);//創建一個表格,放在棧頂 lua_pushstring(L, "mydata");//壓入key lua_pushnumber(L,66);//壓入value lua_settable(L,-3);//彈出key,value,並設置到table裏面去 lua_pushstring(L, "subdata");//壓入key lua_newtable(L);//壓入value,也是一個table lua_pushstring(L, "mydata");//壓入subtable的key lua_pushnumber(L,53);//value lua_settable(L,-3);//彈出key,value,並設置到subtable lua_settable(L,-3);//這時候父table的位置還是-3,彈出key,value(subtable),並設置到table裏去 lua_pushstring(L, "mydata2");//同上 lua_pushnumber(L,77); lua_settable(L,-3); return 1;//堆棧裏現在就一個table.其他都被彈掉了。
如果要返回一個數組,用如下代碼:(注意那個關於trick的註釋,我在等官方的解釋。經過驗證,這個問題只在windows版本調用dll中方法的時候出現。WinCE正常)
lua_pushstring(L,"arri"); lua_newtable(L); { //a trick:otherwise the lua engine will crash. This element is invisible in Lua script lua_pushnumber(L,-1); lua_rawseti(L,-2,0); for(int i = 0; i < arri.size();i++) { lua_pushnumber(L,arri[i]); lua_rawseti(L,-2,i+1); } } lua_settable(L,-3);
這樣產生的數組可以在Lua中如下遍歷:
for i,v in ipairs(data.arri) do print(v) end
或者是
for i=1,table.getn(data.arri) do print(data.arri[i]) end
只有數組才能這樣,name,value構成的Record不行,table.getn也只對數組有效。
-
由於上述代碼的高度相似性,所以很容易實現自動生成這些代碼。比如,根據C的一個struct定義:
typedef enum { BR_9600, BR_4800, } BaudRate; typedef struct flag { int onoff; int j; long l; double d; char* name; BaudRate rate; }flag;
可以自動產生如下代碼:
bool DataToLua(flag data,lua_State *L) { lua_newtable(L); lua_pushstring(L,"onoff"); lua_pushnumber(L,(double)data.onoff); lua_settable(L,-3); lua_pushstring(L,"j"); lua_pushnumber(L,(double)data.j); lua_settable(L,-3); lua_pushstring(L,"l"); lua_pushnumber(L,(double)data.l); lua_settable(L,-3); lua_pushstring(L,"d"); lua_pushnumber(L,(double)data.d); lua_settable(L,-3); lua_pushstring(L,"name"); lua_pushstring(L,data.name.c_str()); lua_settable(L,-3); lua_pushstring(L,"rate"); lua_pushnumber(L,(double)(int)data.rate); lua_settable(L,-3); return true; }
LuaToData也是類似的。
如果使用面向對象的方式封裝起flag來,把DataToLua變成flag類的一個方法,就更加方便了。
首先是C的主程序初始化Lua腳本引擎,並註冊一些函數供腳本中調用:
//function for Lua to call //return a integer array to the script static int l_getarr (lua_State *L) { lua_newtable(L);//create table lua_pushnumber(L,1);//push the value lua_rawseti(L,-2,1);//set t[1]=v lua_pushnumber(L,2); lua_rawseti(L,-2,2); lua_pushnumber(L,3); lua_rawseti(L,-2,3); lua_pushnumber(L,4); lua_rawseti(L,-2,4); return 1; } int main() { lua_State *L = lua_open(); /* opens Lua */ luaopen_base(L); /* opens the basic library */ luaopen_table(L); /* opens the table library */ luaopen_string(L); /* opens the string lib. */ luaopen_math(L); /* opens the math lib. */ lua_pushcfunction(L, l_getarr); // Register a function lua_setglobal(L, "getarr"); if (lua_dofile(L, "testlua.lua"))//Load the script file and Run it { printf("run script failed/n"); } else { lua_getglobal(L, "result"); //Get the global variant in Lua script if(lua_isnumber(L,-1)) { printf("The result of the Lua script is %d/n",lua_tonumber(L,-1)); } } lua_close(L); return 0; }
腳本的代碼如下:
array = getarr() if array ~= nil then result = 1 for i=1,table.getn(array),1 do print(array[i]) end else result = 0 end