Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)

Fri, 19 Nov 2010 19:21:36 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 19 Nov 2010 19:21:36 +0000
changeset 80
beb0bb936aca
parent 79
94c0441b7321
child 81
1deed5894ff6

Give LValue an equals() method, returns true/false if the value == the first argument (checks metamethod)

lvm.js file | annotate | diff | comparison | revisions
--- a/lvm.js	Fri Nov 19 16:57:32 2010 +0000
+++ b/lvm.js	Fri Nov 19 19:21:36 2010 +0000
@@ -118,6 +118,27 @@
 		}
 		else
 			throw "Attempt to perform arithmetic on a "+this.type+" and "+op2.type;
+	},
+	equals: function (op2)
+	{
+		if(this.type != op2.type)
+			return false;
+		if(this.value == op2.value)
+			return true;
+		var __eq = this.vm.LValue("__eq");
+		if(this.metatable && op2.metatable)
+		{
+			var metamethod1 = this.metatable.index(__eq);
+			var metamethod2 = op2.metatable.index(__eq);
+			if(metamethod1.equals(metamethod2))
+			{
+				var result = metamethod1.call([this, op2]);
+				return (result[0].type != "nil"
+					&& (result[0].type != "boolean" || result[0].value == true)
+				);
+			}
+		}
+		return false;
 	}
 };
 

mercurial