From 73931ce3d3e105cdc2477d275a0b7f582fbe09e4 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Mon, 5 Aug 2024 14:35:42 +0200 Subject: [PATCH] Fix Array in NativeArray error, add array get bytes --- src/generators/genhl.ml | 2 +- std/haxe/ds/Vector.hx | 2 +- std/hl/NativeArray.hx | 13 ++++++++++ tests/unit/src/unit/issues/Issue11734.hx | 30 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/generators/genhl.ml b/src/generators/genhl.ml index 06b29a4d7d0..6791eb842d4 100644 --- a/src/generators/genhl.ml +++ b/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] -> diff --git a/std/haxe/ds/Vector.hx b/std/haxe/ds/Vector.hx index f361b715556..4b130c61863 100644 --- a/std/haxe/ds/Vector.hx +++ b/std/haxe/ds/Vector.hx @@ -232,7 +232,7 @@ abstract Vector(VectorData) { #else var a = new Array(); var len = length; - #if (neko) + #if (neko || hl) // prealloc good size if (len > 0) a[len - 1] = get(0); diff --git a/std/hl/NativeArray.hx b/std/hl/NativeArray.hx index 0d484bb5547..c5021f287f7 100644 --- a/std/hl/NativeArray.hx +++ b/std/hl/NativeArray.hx @@ -99,4 +99,17 @@ package hl; public inline function blit(pos:Int, src:NativeArray, 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):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 } diff --git a/tests/unit/src/unit/issues/Issue11734.hx b/tests/unit/src/unit/issues/Issue11734.hx index 467253c8a90..13911d07ad7 100644 --- a/tests/unit/src/unit/issues/Issue11734.hx +++ b/tests/unit/src/unit/issues/Issue11734.hx @@ -5,6 +5,22 @@ import unit.Test; import hl.NativeArray; #end +private class Group { + public var grid : haxe.ds.Vector>; + 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(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(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]); + } }