人人都爱奴儿
Stack
Manipulation(Offical API reforence 3.3 Stack Manipulation)
The API offers the following functions for basic stack manipulation:
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);
As an example, if the stack starts as 10 20 30 40 50* (from bottom to top; the ‘*’ marks the top), then
lua_pushvalue(L, 3) --> 10 20 30 40 50 30*
lua_pushvalue(L, -1) --> 10 20 30 40 50 30 30*
lua_remove(L, -3) --> 10 20 30 40 30 30*
lua_remove(L, 6) --> 10 20 30 40 30*
lua_insert(L, 1) --> 30 10 20 30 40*
lua_insert(L, -1) --> 30 10 20 30 40* (no effect)
lua_replace(L, 2) --> 30 40 20 30*
lua_settop(L, -3) --> 30 40*
lua_settop(L, 6) --> 30 40 nil nil nil nil*
在lua.h中有macro:
#define lua_pop(L,n) lua_settop(L, -(n)-1)
则
pop(n)==settop(-n-1)
settop(n)==pop(-n-1) == push(n)
曰:出栈几个元素,相当于将这个数字加一,然后用和的相反数作为栈顶;从栈顶往下数了几个数标记为栈顶,则将这个数加一,然后用这个和数出栈。好比公司要扩招了,就招n个伢,那就将n个伢入栈,工号一个个的加,加n次。或者要裁员了,就裁m个伢,则按顺序将工号最大的前面m个伢遣散了,老员工暂时不动,公司裁员后的最大的工号嘛就将裁员人数加一后,往下数这个数,数到哪个,哪个就是工号最大的伢了。
Querying(Offical lua api reference 3.4 Querying the Stack)
To check the type of a stack element, the following functions are available:
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);
The API also contains functions to compare two values in the stack:
int lua_equal (lua_State *L, int index1, int index2);
int lua_rawequal (lua_State *L, int index1, int index2);
int lua_lessthan (lua_State *L, int index1, int index2);
曰:黄帝生众人,众人生万物,万物生八卦,八卦生努尔盏,lua栈就是众生能触摸到的祖先的气。你可以在奴儿盏里随心所欲的玩,逾矩的玩,窥视那些终生像:忠奸善恶,数串表函,是不是三聚氰胺防腐剂,是不是裸官醉驾二次碾压.
取栈值(offical api 3.5 Getting Values from the Stack)
To translate a value in the stack to a specific C type, you can use the following conversion functions:
int lua_toboolean (lua_State *L, int index);
lua_Number lua_tonumber (lua_State *L, int index);
const char *lua_tostring (lua_State *L, int index);
size_t lua_strlen (lua_State *L, int index);
lua_CFunction lua_tocfunction (lua_State *L, int index);
void *lua_touserdata (lua_State *L, int index);
lua_State *lua_tothread (lua_State *L, int index);
void *lua_topointer (lua_State *L, int index);
曰:可以将盏里的东西读出来:数、串、真、假、函、线、针。不管盏里放的是神马五谷杂粮,都可以读出来啦。
存栈(offical api reference 3.6 Pushing Values onto the Stack)
The API has the following functions to push C values onto the stack:
void lua_pushboolean (lua_State *L, int b);
void lua_pushnumber (lua_State *L, lua_Number n);
void lua_pushlstring (lua_State *L, const char *s, size_t len);
void lua_pushstring (lua_State *L, const char *s);
void lua_pushnil (lua_State *L);
void lua_pushcfunction (lua_State *L, lua_CFunction f);
void lua_pushlightuserdata (lua_State *L, void *p);
曰:五谷杂粮都可以往盏里装。最好是五年藏梅花上的雪,夏日收在盏里。
栈指针(offical api reference 3.2 The Stack and Indices)
A positive index represents an absolute stack position (starting at 1);
a negative index represents an offset from the top of the stack.
We say that an index is valid if it lies between 1 and the stack top (that is, if 1 <= abs(index) <= top).
At any time, you can get the index of the top element by calling lua_gettop:
int lua_gettop (lua_State *L);
grows the stack size to top + extra elements:
int lua_checkstack (lua_State *L, int extra);
Whenever Lua calls C, it ensures that at least LUA_MINSTACK stack positions are available.LUA_MINSTACK is defined in lua.h
as 20
More formally, we define an acceptable index as follows:
(index < 0 && abs(index) <= top) || (index > 0 && index <= stackspace)
Note that 0 is never an acceptable index
曰:
取栈中顶元指针的值,这个值既指明了栈中顶元的指针,也同时等于栈元的总数。
你有责任使用有效的栈针,不要随便拿个针就到处戳。
可以增加栈,但不能收缩栈。
0针是不能被接受的,不要用0针到处戳。