lua堆栈操作常用函数学习二

  1. /*
  2. ** basic stack manipulation
  3. */
  4. LUA_API int <strong> (lua_gettop) (lua_State *L); </strong><pre class="cpp" name="code"></pre><pre class="cpp" name="code">//返回当前堆栈的大小
  5. LUA_API int lua_gettop (lua_State *L) {
  6. return cast_int(L->top - L->base);
  7. }
  8. int lua_gettop(lua_State *L)
  9. 取得栈的高度
  10. for (int i = 0; i < 10; ++i)
  11. lua_pushnumber(L, i);
  12. printf("%d", lua_gettop(L));
  13. --> 10
  14. LUA_API void (lua_settop) (lua_State *L, int idx);
  15. <pre class="cpp" name="code">//settop()是破坏性的 变小后数据就不恢复了
  16. LUA_API void lua_settop (lua_State *L, int idx) {
  17. lua_lock(L);
  18. if (idx >= 0) { //正数索引 是向外的有可能大于当前大小 但负数则不会过大越界 所以不会有填nil的机会
  19. api_check(L, idx <= L->stack_last - L->base);
  20. while (L->top < L->base + idx)//如果设置的大于当前堆栈大小 则填nil
  21. setnilvalue(L->top++);
  22. L->top = L->base + idx;
  23. }
  24. else {
  25. api_check(L, -(idx+1) <= (L->top - L->base));
  26. L->top += idx+1; /* `subtract' index (index is negative) */
  27. }
  28. lua_unlock(L);
  29. }
  30. void lua_settop(lua_State *L, int idx)
  31. <strong>设置栈的高度,如果之前的栈顶比新设置的更高,那么高出来的元素会被丢弃,反之压入nil来补足大小。</strong>
  32. 另外,Lua提供了一个宏,用来从栈中弹出n个元素:#define lua_pop(L, n) lua_settop(L, -(n)-1)
  33. for (int i = 0; i < 10; ++i)
  34. lua_pushnumber(L, i);
  35. lua_settop(L, 5)
  36. printf("%d", lua_gettop(L));
  37. --> 5
  38. LUA_API void (lua_pushvalue) (lua_State *L, int idx);<pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API void lua_pushvalue (lua_State *L, int idx) {
  39. lua_lock(L);
  40. setobj2s(L, L->top, index2adr(L, idx)); <strong>//拷贝指定索引的值压入栈顶 栈顶位置更新</strong>
  41. api_incr_top(L);
  42. lua_unlock(L);
  43. }
  44. void lua_pushvalue(lua_State *L, int idx)
  45. 将指定索引上值的副本压入栈
  46. for (int i = 1; i <= 3; ++i)
  47. lua_pushnumber(i);
  48. 栈中元素:(从下往上) 1 2 3
  49. lua_pushvalue(L, 2)
  50. 栈中元素:(从下往上) 1 2 3 2</pre><pre class="cpp" name="code"> </pre><br>
  51. LUA_API void (lua_remove) (lua_State *L, int idx);<pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API void lua_remove (lua_State *L, int idx) {
  52. StkId p;
  53. lua_lock(L);
  54. p = index2adr(L, idx);//取得指定索引
  55. api_checkvalidindex(L, p);
  56. while (++p < L->top)
  57. setobjs2s(L, p-1, p);//将索引后的数据一一迁移
  58. L->top--;
  59. lua_unlock(L);
  60. }
  61. void lua_remove(lua_State *L, int idx)
  62. 删除指定索引上的元素,并将该位置之上的所有元素上移以补空缺
  63. for (int i = 1; i <= 3; ++i)
  64. lua_pushnumber(i);
  65. 栈中元素:(从下往上) 1 2 3
  66. lua_remove(L, 2)
  67. 栈中元素:(从下往上) 1 3
  68. </pre><br>
  69. LUA_API void (lua_insert) (lua_State *L, int idx);<pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API void lua_insert (lua_State *L, int idx) {
  70. StkId p;
  71. StkId q;
  72. lua_lock(L);
  73. p = index2adr(L, idx);//找到指定索引值处
  74. api_checkvalidindex(L, p);
  75. for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);//将后面的数据依次后移
  76. setobjs2s(L, p, L->top);
  77. lua_unlock(L);
  78. }
  79. void lua_insert(lua_State *L, int idx)
  80. 移指定位置上的所有元素以开辟一个空间槽的空间,然后将栈顶元素移到该位置
  81. for (int i = 1; i <= 5; ++i)
  82. lua_pushnumber(i);
  83. 栈中元素:(从下往上) 1 2 3 4 5
  84. lua_insert(L, 3)
  85. 栈中元素:(从下往上) 1 2 5 4 3
  86. </pre><br>
  87. LUA_API void (lua_replace) (lua_State *L, int idx);
  88. <pre></pre>
  89. <pre class="cpp" name="code"> </pre><pre class="cpp" name="code"><pre class="cpp" name="code">LUA_API void lua_replace (lua_State *L, int idx) {
  90. StkId o;
  91. lua_lock(L);
  92. api_checknelems(L, 1);//检查是否有元素
  93. o = index2adr(L, idx);
  94. api_checkvalidindex(L, o);
  95. if (idx == LUA_ENVIRONINDEX) {
  96. Closure *func = curr_func(L);
  97. api_check(L, ttistable(L->top - 1));
  98. func->c.env = hvalue(L->top - 1);
  99. luaC_barrier(L, func, L->top - 1);
  100. }
  101. else {
  102. setobj(L, o, L->top - 1);//将栈顶元素 拷贝到指定所以处
  103. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  104. luaC_barrier(L, curr_func(L), L->top - 1);
  105. }
  106. L->top--;//栈顶减少
  107. lua_unlock(L);
  108. }</pre>void lua_replace(lua_State *L, int idx)<br>
  109. <strong>弹出栈顶的值,并将该值设置到指定索引上,但它不会移动任何东西<br>
  110. </strong>for (int i = 1; i <= 5; ++i)<br>
  111. lua_pushnumber(i);<br>
  112. 栈中元素:(从下往上) 1 2 3 4 5<br>
  113. lua_replace(L, 3)<br>
  114. 栈中元素:(从下往上) 1 2 5 4<br>
  115. <br>
  116. <pre></pre>
  117. <pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int (lua_checkstack) (lua_State *L, int sz);</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int lua_checkstack (lua_State *L, int size) {
  118. int res;
  119. lua_lock(L);
  120. if ((L->top - L->base + size) > LUAI_MAXCSTACK)
  121. res = 0; /* stack overflow */
  122. else {
  123. luaD_checkstack(L, size);
  124. if (L->ci->top < L->top + size)
  125. L->ci->top = L->top + size;
  126. res = 1;
  127. }
  128. lua_unlock(L);
  129. return res;
  130. }
  131. </pre><pre class="cpp" name="code">int lua_checkstack(lua_State *L, int sz)
  132. 扩大栈的可用尺寸,栈的默认尺寸是20,此函数会确保堆栈上至少有 sz 个空位。如果不能把堆栈扩展到相应的尺寸,函数返回 false 。这个函数永远不会缩小堆栈;如果堆栈已经比需要的大了,那么就放在那里不会产生变化。
  133. lua_checkstack(L, 100)
  134. </pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);<pre class="cpp" name="code">//将一个堆栈上的从栈顶起的n个元素 移到另一个堆栈上
  135. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  136. int i;
  137. if (from == to) return;
  138. lua_lock(to);
  139. api_checknelems(from, n);//确认 从from有n个元素
  140. api_check(from, G(from) == G(to));
  141. api_check(from, to->ci->top - to->top >= n);//确认to有n个元素
  142. from->top -= n;
  143. for (i = 0; i < n; i++) {
  144. setobj2s(to, to->top++, from->top + i);
  145. }
  146. lua_unlock(to);
  147. }</pre><br>
  148. <pre></pre>
  149. <pre class="cpp" name="code"> </pre><pre class="cpp" name="code">/*** access functions (stack -> C)*/</pre><pre class="cpp" name="code">//类型判断函数</pre><pre class="cpp" name="code">LUA_API int (lua_isnumber) (lua_State *L, int idx);</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int (lua_isstring) (lua_State *L, int idx);</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int (lua_iscfunction) (lua_State *L, int idx);</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int (lua_isuserdata) (lua_State *L, int idx);</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">LUA_API int (lua_type) (lua_State *L, int idx);</pre><pre class="cpp" name="code">int lua_type(lua_State *L, int idx)
  150. 得到一个元素的类型,返回整型,返回值是如下列表之一:<p>int lua_isnil(lua_State *L, int idx);
  151. int lua_isboolean(lua_State *L, int idx);
  152. int lua_istable(lua_State *L, int idx);
  153. int lua_isfunction(lua_State *L, int idx);
  154. int lua_islightuserdata (lua_State *L, int idx);</p><p>#define LUA_TNONE (-1)</p><p>#define LUA_TNIL 0
  155. #define LUA_TBOOLEAN 1
  156. #define LUA_TLIGHTUSERDATA 2
  157. #define LUA_TNUMBER 3
  158. #define LUA_TSTRING 4
  159. #define LUA_TTABLE 5
  160. #define LUA_TFUNCTION 6
  161. #define LUA_TUSERDATA 7
  162. #define LUA_TTHREAD 8</p><p>lua_pushnumber(L, 55);
  163. lua_type(L, 1)-->LUA_TNUMBER
  164. ---------------------------------------------------------------------------------------
  165. const char* lua_typename(lua_State *L, int tp)
  166. 将一个类型编码转换成类型名
  167. lua_typename(L, 1)-->boolean
  168. lua_typename(L, 3)-->number
  169. ---------------------------------------------------------------------------------------</p><p>
  170. int lua_equal(lua_State *L, int idx1, int idx2)
  171. 如果依照 Lua 中 == 操作符语义,索引 index1 和 index2 中的值相同的话,返回 1 。否则返回 0 。如果任何一个索引无效也会返回 0。
  172. lua_pushstring(L, "this");
  173. lua_pushboolean(L, 1);
  174. lua_pushboolean(L, 1);
  175. lua_equal(L, -2, -3)
  176. -->0
  177. lua_equal(L, -1, -2)
  178. -->1
  179. lua_equal(L, -1, -10)
  180. -->0
  181. </p></pre><pre class="cpp" name="code"> int lua_rawequal(lua_State *L, int idx1, int idx2)
  182. int lua_lessthan(lua_State *L, int idx1, int idx2)<p> </p><p>---------------------------------------------------------------------------------------
  183. lua_Number lua_tonumber(lua_State *L, int idx)
  184. lua_Integer lua_tointeger(lua_State *L, int idx)
  185. int lua_toboolean(lua_State *L, int idx)
  186. const char* lua_tolstring(lua_State *L, int idx, size_t *len)
  187. 以上四个函数都有一个原型lua_to*(lua_State *L, int idx),用于从栈中取一个值。如果指定的元素不具有正确的类型,调用这些函数也不会有问题,
  188. 在这种情况下,调用lua_toboolean,lua_tonumber,lua_tointeger会返回0,其它函数会返回NULL。通常不使用lua_is*函数,只需在调用它们之
  189. 后测试返回结果是否为NULL就可以了。
  190. lua_pushnumber(L, 100)
  191. lua_tonumber(L, 1)-->100
  192. lua_pushinteger(L, 200)
  193. lua_tointeger(L, -1)-->200
  194. lua_pushboolean(L, 0)
  195. lua_toboolean(L, -1)-->false
  196. lua_pushstring(L, "hello,lua")
  197. lua_tolstring(L, -1, &len)-->hello,lua
  198. 注:len是传出参数,表示字符串的长度,如果想忽略此参数,传入NULL
  199. ---------------------------------------------------------------------------------------
  200. size_t lua_objlen(lua_State *L, int idx)
  201. 返回值的长度,如果类型不正确,返回0
  202. lua_pushstring(L, "hello,lua")
  203. lua_objlen(L, 1)-->9
  204. ---------------------------------------------------------------------------------------
  205. lua_CFunction lua_tocfunction(lua_State *L, int idx)
  206. void* lua_touserdata(lua_State *L, int idx)
  207. lua_State* lua_tothread(lua_State *L, int idx)
  208. const void* lua_topointer(lua_State *L, int idx)</p><p> </p><p>//push functions (C -> stack)
  209. void lua_pushnil(lua_State *L)
  210. void lua_pushnumber(lua_State *L, lua_Number n)
  211. void lua_pushinteger(lua_State *L, lua_Integer n)
  212. void lua_pushlstring(lua_State *L, const char* s, size_t l)
  213. void lua_pushstring(lua_State *L, const char *s)
  214. const char* lua_pushvfstring(lua_State *L, const char *fmt, va_list argp)
  215. const char* lua_pushfstring(lua_State *L, const char *fmt, ...)
  216. void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
  217. void lua_pushboolean(lua_State *L, void *b)
  218. void lua_pushlightuserdata(lua_State *L, void *p)
  219. int lua_pushthread(lua_State *L)</p><p>
  220. void lua_gettable(lua_State *L, int idx)
  221. ---------------------------------------------------------------------------------------
  222. void lua_getfield(lua_State *L, int idx, const char *k)
  223. 把 t[k] 值压入堆栈,这里的 t 是指有效索引 index 指向的值。在 Lua 中,这个函数可能触发对应 "index" 事件的元方法
  224. ---------------------------------------------------------------------------------------
  225. void lua_rawget(lua_State *L, int idx)
  226. void lua_rawgeti(lua_State *L, int idx, int n)
  227. ---------------------------------------------------------------------------------------
  228. void lua_createtable(lua_State *L, int narr, int nrec)
  229. 创建一个新的空 table 压入堆栈。这个新 table 将被预分配 narr 个元素的数组空间以及 nrec 个元素的非数组空间。当你明确知道表中需要多少个元素时,预分配就非常有用。如果你不知道,可以使用函数 lua_newtable。
  230. 举例暂缺
  231. ---------------------------------------------------------------------------------------
  232. void* lua_newuserdata(lua_State *L, size_t sz)
  233. int lua_getmetatable(lua_State *L, int objindex)
  234. ---------------------------------------------------------------------------------------
  235. void lua_getfenv(lua_State *L, int idx)</p><p>
  236. 把索引处值的环境表压入堆栈
  237. ---------------------------------------------------------------------------------------
  238. void lua_settable(lua_State *L, int idx);
  239. void lua_setfield(lua_State *L, int idx, const char *k)
  240. void lua_rawset(lua_State *L, int idx)
  241. void lua_rawseti(lua_State *L, int idx, int n)
  242. int lua_setmetatable(lua_State *L, int objindex)
  243. int lua_setfenv(lua_State *L, int idx)</p><p>
  244. //'load' and 'call' functions (load and run lua code)
  245. void lua_call(lua_State *L, int nargs, int nresults);
  246. int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
  247. ---------------------------------------------------------------------------------------
  248. int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
  249. 以保护模式调用 C 函数 func 。 func 只有能从堆栈上拿到一个参数,就是包含有 ud 的 light userdata。当有错误时, lua_cpcall 返回和 lua_pcall 相同的错误代码,并在栈顶留下错误对象;否则它返回零,并不会修改堆栈。所有从 func 内返回的值都会被扔掉。
  250. 举例暂缺
  251. ---------------------------------------------------------------------------------------
  252. int lua_load(lua_State *L, lua_Reader reader, void *dt, const char *chunkname);
  253. ---------------------------------------------------------------------------------------
  254. int lua_dump(lua_State *L, lua_Writer writer, void *data);
  255. 把函数 dump 成二进制 chunk 。函数接收栈顶的 Lua 函数做参数,然后生成它的二进制 chunk 。若被 dump 出来的东西被再次加载,加载的结果就相当于原来的函数。当它在产生 chunk 的时候,lua_dump 通过调用函数 writer (参见 lua_Writer)来写入数据,后面的 data 参数会被传入 writer 。
  256. 最后一次由写入器 (writer) 返回值将作为这个函数的返回值返回; 0 表示没有错误。
  257. 这个函数不会把 Lua 返回弹出堆栈。
  258. 举例暂缺
  259. ---------------------------------------------------------------------------------------
  260. int lua_yield(lua_State *L, int nresults)
  261. int lua_resume(lua_State *L, int narg)
  262. int lua_status(lua_State *L)</p><p>---------------------------------------------------------------------------------------
  263. int lua_gc(lua_State *L, int what, int data)</p><p>控制垃圾收集器。 这个函数根据其参数 what 发起几种不同的任务:
  264. * LUA_GCSTOP: 停止垃圾收集器。
  265. * LUA_GCRESTART: 重启垃圾收集器。
  266. * LUA_GCCOLLECT: 发起一次完整的垃圾收集循环。
  267. * LUA_GCCOUNT: 返回 Lua 使用的内存总量(以 K 字节为单位)。
  268. * LUA_GCCOUNTB: 返回当前内存使用量除以 1024 的余数。
  269. * LUA_GCSTEP: 发起一步增量垃圾收集。步数由 data 控制(越大的值意味着越多步),而其具体含义(具体数字表示了多少)并未标准化。如果你想控制这个步数,必须实验性的测试 data 的值。如果这一步结束了一个垃圾收集周期,返回返回 1 。
  270. * LUA_GCSETPAUSE: 把 data/100 设置为 garbage-collector pause 的新值。函数返回以前的值。
  271. * LUA_GCSETSTEPMUL: 把 arg/100 设置成 step multiplier 。函数返回以前的值。
  272. ---------------------------------------------------------------------------------------
  273. int lua_error(lua_State *L)
  274. 产生一个 Lua 错误。错误信息(实际上可以是任何类型的 Lua 值)必须被置入栈顶。这个函数会做一次长跳转,因此它不会再返回。(参见 luaL_error)。
  275. lua_pushstring(L, "one error");
  276. lua_error(L);
  277. printf("%s", "本行已经执行不到了");
  278. ---------------------------------------------------------------------------------------
  279. int lua_next(lua_State *L, int idx)
  280. ---------------------------------------------------------------------------------------
  281. void lua_concat(lua_State *L, int n)
  282. 连接栈顶的 n 个值,然后将这些值出栈,并把结果放在栈顶。如果 n 为 1 ,结果就是一个字符串放在栈上(即,函数什么都不做);如果 n 为 0 ,结果是一个空串。 连接依照 Lua 中创建语义完成,如果尝试把两个不能连接的类型连接,程序会给出错误提示。
  283. lua_pushstring(L, "this");
  284. lua_pushboolean(L, 1);
  285. lua_pushnumber(L, 9989);
  286. lua_pushnumber(L, 1111);
  287. lua_pushboolean(L, 0);
  288. lua_pushstring(L, "满天都是小星星");
  289. lua_pushnumber(L, 1986);
  290. lua_pushstring(L, "onebyone");
  291. -->'this' 'true' '9989' '1111' 'false' '满天都是小星星' '1986' 'onebyone'
  292. lua_concat(L, 3);
  293. -->'this' 'true' '9989' '1111' 'false' '满天都是小星星1986onebyone'
  294. ---------------------------------------------------------------------------------------
  295. lua_Alloc lua_getallocf(lua_State *L, void **ud)
  296. 返回给定状态机的内存分配器函数。如果 ud 不是 NULL ,Lua 把调用 lua_newstate 时传入的那个指针放入 *ud 。
  297. ---------------------------------------------------------------------------------------
  298. void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)</p><p>
  299. //Functions to be called by the debuger in specific events
  300. int lua_getstack(lua_State *L, int level, lua_Debug *ar)
  301. int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
  302. const char* lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
  303. const char* lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
  304. const char* lua_getupvalue(lua_State *L, int funcindex, int n)
  305. const char* lua_setupvalue(lua_State *L, int funcindex, int n)
  306. int lua_sethook(lua_State *L, lua_Hook func, int mask, int count);
  307. lua_Hook lua_gethook(lua_State *L)
  308. int lua_gethookmask(lua_State *L)
  309. int lua_gethookcount(lua_State *L)</p></pre>
  310. <pre></pre>
  311. <pre></pre>
  312. <pre></pre>
  313. <pre></pre>
  314. <pre></pre>
  315. <pre></pre>
  316. <pre></pre>
  317. <pre></pre>
  318. <pre></pre>
  319. <pre></pre>
  320. <pre></pre>
  321. <pre></pre>
  322. </pre></pre></pre></pre>

来源: http://blog.csdn.net/xuzhonghai/article/details/8490547