fix: call move when passing same array to copyTo

This commit is contained in:
TopchetoEU 2024-02-21 11:03:19 +02:00
parent c6dc031cfd
commit c4d44547c8
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 11 additions and 6 deletions

View File

@ -82,15 +82,20 @@ public class ArrayValue extends ObjectValue implements Iterable<Object> {
else arr[i + sourceStart] = values[i + destStart];
}
}
public void copyTo(Context ctx, ArrayValue arr, int sourceStart, int destStart, int count) {
public void copyTo(ArrayValue arr, int sourceStart, int destStart, int count) {
if (arr == this) {
move(sourceStart, destStart, count);
return;
}
// Iterate in reverse to reallocate at most once
if (destStart + count > arr.size) arr.size = destStart + count;
for (var i = count - 1; i >= 0; i--) {
if (i + sourceStart < 0 || i + sourceStart >= size) arr.remove(i + destStart);
if (values[i + sourceStart] == UNDEFINED) arr.set(ctx, i + destStart, null);
if (values[i + sourceStart] == UNDEFINED) arr.set(null, i + destStart, null);
else if (values[i + sourceStart] == null) arr.remove(i + destStart);
else arr.set(ctx, i + destStart, values[i + sourceStart]);
else arr.set(null, i + destStart, values[i + sourceStart]);
}
}

View File

@ -94,7 +94,7 @@ public class ArrayLib {
if (arrs.get(i) instanceof ArrayValue) {
var arrEl = arrs.convert(i, ArrayValue.class);
int n = arrEl.size();
arrEl.copyTo(args.ctx, res, 0, j, n);
arrEl.copyTo(res, 0, j, n);
j += n;
}
else {
@ -382,7 +382,7 @@ public class ArrayLib {
var end = normalizeI(arr.size(), args.getInt(1, arr.size()), true);
var res = new ArrayValue(end - start);
arr.copyTo(args.ctx, res, start, 0, end - start);
arr.copyTo(res, start, 0, end - start);
return res;
}
@ -396,7 +396,7 @@ public class ArrayLib {
var size = arr.size() - deleteCount + items.length;
var res = new ArrayValue(deleteCount);
arr.copyTo(args.ctx, res, start, 0, deleteCount);
arr.copyTo(res, start, 0, deleteCount);
arr.move(start + deleteCount, start + items.length, arr.size() - start - deleteCount);
arr.copyFrom(args.ctx, items, 0, start, items.length);
arr.setSize(size);