Skip to content

Commit

Permalink
Add: modCount to iterator/sublist
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml authored and gbrail committed May 26, 2021
1 parent 6d823b8 commit 55d8d8c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
Expand Down Expand Up @@ -517,6 +518,7 @@ public void put(String id, Scriptable start, Object value) {
long index = toArrayIndex(id);
if (index >= length) {
length = index + 1;
modCount++;
denseOnly = false;
}
}
Expand Down Expand Up @@ -548,13 +550,17 @@ public void put(int index, Scriptable start, Object value) {
return;
} else if (index < dense.length) {
dense[index] = value;
if (this.length <= index) this.length = (long) index + 1;
if (this.length <= index) {
this.length = (long) index + 1;
this.modCount++;
}
return;
} else if (denseOnly
&& index < dense.length * GROW_FACTOR
&& ensureCapacity(index + 1)) {
dense[index] = value;
this.length = (long) index + 1;
this.modCount++;
return;
} else {
denseOnly = false;
Expand All @@ -566,6 +572,7 @@ && ensureCapacity(index + 1)) {
if (this.length <= index) {
// avoid overflowing index!
this.length = (long) index + 1;
this.modCount++;
}
}
}
Expand Down Expand Up @@ -688,6 +695,7 @@ protected void defineOwnProperty(
long index = toArrayIndex(id);
if (index >= length) {
length = index + 1;
modCount++;
}
super.defineOwnProperty(cx, id, desc, checkValid);
}
Expand Down Expand Up @@ -863,11 +871,13 @@ private void setLength(Object val) {
// downcast okay because denseOnly
Arrays.fill(dense, (int) longVal, dense.length, NOT_FOUND);
length = longVal;
modCount++;
return;
} else if (longVal < MAX_PRE_GROW_SIZE
&& longVal < (length * GROW_FACTOR)
&& ensureCapacity((int) longVal)) {
length = longVal;
modCount++;
return;
} else {
denseOnly = false;
Expand Down Expand Up @@ -898,6 +908,7 @@ && ensureCapacity((int) longVal)) {
}
}
length = longVal;
modCount++;
}

/* Support for generic Array-ish objects. Most of the Array
Expand Down Expand Up @@ -1249,6 +1260,7 @@ private static Object js_push(Context cx, Scriptable scope, Scriptable thisObj,
if (na.denseOnly && na.ensureCapacity((int) na.length + args.length)) {
for (int i = 0; i < args.length; i++) {
na.dense[(int) na.length++] = args[i];
na.modCount++;
}
return ScriptRuntime.wrapNumber(na.length);
}
Expand Down Expand Up @@ -1280,6 +1292,7 @@ private static Object js_pop(Context cx, Scriptable scope, Scriptable thisObj, O
NativeArray na = (NativeArray) o;
if (na.denseOnly && na.length > 0) {
na.length--;
na.modCount++;
result = na.dense[(int) na.length];
na.dense[(int) na.length] = NOT_FOUND;
return result;
Expand Down Expand Up @@ -1313,6 +1326,7 @@ private static Object js_shift(
NativeArray na = (NativeArray) o;
if (na.denseOnly && na.length > 0) {
na.length--;
na.modCount++;
Object result = na.dense[0];
System.arraycopy(na.dense, 1, na.dense, 0, (int) na.length);
na.dense[(int) na.length] = NOT_FOUND;
Expand Down Expand Up @@ -1360,6 +1374,7 @@ private static Object js_unshift(
na.dense[i] = args[i];
}
na.length += args.length;
na.modCount++;
return ScriptRuntime.wrapNumber(na.length);
}
}
Expand Down Expand Up @@ -1494,6 +1509,7 @@ private static Object js_splice(
Arrays.fill(na.dense, (int) (length + delta), (int) length, NOT_FOUND);
}
na.length = length + delta;
na.modCount++;
return result;
}

Expand Down Expand Up @@ -2213,6 +2229,7 @@ public ListIterator listIterator(final int start) {
return new ListIterator() {

int cursor = start;
int modCount = NativeArray.this.modCount;

@Override
public boolean hasNext() {
Expand All @@ -2221,6 +2238,7 @@ public boolean hasNext() {

@Override
public Object next() {
checkModCount(modCount);
if (cursor == len) {
throw new NoSuchElementException();
}
Expand All @@ -2234,6 +2252,7 @@ public boolean hasPrevious() {

@Override
public Object previous() {
checkModCount(modCount);
if (cursor == 0) {
throw new NoSuchElementException();
}
Expand Down Expand Up @@ -2324,20 +2343,30 @@ public List subList(int fromIndex, int toIndex) {
if (fromIndex > toIndex)
throw new IllegalArgumentException(
"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");

return new AbstractList() {
private int modCount = NativeArray.this.modCount;

@Override
public Object get(int index) {
checkModCount(modCount);
return NativeArray.this.get(index + fromIndex);
}

@Override
public int size() {
checkModCount(modCount);
return toIndex - fromIndex;
}
};
}

private void checkModCount(int modCount) {
if (this.modCount != modCount) {
throw new ConcurrentModificationException();
}
}

@Override
protected int findPrototypeId(Symbol k) {
if (SymbolKey.ITERATOR.equals(k)) {
Expand Down Expand Up @@ -2579,6 +2608,9 @@ protected int findPrototypeId(String s) {
/** Attributes of the array's length property */
private int lengthAttr = DONTENUM | PERMANENT;

/** modCount required for subList/iterators */
private transient int modCount;

/**
* Fast storage for dense arrays. Sparse arrays will use the superclass's hashtable storage
* scheme.
Expand Down
57 changes: 57 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Bug466207Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -123,6 +124,62 @@ public void testSublist() {
assertTrue(list.subList(5, 5).isEmpty());
}

public void testSublistMod() {

List<Object> sl = reference.subList(2, 4);
reference.remove(0);
try {
sl.get(0);
fail("Exception expected");
} catch (ConcurrentModificationException cme) {

}
sl = list.subList(2, 4);
listPop();
assertEquals(4, list.size());
try {
sl.get(0);
fail("Exception expected");
} catch (ConcurrentModificationException cme) {

}
}

public void testIteratorMod() {

ListIterator<Object> iter = reference.listIterator();
reference.remove(0);
iter.previousIndex();
iter.nextIndex();
iter.hasNext();
try {
iter.next();
fail("Exception expected");
} catch (ConcurrentModificationException cme) {

}
iter = list.listIterator();
listPop();
assertEquals(4, list.size());
iter.previousIndex();
iter.nextIndex();
iter.hasNext();
try {
iter.next();
fail("Exception expected");
} catch (ConcurrentModificationException cme) {

}
}

private void listPop() {
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
scope.put("list", scope, list);
context.evaluateString(scope, "list.pop()", "testsrc", 1, null);
Context.exit();
}

public void testBigList() {
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
Expand Down

0 comments on commit 55d8d8c

Please sign in to comment.