Browse Source

fixed equality of Arrays (closes #2936)

frabbit 11 years ago
parent
commit
b03c919f30
3 changed files with 36 additions and 0 deletions
  1. 15 0
      genpy.ml
  2. 8 0
      std/python/internal/HxOverrides.hx
  3. 13 0
      tests/unit/TestPython.hx

+ 15 - 0
genpy.ml

@@ -1059,6 +1059,21 @@ module Printer = struct
 				Printf.sprintf "(%s is %s)" (print_expr pctx e1) (print_expr pctx e2)
 			| TBinop(OpNotEq,e1,({eexpr = TConst TNull} as e2)) ->
 				Printf.sprintf "(%s is not %s)" (print_expr pctx e1) (print_expr pctx e2)
+			| TBinop(OpEq|OpNotEq as op,e1, e2) ->
+				let ops = match op with
+					| OpEq -> "is", "==", "HxOverrides.eq"
+					| OpNotEq -> "is not", "!=", "not HxOverrides.eq"
+					| _ -> assert false
+				in
+				let third (_,_,x) = x in
+				let fst (x,_,_) = x in
+				let snd (_,x,_) = x in
+				(match follow e1.etype, follow e2.etype with
+				| TInst({cl_path = [],("list")},_), TInst({cl_path = [],("list")},_) ->
+					Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (fst ops) (print_expr pctx e2)
+				| TDynamic _, _ | _, TDynamic _ ->
+					Printf.sprintf "%s(%s,%s)" (third ops) (print_expr pctx e1) (print_expr pctx e2)
+				| _,_ -> Printf.sprintf "(%s %s %s)" (print_expr pctx e1) (snd ops) (print_expr pctx e2))
 			| TBinop(OpMod,e1,e2) when (is_type1 "" "Int")(e1.etype) && (is_type1 "" "Int")(e2.etype) ->
 				Printf.sprintf "(%s %% %s)" (print_expr pctx e1) (print_expr pctx e2)
 			| TBinop(OpMod,e1,e2) ->

+ 8 - 0
std/python/internal/HxOverrides.hx

@@ -7,6 +7,7 @@ import python.Syntax.pythonCode in py;
 @:keep
 @:native("HxOverrides")
 @:access(python.internal.ArrayImpl)
+@:access(python.Boot)
 class HxOverrides {
 
 	// this two cases iterator and shift are like all methods in String and Array and are already handled in Reflect
@@ -19,6 +20,13 @@ class HxOverrides {
 		return Reflect.callMethod(null, Reflect.field(x, "iterator"), []);
 	}
 
+	static function eq( a:Dynamic, b:Dynamic ) : Bool {
+		if (Boot.isArray(a) || Boot.isArray(b)) {
+			return Syntax.pythonCode('$a is $b');
+		}
+		return Syntax.binop(a, "==", b);
+	}
+
 	static public function shift(x) {
 		return Reflect.callMethod(null, Reflect.field(x, "shift"), []);
 	}

+ 13 - 0
tests/unit/TestPython.hx

@@ -289,6 +289,19 @@ class TestPython extends Test {
 		eq(1, test3(1)[0]);
 
 		eq("foo1bar", Syntax.pythonCode("'foo' + str(" + x + ") + 'bar'"));
+	}
 
+	// Issue #2936
+	function testArrayEq () {
+		f([1,2,3] == [1,2,3]);
+		f(([1,2,3]:Dynamic) == ([1,2,3]:Dynamic));
+		f(([1,2,3]:Dynamic) == [1,2,3]);
+		f([1,2,3] == ([1,2,3]:Dynamic));
+
+		t([1,2,3] != [1,2,3]);
+		t(([1,2,3]:Dynamic) != ([1,2,3]:Dynamic));
+		t(([1,2,3]:Dynamic) != [1,2,3]);
+		t([1,2,3] != ([1,2,3]:Dynamic));
 	}
+
 }