Skip to content

Commit c2cdace

Browse files
committedJun 20, 2013
Make minetest.debug accept multiple parameters; convert them to string
1 parent 469d0b1 commit c2cdace

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed
 

‎src/script/lua_api/luaapi.cpp

+21-3
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,31 @@ bool ModApiBasic::Initialize(lua_State* L,int top) {
105105
return retval;
106106
}
107107

108-
// debug(text)
108+
// debug(...)
109109
// Writes a line to dstream
110110
int ModApiBasic::l_debug(lua_State *L)
111111
{
112112
NO_MAP_LOCK_REQUIRED;
113-
std::string text = lua_tostring(L, 1);
114-
dstream << text << std::endl;
113+
// Handle multiple parameters to behave like standard lua print()
114+
int n = lua_gettop(L);
115+
lua_getglobal(L, "tostring");
116+
for(int i = 1; i <= n; i++){
117+
/*
118+
Call tostring(i-th argument).
119+
This is what print() does, and it behaves a bit
120+
differently from directly calling lua_tostring.
121+
*/
122+
lua_pushvalue(L, -1); /* function to be called */
123+
lua_pushvalue(L, i); /* value to print */
124+
lua_call(L, 1, 1);
125+
const char *s = lua_tostring(L, -1);
126+
if(i>1)
127+
dstream << "\t";
128+
if(s)
129+
dstream << s;
130+
lua_pop(L, 1);
131+
}
132+
dstream << std::endl;
115133
return 0;
116134
}
117135

0 commit comments

Comments
 (0)
Please sign in to comment.