浏览代码

[java] Cast to bool makes "null" be converted into "false". Fixes Issue #852

Caue Waneck 13 年之前
父节点
当前提交
4c5dd5124e
共有 2 个文件被更改,包括 17 次插入3 次删除
  1. 9 3
      genjava.ml
  2. 8 0
      std/java/_std/haxe/lang/Runtime.hx

+ 9 - 3
genjava.ml

@@ -479,7 +479,6 @@ struct
     let tbyte = match ( get_type gen (["java"], "Int8") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
     let tbyte = match ( get_type gen (["java"], "Int8") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
     let tshort = match ( get_type gen (["java"], "Int16") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
     let tshort = match ( get_type gen (["java"], "Int16") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
     let tsingle = match ( get_type gen ([], "Single") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
     let tsingle = match ( get_type gen ([], "Single") ) with | TTypeDecl t -> TType(t,[]) | _ -> assert false in
-    let bool_cl = get_cl ( get_type gen (["java";"lang"], "Boolean")) in
     let string_ext = get_cl ( get_type gen (["haxe";"lang"], "StringExt")) 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
     let is_string t = match follow t with | TInst({ cl_path = ([], "String") }, []) -> true | _ -> false in
@@ -514,8 +513,15 @@ struct
           (* let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with *)
           (* let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with *)
           run { e with etype = unboxed_type gen e.etype tbyte tshort tchar tsingle }
           run { e with etype = unboxed_type gen e.etype tbyte tshort tchar tsingle }
         
         
-        | TCast(expr, m) when is_bool e.etype ->
-          { e with eexpr = TCast(mk_cast (TInst(bool_cl, [])) (run expr), m) }
+        | TCast(expr, _) when is_bool e.etype ->
+          {
+            eexpr = TCall(
+              mk_static_field_access_infer runtime_cl "toBool" expr.epos [],
+              [ run expr ]
+            );
+            etype = basic.tbool;
+            epos = e.epos
+          }
         
         
         | TCast(expr, _) when is_int_float gen e.etype && not (is_int_float gen expr.etype) ->
         | TCast(expr, _) when is_int_float gen e.etype && not (is_int_float gen expr.etype) ->
           let needs_cast = match gen.gfollow#run_f e.etype with
           let needs_cast = match gen.gfollow#run_f e.etype with

+ 8 - 0
std/java/_std/haxe/lang/Runtime.hx

@@ -112,6 +112,14 @@ package haxe.lang;
 		return 0.0;
 		return 0.0;
 	}
 	}
 	
 	
+	@:functionBody('
+		return (obj == null) ? false : ((java.lang.Boolean) obj).booleanValue();
+	')
+	public static function toBool(obj:Dynamic):Bool
+	{
+		return false;
+	}
+	
 	@:functionBody('
 	@:functionBody('
 		return (obj == null) ? 0 : ((java.lang.Number) obj).intValue();
 		return (obj == null) ? 0 : ((java.lang.Number) obj).intValue();
 	')
 	')