فهرست منبع

[cs] properly support parametrized types in non-optimized Std.is (closes #4285)

Dan Korostelev 9 سال پیش
والد
کامیت
c281f6fbc0
3فایلهای تغییر یافته به همراه33 افزوده شده و 8 حذف شده
  1. 20 7
      std/cs/_std/Std.hx
  2. 1 1
      std/cs/internal/Runtime.hx
  3. 12 0
      tests/unit/src/unit/issues/Issue4285.hx

+ 20 - 7
std/cs/_std/Std.hx

@@ -30,24 +30,37 @@ import cs.internal.Exceptions;
 			return t == Dynamic;
 		if (t == null)
 			return false;
-		var clt:cs.system.Type = cast t;
+		var clt = cs.Lib.as(t, cs.system.Type);
 		if (clt == null)
 			return false;
-		var name:String = cast clt;
 
-		switch(name)
+		switch(clt.ToString())
 		{
 			case "System.Double":
-				return untyped __cs__('v is double || v is int');
+				return untyped __cs__('{0} is double || {0} is int', v);
 			case "System.Int32":
-				return untyped __cs__('haxe.lang.Runtime.isInt(v)');
+				return cs.internal.Runtime.isInt(v);
 			case "System.Boolean":
-				return untyped __cs__('v is bool');
+				return untyped __cs__('{0} is bool', v);
 			case "System.Object":
 				return true;
 		}
 
-		return clt.IsAssignableFrom(cs.Lib.getNativeType(v));
+		var vt = cs.Lib.getNativeType(v);
+
+		if (clt.IsAssignableFrom(vt))
+			return true;
+
+		#if !erase_generics
+		for (iface in clt.GetInterfaces()) {
+			var g = cs.internal.Runtime.getGenericAttr(iface);
+			if (g != null && g.generic == clt) {
+				return iface.IsAssignableFrom(vt);
+			}
+		}
+		#end
+
+		return false;
 	}
 
 	public static function string( s : Dynamic ) : String {

+ 1 - 1
std/cs/internal/Runtime.hx

@@ -798,7 +798,7 @@ import cs.system.Object;
 
 
 #if !erase_generics
-	private static function getGenericAttr(t:cs.system.Type):cs.internal.HxObject.GenericInterface
+	public static function getGenericAttr(t:cs.system.Type):cs.internal.HxObject.GenericInterface
 	{
 		for (attr in t.GetCustomAttributes(true))
 			if (Std.is(attr,cs.internal.HxObject.GenericInterface))

+ 12 - 0
tests/unit/src/unit/issues/Issue4285.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+class Issue4285 extends Test {
+	function test() {
+		var a = [1, 2, 3];
+		t(myIs(a, Array));
+	}
+
+	static function myIs(d:Dynamic, t:Dynamic) {
+		return Std.is(d, t);
+	}
+}