Bläddra i källkod

[cs] Many unit tests-related fixes

Caue Waneck 12 år sedan
förälder
incheckning
601ea00e21
6 ändrade filer med 118 tillägg och 64 borttagningar
  1. 33 8
      gencs.ml
  2. 32 15
      std/cs/_std/EReg.hx
  3. 14 2
      std/cs/internal/Runtime.hx
  4. 0 0
      std/cs/internal/StringExt.hx
  5. 2 2
      std/haxe/Serializer.hx
  6. 37 37
      tests/unit/TestMisc.hx

+ 33 - 8
gencs.ml

@@ -312,10 +312,7 @@ struct
             | _ -> true
           in
 
-          let fun_name = match follow e.etype with
-            | TInst ({ cl_path = ([], "Float") },[]) -> "toDouble"
-            | _ -> "toInt"
-          in
+          let fun_name = if like_int e.etype then "toInt" else "toDouble" in
 
           let ret = {
             eexpr = TCall(
@@ -626,7 +623,6 @@ let configure gen =
       | TAbstract _
       | TType _ -> t
       | TAnon (anon) when (match !(anon.a_status) with | Statics _ | EnumStatics _ -> true | _ -> false) -> t
-      | TAnon _ -> dynamic_anon
       | TFun _ -> TInst(fn_cl,[])
       | _ -> t_dynamic
     in
@@ -884,9 +880,7 @@ let configure gen =
           write w "( ";
           expr_s w e1; write w ( " " ^ (Ast.s_binop op) ^ " " ); expr_s w e2;
           write w " )"
-        | TField (e, s) | TClosure (e, s) ->
-          expr_s w e; write w "."; write_field w s
-        | TTypeExpr mt ->
+        | TField ({ eexpr = TTypeExpr mt }, s) | TClosure ({ eexpr = TTypeExpr mt }, s) ->
           (match mt with
             | TClassDecl { cl_path = (["haxe"], "Int64") } -> write w (path_s (["haxe"], "Int64"))
             | TClassDecl { cl_path = (["haxe"], "Int32") } -> write w (path_s (["haxe"], "Int32"))
@@ -894,6 +888,19 @@ let configure gen =
             | TEnumDecl en -> write w (t_s (TEnum(en, List.map (fun _ -> t_empty) en.e_types)))
             | TTypeDecl td -> write w (t_s (gen.gfollow#run_f (TType(td, List.map (fun _ -> t_empty) td.t_types))))
             | TAbstractDecl a -> write w (t_s (TAbstract(a, List.map (fun _ -> t_empty) a.a_types)))
+          );
+          write w ".";
+          write_field w s
+        | TField (e, s) | TClosure (e, s) ->
+          expr_s w e; write w "."; write_field w s
+        | TTypeExpr mt ->
+          (match mt with
+            | TClassDecl { cl_path = (["haxe"], "Int64") } -> write w (path_s (["haxe"], "Int64"))
+            | TClassDecl { cl_path = (["haxe"], "Int32") } -> write w (path_s (["haxe"], "Int32"))
+            | TClassDecl cl -> write w (t_s (TInst(cl, List.map (fun _ -> t_dynamic) cl.cl_types)))
+            | TEnumDecl en -> write w (t_s (TEnum(en, List.map (fun _ -> t_dynamic) en.e_types)))
+            | TTypeDecl td -> write w (t_s (gen.gfollow#run_f (TType(td, List.map (fun _ -> t_dynamic) td.t_types))))
+            | TAbstractDecl a -> write w (t_s (TAbstract(a, List.map (fun _ -> t_dynamic) a.a_types)))
           )
         | TParenthesis e ->
           write w "("; expr_s w e; write w ")"
@@ -1247,11 +1254,28 @@ let configure gen =
         (path_s path) ^ "." ^ fn_name, false, true
       | name -> name, false, false
     in
+    let rec loop_static cl =
+      match is_static, cl.cl_super with
+        | false, _ -> []
+        | true, None -> []
+        | true, Some(cl,_) ->
+           (try
+              let cf2 = PMap.find cf.cf_name cl.cl_statics in
+              Gencommon.CastDetect.type_eq gen EqStrict cf.cf_type cf2.cf_type;
+              ["new"]
+            with
+              | Not_found | Unify_error _ ->
+                  loop_static cl
+            )
+    in
+    let modf = loop_static cl in
+
     (match cf.cf_kind with
       | Var _
       | Method (MethDynamic) ->
         if not is_interface then begin
           let access, modifiers = get_fun_modifiers cf.cf_meta "public" [] in
+          let modifiers = modifiers @ modf in
           (match cf.cf_expr with
             | Some e ->
               print w "%s %s%s %s %s = " access (if is_static then "static " else "") (String.concat " " modifiers) (t_s (run_follow gen cf.cf_type)) (change_field name);
@@ -1278,6 +1302,7 @@ let configure gen =
         let visibility = if is_interface then "" else "public" in
 
         let visibility, modifiers = get_fun_modifiers cf.cf_meta visibility [] in
+        let modifiers = modifiers @ modf in
         let visibility, is_virtual = if is_explicit_iface then "",false else visibility, is_virtual in
         let v_n = if is_static then "static " else if is_override && not is_interface then "override " else if is_virtual then "virtual " else "" in
         let ret_type, args = match follow cf.cf_type with | TFun (strbtl, t) -> (t, strbtl) | _ -> assert false in

+ 32 - 15
std/cs/_std/EReg.hx

@@ -27,6 +27,7 @@ import cs.system.text.regularExpressions.Regex;
 	private var m : Match;
 	private var isGlobal : Bool;
 	private var cur : String;
+	private var sub : Int;
 
 	public function new( r : String, opt : String ) : Void {
 		var opts:Int = cast CultureInvariant;
@@ -48,6 +49,7 @@ import cs.system.text.regularExpressions.Regex;
 	}
 
 	public function match( s : String ) : Bool {
+		sub = 0;
 		m = regex.Match(s);
 		cur = s;
 		return m.Success;
@@ -60,21 +62,25 @@ import cs.system.text.regularExpressions.Regex;
 	}
 
 	public function matchedLeft() : String {
-		return untyped cur.Substring(0, m.Index);
+		return untyped cur.Substring(0, sub + m.Index);
 	}
 
 	public function matchedRight() : String {
-		return untyped cur.Substring(m.Index + m.Length);
+		return untyped cur.Substring(sub + m.Index + m.Length);
 	}
 
 	public function matchedPos() : { pos : Int, len : Int } {
-		return { pos : m.Index, len : m.Length };
+		return { pos : sub +  m.Index, len : m.Length };
 	}
 
 	public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
-		return throw "not implemented yet";
-	}	
-	
+		var s2 = (len < 0 ? s.substr(pos) : s.substr(pos, len));
+		sub = pos;
+		m = regex.Match(s2);
+		cur = s;
+		return m.Success;
+	}
+
 	public function split( s : String ) : Array<String> {
 		if (isGlobal)
 			return cs.Lib.array(regex.Split(s));
@@ -90,20 +96,31 @@ import cs.system.text.regularExpressions.Regex;
 	}
 
 	public function map( s : String, f : EReg -> String ) : String {
+		var offset = 0;
 		var buf = new StringBuf();
-		while (true)
-		{
-			if (!match(s))
+		do {
+			if (offset >= s.length)
 				break;
-			buf.add(matchedLeft());
+			else if (!matchSub(s, offset)) {
+				buf.add(s.substr(offset));
+				break;
+			}
+			var p = matchedPos();
+			buf.add(s.substr(offset, p.pos - offset));
 			buf.add(f(this));
-			s = matchedRight();
-		}
-		buf.add(s);
+			if (p.len == 0) {
+				buf.add(s.substr(p.pos, 1));
+				offset = p.pos + 1;
+			}
+			else
+				offset = p.pos + p.len;
+		} while (isGlobal);
+		if (!isGlobal && offset < s.length)
+			buf.add(s.substr(offset));
 		return buf.toString();
 	}
 
 	#if !haxe3
 	public inline function customReplace( s : String, f : EReg -> String ) : String return map(s, f)
-	#end	
-}
+	#end
+}

+ 14 - 2
std/cs/internal/Runtime.hx

@@ -131,6 +131,8 @@ import cs.system.Type;
 	}
 
 	@:functionBody('
+			if (v1 is System.Type)
+				return typeEq(v1 as System.Type, v2 as System.Type);
 			return System.Object.ReferenceEquals(v1, v2);
 	')
 	public static function refEq(v1: { }, v2: { } ):Bool
@@ -428,6 +430,8 @@ import cs.system.Type;
 			}
 
 			methodLength = last;
+		} else if (methodLength == 1 && methods[0].GetParameters().Length != length) {
+			methodLength = 0;
 		}
 
 		//At this time, we should be left with only one method.
@@ -614,13 +618,21 @@ import cs.system.Type;
 	{
 		if (obj == null)
 			return null;
+		if (Std.is(obj, Bool))
+			if(obj)
+				return "true";
+			else
+				return "false";
+
 		return untyped obj.ToString();
 	}
 
 	@:functionBody('
 			if (t1 == null || t2 == null)
 				return t1 == t2;
-			return t1.Name.Equals(t2.Name);
+			string n1 = Type.getClassName(t1);
+			string n2 = Type.getClassName(t2);
+			return n1.Equals(n2);
 	')
 	public static function typeEq(t1:cs.system.Type, t2:cs.system.Type):Bool
 	{
@@ -671,4 +683,4 @@ import cs.system.Type;
 @:keep @:native("haxe.lang.EmptyObject") private enum EmptyObject
 {
 	EMPTY;
-}
+}

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
std/cs/internal/StringExt.hx


+ 2 - 2
std/haxe/Serializer.hx

@@ -254,7 +254,7 @@ class Serializer {
 					var b1 = v.get(i++);
 					var b2 = v.get(i++);
 					var b3 = v.get(i++);
-					
+
 					charsBuf.add(b64.charAt(b1 >> 2));
 					charsBuf.add(b64.charAt(((b1 << 4) | (b2 >> 4)) & 63));
 					charsBuf.add(b64.charAt(((b2 << 2) | (b3 >> 6)) & 63));
@@ -383,7 +383,7 @@ class Serializer {
 			} else {
 				buf.add("0");
 			}
-			
+
 			#else
 			if( useEnumIndex ) {
 				buf.add(":");

+ 37 - 37
tests/unit/TestMisc.hx

@@ -29,7 +29,7 @@ class MyDynamicClass {
 	@:isVar public static var W(get, set) : Int = 55;
 	static function get_W() return W + 2
 	static function set_W(v) { W = v; return v; }
-	
+
 }
 
 class MyDynamicSubClass extends MyDynamicClass {
@@ -37,7 +37,7 @@ class MyDynamicSubClass extends MyDynamicClass {
 	override function add(x,y) {
 		return (v + x + y) * 2;
 	}
-	
+
 }
 
 class MyDynamicSubClass2 extends MyDynamicClass {
@@ -149,7 +149,7 @@ class TestMisc extends Test {
 		var c = MyEnum.C;
 		t( Type.enumEq(MyEnum.C(1,"hello"), c(1,"hello")) );
 	}
-	
+
 	// make sure that captured variables does not overlap each others even if in different scopes
 	function testCaptureUnique() {
 		var foo = null, bar = null;
@@ -165,7 +165,7 @@ class TestMisc extends Test {
 		eq( foo(), 1);
 		eq( bar(), 2);
 	}
-	
+
 	function testCaptureUnique2() {
 		// another more specialized test (was actually the original broken code - but not reproducible when optimization is off)
 		var foo = callback(id, 3);
@@ -173,14 +173,14 @@ class TestMisc extends Test {
 		eq( foo(), 3 );
 		eq( bar(), 25 );
 	}
-	
+
 	function testSelfRef() {
 		// check for self-name binding
 		var bla = 55;
 		var bla = function() return bla;
 		eq( bla(), 55);
 	}
-	
+
 	function testHiddenType() {
 		var haxe = 20;
 		eq( std.haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e");
@@ -201,7 +201,7 @@ class TestMisc extends Test {
 		eq( std.haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e");
 		eq( std.Std.int(45.3), 45);
 	}
-	
+
 	function testHiddenTypeCapture() {
 		var flag = true;
 		var foo = null, bar = null;
@@ -220,11 +220,11 @@ class TestMisc extends Test {
 	function id(x) {
 		return x;
 	}
-	
+
 	function sq(x) {
 		return x * x;
 	}
-	
+
 	function testPropertyInit() {
 		eq(MyDynamicClass.W, 57);
 	}
@@ -256,7 +256,7 @@ class TestMisc extends Test {
 		eq( inst.add(1,2), 206 );
 		eq( callback(inst.add,1)(2), 206 );
 		eq( add(1,2), 206 );
-		
+
 		// check redefined dynamic method
 		inst.add = function(x,y) return inst.get() * 2 + x + y;
 		var add = inst.add;
@@ -282,7 +282,7 @@ class TestMisc extends Test {
 		eq( haxe.crypto.Md5.encode("hello"), "5d41402abc4b2a76b9719d911017c592" );
 		// depending of ISO/UTF8 native
 		allow( haxe.crypto.Md5.encode("héllo"), ["1a722f7e6c801d9e470a10cb91ba406d", "be50e8478cf24ff3595bc7307fb91b50"] );
-		
+
 		eq( haxe.io.Bytes.ofString("héllo").toHex(), "68c3a96c6c6f");
 		eq( haxe.crypto.Md5.make(haxe.io.Bytes.ofString("héllo")).toHex(), "be50e8478cf24ff3595bc7307fb91b50" );
 	}
@@ -292,10 +292,10 @@ class TestMisc extends Test {
 		eq( haxe.crypto.Sha1.encode("hello"), "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" );
 		// depending of ISO/UTF8 native
 		allow( haxe.crypto.Sha1.encode("héllo"), ["028db752c14604d624e8b1c121d600c427b8a3ba","35b5ea45c5e41f78b46a937cc74d41dfea920890"] );
-		
+
 		eq( haxe.crypto.Sha1.make(haxe.io.Bytes.ofString("héllo")).toHex(), "35b5ea45c5e41f78b46a937cc74d41dfea920890" );
 	}
-	
+
 	function testBaseCode() {
 		var b = new haxe.BaseCode(haxe.io.Bytes.ofString("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"));
 		eq( b.encodeString("Héllow"), "iceFr6NLtM" );
@@ -314,11 +314,11 @@ class TestMisc extends Test {
 	function opt2( ?x = 5, ?y = "hello" ) {
 		return { x : x, y : y };
 	}
-	
+
 	function opt3( ?x : Null<Int> = 5, ?y : Null<Float> = 6 ) {
 		return { x : x, y : y };
 	}
-	
+
 	function opt4( x = 10 ) : Null<Int> {
 		return x + 1;
 	}
@@ -335,7 +335,7 @@ class TestMisc extends Test {
 
 		eq( opt2().x, 5 );
 		eq( opt2().y, "hello" );
-		
+
 		#if !(flash9 || cpp || cs || java)
 		eq( opt2(null, null).x, 5 );
 		#end
@@ -353,16 +353,16 @@ class TestMisc extends Test {
 		eq( opt3(null).y, 6 );
 		eq( opt3(null,7).x, 5 );
 		eq( opt3(null, 7).y, 7 );
-		
+
 		// skipping
 		eq( opt3(7.4).x, 5 );
 		eq( opt3(7.4).y, 7.4 );
-		
+
 		eq( opt4(), 11 );
 		#if !(flash9 || cpp || cs || java)
 		eq( opt4(null), 11 );
 		#end
-		
+
 		var opt4b : ?Int -> Null<Int> = opt4;
 		eq( opt4b(), 11 );
 		eq( opt4b(3), 4 );
@@ -373,7 +373,7 @@ class TestMisc extends Test {
 		// don't compile because we restrict nullability of function param or return type
 		// var opt4c : ?Null<Int> -> Null<Int> = opt4;
 		// var opt4c : ?Int -> Int = opt4;
-		
+
 		var opt5 = function(a:Int, ?b = 2) return a + b;
 		eq(3, opt5(1));
 		eq(3, opt5(1, 2));
@@ -407,13 +407,13 @@ class TestMisc extends Test {
 		eq( arr[x++].v++, 3 );
 		eq( x, 1 );
 		eq( arr[0].v, 4 );
-		
+
 		#if !as3
 		x = 0;
 		eq( arr[x++].v += 3, 7 );
 		eq( arr[0].v, 7 );
 		#end
-		
+
 		x = 0;
 		var arr:Dynamic = [{ v : 3 }];
 		eq( arr[x++].v++, 3 );
@@ -442,7 +442,7 @@ class TestMisc extends Test {
 	}
 
 	static inline function foo(x) return x + 5
-	
+
 	function testInline() {
 		// check that operations are correctly generated
 		var x = 3; // prevent optimization
@@ -473,7 +473,7 @@ class TestMisc extends Test {
 	function testStaticVarFun() {
 		eq( add(2,3), 5);
 	}
-	
+
 	function testDefArgs() {
 		var e = new ExtDefArgs();
 		eq( e.get(), 7 );
@@ -495,13 +495,13 @@ class TestMisc extends Test {
 		b.addChar("R".code);
 		eq(b.toString(), "-451.456nulltruefalseHello!laR");
 	}
-	
+
 	function testToString():Void
 	{
 		var x = { toString : function() return "foo" };
 		eq( Std.string(x), "foo" );
 	}
-	
+
 	#if !macro
 	function testFormat()
 	{
@@ -510,7 +510,7 @@ class TestMisc extends Test {
 		eq('$x${x+y}', "511");
 	}
 	#end
-	
+
 	function testRandom() {
 		var x = Std.random(2);
 		t( x == 0 || x == 1);
@@ -518,7 +518,7 @@ class TestMisc extends Test {
 		eq(Std.random(0), 0);
 		eq(Std.random(-100), 0);
 	}
-	
+
 	function testJSon() {
 		var str = haxe.Json.stringify( { x : -4500, y : 1.456, a : ["hello", "wor'\"\n\t\rd"] } );
 		str = str.substr(1, str.length - 2); // remove {}
@@ -528,18 +528,18 @@ class TestMisc extends Test {
 		t( parts.remove('"a":["hello"') );
 		t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
 		eq( parts.join("#"), "" );
-		
+
 		// no support for regexps
 		#if flash8
 		return;
 		#end
-		
+
 		function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v);
 		function deepId(v:Dynamic) {
 			var str = haxe.Json.stringify(v);
 			eq(haxe.Json.stringify(haxe.Json.parse(str)), str);
 		}
-		
+
 		id(true);
 		id(false);
 		id(null);
@@ -558,17 +558,17 @@ class TestMisc extends Test {
 		deepId( { test: { nested: null }} );
 		var mix : Array<Dynamic> = [1, 2, 3, "str"];
 		deepId( {array: mix} );
-		
+
 		eq( haxe.Json.parse('"\\u00E9"'), "é" );
-		
+
 	}
-	
+
 	function testConstructorsOpts() {
 		var b = new BaseConstrOpt();
 		eq(b.s, "test");
 		eq(b.i, -5);
 		eq(b.b, true);
-		
+
 		var b = new BaseConstrOpt(null, 99);
 		eq(b.s, "test");
 		eq(b.i, 99);
@@ -578,12 +578,12 @@ class TestMisc extends Test {
 		eq(b.s, "test");
 		eq(b.i, -5);
 		eq(b.b, true);
-		
+
 		var b = new SubConstrOpt2();
 		eq(b.s, "test");
 		eq(b.i, -5);
 		eq(b.b, true);
-		
+
 		var b = new SubConstrOpt3();
 		eq(b.s, "test2");
 		eq(b.i, -6);

Vissa filer visades inte eftersom för många filer har ändrats