package com.oracle.truffle.js.runtime.builtins;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.runtime.Boundaries;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.array.ScriptArray;
import com.oracle.truffle.js.runtime.array.SparseArray;
import com.oracle.truffle.js.runtime.objects.JSAttributes;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.JSObject;
import com.oracle.truffle.js.runtime.objects.JSObjectUtil;
import com.oracle.truffle.js.runtime.objects.JSShape;
import com.oracle.truffle.js.runtime.objects.PropertyDescriptor;
import com.oracle.truffle.js.runtime.util.DefinePropertyUtil;
public final class JSSlowArray extends JSAbstractArray {
public static final String CLASS_NAME = "Array";
public static final JSSlowArray INSTANCE = new JSSlowArray();
private JSSlowArray() {
}
public static boolean isJSSlowArray(Object obj) {
return JSDynamicObject.isJSDynamicObject(obj) && isJSSlowArray((DynamicObject) obj);
}
public static boolean isJSSlowArray(DynamicObject obj) {
return isInstance(obj, INSTANCE);
}
@Override
public String getClassName(DynamicObject object) {
return CLASS_NAME;
}
@TruffleBoundary
@Override
public Object getOwnHelper(DynamicObject store, Object thisObj, long index, Node encapsulatingNode) {
String indexAsString = Boundaries.stringValueOf(index);
if (JSOrdinary.INSTANCE.hasOwnProperty(store, indexAsString)) {
return JSOrdinary.INSTANCE.getOwnHelper(store, thisObj, indexAsString, encapsulatingNode);
}
return super.getOwnHelper(store, thisObj, index, encapsulatingNode);
}
@TruffleBoundary
@Override
public boolean set(DynamicObject thisObj, long index, Object value, Object receiver, boolean isStrict, Node encapsulatingNode) {
String indexAsString = Boundaries.stringValueOf(index);
if (JSOrdinary.INSTANCE.hasOwnProperty(thisObj, indexAsString)) {
return ordinarySet(thisObj, indexAsString, value, receiver, isStrict, encapsulatingNode);
}
return super.set(thisObj, index, value, receiver, isStrict, encapsulatingNode);
}
@TruffleBoundary
@Override
public boolean delete(DynamicObject thisObj, long index, boolean isStrict) {
ScriptArray array = arrayAccess().getArrayType(thisObj);
if (array.hasElement(thisObj, index)) {
ScriptArray arrayType = arrayGetArrayType(thisObj);
if (arrayType.canDeleteElement(thisObj, index, isStrict)) {
arraySetArrayType(thisObj, arrayType.deleteElement(thisObj, index, isStrict));
return true;
} else {
return false;
}
} else {
return JSOrdinary.INSTANCE.delete(thisObj, index, isStrict);
}
}
@Override
protected DynamicObject makeSlowArray(DynamicObject thisObj) {
assert JSSlowArray.isJSSlowArray(thisObj);
return thisObj;
}
@Override
protected boolean defineOwnPropertyIndex(DynamicObject thisObj, String name, PropertyDescriptor descriptor, boolean doThrow) {
long index = JSRuntime.toUInt32(name);
if (index >= getLength(thisObj)) {
PropertyDescriptor desc = getOwnProperty(thisObj, LENGTH);
if (!desc.getWritable()) {
return DefinePropertyUtil.reject(doThrow, ARRAY_LENGTH_NOT_WRITABLE);
}
}
if (this.getLength(thisObj) <= index) {
this.setLength(thisObj, (index + 1), doThrow);
}
ScriptArray arrayType = arrayGetArrayType(thisObj);
if (arrayType.hasElement(thisObj, index) && !JSOrdinary.INSTANCE.hasOwnProperty(thisObj, name)) {
JSContext context = JSObject.getJSContext(thisObj);
boolean wasNotExtensible = !JSShape.isExtensible(thisObj.getShape());
JSObjectUtil.putDataProperty(context, thisObj, name, get(thisObj, index), JSAttributes.fromConfigurableEnumerableWritable(!arrayType.isSealed(), true, !arrayType.isFrozen()));
if (wasNotExtensible) {
assert !JSShape.isExtensible(thisObj.getShape());
}
arraySetArrayType(thisObj, arrayType.deleteElementImpl(thisObj, index, false));
}
boolean succeeded = jsDefineProperty(thisObj, index, descriptor, false);
if (!succeeded) {
JSContext context = JavaScriptLanguage.getCurrentJSRealm().getContext();
return DefinePropertyUtil.reject(doThrow, context.isOptionNashornCompatibilityMode() ? "cannot set property" : "Cannot redefine property");
}
return true;
}
private static boolean jsDefineProperty(DynamicObject thisObj, long index, PropertyDescriptor descriptor, boolean doThrow) {
ScriptArray internalArray = arrayAccess().getArrayType(thisObj);
boolean copyValue = (internalArray.hasElement(thisObj, index) && (!descriptor.hasValue() && !descriptor.hasGet()));
boolean succeed = DefinePropertyUtil.ordinaryDefineOwnProperty(thisObj, Boundaries.stringValueOf(index), descriptor, doThrow);
if (copyValue) {
JSObject.set(thisObj, index, internalArray.getElement(thisObj, index), doThrow, null);
}
return succeed;
}
@TruffleBoundary
@Override
public boolean setLength(DynamicObject thisObj, long length, boolean doThrow) {
if (testIntegrityLevel(thisObj, true)) {
throw Errors.createTypeError("cannot set length of a frozen array");
}
long oldLen = getLength(thisObj);
long newLen = length;
ScriptArray internalArray = arrayGetArrayType(thisObj);
boolean sealed = internalArray.isSealed();
boolean deleteSucceeded = true;
if (newLen < oldLen) {
for (long idx = oldLen - 1; idx >= newLen; idx--) {
if (internalArray.hasElement(thisObj, idx)) {
deleteSucceeded = !sealed;
} else {
deleteSucceeded = JSOrdinary.INSTANCE.delete(thisObj, idx, false);
}
if (!deleteSucceeded) {
newLen = idx + 1;
break;
}
}
}
if (newLen > Integer.MAX_VALUE && !(internalArray instanceof SparseArray)) {
internalArray = SparseArray.makeSparseArray(thisObj, internalArray);
}
arraySetArrayType(thisObj, internalArray.setLength(thisObj, newLen, doThrow));
if (!deleteSucceeded) {
JSContext context = JavaScriptLanguage.getCurrentJSRealm().getContext();
return DefinePropertyUtil.reject(doThrow, context.isOptionNashornCompatibilityMode() ? "cannot set property: length" : CANNOT_REDEFINE_PROPERTY_LENGTH);
}
return true;
}
}