Skip to content

Commit

Permalink
[hl] use hl.NativeArray for Vector (HaxeFoundation#11568)
Browse files Browse the repository at this point in the history
* [hl] use hl.NativeArray for Vector

* add HArray,HArray case to compare function

* Fix Array in NativeArray error, add array get bytes

---------

Co-authored-by: Yuxiao Mao <yuxiao.mao@outlook.com>
  • Loading branch information
Simn and yuxiaomao authored Aug 29, 2024
1 parent 7415e9f commit 3b050f0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 123 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
38 changes: 18 additions & 20 deletions std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ using cpp.NativeArray;
#end

private typedef VectorData<T> =
#if flash10
flash.Vector<T>
#elseif neko
neko.NativeArray<T>
#elseif java
java.NativeArray<T>
#elseif lua
lua.Table<Int, T>
#elseif eval
eval.Vector<T>
#else
Array<T>
#if flash10 flash.Vector<T>
#elseif neko neko.NativeArray<T>
#elseif java java.NativeArray<T>
#elseif lua lua.Table<Int, T>
#elseif eval eval.Vector<T>
#elseif hl hl.NativeArray<T>
#else Array<T>
#end;

/**
Expand Down Expand Up @@ -76,6 +71,8 @@ abstract Vector<T>(VectorData<T>) {
this = untyped __lua_table__({length: length});
#elseif eval
this = new eval.Vector(length);
#elseif hl
this = new hl.NativeArray<T>(length);
#else
this = [];
untyped this.length = length;
Expand All @@ -95,7 +92,6 @@ abstract Vector<T>(VectorData<T>) {
#elseif python
this = python.Syntax.code("([{0}]*{1})", defaultValue, length);
#else

#if flash10
this = new flash.Vector<T>(length, true);
#elseif neko
Expand All @@ -108,12 +104,13 @@ abstract Vector<T>(VectorData<T>) {
this = untyped __lua_table__({length: length});
#elseif eval
this = new eval.Vector(length);
#elseif hl
this = new hl.NativeArray<T>(length);
#else
this = [];
untyped this.length = length;
#end
fill(defaultValue);

#end
}

Expand Down Expand Up @@ -174,7 +171,8 @@ abstract Vector<T>(VectorData<T>) {
Sets all `length` elements of `this` Vector to `value`.
**/
public inline function fill(value:T):Void
for (i in 0...length) this[i] = value;
for (i in 0...length)
this[i] = value;

/**
Copies `length` of elements from `src` Vector, beginning at `srcPos` to
Expand All @@ -183,12 +181,12 @@ abstract Vector<T>(VectorData<T>) {
The results are unspecified if `length` results in out-of-bounds access,
or if `src` or `dest` are null
**/
public static #if (java || neko || cpp || eval) inline #end function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void {
public static #if (java || neko || cpp || eval || hl) inline #end function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void {
#if neko
untyped __dollar__ablit(dest, destPos, src, srcPos, len);
#elseif java
java.lang.System.arraycopy(src, srcPos, dest, destPos, len);
#elseif cpp
#elseif (cpp || hl)
dest.toData().blit(destPos, src.toData(), srcPos, len);
#elseif eval
src.toData().blit(srcPos, dest.toData(), destPos, len);
Expand Down Expand Up @@ -222,7 +220,7 @@ abstract Vector<T>(VectorData<T>) {
/**
Creates a new Array, copy the content from the Vector to it, and returns it.
**/
public #if (flash || cpp || js || java || eval) inline #end function toArray():Array<T> {
public #if (flash || cpp || js || java || eval || hl) inline #end function toArray():Array<T> {
#if cpp
return this.copy();
#elseif python
Expand All @@ -234,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 Expand Up @@ -377,7 +375,7 @@ abstract Vector<T>(VectorData<T>) {
If `f` is null, the result is unspecified.
**/
public inline function sort(f:T->T->Int):Void {
#if (neko || java || eval)
#if (neko || java || eval || hl)
throw "not yet supported";
#elseif lua
haxe.ds.ArraySort.sort(cast this, f);
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
}
99 changes: 0 additions & 99 deletions std/hl/_std/haxe/ds/Vector.hx

This file was deleted.

2 changes: 1 addition & 1 deletion tests/unit/src/unit/UnitBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class UnitBuilder {
case EBinop(OpEq, e1, e2):
mkEq(e1, e2, e.pos);
case EBinop(OpNotEq, e1, e2):
macro t($e1 != $e2);
macro @:pos(e.pos) t($e1 != $e2);
case EBinop(OpGt | OpGte | OpLt | OpLte, _, _):
{
expr: (macro t($e)).expr,
Expand Down
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]);
}
}
4 changes: 2 additions & 2 deletions tests/unit/src/unitstd/haxe/ds/Vector.unit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ vec.get(2) == vNullBool;
// fromArray
var arr = ["1", "2", "3"];
var vec:haxe.ds.Vector<String> = haxe.ds.Vector.fromArrayCopy(arr);
#if (!flash && !neko && !jvm && !lua && !eval && !php)
#if (!flash && !neko && !jvm && !lua && !eval && !php && !hl)
arr != vec.toData();
#end
vec.length == 3;
Expand Down Expand Up @@ -192,7 +192,7 @@ vec2[1] == "value: 13";

// sort

#if !(neko || jvm || eval)
#if !(neko || jvm || eval || hl)
var vec = new haxe.ds.Vector(4);
vec[0] = 99;
vec[1] = 101;
Expand Down

0 comments on commit 3b050f0

Please sign in to comment.