haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[hl] Fix ArrayBytes not clearing properly after pop/shift/splice

Open trethaller opened this issue 2 years ago • 1 comments

Currently, ArrayBytes.pop/shift/splice do not properly zero the underlying array, causing surprises when resizing it like so:

var a = [for(i in 0...10) i];
while(a.length > 0)
	a.pop();
a[9] = 9;
trace(a);  // [0,1,2,3,4,5,6,7,8,9]


var a = [for(i in 0...10) i];
a.splice(0, 10);
a[9] = 9;
trace(a);  // [0,1,2,3,4,5,6,7,8,9]


var a = [for(i in 0...10) i];
while(a.length > 0)
	a.shift();
a[9] = 9;
trace(a);  // [9,9,9,9,9,9,9,9,9,9]

In all cases the expected result is [0,0,0,0,0,0,0,0,0,9], which PR fixes

trethaller avatar Jun 09 '22 12:06 trethaller

The while might be costly on splice, maybe using a bytes.fill instead would be better ? (might require adding it to byteAccess + multiply the size as in "blit")

ncannasse avatar Jul 08 '22 16:07 ncannasse

I've made changes to use fill instead of a while loop

trethaller avatar Jan 25 '23 11:01 trethaller

Thanks !

ncannasse avatar Jan 30 '23 08:01 ncannasse