Merge pull request #15640 from xpol/better-lua-print

Print as Lua 5.1 does and remove duplicate code.
This commit is contained in:
minggo 2016-05-23 18:42:23 +08:00
commit b8e7f37eaf
1 changed files with 35 additions and 78 deletions

View File

@ -60,89 +60,46 @@ extern "C" {
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
namespace { namespace {
int lua_print(lua_State * luastate) int get_string_for_print(lua_State * L, std::string* out)
{
int nargs = lua_gettop(luastate);
std::string t;
for (int i=1; i <= nargs; i++)
{ {
if (lua_istable(luastate, i)) int n = lua_gettop(L); /* number of arguments */
t += "table"; int i;
else if (lua_isnone(luastate, i))
t += "none"; lua_getglobal(L, "tostring");
else if (lua_isnil(luastate, i)) for (i=1; i<=n; i++) {
t += "nil"; const char *s;
else if (lua_isboolean(luastate, i)) lua_pushvalue(L, -1); /* function to be called */
{ lua_pushvalue(L, i); /* value to print */
if (lua_toboolean(luastate, i) != 0) lua_call(L, 1, 1);
t += "true"; size_t sz;
else s = lua_tolstring(L, -1, &sz); /* get result */
t += "false"; 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)) return 0;
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";
} }
CCLOG("[LUA-print] %s", t.c_str());
return 0; int lua_print(lua_State * L)
}
int lua_release_print(lua_State * L)
{
int nargs = lua_gettop(L);
std::string t;
for (int i=1; i <= nargs; i++)
{ {
if (lua_istable(L, i)) std::string t;
t += "table"; get_string_for_print(L, &t);
else if (lua_isnone(L, i)) CCLOG("[LUA-print] %s", t.c_str());
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());
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 NS_CC_BEGIN
@ -818,7 +775,7 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L)
bool isXXTEA = stack && stack->_xxteaEnabled && size >= stack->_xxteaSignLen bool isXXTEA = stack && stack->_xxteaEnabled && size >= stack->_xxteaSignLen
&& memcmp(stack->_xxteaSign, bytes, stack->_xxteaSignLen) == 0; && memcmp(stack->_xxteaSign, bytes, stack->_xxteaSignLen) == 0;
if (isXXTEA) { // decrypt XXTEA if (isXXTEA) { // decrypt XXTEA
xxtea_long len = 0; xxtea_long len = 0;
buffer = xxtea_decrypt(bytes + stack->_xxteaSignLen, buffer = xxtea_decrypt(bytes + stack->_xxteaSignLen,