Parcourir la source

[java] several fixes related to unit tests

Caue Waneck il y a 13 ans
Parent
commit
9024be6364

+ 39 - 9
gencommon.ml

@@ -1914,10 +1914,32 @@ struct
         | _ -> (gen.gcon.basic.tfloat, { eexpr = TConst(TFloat("1.0")); etype = gen.gcon.basic.tfloat; epos = e.epos })
     in
     
+    let basic = gen.gcon.basic in
+    
     let rec run e = 
       match e.eexpr with
-        | TBinop (OpAssignOp op, e1, e2) when should_change e -> (* e1 will never contain another TBinop (true story) *)
-          mk_paren { e with eexpr = TBinop(OpAssign, e1, run { e with eexpr = TBinop(op, e1, e2) }) }
+        | TBinop (OpAssignOp op, e1, e2) when should_change e -> (* e1 will never contain another TBinop *)
+          (match e1.eexpr with
+            | TLocal _ ->
+              mk_paren { e with eexpr = TBinop(OpAssign, e1, run { e with eexpr = TBinop(op, e1, e2) }) }
+            | TField _ | TArray _ ->
+              let eleft, rest = match e1.eexpr with
+                | TField(ef, f) ->
+                  let v = mk_temp gen "dynop" ef.etype in
+                  { e1 with eexpr = TField(mk_local v ef.epos, f) }, [ { eexpr = TVars([v,Some (run ef)]); etype = basic.tvoid; epos = ef.epos } ]
+                | TArray(e1a, e2a) ->
+                  let v = mk_temp gen "dynop" e1a.etype in
+                  let v2 = mk_temp gen "dynopi" e2a.etype in
+                  { e1 with eexpr = TArray(mk_local v e1a.epos, mk_local v2 e2a.epos) }, [ { eexpr = TVars([v,Some (run e1a); v2, Some (run e2a)]); etype = basic.tvoid; epos = e1.epos } ]
+                | _ -> assert false
+              in
+              { e with
+                eexpr = TBlock (rest @ [ { e with eexpr = TBinop(OpAssign, eleft, run { e with eexpr = TBinop(op, eleft, e2) }) } ]);
+              }
+            | _ ->
+              assert false
+          )
+          
         | TBinop (OpAssign, e1, e2)
         | TBinop (OpInterval, e1, e2) -> Type.map_expr run e
         | TBinop (op, e1, e2) when should_change e->
@@ -4630,7 +4652,7 @@ struct
           (* try / with because TNew might be overloaded *)
           (
           try 
-            { e with eexpr = TNew(cl, tparams, List.map2 (fun e t -> handle (run e) t e.etype) eparams (get_f (get_ctor_p cl tparams))) } 
+            { e with eexpr = TNew(cl, tparams, List.map2 (fun e t -> handle (run e) t e.etype) eparams (get_f (get_ctor_p cl tparams))) }
           with 
             | Invalid_argument(_) -> 
               { e with eexpr = TNew(cl, tparams, List.map run eparams); etype = TInst(cl, tparams) }
@@ -5561,7 +5583,7 @@ struct
   let collect_fields cl (methods : bool option) (statics : bool option) =
     let collected = Hashtbl.create 0 in
     let collect cf acc =
-      if has_meta ":$CompilerGenerated" cf.cf_meta then 
+      if has_meta ":$CompilerGenerated" cf.cf_meta || has_meta ":skipReflection" cf.cf_meta then 
         acc 
       else match methods, cf.cf_kind with
         | None, _ when not (Hashtbl.mem collected cf.cf_name) -> Hashtbl.add collected cf.cf_name true; ([cf.cf_name], cf) :: acc
@@ -7594,6 +7616,15 @@ struct
       let cl = mk_class en.e_module en.e_path pos in
       Hashtbl.add t.ec_tbl en.e_path cl;
       
+      (match Codegen.build_metadata gen.gcon (TEnumDecl en) with
+        | Some expr ->
+          let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
+          cf.cf_expr <- Some expr;
+          cl.cl_statics <- PMap.add "__meta__" cf cl.cl_statics;
+          cl.cl_ordered_statics <- cf :: cl.cl_ordered_statics
+        | _ -> ()
+      );
+      
       cl.cl_super <- Some(base_class,[]);
       cl.cl_extern <- en.e_extern;
       en.e_extern <- true;
@@ -7676,7 +7707,7 @@ struct
         epos = pos;
       };
       
-      cl.cl_ordered_statics <- constructs_cf :: cfs ;
+      cl.cl_ordered_statics <- constructs_cf :: cfs @ cl.cl_ordered_statics ;
       cl.cl_statics <- PMap.add "constructs" constructs_cf cl.cl_statics;
       
       (if should_be_hxgen then 
@@ -8788,9 +8819,7 @@ end;;
   the not-nullable type in the beginning of the function.
   
   dependencies:
-    Since it depends on no other module filter, but since any function programatically created
-    which needs default will only work if added before running DefaultArguments, it's best
-    if we keep it as the last 
+    It must run before OverloadingCtors, since OverloadingCtors will change optional structures behavior
   
 *)
 
@@ -8799,7 +8828,7 @@ struct
 
   let name = "default_arguments"
   
-  let priority = min_dep
+  let priority = solve_deps name [ DBefore OverloadingConstructor.priority ]
   
   let add_opt gen block pos (var,opt) =
     match opt with
@@ -8851,6 +8880,7 @@ struct
               tf_args = tf_args;
               tf_expr = Codegen.concat { tf.tf_expr with eexpr = TBlock(!block); etype = basic.tvoid } tf.tf_expr
             } ); etype = TFun(!args, ret) } );
+            cf.cf_type <- TFun(!args, ret)
             
           | _ -> ()
         );

+ 11 - 0
gencs.ml

@@ -1737,6 +1737,17 @@ let configure gen =
   CSharpSpecificSynf.configure gen (CSharpSpecificSynf.traverse gen runtime_cl);
   CSharpSpecificESynf.configure gen (CSharpSpecificESynf.traverse gen runtime_cl);
   
+  (* add resources array *)
+  (try
+    let res = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
+    let cf = PMap.find "content" res.cl_statics in
+    let res = ref [] in
+    Hashtbl.iter (fun name _ -> 
+      res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res
+    ) gen.gcon.resources;
+    cf.cf_expr <- Some ({ eexpr = TArrayDecl(!res); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = Ast.null_pos })
+  with | Not_found -> ());
+  
   run_filters gen;
   (* after the filters have been run, add all hashed fields to FieldLookup *)
   

+ 39 - 12
genjava.ml

@@ -191,6 +191,11 @@ struct
               }
             | TEnumDecl{ e_path = ([], "Bool") } ->
               mk_is obj bool_md
+            | TClassDecl{ cl_path = ([], "Dynamic") } ->
+              (match obj.eexpr with
+                | TLocal _ | TConst _ -> { e with eexpr = TConst(TBool true) }
+                | _ -> { e with eexpr = TBlock([run obj; { e with eexpr = TConst(TBool true) }]) }
+              )
             | _ ->
               mk_is obj md
           )
@@ -809,20 +814,28 @@ let configure gen =
       | TEnum ({e_path = ([], "Void")}, []) -> "void"
       | _ -> t_s t
   in
-
+  
+  let escape ichar b =
+    match ichar with
+      | 92 (* \ *) -> Buffer.add_string b "\\\\"
+      | 39 (* ' *) -> Buffer.add_string b "\\\'"
+      | 34 -> Buffer.add_string b "\\\""
+      | 13 (* \r *) -> Buffer.add_string b "\\r"
+      | 10 (* \n *) -> Buffer.add_string b "\\n"
+      | 9 (* \t *) -> Buffer.add_string b "\\t"
+      | c when c < 32 || c >= 127 -> Buffer.add_string b (Printf.sprintf "\\u%.4x" c)
+      | c -> Buffer.add_char b (Char.chr c)
+  in
+  
   let escape s =
     let b = Buffer.create 0 in
-    for i = 0 to String.length s - 1 do
-      match String.unsafe_get s i with
-      | '\\' -> Buffer.add_string b "\\\\"
-      | '\'' -> Buffer.add_string b "\\\'"
-      | '\"' -> Buffer.add_string b "\\\""
-      | '\r' -> Buffer.add_string b "\\r"
-      | '\n' -> Buffer.add_string b "\\n"
-      | '\t' -> Buffer.add_string b "\\t"
-      | c when (Char.code c) < 32 -> Buffer.add_string b (Printf.sprintf "\\u00%.2X" (Char.code c))
-      | c -> Buffer.add_char b c
-    done;
+    (try 
+      UTF8.validate s;
+      UTF8.iter (fun c -> escape (UChar.code c) b) s
+    with
+      UTF8.Malformed_code ->
+        String.iter (fun c -> escape (Char.code c) b) s
+    );
     Buffer.contents b
   in
   
@@ -1774,6 +1787,20 @@ let configure gen =
   let str_cl = match gen.gcon.basic.tstring with | TInst(cl,_) -> cl | _ -> assert false in
   str_cl.cl_super <- Some (get_cl (get_type gen (["haxe";"lang"], "NativeString")), []);
   
+  (* add resources array *)
+  (try
+    let res = get_cl (Hashtbl.find gen.gtypes (["haxe"], "Resource")) in
+    let cf = PMap.find "content" res.cl_statics in
+    let res = ref [] in
+    Hashtbl.iter (fun name v -> 
+      res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res;
+      let f = open_out (gen.gcon.file ^ "/src/" ^ name) in
+      output_string f v;
+      close_out f
+    ) gen.gcon.resources;
+    cf.cf_expr <- Some ({ eexpr = TArrayDecl(!res); etype = gen.gcon.basic.tarray gen.gcon.basic.tstring; epos = Ast.null_pos })
+  with | Not_found -> ());
+  
   run_filters gen;
   
   TypeParams.RenameTypeParameters.run gen;

+ 1 - 1
std/StringBuf.hx

@@ -77,7 +77,7 @@ class StringBuf {
 			b.Append(s, pos, l);
 		#elseif java
 			var l:Int = (len == null) ? s.length : len;
-			b.append(s, pos, l);
+			b.append(s, pos, pos + l);
 		#else
 			b += s.substr(pos,len);
 		#end

+ 6 - 2
std/StringTools.hx

@@ -52,7 +52,9 @@ class StringTools {
 		#elseif cpp
 			return s.__URLEncode();
 		#elseif java
-			return untyped __java__("java.net.URLEncoder.encode(s)");
+			try
+				return untyped __java__("java.net.URLEncoder.encode(s, \"UTF-8\")")
+			catch (e:Dynamic) throw e;
 		#elseif cs
 			return untyped __cs__("System.Uri.EscapeUriString(s)");
 		#else
@@ -77,7 +79,9 @@ class StringTools {
 		#elseif cpp
 			return s.__URLDecode();
 		#elseif java
-			return untyped __java__("java.net.URLDecoder.decode(s)");
+			try
+				return untyped __java__("java.net.URLDecoder.decode(s, \"UTF-8\")")
+			catch (e:Dynamic) throw e;
 		#elseif cs
 			return untyped __cs__("System.Uri.UnescapeDataString(s)");
 		#else

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
std/cs/internal/HxObject.hx


+ 13 - 4
std/haxe/Resource.hx

@@ -25,19 +25,28 @@
 package haxe;
 
 class Resource {
-
+	
+	#if !(java || cs)
 	static var content : Array<{ name : String, data : String, str : String }>;
+	#else
+	static var content : Array<String>;
+	#end
 
 	public static function listNames() : Array<String> {
 		var names = new Array();
-		for( x in content )
+		#if (java || cs)
+		for ( x in content )
+			names.push(x);
+		#else
+		for ( x in content )
 			names.push(x.name);
+		#end
 		return names;
 	}
 
 	public static function getString( name : String ) : String {
 		#if java
-		var stream = java.Lib.toNativeType(Resource).getResourceAsStream(name);
+		var stream = java.Lib.toNativeType(Resource).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;
 		var stream = new java.io.NativeInput(stream);
@@ -59,7 +68,7 @@ class Resource {
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
 		#if java
-		var stream = java.Lib.toNativeType(Resource).getResourceAsStream(name);
+		var stream = java.Lib.toNativeType(Resource).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;
 		var stream = new java.io.NativeInput(stream);

+ 17 - 0
std/haxe/Serializer.hx

@@ -367,6 +367,23 @@ class Serializer {
 				for( i in 0...l )
 					serialize(untyped __field__(v, __php__("params"), i));
 			}
+			#elseif (java || cs)
+			if( useEnumIndex ) {
+				buf.add(":");
+				buf.add(Type.enumIndex(v));
+			} else
+				serializeString(Type.enumConstructor(v));
+			buf.add(":");
+			var arr:Array<Dynamic> = Type.enumParameters(v);
+			if (arr != null)
+			{
+				buf.add(arr.length);
+				for (v in arr)
+					serialize(v);
+			} else {
+				buf.add("0");
+			}
+			
 			#else
 			if( useEnumIndex ) {
 				buf.add(":");

+ 17 - 4
std/haxe/io/Bytes.hx

@@ -185,7 +185,9 @@ class Bytes {
 		#elseif cs
 		return system.text.Encoding.UTF8.GetString(b, pos, len);
 		#elseif java
-		return new String(b, pos, len);
+		try
+			return new String(b, pos, len, "UTF-8")
+		catch (e:Dynamic) throw e;
 		#else
 		var s = "";
 		var b = b;
@@ -226,7 +228,11 @@ class Bytes {
 		#elseif cs
 		return system.text.Encoding.UTF8.GetString(b, 0, length);
 		#elseif java
-		return new String(b, 0, length);
+		try
+		{
+			return new String(b, 0, length, "UTF-8");
+		}
+		catch (e:Dynamic) throw e;
 		#else
 		return readString(0,length);
 		#end
@@ -297,9 +303,16 @@ class Bytes {
 		untyped __global__.__hxcpp_bytes_of_string(a,s);
 		return new Bytes(a.length, a);
 		#elseif cs
-		return new Bytes(s.length, system.text.Encoding.UTF8.GetBytes(s));
+		var b = system.text.Encoding.UTF8.GetBytes(s);
+		return new Bytes(b.Length, b);
+		);
 		#elseif java
-		return new Bytes(s.length, untyped s.getBytes());
+		try
+		{
+			var b:BytesData = untyped s.getBytes("UTF-8");
+			return new Bytes(b.length, b);
+		}
+		catch (e:Dynamic) throw e;
 		#else
 		var a = new Array();
 		// utf8-decode

+ 1 - 1
std/java/_std/Date.hx

@@ -15,7 +15,7 @@ class Date
 	**/
 	public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
 	{
-		date = new java.util.Date(year, month, day, hour, min, sec);
+		date = new java.util.Date(year != 0 ? year - 1900 : 0, month, day, hour, min, sec);
 	}
 
 	/**

+ 7 - 3
std/java/_std/Std.hx

@@ -29,12 +29,14 @@ import java.internal.Exceptions;
 @:core_api @:nativegen class Std {
 	public static function is( v : Dynamic, t : Dynamic ) : Bool 
 	{
-		if (v == null) 
-			return v == t;
+		if (v == null)
+			return t == Dynamic;
+		if (t == null)
+			return false;
 		var clt:Class<Dynamic> = cast t;
 		if (clt == null)
 			return false;
-		var name = untyped __java__("clt.getName()");
+		var name:String = untyped __java__("clt.getName()");
 		
 		switch(name)
 		{
@@ -44,6 +46,8 @@ import java.internal.Exceptions;
 				return untyped __java__('haxe.lang.Runtime.isInt(v)');
 			case "boolean", "java.lang.Boolean":
 				return untyped __java__('v instanceof java.lang.Boolean');
+			case "java.lang.Object":
+				return true;
 		}
 		
 		var clv:Class<Dynamic> = untyped __java__('v.getClass()');

+ 2 - 1
std/java/_std/String.hx

@@ -36,6 +36,7 @@ extern class String {
 	/**
 		Creates a copy from a given String.
 	**/
+	@:overload(function(b:haxe.io.BytesData, offset:Int, length:Int, charsetName:String):Void { })
 	@:overload(function(b:haxe.io.BytesData, offset:Int, length:Int):Void { })
 	function new(string:String) : Void;
 
@@ -102,7 +103,7 @@ extern class String {
 	
 	private function codePointAt( idx : Int ) : Int;
 	
-	private function getBytes() : haxe.io.BytesData;
+	private function getBytes(encoding:String) : haxe.io.BytesData;
 
 	static function fromCharCode( code : Int ) : String;
 

+ 26 - 26
std/java/_std/Type.hx

@@ -73,12 +73,14 @@ enum ValueType {
 		var name:String = c.getName();
 		if (name.startsWith("haxe.root."))
 			return name.substr(10);
+		if (name.startsWith("java.lang"))
+			name = name.substr(10);
 			
 		return switch(name)
 		{
-			case "int", "java.lang.Integer": "Int";
-			case "double", "java.lang.Double": "Float";
-			case "java.lang.String": "String";
+			case "int", "Integer": "Int";
+			case "double", "Double": "Float";
+			case "Object": "Dynamic";
 			default: name;
 		}
 	}
@@ -106,6 +108,7 @@ enum ValueType {
 			else if (name.equals("haxe.root.String")) return java.lang.String.class;
 			else if (name.equals("haxe.root.Math")) return java.lang.Math.class;
 			else if (name.equals("haxe.root.Class")) return java.lang.Class.class;
+			else if (name.equals("haxe.root.Dynamic")) return java.lang.Object.class;
 			return null;
 		}
 	')
@@ -149,33 +152,32 @@ enum ValueType {
 				Object o = args.__get(i);
 				objs[i]= o;
 				cls[i] = o.getClass();
+				boolean isNum = false;
 				
-				if (!(o instanceof java.lang.Number))
+				if (o instanceof java.lang.Number)
 				{
-					msl = realMsl;
-					realMsl = 0;
-					
-					for (int j = 0; j < msl; j++)
+					cls[i] = java.lang.Number.class;
+					isNum = hasNumber = true;
+				}
+				
+				msl = realMsl;
+				realMsl = 0;
+				
+				for (int j = 0; j < msl; j++)
+				{
+					java.lang.Class[] allcls = ms[j].getParameterTypes();
+					if (i < allcls.length)
 					{
-						java.lang.Class[] allcls = ms[j].getParameterTypes();
-						if (i < allcls.length)
+						if (! ((isNum && allcls[i].isPrimitive()) || allcls[i].isAssignableFrom(cls[i])) )
 						{
-							if (!allcls[i].isAssignableFrom(cls[i]))
-							{
+							ms[j] = null;
+						} else {
+							ms[realMsl] = ms[j];
+							if (realMsl != j)
 								ms[j] = null;
-							} else {
-								ms[realMsl] = ms[j];
-								if (realMsl != j)
-									ms[j] = null;
-								realMsl++;
-							}
+							realMsl++;
 						}
 					}
-					
-					if (realMsl == 0)
-						return null;
-				} else {
-					hasNumber = true;
 				}
 				
 			}
@@ -336,8 +338,6 @@ enum ValueType {
 			java.lang.Class cl = vobj.getClass();
 			if (v instanceof haxe.lang.DynamicObject)
 				return ValueType.TObject;
-			else if (v instanceof haxe.lang.Enum)
-				return ValueType.TEnum(cl);
 			else
 				return ValueType.TClass(cl);
 		} else if (v instanceof java.lang.Number) {
@@ -348,7 +348,7 @@ enum ValueType {
 				return ValueType.TFloat;
 		} else if (v instanceof haxe.lang.Function) {
 			return ValueType.TFunction;
-		} else if (v instanceof java.lang.Enum) {
+		} else if (v instanceof java.lang.Enum || v instanceof haxe.lang.Enum) {
 			return ValueType.TEnum(v.getClass());
 		} else if (v instanceof java.lang.Boolean) {
 			return ValueType.TBool;

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 1
std/java/internal/HxObject.hx


+ 20 - 19
std/java/internal/Runtime.hx

@@ -355,7 +355,7 @@ package java.internal;
 		int realMsl = 0;
 		for(int i =0; i < msl; i++)
 		{
-			if (ms[i].getName() != field || (!ms[i].isVarArgs() && ms[i].getParameterTypes().length != len))
+			if (!ms[i].getName().equals(field) || (!ms[i].isVarArgs() && ms[i].getParameterTypes().length != len))
 			{
 				ms[i] = null;
 			} else {
@@ -373,31 +373,32 @@ package java.internal;
 			Object o = args.__get(i);
 			objs[i]= o;
 			cls[i] = o.getClass();
+			boolean isNum = false;
 			
-			if (!(o instanceof java.lang.Number))
+			if (o instanceof java.lang.Number)
 			{
-				msl = realMsl;
-				realMsl = 0;
-				
-				for (int j = 0; j < msl; j++)
+				cls[i] = java.lang.Number.class;
+				isNum = hasNumber = true;
+			}
+			
+			msl = realMsl;
+			realMsl = 0;
+			
+			for (int j = 0; j < msl; j++)
+			{
+				java.lang.Class[] allcls = ms[j].getParameterTypes();
+				if (i < allcls.length)
 				{
-					java.lang.Class[] allcls = ms[j].getParameterTypes();
-					if (i < allcls.length)
+					if (!  ((isNum && allcls[i].isPrimitive()) || allcls[i].isAssignableFrom(cls[i])) )
 					{
-						if (!allcls[i].isAssignableFrom(cls[i]))
-						{
+						ms[j] = null;
+					} else {
+						ms[realMsl] = ms[j];
+						if (realMsl != j)
 							ms[j] = null;
-						} else {
-							ms[realMsl] = ms[j];
-							if (realMsl != j)
-								ms[j] = null;
-							realMsl++;
-						}
+						realMsl++;
 					}
 				}
-				
-			} else {
-				hasNumber = true;
 			}
 			
 		}

+ 3 - 3
tests/unit/TestReflect.hx

@@ -63,7 +63,7 @@ class TestReflect extends Test {
 		null,Int,String,Bool,Float,
 		Array,Hash,List,Date,Xml,Math,
 		unit.MyEnum,unit.MyClass,unit.MySubClass,
-		Class,Enum,Void,Dynamic,unit.MyInterface
+		Class,Enum,#if !(java || cs)Void,#end Dynamic,unit.MyInterface
 	];
 
 	static inline function u( s : String ) : String {
@@ -87,7 +87,7 @@ class TestReflect extends Test {
 		"null","Int","String","Bool","Float",
 		"Array",u("Hash"),u("List"),"Date","Xml","Math",
 		u2("unit","MyEnum"),u2("unit","MyClass"),u2("unit","MySubClass"),
-		#if !flash9 u #end("Class"), u("Enum"), u("Void"), u("Dynamic"),
+		#if !flash9 u #end("Class"), u("Enum"), #if !(java || cs) u("Void"), #end u("Dynamic"),
 		u2("unit","MyInterface")
 	];
 
@@ -99,7 +99,7 @@ class TestReflect extends Test {
 			f( t == null );
 			if( name == u("Enum") ) {
 				// neither an enum or a class
-			} else if( t == MyEnum || t == Void || t == Bool ) {
+			} else if( t == MyEnum || #if !(java || cs) t == Void || #end t == Bool ) {
 				eq( Type.getEnumName(t), name );
 				eq( Type.resolveEnum(name), t );
 			} else {

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff