Browse Source

Modify __tostring so it also shows a pointer, like lua does for tables

This should make it easier to identify unique objects.
Also modified internal code relying on tostring behaviour, and it should now
call its type method instead.
Bart van Strien 10 years ago
parent
commit
c15f4b5f26
2 changed files with 14 additions and 6 deletions
  1. 1 1
      src/common/Variant.cpp
  2. 13 5
      src/common/runtime.cpp

+ 1 - 1
src/common/Variant.cpp

@@ -31,7 +31,7 @@ static love::Type extractudatatype(lua_State *L, int idx)
 	Type t = INVALID_ID;
 	if (!lua_isuserdata(L, idx))
 		return t;
-	if (luaL_getmetafield(L, idx, "__tostring") == 0)
+	if (luaL_getmetafield(L, idx, "type") == 0)
 		return t;
 	lua_pushvalue(L, idx);
 	int result = lua_pcall(L, 1, 1, 0);

+ 13 - 5
src/common/runtime.cpp

@@ -47,6 +47,14 @@ static int w__gc(lua_State *L)
 }
 
 static int w__tostring(lua_State *L)
+{
+	Proxy *p = (Proxy *) lua_touserdata(L, 1);
+	const char *typname = lua_tostring(L, lua_upvalueindex(1));
+	lua_pushfstring(L, "%s: %p", typname, p->object);
+	return 1;
+}
+
+static int w__type(lua_State *L)
 {
 	lua_pushvalue(L, lua_upvalueindex(1));
 	return 1;
@@ -313,9 +321,9 @@ int luax_register_type(lua_State *L, love::Type type, const luaL_Reg *f, bool pu
 	lua_pushcclosure(L, w__tostring, 1);
 	lua_setfield(L, -2, "__tostring");
 
-	// Add tostring to as type() as well.
+	// Add type
 	lua_pushstring(L, tname);
-	lua_pushcclosure(L, w__tostring, 1);
+	lua_pushcclosure(L, w__type, 1);
 	lua_setfield(L, -2, "type");
 
 	// Add typeOf
@@ -652,15 +660,15 @@ extern "C" int luax_typerror(lua_State *L, int narg, const char *tname)
 	const char *argtname = 0;
 
 	// We want to use the love type name for userdata, if possible.
-	if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "__tostring") != 0)
+	if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "type") != 0)
 	{
 		lua_pushvalue(L, narg);
 		if (lua_pcall(L, 1, 1, 0) == 0 && lua_type(L, -1) == LUA_TSTRING)
 		{
 			argtname = lua_tostring(L, -1);
 
-			// Non-love userdata might have a tostring metamethod which doesn't
-			// describe its type, so we only use __tostring for love types.
+			// Non-love userdata might have a type metamethod which doesn't
+			// describe its type properly, so we only use it for love types.
 			love::Type t;
 			if (!love::getType(argtname, t))
 				argtname = 0;