开云官网优惠活动

3年前 (2023-03-14)

lua的闭包包括CClosure和FClosure两种类型。 同题参见下面的例子介绍如何在C中使用C创建的闭包函数,C中使用lua中创建的闭包函数,Lua中使用C中创建的闭包函数,Lua中使用Lua闭包就不赘述了。

C中通过lua_pushclosure创建闭包函数,在将闭包函数推到堆栈之前需要先把n个上值推到lua_state上。 对照阅读取用的时使用lua_upvalueindex(n)取的特定索引上的上值,更改的话可以使用lua_replace更改上值。例子中每次将上值+5,所以在lua中每次调用upvalue_test函数都会获得正确的上值。lua中的closure想在C中进行upvalue的更改有一点麻烦,首先你需要获取closure函数对象,使用lua_getupvalue将特定索引上的上值放到栈顶,可以使用lua_setupvalue重设上值。如果你的fclosure中没有使用全局变量,那么其上值索引应该是按照local变量的声明顺序来的,然而一旦使用了全局变量,第一个上值必定是_ENV环境,也就是说你可以通过重新设置_ENV环境,修改全局的上值,这也是修改环境变量的一种方法。

C 文件:

#include #include #include #include #include #include #include static int mytest(lua_State *L) { //获取上值 int upv = (int)lua_tonumber(L, lua_upvalueindex(1)); printf('%d ', upv); upv += 5; lua_pushinteger(L, upv); lua_replace(L, lua_upvalueindex(1)); //获取一般参数 printf('%d ', (int)lua_tonumber(L,1)); return 0; } int main(void) { lua_State *L = luaL_newstate(); luaL_openlibs(L); //设置Cclosure函数的上值 lua_pushinteger(L,10); lua_pushinteger(L,11); lua_pushcclosure(L,mytest,2); lua_setglobal(L,'upvalue_test'); luaL_dofile(L, './luatest.lua'); //获取fclosure上值的名称(临时值, 不带env) lua_getglobal(L, 'l_counter'); const char *name = lua_getupvalue(L, -1, 1); printf('%s ', name); //设置fclosure上值 lua_getglobal(L, 'l_counter'); lua_pushinteger(L,1000); name = lua_setupvalue(L, -2, 1); printf('%s ', name); lua_getglobal(L,'ltest'); lua_pcall(L,0,0,0); lua_getglobal(L,'ltest'); lua_pcall(L,0,0,0); //获取fclosure的上值(带env) lua_getglobal(L, 'g_counter'); lua_getupvalue(L, -1, 1); //通过settable重新设置env中对应的值 lua_pushstring(L, 'gloval_upvalue'); lua_pushinteger(L,10000); lua_settable(L,-3); lua_pushstring(L, 'gloval_upvalue1'); lua_pushinteger(L,20000); lua_settable(L,-3); lua_getglobal(L,'gtest'); lua_pcall(L,0,0,0); lua_close(L); return 0; }

Lua 文件

gloval_upvalue = 10 gloval_upvalue1 = 20 local local_upvalue = 100 function l_counter() return function () local_upvalue = local_upvalue + 1 return local_upvalue end end function g_counter() return function () gloval_upvalue = gloval_upvalue + 1 return gloval_upvalue,gloval_upvalue1 end end g_testf = g_counter() l_testf = l_counter() function gtest() print(g_testf()) end function ltest() print(l_testf()) end upvalue_test(1,2,3) upvalue_test(4,5,6)

你可能想看:

曝光(pùguāng)?曝光(bàoguāng)?周杰在《非常静距离》中 相关阅读

刚刚看了"非常静距离"中李静访谈周杰,这个节目似乎有意给周杰一个回应是非传闻机会,当然媒体有误解地方解释清楚也是应该,我们并不知道演艺界真实面目,姑妄听之。也就是说究竟应读开云官网优惠活动音呢,不知道周杰翻是开云官网优惠活动...

分享给朋友:

继续阅读

若需了解「开云官网优惠活动」的上下文,可结合站内栏目与相关篇目交叉阅读。

栏目用于追新,文内链接用于对照细节。两者配合可减少漏读与重复检索。

继续浏览时优先选择同栏目或相近主题,避免被站外镜像带偏。

站点导览

wiki · 科技 · 文化 · 主页

页面按栏目组织。可先看导读,再进入相关篇目或返回列表。