package com.oracle.truffle.js.nodes.access;
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.CachedLanguage;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.ReportPolymorphism;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.dsl.TypeSystemReference;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.nodes.IntToLongTypeSystem;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.cast.JSToPropertyKeyNode;
import com.oracle.truffle.js.nodes.cast.JSToStringNode;
import com.oracle.truffle.js.nodes.interop.ForeignObjectPrototypeNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSConfig;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.Symbol;
import com.oracle.truffle.js.runtime.array.ScriptArray;
import com.oracle.truffle.js.runtime.builtins.JSAbstractArray;
import com.oracle.truffle.js.runtime.builtins.JSArgumentsArray;
import com.oracle.truffle.js.runtime.builtins.JSArray;
import com.oracle.truffle.js.runtime.builtins.JSArrayBufferView;
import com.oracle.truffle.js.runtime.builtins.JSClass;
import com.oracle.truffle.js.runtime.builtins.JSString;
import com.oracle.truffle.js.runtime.interop.JSInteropUtil;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.JSObject;
import com.oracle.truffle.js.runtime.util.JSClassProfile;
@TypeSystemReference(IntToLongTypeSystem.class)
@ImportStatic(value = {JSRuntime.class, JSInteropUtil.class, JSConfig.class})
public abstract class JSHasPropertyNode extends JavaScriptBaseNode {
private final boolean hasOwnProperty;
private final JSClassProfile classProfile = JSClassProfile.create();
private final ConditionProfile hasElementProfile = ConditionProfile.createBinaryProfile();
static final int MAX_ARRAY_TYPES = 3;
protected JSHasPropertyNode(boolean hasOwnProperty) {
this.hasOwnProperty = hasOwnProperty;
}
public static JSHasPropertyNode create() {
return JSHasPropertyNodeGen.create(false);
}
public static JSHasPropertyNode create(boolean hasOwnProperty) {
return JSHasPropertyNodeGen.create(hasOwnProperty);
}
public abstract boolean executeBoolean(Object object, Object propertyName);
public abstract boolean executeBoolean(Object object, long index);
@Specialization(guards = {"isJSFastArray(object)", "isArrayIndex(index)", "cachedArrayType.isInstance(getArrayType(object))"}, limit = "MAX_ARRAY_TYPES")
public boolean arrayLongCached(DynamicObject object, long index,
@Cached("getArrayType(object)") ScriptArray cachedArrayType) {
return checkInteger(object, index, cachedArrayType.cast(getArrayType(object)));
}
@Specialization(guards = {"isJSFastArray(object)", "isArrayIndex(index)"}, replaces = {"arrayLongCached"})
public boolean arrayLong(DynamicObject object, long index) {
return checkInteger(object, index, getArrayType(object));
}
private boolean checkInteger(DynamicObject object, long index, ScriptArray arrayType) {
if (hasElementProfile.profile(arrayType.hasElement(object, index))) {
return true;
} else {
return objectLong(object, index);
}
}
@SuppressWarnings("unused")
@Specialization(guards = {"cachedObjectType != null", "cachedObjectType.isInstance(object)", "cachedName.equals(propertyName)"}, limit = "1")
public boolean objectStringCached(DynamicObject object, String propertyName,
@Cached("getCacheableObjectType(object)") JSClass cachedObjectType,
@Cached("propertyName") String cachedName,
@Cached("getCachedPropertyGetter(object,propertyName)") HasPropertyCacheNode hasPropertyNode) {
return hasPropertyNode.hasProperty(object);
}
@SuppressWarnings("unused")
@Specialization(guards = {"isJSArray(object)", "!isArrayIndex(cachedName)", "cachedName.equals(propertyName)"}, limit = "1")
public boolean arrayStringCached(DynamicObject object, String propertyName,
@Cached("propertyName") String cachedName,
@Cached("getCachedPropertyGetter(object,propertyName)") HasPropertyCacheNode hasPropertyNode) {
return hasPropertyNode.hasProperty(object);
}
@ReportPolymorphism.Megamorphic
@Specialization(guards = {"isJSDynamicObject(object)"}, replaces = {"objectStringCached", "arrayStringCached"})
public boolean objectOrArrayString(DynamicObject object, String propertyName) {
return hasPropertyGeneric(object, propertyName);
}
@ReportPolymorphism.Megamorphic
@Specialization(guards = {"isJSDynamicObject(object)"})
public boolean objectSymbol(DynamicObject object, Symbol propertyName) {
return hasPropertyGeneric(object, propertyName);
}
@Specialization(guards = {"isJSDynamicObject(object)", "!isJSFastArray(object)"})
public boolean objectLong(DynamicObject object, long propertyIdx) {
if (hasOwnProperty) {
return JSObject.hasOwnProperty(object, propertyIdx, classProfile);
} else {
return JSObject.hasProperty(object, propertyIdx, classProfile);
}
}
private boolean hasPropertyGeneric(DynamicObject object, Object propertyKey) {
assert JSRuntime.isPropertyKey(propertyKey);
if (hasOwnProperty) {
return JSObject.hasOwnProperty(object, propertyKey, classProfile);
} else {
return JSObject.hasProperty(object, propertyKey, classProfile);
}
}
@Specialization(guards = "isForeignObject(object)", limit = "InteropLibraryLimit")
public boolean foreignObject(Object object, Object propertyName,
@CachedLibrary("object") InteropLibrary interop,
@Cached("create()") JSToStringNode toStringNode,
@Cached("create()") ForeignObjectPrototypeNode foreignObjectPrototypeNode,
@Cached("create()") JSHasPropertyNode hasInPrototype,
@CachedLanguage LanguageReference<JavaScriptLanguage> languageRef) {
if (isForeignValueOfTypeObject(object, interop)) {
if (propertyName instanceof Number && interop.hasArrayElements(object)) {
long index = JSRuntime.longValue((Number) propertyName);
return index >= 0 && index < JSInteropUtil.getArraySize(object, interop, this);
} else {
if (!(propertyName instanceof Symbol) && interop.isMemberExisting(object, toStringNode.executeString(propertyName))) {
return true;
}
if (languageRef.get().getJSContext().getContextOptions().hasForeignObjectPrototype()) {
DynamicObject prototype = foreignObjectPrototypeNode.executeDynamicObject(object);
return hasInPrototype.executeBoolean(prototype, propertyName);
} else {
return false;
}
}
} else {
throw Errors.createTypeErrorNotAnObject(object, this);
}
}
private static boolean isForeignValueOfTypeObject(Object object, InteropLibrary interop) {
if (interop.isNull(object)) {
return false;
} else if (interop.hasMembers(object) && !interop.isBoolean(object) && !interop.isString(object) && !interop.isNumber(object)) {
return true;
} else if (interop.hasArrayElements(object)) {
return true;
} else if (interop.isExecutable(object) || interop.isInstantiable(object)) {
return true;
} else {
return false;
}
}
@ReportPolymorphism.Megamorphic
@Specialization(guards = "isJSDynamicObject(object)")
public boolean objectObject(DynamicObject object, Object propertyName,
@Cached("create()") JSToPropertyKeyNode toPropertyKeyNode) {
Object propertyKey = toPropertyKeyNode.execute(propertyName);
return hasPropertyGeneric(object, propertyKey);
}
protected static boolean isCacheableObjectType(DynamicObject obj) {
return JSDynamicObject.isJSDynamicObject(obj) && (!JSRuntime.isNullOrUndefined(obj) &&
!JSString.isJSString(obj) &&
!JSArray.isJSArray(obj) &&
!JSArgumentsArray.isJSArgumentsObject(obj) &&
!JSArrayBufferView.isJSArrayBufferView(obj));
}
protected static JSClass getCacheableObjectType(DynamicObject obj) {
if (isCacheableObjectType(obj)) {
return JSObject.getJSClass(obj);
}
return null;
}
protected static ScriptArray getArrayType(DynamicObject object) {
return JSAbstractArray.arrayGetArrayType(object);
}
protected HasPropertyCacheNode getCachedPropertyGetter(DynamicObject object, Object key) {
return HasPropertyCacheNode.create(key, JSObject.getJSContext(object), hasOwnProperty);
}
}