lua_pushvalue:Pushes a copy of the element at the given valid index onto the stack ,也就是说复制指定索引的值到栈顶,指定索引的值不变,影响栈大小。 lua_remove:Removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with ...
static int _counter (lua_State *L) { // //获取第一个upvalue // double val = lua_tonumber(L, lua_upvalueindex(1)); lua_pushnumber(L, ++val); /* void lua_pushvalue (lua_State *L, int index); 把栈上给定索引处的元素作一个副本压栈。 */ lua_pushvalue(L, -1); /* void lua_...
热更新用到的 Lua 关键特性 Upvalue:闭包所引用的变量 _ENV:提供沙盒功能,包含数据 函数的 Upvalue ...
lua_pushnumber 压入的初始值0。 其中压入一个upvalues值,则在pushcclosure第三个参数个数则为1. 在函数中 static int counter (lua_State *L) { double val = lua_tonumber(L, lua_upvalueindex(1)); lua_pushnumber(L, ++val); lua_pushvalue(L, -1); /* duplicate it */ lua_replace(L, lu...
lua_pushvalue## 原型:void lua_pushvalue (lua_State *L, int index); 解释:把堆栈上给定有效处索引处的元素作一个拷贝压栈。 lua_pushvfstring## 原型:const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp); 解释:等价于 lua_pushfstring, 不过是用 va_list 接收参数,而...
lua_pushvalue(L, -nup); ///这里将函数压入栈,这个函数我们前面分析过,他最终会把当前state的env赋值给新建的closure,也就是说这里最终模块内的所有函数都会共享当前的state的env。 lua_pushcclosure(L, l->func, nup); lua_setfield(L, -(nup+2), l->name); ...
lua_pushvalue函数Notice lua_pushvalue [-0, +1,-] void lua_pushvalue (lua_State *L, int index); Pushes a copy of the element at the given valid index onto the stack 如上所述, lua_pushvalue(L, -4) 并不是往栈顶插入元素-4, 而是把在栈中位置为-4的元素copy之后插入于栈顶中!!!
lua_pushvalue(L, lua_upvalueindex(op)); return 1; /*这个索引能找到值,返回1个结果*/ } } 首先使用了luaL_optinteger: lua_Integer luaL_optinteger (lua_State *L,int arg,lua_Integer d); 如果函数的第 arg 个参数是一个 整数(或可以转换为一个整数), 返回该整数。 若该参数不存在或是 nil, 返...
// 创建一个协程对象staticintluaB_cocreate(lua_State*L){lua_State*NL;// 第一个参数一定是一个函数对象luaL_checktype(L,1,LUA_TFUNCTION);// 新建线程NL=lua_newthread(L);lua_pushvalue(L,1);/* move function to top */// 将函数对象转移到NLlua_xmove(L,NL,1);/* move function from L ...
lua_pushvalue方法表示将栈中某个元素的副本压入栈顶。之前的栈元素不会发生变动。如图所示: lua_pushvalue示意图 lua_remove方法用于移除指定索引上的元素,然后再该元素之上的所有元素会下移填补空缺(即元素的索引会发生变更)。如图所示: lua_remove示意图 ...