Lua中C API棧操作

來源:互聯網
上載者:User

標籤:style   blog   color   使用   資料   cti   

向棧中壓入資料:

lua_pushnil(lua_State*);

lua_pushboolean(lua_State*, bool);

lua_pushnumber(lua_State*, lua_Number);

lua_pushinteger(lua_State*, lua_Integer)

lua_pushlstring(lua_State*, const char*, size_t);

lua_pushstring(lua_State*, const char*);

擷取棧中元素的類型

lua_type(lua_State* L, int index);    

類型包括 LUA_TNIL, LUA_TBOOLEAN, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE, LUA_TFUNCTION, LUA_TUSERDATA

驗證棧中的元素類型;

lua_is*(lua_State* L, int index)

其實有了lua_type()完全可以自己寫類型驗證的 每必要使用lua提供的API

比如可以定義

 bool lua_is_number (lua_State* L, int index)  {    return lua_type(L, index) == LUA_TNUMBER ? true : false; }

所以完全是可以利用lua_type來自行定義對棧中元素的驗證的。

擷取棧中元素的類型的字串表示:

lua_typename(lua_State*, int )這裡的int值是通過lua_type()擷取到的值

其實這些都可以通過lua_type(lua_State*)來進行實現

const char*  lua_type_str(int lua_type) {    static char type[LUA_TYPE_LEN_MAX];    switch (lua_type) {        case  LUA_TNIL:            strcpy(type, "nil");        break;        case  LUA_TBOOLEAN:            strcpy(type, "boolean");            break;        case  LUA_TNUMBER:            strcpy(type, "number");            break;        case  LUA_TSTRING:            strcpy(type, "string");            break;        case  LUA_TTABLE:            strcpy(type, "table");            break;        case  LUA_TTHREAD:            strcpy(type, "thread");            break;        case  LUA_TFUNCTION:            strcpy(type, "function");            break;        case  LUA_TUSERDATA:            strcpy(type, "userdata");            break;        default:            strcpy(type, "unknown");            break;    }    return  type;}

這樣就可以實現你自己的棧元素類型的字串顯示了

擷取指定位置的元素可以使用lua_to*(lua_State*, int )

int  lua_toboolean(lua_State*, int);

lua_Number lua_tonumber(lua_State*, int);

lua_Integer lua_tointeger(lua_State*, int );

const char* lua_tolstring(lua_State*, int, size_t length);

lua_gettop(lua_State*)      得到棧中元素的個數

lua_settop(lua_State*, int index)

設定棧中元素的個數,如果設定的值比當前棧中元素的個數多,則將多出的那部分全部丟棄,如果設定的值比當前棧中元素個數多,則將新加的值全部設定為nil.

lua_pushvalue(lua_State*, int index) 將index位置的值的副本壓入棧頂

lua_remove(lua_State* L, int index)將指定位置的元素刪除,並且其上的所有元素下移

lua_insert(lua_State*, int index) 將指定位置之上的元素上移,空出該位置,並將棧頂元素移到此處

lua_replace(lua_State*, int index)將棧頂元素彈出,並將其設定到指定的索引上。

 

lua_pop是以一個宏的形式進行定義的

#define    lua_pop(L, n)     lua_settop(L, -(n) - 1);

比如彈出棧頂的元素則可以使用 lua_pop(L, 1)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.