Skip to content

Commit

Permalink
Fix Array in NativeArray error, add array get bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxiaomao committed Aug 5, 2024
1 parent 6bd5bd5 commit 73931ce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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] ->
Expand Down
2 changes: 1 addition & 1 deletion std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions std/hl/NativeArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 30 additions & 0 deletions tests/unit/src/unit/issues/Issue11734.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]);
}
}

0 comments on commit 73931ce

Please sign in to comment.