Fixing bug 233274: for/in loop goes through array elements in wrong order

Fix: populate ids array in NativeArray with dense indexes first

Since array literals in Rhino creates instances of NativeArray with the
internal dense array containing literal elements, the patch changes
NativeArray.getIds to return ids array with dense indexes coming first and
indexes for elements added later after that.
This commit is contained in:
igor%mir2.org 2004-02-06 20:16:36 +00:00
Родитель c12df996a2
Коммит 83ea5840cf
1 изменённых файлов: 8 добавлений и 8 удалений

Просмотреть файл

@ -284,26 +284,26 @@ public class NativeArray extends IdScriptable {
N = (int)currentLength; N = (int)currentLength;
} }
if (N == 0) { return superIds; } if (N == 0) { return superIds; }
int shift = superIds.length; int superLength = superIds.length;
Object[] ids = new Object[shift + N]; Object[] ids = new Object[N + superLength];
// Make a copy of dense to be immune to removing // Make a copy of dense to be immune to removing
// of array elems from other thread when calculating presentCount // of array elems from other thread when calculating presentCount
System.arraycopy(dense, 0, ids, shift, N); System.arraycopy(dense, 0, ids, 0, N);
int presentCount = 0; int presentCount = 0;
for (int i = 0; i != N; ++i) { for (int i = 0; i != N; ++i) {
// Replace existing elements by their indexes // Replace existing elements by their indexes
if (ids[shift + i] != NOT_FOUND) { if (ids[i] != NOT_FOUND) {
ids[shift + presentCount] = new Integer(i); ids[presentCount] = new Integer(i);
++presentCount; ++presentCount;
} }
} }
if (presentCount != N) { if (presentCount != N) {
// dense contains deleted elems, need to shrink the result // dense contains deleted elems, need to shrink the result
Object[] tmp = new Object[shift + presentCount]; Object[] tmp = new Object[presentCount + superLength];
System.arraycopy(ids, shift, tmp, shift, presentCount); System.arraycopy(ids, 0, tmp, 0, presentCount);
ids = tmp; ids = tmp;
} }
System.arraycopy(superIds, 0, ids, 0, shift); System.arraycopy(superIds, 0, ids, presentCount, superLength);
return ids; return ids;
} }