#Lua# Stack Manipulation

2017-03-28  本文已影响41人  KomalZheng

Lua C API提供了操纵The Stack的函数:

获取The Stack元素个数:

int lua_gettop(lua_State *L);

将栈顶设置到某个index:

void lua_settop(lua_State *L, int index);

如果index小于The Stack中的元素个数,那么The Stack发生截断;
如果index对应的即是栈顶元素,那么不做任何操作;
如果index对应的元素不存在,那么,The Stack将设置栈顶到index对应除,并使用nil填充扩展的值;

将某个元素复制后,push到栈顶

void lua_pushvalue(lua_State *L, int index);

旋转操作(暂时未看源码,不知何用)

void lua_rotate(lua_State *L, int index, int n);

拷贝指定位置的元素值到目标位置

拷贝 fromidx 对应的值到 toidx位置,于是toidx位置的值便无法再被访问

void lua_copy (lua_State *L, int fromidx, int toidx);

宏定义

#define lua_insert(L,idx)   lua_rotate(L, (idx), 1)

#define lua_remove(L,idx)   (lua_rotate(L, (idx), -1), lua_pop(L, 1))

#define lua_replace(L,idx)  (lua_copy(L, -1, (idx)), lua_pop(L, 1))

平凡操作

以下的操作,将不发生任何变化

lua_settop(L, -1);
lua_insert(L, -1);
lua_copy(L, x, x);
上一篇 下一篇

猜你喜欢

热点阅读