package com.oracle.truffle.js.runtime.builtins;
import java.util.Map;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
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.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.PropertyDescriptor;
import com.oracle.truffle.js.runtime.util.DefinePropertyUtil;
public abstract class JSAbstractArgumentsArray extends JSAbstractArray {
public static final String CALLEE = "callee";
public static final String CALLER = "caller";
protected static final String CLASS_NAME = "Arguments";
@TruffleBoundary
@Override
public long getLength(DynamicObject thisObj) {
Object lengthValue = get(thisObj, LENGTH);
return JSRuntime.toInteger(JSRuntime.toNumber(lengthValue));
}
@TruffleBoundary
@Override
public boolean delete(DynamicObject thisObj, long index, boolean isStrict) {
if (isMappedArguments(thisObj)) {
makeSlowArray(thisObj);
return JSObject.delete(thisObj, index, isStrict);
} else {
return super.delete(thisObj, index, isStrict);
}
}
@TruffleBoundary
@Override
public boolean delete(DynamicObject thisObj, Object key, boolean isStrict) {
long index = JSRuntime.propertyKeyToArrayIndex(key);
if (index >= 0 && JSRuntime.isArrayIndex(index)) {
return delete(thisObj, index, isStrict);
} else {
return super.delete(thisObj, key, isStrict);
}
}
@Override
public String getClassName(DynamicObject object) {
return CLASS_NAME;
}
protected static boolean isMappedArguments(DynamicObject thisObj) {
return thisObj instanceof JSArgumentsObject.Mapped;
}
@Override
protected DynamicObject makeSlowArray(DynamicObject thisObj) {
CompilerAsserts.neverPartOfCompilation(MAKE_SLOW_ARRAY_NEVER_PART_OF_COMPILATION_MESSAGE);
if (isSlowArray(thisObj)) {
return thisObj;
}
assert JSArgumentsArray.isJSFastArgumentsObject(thisObj);
JSDynamicObject.setJSClass(thisObj, JSSlowArgumentsArray.INSTANCE);
if (isMappedArguments(thisObj)) {
((JSArgumentsObject.Mapped) thisObj).initDisconnectedIndices();
}
JSObject.getJSContext(thisObj).getFastArgumentsObjectAssumption().invalidate("create slow ArgumentsObject");
return thisObj;
}
public static int getConnectedArgumentCount(DynamicObject argumentsArray) {
assert JSArgumentsArray.isJSArgumentsObject(argumentsArray);
return ((JSArgumentsObject.Mapped) argumentsArray).getConnectedArgumentCount();
}
@TruffleBoundary
private static Map<Long, Object> getDisconnectedIndices(DynamicObject argumentsArray) {
assert hasDisconnectedIndices(argumentsArray);
return ((JSArgumentsObject.Mapped) argumentsArray).getDisconnectedIndices();
}
@TruffleBoundary
public static boolean wasIndexDisconnected(DynamicObject argumentsArray, long index) {
assert hasDisconnectedIndices(argumentsArray);
return getDisconnectedIndices(argumentsArray).containsKey(index);
}
@TruffleBoundary
public static Object getDisconnectedIndexValue(DynamicObject argumentsArray, long index) {
assert hasDisconnectedIndices(argumentsArray);
assert wasIndexDisconnected(argumentsArray, index);
return getDisconnectedIndices(argumentsArray).get(index);
}
@TruffleBoundary
public static Object setDisconnectedIndexValue(DynamicObject argumentsArray, long index, Object value) {
assert hasDisconnectedIndices(argumentsArray);
assert wasIndexDisconnected(argumentsArray, index);
getDisconnectedIndices(argumentsArray).put(index, value);
return value;
}
@TruffleBoundary
public static void disconnectIndex(DynamicObject argumentsArray, long index, Object oldValue) {
if (!hasDisconnectedIndices(argumentsArray)) {
JSArgumentsArray.INSTANCE.makeSlowArray(argumentsArray);
}
getDisconnectedIndices(argumentsArray).put(index, oldValue);
}
public static boolean hasDisconnectedIndices(DynamicObject argumentsArray) {
return JSSlowArgumentsArray.isJSSlowArgumentsObject(argumentsArray);
}
@TruffleBoundary
@Override
public boolean defineOwnProperty(DynamicObject thisObj, Object key, PropertyDescriptor descriptor, boolean doThrow) {
boolean isMappedArguments = isMappedArguments(thisObj);
long index = JSRuntime.propertyKeyToArrayIndex(key);
Object oldValue = null;
boolean isIndexConnected = false;
if (index >= 0) {
makeSlowArray(thisObj);
isIndexConnected = isMappedArguments && !wasIndexDisconnected(thisObj, index);
oldValue = super.get(thisObj, index);
ScriptArray arrayType = arrayGetArrayType(thisObj);
if (arrayType.hasElement(thisObj, index)) {
JSContext context = JSObject.getJSContext(thisObj);
JSObjectUtil.putDataProperty(context, thisObj, key, oldValue, JSAttributes.getDefault());
if (arrayType.canDeleteElement(thisObj, index, false)) {
arraySetArrayType(thisObj, arrayType.deleteElement(thisObj, index, false));
}
}
}
boolean allowed = DefinePropertyUtil.ordinaryDefineOwnProperty(thisObj, key, descriptor, doThrow);
if (!allowed) {
return DefinePropertyUtil.reject(doThrow, "not allowed to defineProperty on an arguments object");
}
if (isIndexConnected) {
assert key instanceof String : key;
definePropertyMapped(thisObj, (String) key, descriptor, index, oldValue, thisObj);
}
return true;
}
@TruffleBoundary
private static void definePropertyMapped(DynamicObject thisObj, String name, PropertyDescriptor descriptor, long index, Object oldValueParam, DynamicObject obj) {
if (descriptor.isAccessorDescriptor()) {
disconnectIndex(thisObj, index, oldValueParam);
} else {
Object oldValue = oldValueParam;
if (descriptor.hasValue()) {
Object value = descriptor.getValue();
JSObject.set(obj, name, value);
oldValue = value;
}
if (descriptor.hasWritable()) {
Object value = descriptor.getWritable();
if (Boolean.FALSE.equals(value)) {
disconnectIndex(thisObj, index, oldValue);
}
}
}
}
@TruffleBoundary
@Override
public PropertyDescriptor getOwnProperty(DynamicObject thisObj, Object key) {
assert JSRuntime.isPropertyKey(key);
PropertyDescriptor desc = ordinaryGetOwnPropertyArray(thisObj, key);
if (desc == null) {
return null;
}
long index = JSRuntime.propertyKeyToArrayIndex(key);
if (index >= 0) {
boolean isMapped = JSArgumentsArray.isJSFastArgumentsObject(thisObj) || (isMappedArguments(thisObj) && !wasIndexDisconnected(thisObj, index));
if (isMapped) {
desc.setValue(super.get(thisObj, index));
}
}
if (desc.isDataDescriptor() && CALLER.equals(key) && JSFunction.isJSFunction(desc.getValue()) && JSFunction.isStrict((DynamicObject) desc.getValue())) {
throw Errors.createTypeError("caller not allowed in strict mode");
}
return desc;
}
@Override
protected boolean isSlowArray(DynamicObject thisObj) {
return JSSlowArgumentsArray.isJSSlowArgumentsObject(thisObj);
}
}