Browse Source

[java] fix Rest.of

see #10906
Simon Krajewski 2 years ago
parent
commit
3b1488f6b7
2 changed files with 31 additions and 8 deletions
  1. 11 8
      std/java/_std/haxe/Rest.hx
  2. 20 0
      tests/unit/src/unit/issues/Issue10906.hx

+ 11 - 8
std/java/_std/haxe/Rest.hx

@@ -3,16 +3,17 @@ package haxe;
 import haxe.iterators.RestIterator;
 import haxe.iterators.RestKeyValueIterator;
 import java.NativeArray;
-import java.lang.System;
+import java.StdTypes;
 import java.lang.Object;
+import java.lang.System;
 import java.util.Arrays;
-import java.StdTypes;
 
 private typedef NativeRest<T> = NativeArray<T>;
 
 @:coreApi
 abstract Rest<T>(NativeRest<T>) {
-	public var length(get,never):Int;
+	public var length(get, never):Int;
+
 	inline function get_length():Int
 		return this.length;
 
@@ -24,7 +25,7 @@ abstract Rest<T>(NativeRest<T>) {
 	@:from extern inline static public function of<T>(array:Array<T>):Rest<T> {
 		var result = createNative(array.length);
 		var src:NativeArray<Object> = cast @:privateAccess array.__a;
-		for(i in 0...src.length)
+		for (i in 0...result.length)
 			result[i] = cast src[i];
 		return new Rest(result);
 	}
@@ -32,8 +33,8 @@ abstract Rest<T>(NativeRest<T>) {
 
 	@:noDoc
 	@:generic
-	static function ofNativePrimitive<TBoxed,TRest>(result:NativeRest<TBoxed>, collection:NativeArray<TRest>):Rest<TRest> {
-		for(i in 0...collection.length)
+	static function ofNativePrimitive<TBoxed, TRest>(result:NativeRest<TBoxed>, collection:NativeArray<TRest>):Rest<TRest> {
+		for (i in 0...collection.length)
 			result[i] = cast collection[i];
 		return new Rest(cast result);
 	}
@@ -88,7 +89,7 @@ abstract Rest<T>(NativeRest<T>) {
 		return this[index];
 
 	@:to public function toArray():Array<T> {
-		return [for(i in 0...this.length) this[i]];
+		return [for (i in 0...this.length) this[i]];
 	}
 
 	public inline function iterator():RestIterator<T>
@@ -100,6 +101,7 @@ abstract Rest<T>(NativeRest<T>) {
 	extern inline public function append(item:T):Rest<T> {
 		return _append(createNative(this.length + 1), item);
 	}
+
 	function _append(result:NativeRest<T>, item:T):Rest<T> {
 		System.arraycopy(this, 0, result, 0, this.length);
 		result[this.length] = cast item;
@@ -109,6 +111,7 @@ abstract Rest<T>(NativeRest<T>) {
 	extern inline public function prepend(item:T):Rest<T> {
 		return _prepend(createNative(this.length + 1), item);
 	}
+
 	function _prepend(result:NativeRest<T>, item:T):Rest<T> {
 		System.arraycopy(this, 0, result, 1, this.length);
 		result[0] = cast item;
@@ -118,4 +121,4 @@ abstract Rest<T>(NativeRest<T>) {
 	public function toString():String {
 		return toArray().toString();
 	}
-}
+}

+ 20 - 0
tests/unit/src/unit/issues/Issue10906.hx

@@ -0,0 +1,20 @@
+package unit.issues;
+
+import haxe.Rest;
+import utest.Assert;
+
+class Issue10906 extends Test {
+	#if !jvm
+	function test() {
+		var a:Array<Any> = new Array<Any>();
+		a.push(1);
+		a.push(2);
+		a.push(3);
+		Assert.same([1, 2, 3], a);
+		eq(3, a.length);
+		var r = Rest.of(a);
+		eq(1, r[0]);
+		eq(3, r.length);
+	}
+	#end
+}