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;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
public final class ZeroBasedJSObjectArray extends AbstractJSObjectArray {
private static final ZeroBasedJSObjectArray ZERO_BASED_JSOBJECT_ARRAY = new ZeroBasedJSObjectArray(INTEGRITY_LEVEL_NONE, createCache());
public static <T> ZeroBasedJSObjectArray makeZeroBasedJSObjectArray(DynamicObject object, int length, int usedLength, T[] array, int integrityLevel) {
ZeroBasedJSObjectArray arrayType = createZeroBasedJSObjectArray().setIntegrityLevel(integrityLevel);
arraySetLength(object, length);
arraySetUsedLength(object, usedLength);
arraySetArray(object, array);
return arrayType;
}
public static ZeroBasedJSObjectArray createZeroBasedJSObjectArray() {
return ZERO_BASED_JSOBJECT_ARRAY;
}
private ZeroBasedJSObjectArray(int integrityLevel, DynamicArrayCache cache) {
super(integrityLevel, cache);
}
@Override
public boolean isSupported(DynamicObject object, long index) {
return isSupportedZeroBased(object, (int) index);
}
@Override
public DynamicObject getInBoundsFastJSObject(DynamicObject object, int index) {
return castNonNull(getArray(object)[index]);
}
@Override
public void setInBoundsFast(DynamicObject object, int index, JSDynamicObject value) {
getArray(object)[index] = checkNonNull(value);
if (JSConfig.TraceArrayWrites) {
traceWriteValue("InBoundsFast", index, value);
}
}
@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 ContiguousJSObjectArray toContiguous(DynamicObject object, long index, Object value) {
JSDynamicObject[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
ContiguousJSObjectArray newArray = ContiguousJSObjectArray.makeContiguousJSObjectArray(object, length, array, 0, 0, usedLength, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public HolesJSObjectArray toHoles(DynamicObject object, long index, Object value) {
JSDynamicObject[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
HolesJSObjectArray newArray = HolesJSObjectArray.makeHolesJSObjectArray(object, length, array, 0, 0, usedLength, 0, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public ZeroBasedObjectArray toObject(DynamicObject object, long index, Object value) {
JSDynamicObject[] array = getArray(object);
int length = lengthInt(object);
int usedLength = getUsedLength(object);
Object[] doubleCopy = ArrayCopy.jsobjectToObject(array, 0, usedLength);
ZeroBasedObjectArray newArray = ZeroBasedObjectArray.makeZeroBasedObjectArray(object, length, usedLength, doubleCopy, integrityLevel);
if (JSConfig.TraceArrayTransitions) {
traceArrayTransition(this, newArray, index, value);
}
return newArray;
}
@Override
public Object[] toArray(DynamicObject object) {
return toArrayZeroBased(object);
}
@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) {
JSDynamicObject[] 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 ZeroBasedJSObjectArray withIntegrityLevel(int newIntegrityLevel) {
return new ZeroBasedJSObjectArray(newIntegrityLevel, cache);
}
@Override
public long nextElementIndex(DynamicObject object, long index) {
return nextElementIndexZeroBased(object, index);
}
}