package com.oracle.truffle.js.runtime.array.dyn;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetArray;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetLength;
import static com.oracle.truffle.js.runtime.builtins.JSAbstractArray.arraySetUsedLength;
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 ZeroBasedDoubleArray extends AbstractDoubleArray {
private static final ZeroBasedDoubleArray ZERO_BASED_DOUBLE_ARRAY = new ZeroBasedDoubleArray(INTEGRITY_LEVEL_NONE, createCache());
public static ZeroBasedDoubleArray makeZeroBasedDoubleArray(DynamicObject object, int length, int usedLength, double[] array, int integrityLevel) {
ZeroBasedDoubleArray arrayType = createZeroBasedDoubleArray().setIntegrityLevel(integrityLevel);
arraySetLength(object, length);
arraySetUsedLength(object, usedLength);
arraySetArray(object, array);
return arrayType;
}
public static ZeroBasedDoubleArray createZeroBasedDoubleArray() {
return ZERO_BASED_DOUBLE_ARRAY;
}
private ZeroBasedDoubleArray(int integrityLevel, DynamicArrayCache cache) {
super(integrityLevel, cache);
}
@Override
public double getInBoundsFastDouble(DynamicObject object, int index) {
return getArray(object)[index];
}
@Override
public void setInBoundsFast(DynamicObject object, int index, double value) {
getArray(object)[index] = value;
if (JSConfig.TraceArrayWrites) {
traceWriteValue("InBoundsFast", index, value);
}
}
@Override
public boolean isSupported(DynamicObject object, long index) {
return isSupportedZeroBased(object, (int) index);
}
@Override
protected int prepareInBoundsFast(DynamicObject object, long index) {
return (int) index;
}
@Override
protected int prepareInBounds(DynamicObject object, int index, ProfileHolder profile) {
prepareInBoundsZeroBased(object, index, profile);
return index;
}
@Override
protected int prepareSupported(DynamicObject object, int index, ProfileHolder profile) {
prepareSupportedZeroBased(object, index, profile);
return index;
}
@Override
protected void setLengthLess(DynamicObject object, long length, ProfileHolder profile) {
setLengthLessZeroBased(object, length, profile);
}
@Override
public Object[] toArray(DynamicObject object) {
return toArrayZeroBased(object);
}
@Override
public ZeroBasedObjectArray toObject(DynamicObject object, long index, Object value) {
double[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
Object[] doubleCopy = ArrayCopy.doubleToObject(array, 0, usedLength);
ZeroBasedObjectArray newArray = ZeroBasedObjectArray.makeZeroBasedObjectArray(object, length, usedLength, doubleCopy, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public ContiguousDoubleArray toContiguous(DynamicObject object, long index, Object value) {
double[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
ContiguousDoubleArray newArray = ContiguousDoubleArray.makeContiguousDoubleArray(object, length, array, 0, 0, usedLength, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public HolesDoubleArray toHoles(DynamicObject object, long index, Object value) {
double[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
HolesDoubleArray newArray = HolesDoubleArray.makeHolesDoubleArray(object, length, array, 0, 0, usedLength, 0, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public long firstElementIndex(DynamicObject object) {
return 0;
}
@Override
public long lastElementIndex(DynamicObject object) {
return getUsedLength(object) - 1;
}
@Override
public ScriptArray removeRangeImpl(DynamicObject object, long start, long end) {
double[] array = getArray(object);
int usedLength = getUsedLength(object);
System.arraycopy(array, (int) end, array, (int) start, Math.max(0, (int) (usedLength - end)));
return this;
}
@Override
public ScriptArray addRangeImpl(DynamicObject object, long offset, int size) {
return addRangeImplZeroBased(object, offset, size);
}
@Override
public boolean hasHoles(DynamicObject object) {
int length = lengthInt(object);
int usedLength = getUsedLength(object);
return usedLength < length;
}
@Override
protected ZeroBasedDoubleArray withIntegrityLevel(int newIntegrityLevel) {
return new ZeroBasedDoubleArray(newIntegrityLevel, cache);
}
@Override
public long nextElementIndex(DynamicObject object, long index) {
return nextElementIndexZeroBased(object, index);
}
}