Commits

Igor Baidiuk committed ea697c7

Finished table. Crashes somewhere on pure virtual call. Need to debug

  • Participants
  • Parent commits 2289c18

Comments (0)

Files changed (2)

File src/common/Variant.cpp

 
 		static RefArray* create(size_t size, T* values)
 		{
-			if (!size)
+			if (!size || !values)
 				return NULL;
 			RefArray* newArray = allocate(size);
 			// Initialize all elements with values from array
 
 		static RefArray* create(size_t size, const T* values)
 		{
-			if (!size)
+			if (!size || !values)
 				return NULL;
 			RefArray* newArray = allocate(size);
 			// Initialize all elements with values from array
 		size_t dataSize;
 	};
 
+	typedef std::pair<Variant, Variant> Pair;
+
 	typedef RefArray<char>    RefString;
 	typedef RefArray<Variant> RefTuple;
+	typedef RefArray<Pair>    RefTable;
 
 	extern StringMap<Type, TYPE_MAX_ENUM> types;
 
 			data.full.data = userdata;
 	}
 
-	Variant::Variant(Pair* table, size_t size)
-	{
-		type = TABLE;
-		data.table.pairs = new Pair[size];
-		data.table.len = size;
-
-		for (size_t i = 0; i < size; ++i)
-			data.table.pairs[i] = table[i];
-	}
-
 	Variant::Variant(const Variant& other)
 		: type(UNKNOWN)
 	{
 				((RefTuple*)data.tuple)->retain();
 			break;
 		case TABLE:
+			if (data.table = other.data.table)
+				((RefTable*)data.table)->retain();
 			break;
 		}
 		return *this;
 					((RefTuple*)data.tuple)->release();
 				break;
 			case TABLE:
-				if (data.table.pairs)
-					delete[] data.table.pairs;
+				if (data.table)
+					((RefTable*)data.table)->release();
 				break;
 		}
 	}
 			case LUA_TTABLE:
 				{
 					lua_checkstack(L, 4);
-					std::vector<Variant::Pair> pairs;
+					std::vector<Pair> pairs;
 					lua_pushnil(L);
 					while (lua_next(L, n))
 					{
-						Variant::Pair pair = { new Variant(getVariant(L, -2)), new Variant(getVariant(L, -1)) };
-						if (pair.key && pair.value)
-							pairs.push_back(pair);
+						pairs.push_back(Pair(getVariant(L, -2), getVariant(L, -1)));
 						lua_pop(L, 1);
 					}
-					return Variant(&pairs.front(), pairs.size());
+					Variant v;
+					v.type = TABLE;
+					v.data.table = RefTable::create(pairs.size(), &pairs.front());
+					return v;
 				}
 		}
 		return Variant();
 				// I can do (at the moment).
 				break;
 			case TUPLE: // Tuple, all its values are placed on stack in direct order
-				if (!data.tuple)
-					return 0;
+				if (data.tuple)
 				{
 					RefTuple* t = (RefTuple*)data.tuple;
 					lua_checkstack(L, t->size() + 1);
 						i->toLua(L);
 					return t->size();
 				}
+				return 0;
 			case TABLE: // de-marshal table
 				lua_checkstack(L, 4);
 				lua_newtable(L);
-				for (size_t i = 0; i < data.table.len; ++i)
+				if (data.table)
 				{
-					Pair& p = data.table.pairs[i];
-					if (p.key && p.value)
+					RefTable *t = (RefTable*)data.table;
+					lua_checkstack(L, 4);
+					for (Pair *i = t->begin(), *e = t->end(); i != e; ++i)
 					{
-						p.key->toLua(L);
-						p.value->toLua(L);
-						// place new key-value pair into new table
-						// value is at top, key is one pos below and table is two pos below
+						i->first.toLua(L);
+						i->second.toLua(L);
 						lua_settable(L, -3);
 					}
 				}
 				printf("0-tuple\n");
 			break;
 		case TABLE:
-			printf("%u-table:\n", data.table.len);
-			++offset;
-			for (size_t i = 0; i < data.table.len; ++i)
+			if (data.table)
 			{
-				printf("%*sPair:\n", offset*2, "");
+				RefTable *t = (RefTable*)data.table;
+				printf("%u-table:\n", t->size());
 				++offset;
-				if (data.table.pairs[i].key)
-					data.table.pairs[i].key->prettyPrint();
-				else
-					printf("%*s NIL\n", offset*2, "");
-
-				if (data.table.pairs[i].value)
-					data.table.pairs[i].value->prettyPrint();
-				else
-					printf("%*s NIL\n", offset*2, "");
-
+				for (Pair *i = t->begin(), *e = t->end(); i != e; ++i)
+				{
+					printf("%*sPair:\n", offset*2, "");
+					++offset;
+					i->first.prettyPrint();
+					i->second.prettyPrint();
+					--offset;
+				}
 				--offset;
 			}
-			--offset;
+			else
+				printf("0-table\n");
 			break;
 		default:
-			printf("Unknown\n");
+			printf("NIL\n");
 		}
 	}
 } // love

File src/common/Variant.h

 	class Variant : public love::Object
 	{
 	private:
-		struct Pair
-		{
-			Ptr<Variant> key;
-			Ptr<Variant> value;
-		};
-
 		enum Type
 		{
 			UNKNOWN = 0,
 				love::Type type;
 			} full;
 			void* tuple;
-			struct {
-				Pair* pairs;
-				size_t len;
-			} table;
+			void* table;
 		} data;
 		bits flags;
 
 		Variant();
 		Variant(void *userdata);
 		Variant(love::Type udatatype, void *userdata);
-		Variant(Pair* table, size_t size);
 
 		static Variant getVariant(lua_State* L, int n);