From 57bccc7e4ab79688ee56beecdad644feb9f56232 Mon Sep 17 00:00:00 2001 From: dualface Date: Wed, 26 Dec 2012 18:33:55 +0800 Subject: [PATCH] remove CC_LUA_ENGINE_ENABLED macro --- cocos2dx/include/ccMacros.h | 8 ++-- cocos2dx/script_support/CCScriptSupport.cpp | 11 +++++ cocos2dx/script_support/CCScriptSupport.h | 2 + scripting/javascript/bindings/ScriptingCore.h | 1 + .../lua/cocos2dx_support/CCLuaEngine.cpp | 47 +++++++------------ scripting/lua/cocos2dx_support/CCLuaEngine.h | 4 ++ 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/cocos2dx/include/ccMacros.h b/cocos2dx/include/ccMacros.h index 82fe93c11f..a47affd751 100644 --- a/cocos2dx/include/ccMacros.h +++ b/cocos2dx/include/ccMacros.h @@ -35,11 +35,11 @@ THE SOFTWARE. #include "CCStdC.h" #ifndef CCAssert -#if CC_LUA_ENGINE_ENABLED > 0 -extern void cc_lua_assert(bool cond, const char *msg); -#define CCAssert(cond, msg) cc_lua_assert(cond, msg) +#if COCOS2D_DEBUG > 0 +extern void cc_assert_script_compatible(bool cond, const char *msg); +#define CCAssert(cond, msg) cc_assert_script_compatible(cond, msg) #else -#define CCAssert(cond, msg) CC_ASSERT(cond) +#define CCAssert(cond, msg) #endif #endif // CCAssert diff --git a/cocos2dx/script_support/CCScriptSupport.cpp b/cocos2dx/script_support/CCScriptSupport.cpp index bcaab5f11e..2538441e19 100644 --- a/cocos2dx/script_support/CCScriptSupport.cpp +++ b/cocos2dx/script_support/CCScriptSupport.cpp @@ -25,6 +25,17 @@ #include "CCScriptSupport.h" #include "CCScheduler.h" +void cc_assert_script_compatible(bool cond, const char *msg) +{ + cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine(); + if (!cond && pEngine && pEngine->executeAssert(cond, msg)) + { + return; + } + + CC_ASSERT(cond); +} + NS_CC_BEGIN // #pragma mark - diff --git a/cocos2dx/script_support/CCScriptSupport.h b/cocos2dx/script_support/CCScriptSupport.h index 96e3b3eaf6..dcf706264c 100644 --- a/cocos2dx/script_support/CCScriptSupport.h +++ b/cocos2dx/script_support/CCScriptSupport.h @@ -222,6 +222,8 @@ public: /** execute a accelerometer event */ virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue) = 0; + /** function for assert test */ + virtual bool executeAssert(bool cond, const char *msg = NULL) = 0; }; /** diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index ed3462110e..9f88774997 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -85,6 +85,7 @@ public: virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch); virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue); virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType); + virtual bool executeAssert(bool cond, const char *msg = NULL) {return false;} bool executeFunctionWithObjectData(CCNode *self, const char *name, JSObject *obj); int executeFunctionWithOwner(jsval owner, const char *name, jsval data); diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp index 4051b2cc3e..2e8c00977d 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.cpp +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.cpp @@ -40,20 +40,6 @@ extern "C" { #include "Cocos2dxLuaLoader.h" #endif -static bool in_lua_execute = false; -static lua_State *in_lua_state = NULL; - -void cc_lua_assert(bool cond, const char *msg) -{ - if (!cond && in_lua_execute && in_lua_state) - { - lua_pushfstring(in_lua_state, "ASSERT FAILED ON LUA EXECUTE: %s", msg ? msg : "unknown"); - lua_error(in_lua_state); - return; - } - CC_ASSERT(cond); -} - NS_CC_BEGIN // #pragma mark - @@ -252,11 +238,9 @@ void CCLuaEngine::addSearchPath(const char* path) int CCLuaEngine::executeString(const char *codes) { - in_lua_execute = true; - in_lua_state = m_state; + m_callFromLua = true; int nRet = luaL_dostring(m_state, codes); - in_lua_execute = false; - in_lua_state = NULL; + m_callFromLua = false; lua_gc(m_state, LUA_GCCOLLECT, 0); if (nRet != 0) @@ -270,11 +254,9 @@ int CCLuaEngine::executeString(const char *codes) int CCLuaEngine::executeScriptFile(const char* filename) { - in_lua_execute = true; - in_lua_state = m_state; + m_callFromLua = true; int nRet = luaL_dofile(m_state, filename); - in_lua_execute = false; - in_lua_state = NULL; + m_callFromLua = false; // lua_gc(m_state, LUA_GCCOLLECT, 0); if (nRet != 0) @@ -296,11 +278,9 @@ int CCLuaEngine::executeGlobalFunction(const char* functionName) return 0; } - in_lua_execute = true; - in_lua_state = m_state; + m_callFromLua = true; int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */ - in_lua_execute = false; - in_lua_state = NULL; + m_callFromLua = false; // lua_gc(m_state, LUA_GCCOLLECT, 0); if (error) @@ -502,6 +482,15 @@ int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAcc return ret; } +bool CCLuaEngine::executeAssert(bool cond, const char *msg/* = NULL */) +{ + if (!m_callFromLua) return false; + + lua_pushfstring(m_state, "ASSERT FAILED ON LUA EXECUTE: %s", msg ? msg : "unknown"); + lua_error(m_state); + return true; +} + int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs) { if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */ @@ -524,11 +513,9 @@ int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs) } int error = 0; - in_lua_execute = true; - in_lua_state = m_state; + m_callFromLua = true; error = lua_pcall(m_state, numArgs, 1, traceback); /* stack: ... ret */ - in_lua_execute = false; - in_lua_state = NULL; + m_callFromLua = false; if (error) { if (traceback == 0) diff --git a/scripting/lua/cocos2dx_support/CCLuaEngine.h b/scripting/lua/cocos2dx_support/CCLuaEngine.h index fef73d5e4b..b1f8375ef6 100644 --- a/scripting/lua/cocos2dx_support/CCLuaEngine.h +++ b/scripting/lua/cocos2dx_support/CCLuaEngine.h @@ -200,6 +200,7 @@ public: virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType); /** execute a accelerometer event */ virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue); + virtual bool executeAssert(bool cond, const char *msg = NULL); /** @brief Method used to get a pointer to the lua_State that the script module is attached to. @return A pointer to the lua_State that the script module is attached to. @@ -227,6 +228,7 @@ public: private: CCLuaEngine(void) : m_state(NULL) + , m_callFromLua(false) { } @@ -234,6 +236,8 @@ private: bool pushFunction(int nHandler); lua_State* m_state; + bool m_callFromLua; + static CCLuaEngine* m_defaultEngine; };