From a7d9dd9a75f57b1080da7cabcfd43321df8effe3 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Sat, 14 May 2016 14:09:58 +0800 Subject: [PATCH] Print as Lua 5.1 does and remove duplicate code. --- .../lua-bindings/manual/CCLuaStack.cpp | 113 ++++++------------ 1 file changed, 35 insertions(+), 78 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index 3e56bbf6bd..be53250526 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -60,89 +60,46 @@ extern "C" { #include "platform/CCFileUtils.h" namespace { -int lua_print(lua_State * luastate) -{ - int nargs = lua_gettop(luastate); - - std::string t; - for (int i=1; i <= nargs; i++) + int get_string_for_print(lua_State * L, std::string* out) { - if (lua_istable(luastate, i)) - t += "table"; - else if (lua_isnone(luastate, i)) - t += "none"; - else if (lua_isnil(luastate, i)) - t += "nil"; - else if (lua_isboolean(luastate, i)) - { - if (lua_toboolean(luastate, i) != 0) - t += "true"; - else - t += "false"; + int n = lua_gettop(L); /* number of arguments */ + int i; + + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + size_t sz; + s = lua_tolstring(L, -1, &sz); /* get result */ + if (s == NULL) + return luaL_error(L, LUA_QL("tostring") " must return a string to " + LUA_QL("print")); + if (i>1) out->append("\t"); + out->append(s, sz); + lua_pop(L, 1); /* pop result */ } - else if (lua_isfunction(luastate, i)) - t += "function"; - else if (lua_islightuserdata(luastate, i)) - t += "lightuserdata"; - else if (lua_isthread(luastate, i)) - t += "thread"; - else - { - const char * str = lua_tostring(luastate, i); - if (str) - t += lua_tostring(luastate, i); - else - t += lua_typename(luastate, lua_type(luastate, i)); - } - if (i!=nargs) - t += "\t"; + return 0; } - CCLOG("[LUA-print] %s", t.c_str()); - return 0; -} - -int lua_release_print(lua_State * L) -{ - int nargs = lua_gettop(L); - - std::string t; - for (int i=1; i <= nargs; i++) + int lua_print(lua_State * L) { - if (lua_istable(L, i)) - t += "table"; - else if (lua_isnone(L, i)) - t += "none"; - else if (lua_isnil(L, i)) - t += "nil"; - else if (lua_isboolean(L, i)) - { - if (lua_toboolean(L, i) != 0) - t += "true"; - else - t += "false"; - } - else if (lua_isfunction(L, i)) - t += "function"; - else if (lua_islightuserdata(L, i)) - t += "lightuserdata"; - else if (lua_isthread(L, i)) - t += "thread"; - else - { - const char * str = lua_tostring(L, i); - if (str) - t += lua_tostring(L, i); - else - t += lua_typename(L, lua_type(L, i)); - } - if (i!=nargs) - t += "\t"; - } - log("[LUA-print] %s", t.c_str()); + std::string t; + get_string_for_print(L, &t); + CCLOG("[LUA-print] %s", t.c_str()); - return 0; -} + return 0; + } + + int lua_release_print(lua_State * L) + { + std::string t; + get_string_for_print(L, &t); + log("[LUA-print] %s", t.c_str()); + + return 0; + } } NS_CC_BEGIN @@ -818,7 +775,7 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) bool isXXTEA = stack && stack->_xxteaEnabled && size >= stack->_xxteaSignLen && memcmp(stack->_xxteaSign, bytes, stack->_xxteaSignLen) == 0; - + if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(bytes + stack->_xxteaSignLen,