lua由c語言實現的嵌入式語言。c api是一組能使c**和lua互動的函式。其中包括讀寫lua全域性變數、呼叫lua函式、執行lua**,以及註冊c函式以供lua**呼叫。
lua_state *l = lual_newstate(); /*開啟lua*/
lual_openlibs(l); /*開啟標準庫*/
lua_pop(l, 1); /*從棧中彈出錯誤訊息*/
lua壓入元素
void lua_pushnil (lua_state *l);
void lua_pushboolean (lua_state *l, int bool);
void lua_pushnumber (lua_state *l, lua_number n);
void lua_pushinteger (lua_state *l, lua_integer n); // 整形,儲存大型字串的長度,定義ptrdiff_t
void lua_pushlstring (lua_state *l, const char *s , size_t len);
void lua_pushstring (lua_state *l, const char *s);
檢查棧空間
int lua_checkstack(lua_state *l ,int sz);
檢查特定型別 (檢查值是否能轉換為數字,對任意數字,lua_isstring都返回真)
int lua_is* (lua_state *l, int index); //*可以是任意lua型別。lua_isnumber\lua_isstring\lua_istable
int lua_type (lua_state *l, int index);
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)
lua_type返回堆疊元素的值型別,當使用無效索引時返回lua_tnone(如當堆疊為空的時候)。lua_type返回的型別**為如下在lua.h中定義的常量:lua_tnil,lua_tnumber,lua_tboolean,lua_tstring,lua_ttable,lua_tfunction,lua_userdata,lua_ttheard,lua_tlightuserdata。下面的函式可以將這些常量轉換為字串:
const char* lua_typename (lua_state* l, int type);
從棧中獲取乙個值
lua_to*()
int lua_toboolean(lua_state *l, int index);
lua_number lua_tonumber(lua_state *l, index);
lua_integer lua_tointeger(lua_state *l, index);
const char *lua_tolstring (lua_state *l, int index, size_t *len);
size_t lua_objlen (lua_state *l, int index);
其他棧操作
int lua_gettop (lua_state *l);//返回棧中元素個數,也可以說事站定元素的引索
void lua_settop(lua_state *l, int index);//將棧頂設定為乙個制定的位置,即修改佔中元素的數量。多出來的將被捨棄
void lua_pushvalue(lua_state *l, int index);//將制定的索引上值得副本壓入棧。
void lua_remove(lua_state *l , int index);//刪除指定索引上的元素,並將該位置之上的所有元素下移以填補空缺。
void lua_insert(lua_state *l, int index);//移動指定位置之上的所有元素以開闢乙個槽的空間,然後將棧頂元素移到該位置。
void lua_replace (lua_state *l, int index);//彈出棧頂的值,並將該值設定到制定索引上,但他不會移動任何東西,
#define lua_pop(l, n) lua_settop(l, -(n) - 1); //定義巨集從棧彈出n個元素
以下操作不會對棧產生影響:
lua_settop(l, -1); /*將棧頂元素設定為它的當前值*/
lua_insert(l, -1) ; /*將棧頂元素移動到棧頂*/
lual_loadfile(l, fname); //從檔案fname引導程式塊
lua_pcall(l,0,0,0);//執行編譯好的程式塊。第二個引數是傳給待呼叫函式的引數個數,
第三個引數是期望的結果個數,第四個引數是乙個錯誤處理函式的索引。
若發生錯誤(檔案fname中得語法錯誤),這兩個函式都會把錯誤訊息壓入棧,並返回乙個非零的錯誤**。
此時,程式就呼叫lua_tostring從棧頂獲取該訊息。
lua_getglobal(l,"var")
會執行兩步操作:1.將var放入棧中,2.由lua去尋找變數var的值,並將變數var的值返回棧頂(替換var)。
lua與C(一) C呼叫lua
lua和c有兩種關係 一種是在lua中呼叫c的函式,c稱為庫 一種是c中呼叫lua,c就稱為應用程式 此時c中包含了lua的直譯器 c 部分 注意在c 中,通常要把lua的一些標頭檔案定義在extern c 中,因為他們是c語言實現的。1.作為應用程式呼叫lua char buffer print ...
C 與Lua互動(一)
之前做手遊專案時,客戶端用lua做指令碼,基本所有遊戲邏輯都用它完成,玩起來有點不爽,感覺 太重 了。而我又比較偏服務端這邊 僅有c 所以熱情不高。最近,加入了乙個端遊專案,客戶端和伺服器都用了lua,各種玩法是讓我眼界大開。在此,寫下自己的心得與體會,希望能與大家共同 進步。lua官網 cmake...
Cocos2d x中Lua與C 通訊
int lual dofile lua state l,const char filename 載入並執行指定檔案,沒有錯誤返回0 void lua settop lua state l,int index 引數允許傳入任何可接受的索引以及 0 它將把堆疊的棧頂設為這個索引。如果新的棧頂比原來的大,...