From bf450c731ab23e4eb554f75d53a1cd69b57cebc4 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Wed, 11 Sep 2024 11:10:46 +0200 Subject: [PATCH 1/2] Revert "[hl] use hl.NativeArray for Vector (#11568)" This reverts commit 3b050f0a108d47c71622a70e330ad38c1ef43d15. --- src/generators/genhl.ml | 2 +- std/haxe/ds/Vector.hx | 38 +++---- std/hl/NativeArray.hx | 13 --- std/hl/_std/haxe/ds/Vector.hx | 99 +++++++++++++++++++ tests/unit/src/unit/UnitBuilder.hx | 2 +- tests/unit/src/unit/issues/Issue11734.hx | 30 ------ tests/unit/src/unitstd/haxe/ds/Vector.unit.hx | 4 +- 7 files changed, 123 insertions(+), 65 deletions(-) create mode 100644 std/hl/_std/haxe/ds/Vector.hx diff --git a/src/generators/genhl.ml b/src/generators/genhl.ml index 6791eb842d4..06b29a4d7d0 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 = if is_array_type at then alloc_tmp ctx HDyn else alloc_tmp ctx at in + let r = 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 4b130c61863..14b60b90ce6 100644 --- a/std/haxe/ds/Vector.hx +++ b/std/haxe/ds/Vector.hx @@ -27,13 +27,18 @@ using cpp.NativeArray; #end private typedef VectorData = - #if flash10 flash.Vector - #elseif neko neko.NativeArray - #elseif java java.NativeArray - #elseif lua lua.Table - #elseif eval eval.Vector - #elseif hl hl.NativeArray - #else Array + #if flash10 + flash.Vector + #elseif neko + neko.NativeArray + #elseif java + java.NativeArray + #elseif lua + lua.Table + #elseif eval + eval.Vector + #else + Array #end; /** @@ -71,8 +76,6 @@ abstract Vector(VectorData) { this = untyped __lua_table__({length: length}); #elseif eval this = new eval.Vector(length); - #elseif hl - this = new hl.NativeArray(length); #else this = []; untyped this.length = length; @@ -92,6 +95,7 @@ abstract Vector(VectorData) { #elseif python this = python.Syntax.code("([{0}]*{1})", defaultValue, length); #else + #if flash10 this = new flash.Vector(length, true); #elseif neko @@ -104,13 +108,12 @@ abstract Vector(VectorData) { this = untyped __lua_table__({length: length}); #elseif eval this = new eval.Vector(length); - #elseif hl - this = new hl.NativeArray(length); #else this = []; untyped this.length = length; #end fill(defaultValue); + #end } @@ -171,8 +174,7 @@ abstract Vector(VectorData) { 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 @@ -181,12 +183,12 @@ abstract Vector(VectorData) { 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 || hl) inline #end function blit(src:Vector, srcPos:Int, dest:Vector, destPos:Int, len:Int):Void { + public static #if (java || neko || cpp || eval) inline #end function blit(src:Vector, srcPos:Int, dest:Vector, 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 || hl) + #elseif cpp dest.toData().blit(destPos, src.toData(), srcPos, len); #elseif eval src.toData().blit(srcPos, dest.toData(), destPos, len); @@ -220,7 +222,7 @@ abstract Vector(VectorData) { /** Creates a new Array, copy the content from the Vector to it, and returns it. **/ - public #if (flash || cpp || js || java || eval || hl) inline #end function toArray():Array { + public #if (flash || cpp || js || java || eval) inline #end function toArray():Array { #if cpp return this.copy(); #elseif python @@ -232,7 +234,7 @@ abstract Vector(VectorData) { #else var a = new Array(); var len = length; - #if (neko || hl) + #if (neko) // prealloc good size if (len > 0) a[len - 1] = get(0); @@ -375,7 +377,7 @@ abstract Vector(VectorData) { If `f` is null, the result is unspecified. **/ public inline function sort(f:T->T->Int):Void { - #if (neko || java || eval || hl) + #if (neko || java || eval) throw "not yet supported"; #elseif lua haxe.ds.ArraySort.sort(cast this, f); diff --git a/std/hl/NativeArray.hx b/std/hl/NativeArray.hx index c5021f287f7..0d484bb5547 100644 --- a/std/hl/NativeArray.hx +++ b/std/hl/NativeArray.hx @@ -99,17 +99,4 @@ 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/std/hl/_std/haxe/ds/Vector.hx b/std/hl/_std/haxe/ds/Vector.hx new file mode 100644 index 00000000000..49c43198e6f --- /dev/null +++ b/std/hl/_std/haxe/ds/Vector.hx @@ -0,0 +1,99 @@ +/* + * Copyright (C)2005-2019 Haxe Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +package haxe.ds; + +private typedef VectorData = Array + +@:coreApi +abstract Vector(VectorData) { + extern overload public inline function new(length:Int) { + this = []; + if (length > 0) + this[length - 1] = @:nullSafety(Off) cast null; + } + + extern overload public inline function new(length:Int, defaultValue:T):Vector { + this = [ + for (i in 0...length) defaultValue + ]; + } + + @:op([]) public inline function get(index:Int):T { + return this[index]; + } + + @:op([]) public inline function set(index:Int, val:T):T { + return this[index] = val; + } + + public var length(get, never):Int; + + inline function get_length():Int { + return this.length; + } + + public inline function fill(value:T):Void + for (i in 0...length) this[i] = value; + + public static inline function blit(src:Vector, srcPos:Int, dest:Vector, destPos:Int, len:Int):Void { + (cast dest : hl.types.ArrayBase.ArrayAccess).blit(destPos, (cast src : hl.types.ArrayBase.ArrayAccess), srcPos, len); + } + + public inline function toArray():Array { + return this.copy(); + } + + public inline function toData():VectorData + return this; + + static public inline function fromData(data:VectorData):Vector + return cast data; + + static public inline function fromArrayCopy(array:Array):Vector { + return cast array.copy(); + } + + public inline function copy():Vector { + return cast this.copy(); + } + + public inline function join(sep:String):String { + return this.join(sep); + } + + public inline function sort(f:T->T->Int):Void { + this.sort(f); + } + + public inline function map(f:T->S):Vector { + var length = length; + var r = new Vector(length); + var i = 0; + var len = length; + for (i in 0...len) { + var v = f(get(i)); + r.set(i, v); + } + return r; + } +} diff --git a/tests/unit/src/unit/UnitBuilder.hx b/tests/unit/src/unit/UnitBuilder.hx index 8a7ac737819..43c6e396c75 100644 --- a/tests/unit/src/unit/UnitBuilder.hx +++ b/tests/unit/src/unit/UnitBuilder.hx @@ -174,7 +174,7 @@ class UnitBuilder { case EBinop(OpEq, e1, e2): mkEq(e1, e2, e.pos); case EBinop(OpNotEq, e1, e2): - macro @:pos(e.pos) t($e1 != $e2); + macro t($e1 != $e2); case EBinop(OpGt | OpGte | OpLt | OpLte, _, _): { expr: (macro t($e)).expr, diff --git a/tests/unit/src/unit/issues/Issue11734.hx b/tests/unit/src/unit/issues/Issue11734.hx index 13911d07ad7..467253c8a90 100644 --- a/tests/unit/src/unit/issues/Issue11734.hx +++ b/tests/unit/src/unit/issues/Issue11734.hx @@ -5,22 +5,6 @@ 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() { @@ -32,18 +16,4 @@ 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]); - } } diff --git a/tests/unit/src/unitstd/haxe/ds/Vector.unit.hx b/tests/unit/src/unitstd/haxe/ds/Vector.unit.hx index 582f3603f8c..33aeff3f9d2 100644 --- a/tests/unit/src/unitstd/haxe/ds/Vector.unit.hx +++ b/tests/unit/src/unitstd/haxe/ds/Vector.unit.hx @@ -27,7 +27,7 @@ vec.get(2) == vNullBool; // fromArray var arr = ["1", "2", "3"]; var vec:haxe.ds.Vector = haxe.ds.Vector.fromArrayCopy(arr); -#if (!flash && !neko && !jvm && !lua && !eval && !php && !hl) +#if (!flash && !neko && !jvm && !lua && !eval && !php) arr != vec.toData(); #end vec.length == 3; @@ -192,7 +192,7 @@ vec2[1] == "value: 13"; // sort -#if !(neko || jvm || eval || hl) +#if !(neko || jvm || eval) var vec = new haxe.ds.Vector(4); vec[0] = 99; vec[1] = 101; From 5f43ad68df31b33f7e63388dfdec0a5af10c7ff6 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Wed, 11 Sep 2024 11:36:17 +0200 Subject: [PATCH 2/2] [tests] test change can remains --- tests/unit/src/unit/UnitBuilder.hx | 2 +- tests/unit/src/unit/issues/Issue11734.hx | 30 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/unit/src/unit/UnitBuilder.hx b/tests/unit/src/unit/UnitBuilder.hx index 43c6e396c75..8a7ac737819 100644 --- a/tests/unit/src/unit/UnitBuilder.hx +++ b/tests/unit/src/unit/UnitBuilder.hx @@ -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, 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]); + } }