Browse Source

[java/cs] Fix Null<Int64> compare

Closes #4436
Cauê Waneck 9 years ago
parent
commit
ae2309311f

+ 12 - 0
src/generators/gencs.ml

@@ -505,6 +505,17 @@ struct
 					} in
 
 					if needs_cast then mk_cast e.etype ret else ret
+
+				| TCast(expr, _) when Common.defined gen.gcon Define.EraseGenerics && like_i64 e.etype && not (like_i64 expr.etype) ->
+					{
+						eexpr = TCall(
+							mk_static_field_access_infer runtime_cl "toLong" expr.epos [],
+							[ run expr ]
+						);
+						etype = ti64;
+						epos = expr.epos
+					}
+
 				| TCast(expr, _) when (is_string e.etype) && (not (is_string expr.etype)) && name() <> "haxe.lang.Runtime" ->
 					{ e with eexpr = TCall( mk_static_field_access_infer runtime_cl "toString" expr.epos [], [run expr] ) }
 				| TBinop( (Ast.OpNotEq as op), e1, e2)
@@ -1298,6 +1309,7 @@ let configure gen =
 					(match c with
 						| TInt i32 ->
 							write w (Int32.to_string i32);
+							(* these suffixes won't work because of the cast detector which will set each constant to its expected type *)
 							(*match real_type e.etype with
 								| TType( { t_path = (["haxe";"_Int64"], "NativeInt64") }, [] ) -> write w "L";
 								| _ -> ()

+ 11 - 8
src/generators/genjava.ml

@@ -568,6 +568,7 @@ struct
 		(* let tbyte = mt_to_t_dyn ( get_type gen (["java"], "Int8") ) in *)
 		(* let tshort = mt_to_t_dyn ( get_type gen (["java"], "Int16") ) in *)
 		(* let tsingle = mt_to_t_dyn ( get_type gen ([], "Single") ) in *)
+		let ti64 = mt_to_t_dyn ( get_type gen (["java"], "Int64") ) in
 		let string_ext = get_cl ( get_type gen (["haxe";"lang"], "StringExt")) in
 
 		let is_string t = match follow t with | TInst({ cl_path = ([], "String") }, []) -> true | _ -> false in
@@ -646,6 +647,16 @@ struct
 						| _ -> true
 					in
 					if need_second_cast then { e with eexpr = TCast(mk_cast (follow e.etype) (run expr), c) }  else Type.map_expr run e*)
+				| TCast(expr, _) when like_i64 e.etype && not (like_i64 expr.etype) ->
+					{
+						eexpr = TCall(
+							mk_static_field_access_infer runtime_cl "toLong" expr.epos [],
+							[ run expr ]
+						);
+						etype = ti64;
+						epos = expr.epos
+					}
+
 				| TBinop( (Ast.OpAssignOp OpAdd as op), e1, e2)
 				| TBinop( (Ast.OpAdd as op), e1, e2) when is_string e.etype || is_string e1.etype || is_string e2.etype ->
 						let is_assign = match op with Ast.OpAssignOp _ -> true | _ -> false in
@@ -1253,18 +1264,10 @@ let configure gen =
 					(match c with
 						| TInt i32 ->
 							print w "%ld" i32;
-							(match real_type e.etype with
-								| TType( { t_path = (["java"], "Int64") }, [] ) -> write w "L";
-								| _ -> ()
-							)
 						| TFloat s ->
 							write w s;
 							(* fix for Int notation, which only fit in a Float *)
 							(if not (String.contains s '.' || String.contains s 'e' || String.contains s 'E') then write w ".0");
-							(match real_type e.etype with
-								| TType( { t_path = ([], "Single") }, [] ) -> write w "f"
-								| _ -> ()
-							)
 						| TString s -> print w "\"%s\"" (escape s)
 						| TBool b -> write w (if b then "true" else "false")
 						| TNull ->

+ 8 - 0
std/cs/internal/Runtime.hx

@@ -24,6 +24,7 @@ import cs.Lib;
 import cs.Lib.*;
 import cs.NativeArray;
 import cs.NativeArray;
+import cs.StdTypes;
 import cs.system.Activator;
 import cs.system.IConvertible;
 import cs.system.IComparable;
@@ -154,6 +155,13 @@ import cs.system.Object;
 		return (obj == null) ? 0 : Std.is(obj,Int) ? cast obj : Lib.as(obj,IConvertible).ToInt32(null);
 	}
 
+#if erase_generics
+	public static function toLong(obj:Dynamic):Int64
+	{
+		return (obj == null) ? 0 : Std.is(obj,Int64) ? cast obj : Lib.as(obj,IConvertible).ToInt64(null);
+	}
+#end
+
 	public static function isInt(obj:Dynamic):Bool
 	{
 		var cv1 = Lib.as(obj, IConvertible);

+ 5 - 0
std/java/internal/Runtime.hx

@@ -143,6 +143,11 @@ package java.internal;
 		return 0;
 	}
 
+	public static function toLong(obj:Dynamic):haxe.Int64
+	{
+		return obj == null ? 0 : (obj : java.lang.Number).longValue();
+	}
+
 	@:functionCode('
 		if (obj != null && obj instanceof java.lang.Number)
 		{

+ 0 - 0
tests/unit/src/unit/issues/Issue4436.hx.disabled → tests/unit/src/unit/issues/Issue4436.hx