Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hl] Revert use hl.NativeArray for Vector #11568 #11766

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 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] ->
Expand Down
38 changes: 20 additions & 18 deletions std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ 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>
#elseif hl hl.NativeArray<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>
#else
Array<T>
#end;

/**
Expand Down Expand Up @@ -71,8 +76,6 @@ 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 @@ -92,6 +95,7 @@ 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 @@ -104,13 +108,12 @@ 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 @@ -171,8 +174,7 @@ 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 @@ -181,12 +183,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 || hl) 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) 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 || hl)
#elseif cpp
dest.toData().blit(destPos, src.toData(), srcPos, len);
#elseif eval
src.toData().blit(srcPos, dest.toData(), destPos, len);
Expand Down Expand Up @@ -220,7 +222,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 || hl) inline #end function toArray():Array<T> {
public #if (flash || cpp || js || java || eval) inline #end function toArray():Array<T> {
#if cpp
return this.copy();
#elseif python
Expand All @@ -232,7 +234,7 @@ abstract Vector<T>(VectorData<T>) {
#else
var a = new Array();
var len = length;
#if (neko || hl)
#if (neko)
// prealloc good size
if (len > 0)
a[len - 1] = get(0);
Expand Down Expand Up @@ -375,7 +377,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 || hl)
#if (neko || java || eval)
throw "not yet supported";
#elseif lua
haxe.ds.ArraySort.sort(cast this, f);
Expand Down
13 changes: 0 additions & 13 deletions std/hl/NativeArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,4 @@ 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: 99 additions & 0 deletions std/hl/_std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
@@ -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<T> = Array<T>

@:coreApi
abstract Vector<T>(VectorData<T>) {
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<T> {
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<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, 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<T> {
return this.copy();
}

public inline function toData():VectorData<T>
return this;

static public inline function fromData<T>(data:VectorData<T>):Vector<T>
return cast data;

static public inline function fromArrayCopy<T>(array:Array<T>):Vector<T> {
return cast array.copy();
}

public inline function copy<T>():Vector<T> {
return cast this.copy();
}

public inline function join<T>(sep:String):String {
return this.join(sep);
}

public inline function sort(f:T->T->Int):Void {
this.sort(f);
}

public inline function map<S>(f:T->S):Vector<S> {
var length = length;
var r = new Vector<S>(length);
var i = 0;
var len = length;
for (i in 0...len) {
var v = f(get(i));
r.set(i, v);
}
return r;
}
}
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 && !hl)
#if (!flash && !neko && !jvm && !lua && !eval && !php)
arr != vec.toData();
#end
vec.length == 3;
Expand Down Expand Up @@ -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;
Expand Down
Loading