Skip to content

Commit

Permalink
Fix wesnoth.print
Browse files Browse the repository at this point in the history
Pass each parameter through Lua tostring() to convert to strings. This also allows meta-method tostring on userdata.

This aligns the operation of wesnoth print() to be closer to what a Lua programmer would expect from the normal Lua print() which is not available.

Closes Issue #2236
  • Loading branch information
GregoryLundberg committed Nov 23, 2017
1 parent 2150cdb commit 0eb25f4
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/scripting/lua_kernel_base.cpp
Expand Up @@ -100,17 +100,24 @@ int lua_kernel_base::intf_print(lua_State* L)
DBG_LUA << "intf_print called:\n";
size_t nargs = lua_gettop(L);

lua_getglobal(L, "tostring");
for (size_t i = 1; i <= nargs; ++i) {
const char * str = lua_tostring(L,i);
lua_pushvalue(L, -1); // function to call: "tostring"
lua_pushvalue(L, i); // value to pass through tostring() before printing
lua_call(L, 1, 1);
const char * str = lua_tostring(L, -1);
if (!str) {
LOG_LUA << "'tostring' must return a value to 'print'\n";
str = "";
}
if (i > 1) {
cmd_log_ << "\t"; //separate multiple args with tab character
}
cmd_log_ << str;
DBG_LUA << "'" << str << "'\n";
lua_pop(L, 1); // Pop the output of tostrring()
}
lua_pop(L, 1); // Pop 'tostring' global

cmd_log_ << "\n";
DBG_LUA << "\n";
Expand Down

0 comments on commit 0eb25f4

Please sign in to comment.