Print as Lua 5.1 does and remove duplicate code.

This commit is contained in:
Xpol Wan 2016-05-14 14:09:58 +08:00
parent 0403541693
commit a7d9dd9a75
1 changed files with 35 additions and 78 deletions

View File

@ -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