package com.oracle.truffle.js.runtime.array.dyn;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arrayGetHoleCount;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetHoleCount;
import java.util.List;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.array.ScriptArray;
import com.oracle.truffle.js.runtime.objects.Undefined;
public final class HolesObjectArray extends AbstractContiguousObjectArray {
private static final HolesObjectArray HOLES_OBJECT_ARRAY = new HolesObjectArray(INTEGRITY_LEVEL_NONE, createCache());
private HolesObjectArray(int integrityLevel, DynamicArrayCache cache) {
super(integrityLevel, cache);
}
public static HolesObjectArray makeHolesObjectArray(DynamicObject object, int length, Object[] array, long indexOffset, int arrayOffset, int usedLength, int holeCount, int integrityLevel) {
HolesObjectArray arrayType = createHolesObjectArray().setIntegrityLevel(integrityLevel);
setArrayProperties(object, array, length, usedLength, indexOffset, arrayOffset);
arraySetHoleCount(object, holeCount);
assert holeCount == arrayType.countHoles(object) : String.format("holeCount, %d, differs from the actual count, %d", holeCount, arrayType.countHoles(object));
return arrayType;
}
public static HolesObjectArray createHolesObjectArray() {
return HOLES_OBJECT_ARRAY;
}
@Override
AbstractWritableArray sameTypeHolesArray(DynamicObject object, int length, Object array, long indexOffset, int arrayOffset, int usedLength, int holeCount) {
setArrayProperties(object, array, length, usedLength, indexOffset, arrayOffset);
arraySetHoleCount(object, holeCount);
return this;
}
@Override
public void setInBoundsFast(DynamicObject object, int index, Object value) {
throw Errors.shouldNotReachHere("should not call this method, use setInBounds(Non)Hole");
}
public boolean isHoleFast(DynamicObject object, int index) {
int internalIndex = (int) (index - getIndexOffset(object));
return isHolePrepared(object, internalIndex);
}
public void setInBoundsFastHole(DynamicObject object, int index, Object value) {
int internalIndex = (int) (index - getIndexOffset(object));
assert isHolePrepared(object, internalIndex);
incrementHolesCount(object, -1);
setInBoundsFastIntl(object, index, internalIndex, value);
}
public void setInBoundsFastNonHole(DynamicObject object, int index, Object value) {
int internalIndex = (int) (index - getIndexOffset(object));
assert !isHolePrepared(object, internalIndex);
setInBoundsFastIntl(object, index, internalIndex, checkNonNull(value));
}
private void setInBoundsFastIntl(DynamicObject object, int index, int internalIndex, Object value) {
getArray(object)[internalIndex] = value;
if (JSConfig.TraceArrayWrites) {
traceWriteValue("InBoundsFast", index, value);
}
}
@Override
public boolean containsHoles(DynamicObject object, long index) {
return arrayGetHoleCount(object) > 0 || !isInBoundsFast(object, index);
}
@Override
public AbstractObjectArray toNonHoles(DynamicObject object, long index, Object value) {
assert !containsHoles(object, index);
Object[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
int arrayOffset = getArrayOffset(object);
long indexOffset = getIndexOffset(object);
AbstractObjectArray newArray;
setInBoundsFastNonHole(object, (int) index, value);
if (isInBoundsFast(object, 0)) {
newArray = ZeroBasedObjectArray.makeZeroBasedObjectArray(object, length, usedLength, array, integrityLevel);
} else {
newArray = ContiguousObjectArray.makeContiguousObjectArray(object, length, array, indexOffset, arrayOffset, usedLength, integrityLevel);
}
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
protected void incrementHolesCount(DynamicObject object, int offset) {
arraySetHoleCount(object, arrayGetHoleCount(object) + offset);
}
@Override
public int prepareInBounds(DynamicObject object, int index, ProfileHolder profile) {
return prepareInBoundsHoles(object, index, profile);
}
@Override
public boolean isSupported(DynamicObject object, long index) {
return isSupportedHoles(object, index);
}
@Override
public int prepareSupported(DynamicObject object, int index, ProfileHolder profile) {
return prepareSupportedHoles(object, index, profile);
}
@Override
public Object getInBoundsFast(DynamicObject object, int index) {
Object value = getInBoundsFastObject(object, index);
if (HolesObjectArray.isHoleValue(value)) {
return Undefined.instance;
}
return value;
}
@Override
public HolesObjectArray toHoles(DynamicObject object, long index, Object value) {
return this;
}
public static boolean isHoleValue(Object value) {
return value == null;
}
@Override
public long nextElementIndex(DynamicObject object, long index0) {
return nextElementIndexHoles(object, index0);
}
@Override
public long previousElementIndex(DynamicObject object, long index0) {
return previousElementIndexHoles(object, index0);
}
@Override
public boolean hasElement(DynamicObject object, long index) {
return super.hasElement(object, index) && !isHolePrepared(object, prepareInBoundsFast(object, (int) index));
}
@Override
public ScriptArray deleteElementImpl(DynamicObject object, long index, boolean strict) {
return deleteElementHoles(object, index);
}
@Override
public boolean isHolesType() {
return true;
}
@Override
public ScriptArray removeRangeImpl(DynamicObject object, long start, long end) {
return removeRangeHoles(object, start, end);
}
@Override
protected Object castNonNull(Object value) {
return value;
}
@Override
protected HolesObjectArray withIntegrityLevel(int newIntegrityLevel) {
return new HolesObjectArray(newIntegrityLevel, cache);
}
@Override
public List<Object> ownPropertyKeys(DynamicObject object) {
return ownPropertyKeysHoles(object);
}
}