Skip to content

Commit 33cb0e5

Browse files
committed
Clear unused elements during transition from zero-based to contiguous array.
1 parent eeca779 commit 33cb0e5

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/AbstractWritableArray.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,20 @@ public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
846846
return removeRangeImpl(object, 0, from);
847847
}
848848

849+
protected static boolean unusedElementsAreHoles(Object[] array, int usedStart, int usedLength) {
850+
for (int i = 0; i < usedStart; i++) {
851+
if (array[i] != null) {
852+
return false;
853+
}
854+
}
855+
for (int i = usedStart + usedLength; i < array.length; i++) {
856+
if (array[i] != null) {
857+
return false;
858+
}
859+
}
860+
return true;
861+
}
862+
849863
public static class SetSupportedProfileAccess implements InlinedProfileBag {
850864

851865
private final InlinedConditionProfile ensureCapacityGrow;

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ContiguousJSObjectArray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static ContiguousJSObjectArray makeContiguousJSObjectArray(JSDynamicObjec
5353
int integrityLevel) {
5454
ContiguousJSObjectArray arrayType = createContiguousJSObjectArray().setIntegrityLevel(integrityLevel);
5555
setArrayProperties(object, array, length, usedLength, indexOffset, arrayOffset);
56+
assert unusedElementsAreHoles(array, arrayOffset, usedLength);
5657
return arrayType;
5758
}
5859

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ContiguousObjectArray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class ContiguousObjectArray extends AbstractContiguousObjectArray {
5252
public static ContiguousObjectArray makeContiguousObjectArray(JSDynamicObject object, long length, Object[] array, long indexOffset, int arrayOffset, int usedLength, int integrityLevel) {
5353
ContiguousObjectArray arrayType = createContiguousObjectArray().setIntegrityLevel(integrityLevel);
5454
setArrayProperties(object, array, length, usedLength, indexOffset, arrayOffset);
55+
assert unusedElementsAreHoles(array, arrayOffset, usedLength);
5556
return arrayType;
5657
}
5758

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ZeroBasedJSObjectArray.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end)
178178
public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
179179
int usedLength = getUsedLength(object);
180180
if (from < usedLength) {
181-
return ContiguousJSObjectArray.makeContiguousJSObjectArray(object, lengthInt(object) - from, getArray(object), -from, (int) from, (int) (usedLength - from), integrityLevel);
181+
JSDynamicObject[] array = getArray(object);
182+
Arrays.fill(array, 0, (int) from, null);
183+
return ContiguousJSObjectArray.makeContiguousJSObjectArray(object, lengthInt(object) - from, array, -from, (int) from, (int) (usedLength - from), integrityLevel);
182184
} else {
183185
return removeRangeImpl(object, 0, from);
184186
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/array/dyn/ZeroBasedObjectArray.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end)
167167
public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
168168
int usedLength = getUsedLength(object);
169169
if (from < usedLength) {
170-
return ContiguousObjectArray.makeContiguousObjectArray(object, lengthInt(object) - from, getArray(object), -from, (int) from, (int) (usedLength - from), integrityLevel);
170+
Object[] array = getArray(object);
171+
Arrays.fill(array, 0, (int) from, null);
172+
return ContiguousObjectArray.makeContiguousObjectArray(object, lengthInt(object) - from, array, -from, (int) from, (int) (usedLength - from), integrityLevel);
171173
} else {
172174
return removeRangeImpl(object, 0, from);
173175
}

0 commit comments

Comments
 (0)