第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 0; 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, -3); /* refid_fun[refid] = fun, stack: fun ... refid_ptr */ lua_pop(L, 1); /* 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,1,"CCScriptEventDispatcher",0,&tolua_err) || !tolua_isnumber(tolua_S,2,0,&tolua_err) || (tolua_isvaluenil(tolua_S,3,&tolua_err) || !toluafix_isfunction(tolua_S,3,"LUA_FUNCTION",0,&tolua_err)) || !tolua_isnumber(tolua_S,4,1,&tolua_err) || !tolua_isnoobj(tolua_S,5,&tolua_err) ) goto tolua_lerror; else #endif { CCScriptEventDispatcher* self = (CCScriptEventDispatcher*) tolua_tousertype(tolua_S,1,0); int event = ((int) tolua_tonumber(tolua_S,2,0)); LUA_FUNCTION listener = ( toluafix_ref_function(tolua_S,3,0)); int tag = ((int) tolua_tonumber(tolua_S,4,0)); #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 1; #ifndef TOLUA_RELEASE tolua_lerror: tolua_error(tolua_S,"#ferror in function 'addNodeEventListener'.",&tolua_err); return 0; #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 = 30, align = ui.TEXT_ALIGN_CENTER, x = display.cx, y = display.cy + 40, color = display.COLOR_GREEN, listener = function() app:setData("currentLv", 1) app:enterScene("MainScene",1) end})
int CCLuaStack::executeFunctionByHandler(int nHandler, int numArgs) { int ret = 0; if (pushFunctionByHandler(nHandler)) /* L: ... arg1 arg2 ... func */ { if (numArgs > 0) { lua_insert(m_state, -(numArgs + 1)); /* 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