瀏覽代碼

[java] Create speciailized Lib.array versions for basic types. Closes #2927

Cauê Waneck 11 年之前
父節點
當前提交
023b96535e
共有 2 個文件被更改,包括 70 次插入1 次删除
  1. 51 1
      std/java/Lib.hx
  2. 19 0
      tests/unit/issues/Issue2927.hx

+ 51 - 1
std/java/Lib.hx

@@ -84,11 +84,61 @@ package java;
 		It won't copy the contents of the native array, so unless any operation triggers an array resize,
 		It won't copy the contents of the native array, so unless any operation triggers an array resize,
 		all changes made to the Haxe array will affect the native array argument.
 		all changes made to the Haxe array will affect the native array argument.
 	**/
 	**/
-	public static function array<T>(native:java.NativeArray<T>):Array<T>
+	@:generic public static function array<T>(native:java.NativeArray<T>):Array<T>
 	{
 	{
 		return untyped Array.ofNative(native);
 		return untyped Array.ofNative(native);
 	}
 	}
 
 
+	@:extern inline private static function doArray<T>(native:java.NativeArray<T>):Array<T>
+	{
+		var ret:NativeArray<Null<T>> = new NativeArray(native.length);
+		for (i in 0...native.length)
+		{
+			ret[i] = native[i];
+		}
+		return untyped Array.ofNative(ret);
+	}
+
+	public static function array_Int(native:java.NativeArray<Int>):Array<Int>
+	{
+		return doArray(native);
+	}
+
+	public static function array_Float(native:java.NativeArray<Float>):Array<Float>
+	{
+		return doArray(native);
+	}
+
+	public static function array_Bool(native:java.NativeArray<Bool>):Array<Bool>
+	{
+		return doArray(native);
+	}
+
+	public static function array_java_Int8(native:java.NativeArray<java.StdTypes.Int8>):Array<java.StdTypes.Int8>
+	{
+		return doArray(native);
+	}
+
+	public static function array_java_Int16(native:java.NativeArray<java.StdTypes.Int16>):Array<java.StdTypes.Int16>
+	{
+		return doArray(native);
+	}
+
+	public static function array_java_Char16(native:java.NativeArray<java.StdTypes.Char16>):Array<java.StdTypes.Char16>
+	{
+		return doArray(native);
+	}
+
+	public static function array_Single(native:java.NativeArray<Single>):Array<Single>
+	{
+		return doArray(native);
+	}
+
+	public static function array_haxe_Int64(native:java.NativeArray<haxe.Int64>):Array<haxe.Int64>
+	{
+		return doArray(native);
+	}
+
 	/**
 	/**
 		Allocates a new Haxe Array with a predetermined size
 		Allocates a new Haxe Array with a predetermined size
 	**/
 	**/

+ 19 - 0
tests/unit/issues/Issue2927.hx

@@ -0,0 +1,19 @@
+package unit.issues;
+#if java
+import java.Lib;
+import java.NativeArray;
+#elseif cs
+import cs.Lib;
+import cs.NativeArray;
+#end
+
+class Issue2927 extends Test {
+#if (java || cs)
+	public function test()
+	{
+		var arr = Lib.array(new NativeArray<Int>(1));
+		eq(arr.length,1);
+	}
+#end
+}
+