lua堆栈操作常用函数学习二
- /*
- ** basic stack manipulation
- */
- LUA_API int <strong> (lua_gettop) (lua_State *L); </strong><pre class="cpp" name="code"></pre><pre class="cpp" name="code">//返回当前堆栈的大小
- LUA_API int lua_gettop (lua_State *L) {
- return cast_int(L->top - L->base);
- }
- int lua_gettop(lua_State *L)
- 取得栈的高度
- for (int i = 0; i < 10; ++i)
- lua_pushnumber(L, i);
- printf("%d", lua_gettop(L));
- --> 10
- LUA_API void (lua_settop) (lua_State *L, int idx);
- <pre class="cpp" name="code">//settop()是破坏性的 变小后数据就不恢复了
- LUA_API void lua_settop (lua_State *L, int idx) {
- lua_lock(L);
- if (idx >= 0) { //正数索引 是向外的有可能大于当前大小 但负数则不会过大越界 所以不会有填nil的机会
- api_check(L, idx <= L->stack_last - L->base);
- while (L->top < L->base + idx)//如果设置的大于当前堆栈大小 则填nil
- setnilvalue(L->top++);
- L->top = L->base + idx;
- }
- else {
- api_check(L, -(idx+1) <= (L->top - L->base));
- L->top += idx+1; /* `subtract' index (index is negative) */
- }
- lua_unlock(L);
- }
- void lua_settop(lua_State *L, int idx)
- <strong>设置栈的高度,如果之前的栈顶比新设置的更高,那么高出来的元素会被丢弃,反之压入nil来补足大小。</strong>
- 另外,Lua提供了一个宏,用来从栈中弹出n个元素:#define lua_pop(L, n) lua_settop(L, -(n)-1)
- for (int i = 0; i < 10; ++i)
- lua_pushnumber(L, i);
- lua_settop(L, 5)
- printf("%d", lua_gettop(L));
- --> 5
- 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) {
- lua_lock(L);
- setobj2s(L, L->top, index2adr(L, idx)); <strong>//拷贝指定索引的值压入栈顶 栈顶位置更新</strong>
- api_incr_top(L);
- lua_unlock(L);
- }
- void lua_pushvalue(lua_State *L, int idx)
- 将指定索引上值的副本压入栈
- for (int i = 1; i <= 3; ++i)
- lua_pushnumber(i);
- 栈中元素:(从下往上) 1 2 3
- lua_pushvalue(L, 2)
- 栈中元素:(从下往上) 1 2 3 2</pre><pre class="cpp" name="code"> </pre><br>
- 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) {
- StkId p;
- lua_lock(L);
- p = index2adr(L, idx);//取得指定索引
- api_checkvalidindex(L, p);
- while (++p < L->top)
- setobjs2s(L, p-1, p);//将索引后的数据一一迁移
- L->top--;
- lua_unlock(L);
- }
- void lua_remove(lua_State *L, int idx)
- 删除指定索引上的元素,并将该位置之上的所有元素上移以补空缺
- for (int i = 1; i <= 3; ++i)
- lua_pushnumber(i);
- 栈中元素:(从下往上) 1 2 3
- lua_remove(L, 2)
- 栈中元素:(从下往上) 1 3
- </pre><br>
- 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) {
- StkId p;
- StkId q;
- lua_lock(L);
- p = index2adr(L, idx);//找到指定索引值处
- api_checkvalidindex(L, p);
- for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);//将后面的数据依次后移
- setobjs2s(L, p, L->top);
- lua_unlock(L);
- }
- void lua_insert(lua_State *L, int idx)
- 移指定位置上的所有元素以开辟一个空间槽的空间,然后将栈顶元素移到该位置
- for (int i = 1; i <= 5; ++i)
- lua_pushnumber(i);
- 栈中元素:(从下往上) 1 2 3 4 5
- lua_insert(L, 3)
- 栈中元素:(从下往上) 1 2 5 4 3
- </pre><br>
- LUA_API void (lua_replace) (lua_State *L, int idx);
- <pre></pre>
- <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) {
- StkId o;
- lua_lock(L);
- api_checknelems(L, 1);//检查是否有元素
- o = index2adr(L, idx);
- api_checkvalidindex(L, o);
- if (idx == LUA_ENVIRONINDEX) {
- Closure *func = curr_func(L);
- api_check(L, ttistable(L->top - 1));
- func->c.env = hvalue(L->top - 1);
- luaC_barrier(L, func, L->top - 1);
- }
- else {
- setobj(L, o, L->top - 1);//将栈顶元素 拷贝到指定所以处
- if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
- luaC_barrier(L, curr_func(L), L->top - 1);
- }
- L->top--;//栈顶减少
- lua_unlock(L);
- }</pre>void lua_replace(lua_State *L, int idx)<br>
- <strong>弹出栈顶的值,并将该值设置到指定索引上,但它不会移动任何东西<br>
- </strong>for (int i = 1; i <= 5; ++i)<br>
- lua_pushnumber(i);<br>
- 栈中元素:(从下往上) 1 2 3 4 5<br>
- lua_replace(L, 3)<br>
- 栈中元素:(从下往上) 1 2 5 4<br>
- <br>
- <pre></pre>
- <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) {
- int res;
- lua_lock(L);
- if ((L->top - L->base + size) > LUAI_MAXCSTACK)
- res = 0; /* stack overflow */
- else {
- luaD_checkstack(L, size);
- if (L->ci->top < L->top + size)
- L->ci->top = L->top + size;
- res = 1;
- }
- lua_unlock(L);
- return res;
- }
- </pre><pre class="cpp" name="code">int lua_checkstack(lua_State *L, int sz)
- 扩大栈的可用尺寸,栈的默认尺寸是20,此函数会确保堆栈上至少有 sz 个空位。如果不能把堆栈扩展到相应的尺寸,函数返回 false 。这个函数永远不会缩小堆栈;如果堆栈已经比需要的大了,那么就放在那里不会产生变化。
- lua_checkstack(L, 100)
- </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个元素 移到另一个堆栈上
- LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
- int i;
- if (from == to) return;
- lua_lock(to);
- api_checknelems(from, n);//确认 从from有n个元素
- api_check(from, G(from) == G(to));
- api_check(from, to->ci->top - to->top >= n);//确认to有n个元素
- from->top -= n;
- for (i = 0; i < n; i++) {
- setobj2s(to, to->top++, from->top + i);
- }
- lua_unlock(to);
- }</pre><br>
- <pre></pre>
- <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)
- 得到一个元素的类型,返回整型,返回值是如下列表之一:<p>int lua_isnil(lua_State *L, int idx);
- int lua_isboolean(lua_State *L, int idx);
- int lua_istable(lua_State *L, int idx);
- int lua_isfunction(lua_State *L, int idx);
- int lua_islightuserdata (lua_State *L, int idx);</p><p>#define LUA_TNONE (-1)</p><p>#define LUA_TNIL 0
- #define LUA_TBOOLEAN 1
- #define LUA_TLIGHTUSERDATA 2
- #define LUA_TNUMBER 3
- #define LUA_TSTRING 4
- #define LUA_TTABLE 5
- #define LUA_TFUNCTION 6
- #define LUA_TUSERDATA 7
- #define LUA_TTHREAD 8</p><p>lua_pushnumber(L, 55);
- lua_type(L, 1)-->LUA_TNUMBER
- ---------------------------------------------------------------------------------------
- const char* lua_typename(lua_State *L, int tp)
- 将一个类型编码转换成类型名
- lua_typename(L, 1)-->boolean
- lua_typename(L, 3)-->number
- ---------------------------------------------------------------------------------------</p><p>
- int lua_equal(lua_State *L, int idx1, int idx2)
- 如果依照 Lua 中 == 操作符语义,索引 index1 和 index2 中的值相同的话,返回 1 。否则返回 0 。如果任何一个索引无效也会返回 0。
- lua_pushstring(L, "this");
- lua_pushboolean(L, 1);
- lua_pushboolean(L, 1);
- lua_equal(L, -2, -3)
- -->0
- lua_equal(L, -1, -2)
- -->1
- lua_equal(L, -1, -10)
- -->0
- </p></pre><pre class="cpp" name="code"> int lua_rawequal(lua_State *L, int idx1, int idx2)
- int lua_lessthan(lua_State *L, int idx1, int idx2)<p> </p><p>---------------------------------------------------------------------------------------
- lua_Number lua_tonumber(lua_State *L, int idx)
- lua_Integer lua_tointeger(lua_State *L, int idx)
- int lua_toboolean(lua_State *L, int idx)
- const char* lua_tolstring(lua_State *L, int idx, size_t *len)
- 以上四个函数都有一个原型lua_to*(lua_State *L, int idx),用于从栈中取一个值。如果指定的元素不具有正确的类型,调用这些函数也不会有问题,
- 在这种情况下,调用lua_toboolean,lua_tonumber,lua_tointeger会返回0,其它函数会返回NULL。通常不使用lua_is*函数,只需在调用它们之
- 后测试返回结果是否为NULL就可以了。
- lua_pushnumber(L, 100)
- lua_tonumber(L, 1)-->100
- lua_pushinteger(L, 200)
- lua_tointeger(L, -1)-->200
- lua_pushboolean(L, 0)
- lua_toboolean(L, -1)-->false
- lua_pushstring(L, "hello,lua")
- lua_tolstring(L, -1, &len)-->hello,lua
- 注:len是传出参数,表示字符串的长度,如果想忽略此参数,传入NULL
- ---------------------------------------------------------------------------------------
- size_t lua_objlen(lua_State *L, int idx)
- 返回值的长度,如果类型不正确,返回0
- lua_pushstring(L, "hello,lua")
- lua_objlen(L, 1)-->9
- ---------------------------------------------------------------------------------------
- lua_CFunction lua_tocfunction(lua_State *L, int idx)
- void* lua_touserdata(lua_State *L, int idx)
- lua_State* lua_tothread(lua_State *L, int idx)
- const void* lua_topointer(lua_State *L, int idx)</p><p> </p><p>//push functions (C -> stack)
- void lua_pushnil(lua_State *L)
- void lua_pushnumber(lua_State *L, lua_Number n)
- void lua_pushinteger(lua_State *L, lua_Integer n)
- void lua_pushlstring(lua_State *L, const char* s, size_t l)
- void lua_pushstring(lua_State *L, const char *s)
- const char* lua_pushvfstring(lua_State *L, const char *fmt, va_list argp)
- const char* lua_pushfstring(lua_State *L, const char *fmt, ...)
- void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
- void lua_pushboolean(lua_State *L, void *b)
- void lua_pushlightuserdata(lua_State *L, void *p)
- int lua_pushthread(lua_State *L)</p><p>
- void lua_gettable(lua_State *L, int idx)
- ---------------------------------------------------------------------------------------
- void lua_getfield(lua_State *L, int idx, const char *k)
- 把 t[k] 值压入堆栈,这里的 t 是指有效索引 index 指向的值。在 Lua 中,这个函数可能触发对应 "index" 事件的元方法
- ---------------------------------------------------------------------------------------
- void lua_rawget(lua_State *L, int idx)
- void lua_rawgeti(lua_State *L, int idx, int n)
- ---------------------------------------------------------------------------------------
- void lua_createtable(lua_State *L, int narr, int nrec)
- 创建一个新的空 table 压入堆栈。这个新 table 将被预分配 narr 个元素的数组空间以及 nrec 个元素的非数组空间。当你明确知道表中需要多少个元素时,预分配就非常有用。如果你不知道,可以使用函数 lua_newtable。
- 举例暂缺
- ---------------------------------------------------------------------------------------
- void* lua_newuserdata(lua_State *L, size_t sz)
- int lua_getmetatable(lua_State *L, int objindex)
- ---------------------------------------------------------------------------------------
- void lua_getfenv(lua_State *L, int idx)</p><p>
- 把索引处值的环境表压入堆栈
- ---------------------------------------------------------------------------------------
- void lua_settable(lua_State *L, int idx);
- void lua_setfield(lua_State *L, int idx, const char *k)
- void lua_rawset(lua_State *L, int idx)
- void lua_rawseti(lua_State *L, int idx, int n)
- int lua_setmetatable(lua_State *L, int objindex)
- int lua_setfenv(lua_State *L, int idx)</p><p>
- //'load' and 'call' functions (load and run lua code)
- void lua_call(lua_State *L, int nargs, int nresults);
- int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc)
- ---------------------------------------------------------------------------------------
- int lua_cpcall(lua_State *L, lua_CFunction func, void *ud)
- 以保护模式调用 C 函数 func 。 func 只有能从堆栈上拿到一个参数,就是包含有 ud 的 light userdata。当有错误时, lua_cpcall 返回和 lua_pcall 相同的错误代码,并在栈顶留下错误对象;否则它返回零,并不会修改堆栈。所有从 func 内返回的值都会被扔掉。
- 举例暂缺
- ---------------------------------------------------------------------------------------
- int lua_load(lua_State *L, lua_Reader reader, void *dt, const char *chunkname);
- ---------------------------------------------------------------------------------------
- int lua_dump(lua_State *L, lua_Writer writer, void *data);
- 把函数 dump 成二进制 chunk 。函数接收栈顶的 Lua 函数做参数,然后生成它的二进制 chunk 。若被 dump 出来的东西被再次加载,加载的结果就相当于原来的函数。当它在产生 chunk 的时候,lua_dump 通过调用函数 writer (参见 lua_Writer)来写入数据,后面的 data 参数会被传入 writer 。
- 最后一次由写入器 (writer) 返回值将作为这个函数的返回值返回; 0 表示没有错误。
- 这个函数不会把 Lua 返回弹出堆栈。
- 举例暂缺
- ---------------------------------------------------------------------------------------
- int lua_yield(lua_State *L, int nresults)
- int lua_resume(lua_State *L, int narg)
- int lua_status(lua_State *L)</p><p>---------------------------------------------------------------------------------------
- int lua_gc(lua_State *L, int what, int data)</p><p>控制垃圾收集器。 这个函数根据其参数 what 发起几种不同的任务:
- * LUA_GCSTOP: 停止垃圾收集器。
- * LUA_GCRESTART: 重启垃圾收集器。
- * LUA_GCCOLLECT: 发起一次完整的垃圾收集循环。
- * LUA_GCCOUNT: 返回 Lua 使用的内存总量(以 K 字节为单位)。
- * LUA_GCCOUNTB: 返回当前内存使用量除以 1024 的余数。
- * LUA_GCSTEP: 发起一步增量垃圾收集。步数由 data 控制(越大的值意味着越多步),而其具体含义(具体数字表示了多少)并未标准化。如果你想控制这个步数,必须实验性的测试 data 的值。如果这一步结束了一个垃圾收集周期,返回返回 1 。
- * LUA_GCSETPAUSE: 把 data/100 设置为 garbage-collector pause 的新值。函数返回以前的值。
- * LUA_GCSETSTEPMUL: 把 arg/100 设置成 step multiplier 。函数返回以前的值。
- ---------------------------------------------------------------------------------------
- int lua_error(lua_State *L)
- 产生一个 Lua 错误。错误信息(实际上可以是任何类型的 Lua 值)必须被置入栈顶。这个函数会做一次长跳转,因此它不会再返回。(参见 luaL_error)。
- lua_pushstring(L, "one error");
- lua_error(L);
- printf("%s", "本行已经执行不到了");
- ---------------------------------------------------------------------------------------
- int lua_next(lua_State *L, int idx)
- ---------------------------------------------------------------------------------------
- void lua_concat(lua_State *L, int n)
- 连接栈顶的 n 个值,然后将这些值出栈,并把结果放在栈顶。如果 n 为 1 ,结果就是一个字符串放在栈上(即,函数什么都不做);如果 n 为 0 ,结果是一个空串。 连接依照 Lua 中创建语义完成,如果尝试把两个不能连接的类型连接,程序会给出错误提示。
- lua_pushstring(L, "this");
- lua_pushboolean(L, 1);
- lua_pushnumber(L, 9989);
- lua_pushnumber(L, 1111);
- lua_pushboolean(L, 0);
- lua_pushstring(L, "满天都是小星星");
- lua_pushnumber(L, 1986);
- lua_pushstring(L, "onebyone");
- -->'this' 'true' '9989' '1111' 'false' '满天都是小星星' '1986' 'onebyone'
- lua_concat(L, 3);
- -->'this' 'true' '9989' '1111' 'false' '满天都是小星星1986onebyone'
- ---------------------------------------------------------------------------------------
- lua_Alloc lua_getallocf(lua_State *L, void **ud)
- 返回给定状态机的内存分配器函数。如果 ud 不是 NULL ,Lua 把调用 lua_newstate 时传入的那个指针放入 *ud 。
- ---------------------------------------------------------------------------------------
- void lua_setallocf(lua_State *L, lua_Alloc f, void *ud)</p><p>
- //Functions to be called by the debuger in specific events
- int lua_getstack(lua_State *L, int level, lua_Debug *ar)
- int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
- const char* lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
- const char* lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
- const char* lua_getupvalue(lua_State *L, int funcindex, int n)
- const char* lua_setupvalue(lua_State *L, int funcindex, int n)
- int lua_sethook(lua_State *L, lua_Hook func, int mask, int count);
- lua_Hook lua_gethook(lua_State *L)
- int lua_gethookmask(lua_State *L)
- int lua_gethookcount(lua_State *L)</p></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre></pre></pre></pre>
来源: http://blog.csdn.net/xuzhonghai/article/details/8490547
lua堆栈操作常用函数学习二的更多相关文章
- lua操作常用函数学习一
(1)lua 和 C++之间的交互的基本知识: lua 和 C++ 之间的数据交互通过堆栈进行,栈中的数据通过索引值进行定位,(栈就像是一个容器一样,放进去的东西都要有标号)其中栈顶是-1,栈底是1, ...
- php中文件操作常用函数有哪些
php中文件操作常用函数有哪些 一.总结 一句话总结:读写文件函数 判断文件或者目录是否存在函数 创建目录函数 file_exists() mkdir() file_get_content() fil ...
- go语言之进阶篇字符串操作常用函数介绍
下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档. 一.字符串操作常用函数介绍 1.Contains func Contains(s, substr st ...
- numpy常用函数学习
目录numpy常用函数学习点乘法线型预测线性拟合裁剪.压缩和累乘相关性多项式拟合提取符号数组杂项点乘法该方法为数学方法,但是在numpy使用的时候略坑.numpy的点乘为a.dot(b)或numpy. ...
- dplyr 数据操作 常用函数(5)
继续来了解dplyr中的其他有用函数 1.sample() 目的是可以从一个数据框中,随机抽取一些行,然后组成新的数据框. sample_n(tbl, size, replace = FALSE, w ...
- dplyr 数据操作 常用函数(4)
接下来我们继续了解一些dplyr中的常用函数. 1.ranking 以下各个函数可以实现对数据进行不同的排序 row_number(x) ntile(x, n) min_rank(x) dense_r ...
- dplyr 数据操作 常用函数(2)
继上一节常用函数,继续了解其他函数 1.desc() 这个函数和SQL中的排序用法是一样的,表示对数据进行倒序排序. 接下来我们看些例子. a=sample(20,50,rep=T)a desc(a) ...
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- JavaScript学习总结(11)——JS常用函数(二)
37. getElementsByClassName ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function getElementsByClassName( ...
随机推荐
- Ubuntu 14.10 下Ganglia监控Hadoop集群
前提是已经安装好Ganglia和Hadoop集群 1 Master节点配置hadoop-metrics2.properties # syntax: [prefix].[source|sink|jmx] ...
- hdu 2071
Ps:输出n个数里最大的 #include "stdio.h" int main(){ ],max; int i,j,n,t; while(~scanf("%d" ...
- hdu 2099
PS:因为还是不爽...继续水题...感觉这道题就是考输出.. 代码: #include "stdio.h" void cal(int a,int b); int main(){ ...
- PHP使用mysqli操作MySQL数据库
PHP的 mysqli 扩展提供了其先行版本的所有功能,此外,由于 MySQL 已经是一个 具有完整特性的数据库服务器 , 这为PHP 又添加了一些新特性 . 而 mysqli 恰恰也支持了 这些新特 ...
- Windows共享设定-使用net use添加网络盘带上账号密码
食欲 net use \\10.11.1.2\ipc$ /user:dmnm\usr "pwd"
- 解决magento新闻邮件发送一直处于“正在发送”状态问题
今天在弄magento新闻邮件发送时候发现,单个邮件发送完全没有问题,但是新闻邮件订阅死活都不成功,国内国外的帖子都翻了一遍没有用,最后还是得靠自己了,于是开始慢慢找问题 首先想到是不是cront ...
- 暴力枚举——Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3394 Accepted: 2172 Description You ...
- 使用innerHTML生成的script节点不会发出请求与执行text属性
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 对UICollectionView的学习
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...
- CentOS云服务器数据盘分区和格式化
1. 查看数据盘信息 登录CentOS云服务器后,可以使用“fdisk -l”命令查看数据盘相关信息. 使用“df –h”命令,无法看到未分区和格式化的数据盘,只能看到已挂载的. [root@VM_7 ...