Author: silene
Date: Sat Apr 18 20:38:24 2009
New Revision: 35023

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35023&view=rev
Log:
Fixed incorrect assumption on concat operator.

Modified:
    trunk/src/scripting/lua.cpp

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=35023&r1=35022&r2=35023&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Sat Apr 18 20:38:24 2009
@@ -219,32 +219,45 @@
 }
 
 /**
- * Appends a scalar to a t_string object.
- */
-static int lua_tstring_concat(lua_State *L)
-{
-       t_string *t = static_cast<t_string *>(lua_touserdata(L, 1));
-       // Hidden metamethod, so *t has to be a t_string object. Copy it in a 
new t_string.
-       t = new(lua_newuserdata(L, sizeof(t_string))) t_string(*t);
-
-       lua_pushlightuserdata(L, (void *)&tstringKey);
-       lua_gettable(L, LUA_REGISTRYINDEX);
-
-       switch (lua_type(L, 2)) {
+ * Converts a Lua value at position @a src and appends it to @a dst.
+ * @note This function is private to lua_tstring_concat. It expects two things.
+ *       First, the t_string metatable is at the top of the stack on entry. (It
+ *       is still there on exit.) Second, the caller hasn't any valuable object
+ *       with dynamic lifetime, since they would leaked on error.
+ */
+static void lua_tstring_concat_aux(lua_State *L, t_string &dst, int src)
+{
+       switch (lua_type(L, src)) {
                case LUA_TNUMBER:
                case LUA_TSTRING:
-                       *t += lua_tostring(L, 2);
+                       dst += lua_tostring(L, src);
                        break;
                case LUA_TUSERDATA:
                        // Compare its metatable with t_string's metatable.
-                       if (!lua_getmetatable(L, 2) || !lua_rawequal(L, -1, -2))
-                               return luaL_typerror(L, 2, "string");
-                       *t += *static_cast<t_string *>(lua_touserdata(L, 2));
+                       if (!lua_getmetatable(L, src) || !lua_rawequal(L, -1, 
-2))
+                               luaL_typerror(L, src, "string");
+                       dst += *static_cast<t_string *>(lua_touserdata(L, src));
                        lua_pop(L, 1);
                        break;
                default:
-                       return luaL_typerror(L, 2, "string");
-       }
+                       luaL_typerror(L, src, "string");
+       }
+}
+
+/**
+ * Appends a scalar to a t_string object.
+ */
+static int lua_tstring_concat(lua_State *L)
+{
+       // Create a new t_string.
+       t_string *t = new(lua_newuserdata(L, sizeof(t_string))) t_string;
+
+       lua_pushlightuserdata(L, (void *)&tstringKey);
+       lua_gettable(L, LUA_REGISTRYINDEX);
+
+       // Append both arguments to t.
+       lua_tstring_concat_aux(L, *t, 1);
+       lua_tstring_concat_aux(L, *t, 2);
 
        lua_setmetatable(L, -2);
        return 1;


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to