[luabinding] make CCAssert compatible Lua

This commit is contained in:
dualface 2012-12-22 10:13:37 +08:00
parent 4615a4a1ff
commit cc69e39c8a
4 changed files with 39 additions and 2 deletions

View File

@ -35,7 +35,12 @@ THE SOFTWARE.
#include "CCStdC.h" #include "CCStdC.h"
#ifndef CCAssert #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)
#else
#define CCAssert(cond, msg) CC_ASSERT(cond) #define CCAssert(cond, msg) CC_ASSERT(cond)
#endif
#endif // CCAssert #endif // CCAssert
#include "ccConfig.h" #include "ccConfig.h"

View File

@ -440,7 +440,7 @@ bool CCTexturePVR::unpackPVRv3Data(unsigned char* dataPointer, unsigned int data
bytes = dataPointer; bytes = dataPointer;
m_uNumberOfMipmaps = header->numberOfMipmaps; m_uNumberOfMipmaps = header->numberOfMipmaps;
CCAssert(m_uNumberOfMipmaps < CC_PVRMIPMAP_MAX, @"TexturePVR: Maximum number of mimpaps reached. Increate the CC_PVRMIPMAP_MAX value"); CCAssert(m_uNumberOfMipmaps < CC_PVRMIPMAP_MAX, "TexturePVR: Maximum number of mimpaps reached. Increate the CC_PVRMIPMAP_MAX value");
for (unsigned int i = 0; i < m_uNumberOfMipmaps; i++) for (unsigned int i = 0; i < m_uNumberOfMipmaps; i++)
{ {

View File

@ -40,6 +40,20 @@ extern "C" {
#include "Cocos2dxLuaLoader.h" #include "Cocos2dxLuaLoader.h"
#endif #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 NS_CC_BEGIN
// #pragma mark - // #pragma mark -
@ -238,7 +252,11 @@ void CCLuaEngine::addSearchPath(const char* path)
int CCLuaEngine::executeString(const char *codes) int CCLuaEngine::executeString(const char *codes)
{ {
in_lua_execute = true;
in_lua_state = m_state;
int nRet = luaL_dostring(m_state, codes); int nRet = luaL_dostring(m_state, codes);
in_lua_execute = false;
in_lua_state = NULL;
lua_gc(m_state, LUA_GCCOLLECT, 0); lua_gc(m_state, LUA_GCCOLLECT, 0);
if (nRet != 0) if (nRet != 0)
@ -252,7 +270,11 @@ int CCLuaEngine::executeString(const char *codes)
int CCLuaEngine::executeScriptFile(const char* filename) int CCLuaEngine::executeScriptFile(const char* filename)
{ {
in_lua_execute = true;
in_lua_state = m_state;
int nRet = luaL_dofile(m_state, filename); int nRet = luaL_dofile(m_state, filename);
in_lua_execute = false;
in_lua_state = NULL;
// lua_gc(m_state, LUA_GCCOLLECT, 0); // lua_gc(m_state, LUA_GCCOLLECT, 0);
if (nRet != 0) if (nRet != 0)
@ -274,7 +296,11 @@ int CCLuaEngine::executeGlobalFunction(const char* functionName)
return 0; return 0;
} }
in_lua_execute = true;
in_lua_state = m_state;
int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */ int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */
in_lua_execute = false;
in_lua_state = NULL;
// lua_gc(m_state, LUA_GCCOLLECT, 0); // lua_gc(m_state, LUA_GCCOLLECT, 0);
if (error) if (error)
@ -498,7 +524,11 @@ int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs)
} }
int error = 0; int error = 0;
in_lua_execute = true;
in_lua_state = m_state;
error = lua_pcall(m_state, numArgs, 1, traceback); /* stack: ... ret */ error = lua_pcall(m_state, numArgs, 1, traceback); /* stack: ... ret */
in_lua_execute = false;
in_lua_state = NULL;
if (error) if (error)
{ {
if (traceback == 0) if (traceback == 0)

View File

@ -36,6 +36,8 @@ extern "C" {
#include "base_nodes/CCNode.h" #include "base_nodes/CCNode.h"
#include "script_support/CCScriptSupport.h" #include "script_support/CCScriptSupport.h"
void cc_lua_assert(bool cond, const char *msg);
NS_CC_BEGIN NS_CC_BEGIN
typedef int LUA_FUNCTION; typedef int LUA_FUNCTION;