Browse Source

[java/cs] Lib.nativeArray works correctly now. Closes #2049

Cauê Waneck 11 years ago
parent
commit
89de9b2624
4 changed files with 34 additions and 7 deletions
  1. 1 1
      gencommon.ml
  2. 4 0
      std/cs/internal/Runtime.hx
  3. 5 6
      std/java/Lib.hx
  4. 24 0
      tests/unit/issues/Issue2049.hx

+ 1 - 1
gencommon.ml

@@ -75,7 +75,7 @@ let rec like_float t =
 let rec like_int t =
 let rec like_int t =
   match follow t with
   match follow t with
     | TAbstract({ a_path = ([], "Int") },[]) -> true
     | TAbstract({ a_path = ([], "Int") },[]) -> true
-    | TAbstract(a, _) -> List.exists (fun (t,_) -> like_int t) a.a_from || List.exists (fun (t,_) -> like_float t) a.a_to
+    | TAbstract(a, _) -> List.exists (fun (t,_) -> like_int t) a.a_from || List.exists (fun (t,_) -> like_int t) a.a_to
     | _ -> false
     | _ -> false
 
 
 
 

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

@@ -752,6 +752,10 @@ import cs.system.Type;
 			return (To)(object) toDouble(obj);
 			return (To)(object) toDouble(obj);
 		else if (typeof(To) == typeof(int))
 		else if (typeof(To) == typeof(int))
 			return (To)(object) toInt(obj);
 			return (To)(object) toInt(obj);
+		else if (typeof(To) == typeof(float))
+			return (To)(object)(float)toDouble(obj);
+		else if (typeof(To) == typeof(long))
+			return (To)(object)(long)toDouble(obj);
 		else
 		else
 			return (To) obj;
 			return (To) obj;
 	')
 	')

+ 5 - 6
std/java/Lib.hx

@@ -35,15 +35,14 @@ package java;
 
 
 		If equalLengthRequired is true, the result might be a copy of an array with the correct size.
 		If equalLengthRequired is true, the result might be a copy of an array with the correct size.
 	**/
 	**/
-	public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
+	@:generic public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
 	{
 	{
-		var native:NativeArray<T> = untyped arr.__a;
-		if (native.length == arr.length)
+		var ret = new NativeArray(arr.length);
+		for (i in 0...arr.length)
 		{
 		{
-			return native;
-		} else {
-			return null;
+			ret[i] = arr[i];
 		}
 		}
+		return ret;
 	}
 	}
 
 
 	/**
 	/**

+ 24 - 0
tests/unit/issues/Issue2049.hx

@@ -0,0 +1,24 @@
+package unit.issues;
+#if cs
+import cs.NativeArray;
+import cs.Lib;
+#elseif java
+import java.NativeArray;
+import java.Lib;
+#end
+
+class Issue2049 extends unit.Test
+{
+#if (java || cs)
+	public function test()
+	{
+		var arr = [ 1., 1., 1., 0.5 ].map( function( n: Float ): Single { return n; });
+		var scaleFactors:NativeArray<Single> = Lib.nativeArray( arr, true );
+		eq(1.,scaleFactors[0]);
+		eq(1.,scaleFactors[1]);
+		eq(1.,scaleFactors[2]);
+		eq(.5,scaleFactors[3]);
+	}
+#end
+
+}