2
0
Эх сурвалжийг харах

Fix Array in NativeArray error, add array get bytes

Yuxiao Mao 1 жил өмнө
parent
commit
73931ce3d3

+ 1 - 1
src/generators/genhl.ml

@@ -2034,7 +2034,7 @@ and eval_expr ctx e =
 			hold ctx arr;
 			let pos = eval_to ctx pos HI32 in
 			free ctx arr;
-			let r = alloc_tmp ctx at in
+			let r = if is_array_type at then alloc_tmp ctx HDyn else alloc_tmp ctx at in
 			op ctx (OGetArray (r, arr, pos));
 			cast_to ctx r (to_type ctx e.etype) e.epos
 		| "$aset", [a; pos; value] ->

+ 1 - 1
std/haxe/ds/Vector.hx

@@ -232,7 +232,7 @@ abstract Vector<T>(VectorData<T>) {
 		#else
 		var a = new Array();
 		var len = length;
-		#if (neko)
+		#if (neko || hl)
 		// prealloc good size
 		if (len > 0)
 			a[len - 1] = get(0);

+ 13 - 0
std/hl/NativeArray.hx

@@ -99,4 +99,17 @@ package hl;
 	public inline function blit(pos:Int, src:NativeArray<T>, srcPos:Int, srcLen:Int):Void {
 		real_blit(cast this, pos, cast src, srcPos, srcLen);
 	}
+
+	#if (hl_ver >= version("1.15.0"))
+	@:hlNative("std", "array_bytes") static function get_bytes(a:NativeArray<Any>):Bytes {
+		return null;
+	}
+
+	/**
+		Get the bytes reference from an native array (no copy occurs)
+	**/
+	public inline function getBytes():Bytes {
+		return get_bytes(cast this);
+	}
+	#end
 }

+ 30 - 0
tests/unit/src/unit/issues/Issue11734.hx

@@ -5,6 +5,22 @@ import unit.Test;
 import hl.NativeArray;
 #end
 
+private class Group<T> {
+	public var grid : haxe.ds.Vector<Array<T>>;
+	public function new(size:Int) {
+		grid = new haxe.ds.Vector(size);
+		for (i in 0...size)
+			grid[i] = [];
+	}
+}
+
+private class Foo {
+	public var x : Int;
+	public function new(x:Int) {
+		this.x = x;
+	}
+}
+
 class Issue11734 extends Test {
 	#if hl
 	function test() {
@@ -16,4 +32,18 @@ class Issue11734 extends Test {
 		feq(1.0, a[0]);
 	}
 	#end
+
+	function testArrayInVector() {
+		var g = new Group<Foo>(5);
+		for (i in 0...5)
+			g.grid[i].push(new Foo(10+i));
+		eq(10, g.grid[0][0].x);
+		eq(14, g.grid[4][0].x);
+
+		var g = new Group<Float>(5);
+		for (i in 0...5)
+			g.grid[i].push(10.0+i);
+		feq(10.0, g.grid[0][0]);
+		feq(14.0, g.grid[4][0]);
+	}
 }