Lua 中棧操作的C API樣本

來源:互聯網
上載者:User

標籤:lua stack   lua_typename   


這是《Lua程式設計》中的例子,做個簡單記錄。
#include <stdio.h>#include <lua5.2/lua.h>#include <lua5.2/lauxlib.h>static void stackDump(lua_State *L){    int i;    int top = lua_gettop(L);    for(i = 1; i <= top; i++){        int t = lua_type(L, i);        switch(t){            case LUA_TSTRING:                printf("'%s'", lua_tostring(L, i));                break;            case LUA_TBOOLEAN:                printf(lua_toboolean(L, i) ? "true":"false");                break;            case LUA_TNUMBER:                printf("%g", lua_tonumber(L, i));                break;            default:                printf("%s", lua_typename(L, t));                break;        }        printf("  ");    }    printf("\n");}int main(void){    lua_State *L = luaL_newstate();    lua_pushboolean(L, 1);    lua_pushnumber(L, 10);    lua_pushnil(L);    lua_pushstring(L, "vonzhou");    stackDump(L);    // dump the stack    lua_pushvalue(L, -4);// push the value of the index to the stack    stackDump(L);    lua_replace(L, 3);  // pop a value and replace the index's    stackDump(L);    lua_settop(L, 6);   // set the top index, fill 'nil'    stackDump(L);    lua_remove(L, -3);   //     stackDump(L);    lua_settop(L, -5);    stackDump(L);    lua_close(L);    return 0;}
運行結果:

簡單畫個:



Lua 中棧操作的C API樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.