设为首页 加入收藏

TOP

Lua 中栈操作的C API示例
2015-07-20 17:47:10 来源: 作者: 【 】 浏览:4
Tags:Lua 操作 API 示例

这是《Lua程序设计》中的例子,做个简单记录。
#include 
  
   
#include 
   
     #include 
    
      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; }
    
   
  
运行结果: \

简单画个示意图:



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++设计模式之状态模式(四) 下一篇C++设计模式之外观模式(二)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)