package com.oracle.truffle.js.runtime.array.dyn;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.array.ScriptArray;
public final class ContiguousIntArray extends AbstractContiguousIntArray {
private static final ContiguousIntArray CONTIGUOUS_INT_ARRAY = new ContiguousIntArray(INTEGRITY_LEVEL_NONE, createCache());
public static ContiguousIntArray makeContiguousIntArray(DynamicObject object, long length, int[] array, long indexOffset, int arrayOffset, int usedLength, int integrityLevel) {
ContiguousIntArray arrayType = createContiguousIntArray().setIntegrityLevel(integrityLevel);
setArrayProperties(object, array, length, usedLength, indexOffset, arrayOffset);
return arrayType;
}
private static ContiguousIntArray createContiguousIntArray() {
return CONTIGUOUS_INT_ARRAY;
}
private ContiguousIntArray(int integrityLevel, DynamicArrayCache cache) {
super(integrityLevel, cache);
}
@Override
protected int prepareInBounds(DynamicObject object, int index, ProfileHolder profile) {
return prepareInBoundsContiguous(object, index, profile);
}
@Override
protected int prepareSupported(DynamicObject object, int index, ProfileHolder profile) {
return prepareSupportedContiguous(object, index, profile);
}
@Override
public boolean isSupported(DynamicObject object, long index) {
return isSupportedContiguous(object, index);
}
@Override
public ContiguousDoubleArray toDouble(DynamicObject object, long index, double value) {
int[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
int arrayOffset = getArrayOffset(object);
long indexOffset = getIndexOffset(object);
double[] doubleCopy = ArrayCopy.intToDouble(array, arrayOffset, usedLength);
ContiguousDoubleArray newArray = ContiguousDoubleArray.makeContiguousDoubleArray(object, length, doubleCopy, indexOffset, arrayOffset, usedLength, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public ContiguousObjectArray toObject(DynamicObject object, long index, Object value) {
int[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
int arrayOffset = getArrayOffset(object);
long indexOffset = getIndexOffset(object);
Object[] doubleCopy = ArrayCopy.intToObject(array, arrayOffset, usedLength);
ContiguousObjectArray newArray = ContiguousObjectArray.makeContiguousObjectArray(object, length, doubleCopy, indexOffset, arrayOffset, usedLength, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public AbstractWritableArray toHoles(DynamicObject object, long index, Object value) {
int[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
int arrayOffset = getArrayOffset(object);
long indexOffset = getIndexOffset(object);
AbstractWritableArray newArray;
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, containsHoleValue(object))) {
newArray = toObjectHoles(object);
} else {
newArray = HolesIntArray.makeHolesIntArray(object, length, array, indexOffset, arrayOffset, usedLength, 0, integrityLevel);
}
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
protected HolesObjectArray toObjectHoles(DynamicObject object) {
int length = lengthInt(object);
int usedLength = getUsedLength(object);
int arrayOffset = getArrayOffset(object);
long indexOffset = getIndexOffset(object);
return HolesObjectArray.makeHolesObjectArray(object, length, convertToObject(object), indexOffset, arrayOffset, usedLength, 0, integrityLevel);
}
@Override
public ZeroBasedIntArray toNonContiguous(DynamicObject object, int index, Object value, ProfileHolder profile) {
setSupported(object, index, (int) value, profile);
int[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
ZeroBasedIntArray newArray = ZeroBasedIntArray.makeZeroBasedIntArray(object, length, usedLength, array, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public ScriptArray removeRangeImpl(DynamicObject object, long start, long end) {
return removeRangeContiguous(object, start, end);
}
@Override
protected ContiguousIntArray withIntegrityLevel(int newIntegrityLevel) {
return new ContiguousIntArray(newIntegrityLevel, cache);
}
}