Skip to content

Commit 83231c4

Browse files
committed
Clear unused elements during transition from zero-based to contiguous array.
1 parent 90885dd commit 83231c4

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
@@ -838,6 +838,20 @@ public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
838838
return removeRangeImpl(object, 0, from);
839839
}
840840

841+
protected static boolean unusedElementsAreHoles(Object[] array, int usedStart, int usedLength) {
842+
for (int i = 0; i < usedStart; i++) {
843+
if (array[i] != null) {
844+
return false;
845+
}
846+
}
847+
for (int i = usedStart + usedLength; i < array.length; i++) {
848+
if (array[i] != null) {
849+
return false;
850+
}
851+
}
852+
return true;
853+
}
854+
841855
protected interface SetSupportedProfileAccess extends ProfileAccess {
842856
default boolean ensureCapacityGrow(ProfileHolder profile, boolean condition) {
843857
return profile.profile(this, 0, condition);

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
@@ -52,6 +52,7 @@ public static ContiguousJSObjectArray makeContiguousJSObjectArray(JSDynamicObjec
5252
int integrityLevel) {
5353
ContiguousJSObjectArray arrayType = createContiguousJSObjectArray().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/ContiguousObjectArray.java

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

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
@@ -177,7 +177,9 @@ public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end)
177177
public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
178178
int usedLength = getUsedLength(object);
179179
if (from < usedLength) {
180-
return ContiguousJSObjectArray.makeContiguousJSObjectArray(object, lengthInt(object) - from, getArray(object), -from, (int) from, (int) (usedLength - from), integrityLevel);
180+
JSDynamicObject[] array = getArray(object);
181+
Arrays.fill(array, 0, (int) from, null);
182+
return ContiguousJSObjectArray.makeContiguousJSObjectArray(object, lengthInt(object) - from, array, -from, (int) from, (int) (usedLength - from), integrityLevel);
181183
} else {
182184
return removeRangeImpl(object, 0, from);
183185
}

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
@@ -166,7 +166,9 @@ public ScriptArray removeRangeImpl(JSDynamicObject object, long start, long end)
166166
public ScriptArray shiftRangeImpl(JSDynamicObject object, long from) {
167167
int usedLength = getUsedLength(object);
168168
if (from < usedLength) {
169-
return ContiguousObjectArray.makeContiguousObjectArray(object, lengthInt(object) - from, getArray(object), -from, (int) from, (int) (usedLength - from), integrityLevel);
169+
Object[] array = getArray(object);
170+
Arrays.fill(array, 0, (int) from, null);
171+
return ContiguousObjectArray.makeContiguousObjectArray(object, lengthInt(object) - from, array, -from, (int) from, (int) (usedLength - from), integrityLevel);
170172
} else {
171173
return removeRangeImpl(object, 0, from);
172174
}

0 commit comments

Comments
 (0)