package com.oracle.truffle.js.nodes.interop;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.DynamicObjectLibrary;
import com.oracle.truffle.api.object.Property;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.access.GetPrototypeNode;
import com.oracle.truffle.js.nodes.access.IsExtensibleNode;
import com.oracle.truffle.js.nodes.unary.IsCallableNode;
import com.oracle.truffle.js.runtime.builtins.JSProxy;
import com.oracle.truffle.js.runtime.objects.Accessor;
import com.oracle.truffle.js.runtime.objects.JSObject;
import com.oracle.truffle.js.runtime.objects.JSProperty;
import com.oracle.truffle.js.runtime.objects.Null;
import com.oracle.truffle.js.runtime.objects.PropertyDescriptor;
import com.oracle.truffle.js.runtime.objects.Undefined;
@GenerateUncached
public abstract class KeyInfoNode extends JavaScriptBaseNode {
public static final int READABLE = 1 << 0;
public static final int MODIFIABLE = 1 << 1;
public static final int INSERTABLE = 1 << 2;
public static final int INVOCABLE = 1 << 3;
public static final int REMOVABLE = 1 << 4;
public static final int READ_SIDE_EFFECTS = 1 << 5;
public static final int WRITE_SIDE_EFFECTS = 1 << 6;
public static final int WRITABLE = INSERTABLE | MODIFIABLE;
KeyInfoNode() {
}
public abstract boolean execute(DynamicObject receiver, String key, int query);
@Specialization(guards = {"!isJSProxy(target)", "property != null"}, limit = "2")
static boolean cachedOwnProperty(DynamicObject target, String key, int query,
@CachedLibrary("target") DynamicObjectLibrary objectLibrary,
@Bind("objectLibrary.getProperty(target, key)") Property property,
@Cached IsCallableNode isCallable) {
if (JSProperty.isAccessor(property)) {
Accessor accessor = (Accessor) objectLibrary.getOrDefault(target, key, null);
if ((query & READABLE) != 0 && accessor.hasGetter()) {
return true;
}
if ((query & MODIFIABLE) != 0 && accessor.hasSetter()) {
return true;
}
if ((query & READ_SIDE_EFFECTS) != 0 && accessor.hasGetter()) {
return true;
}
if ((query & WRITE_SIDE_EFFECTS) != 0 && accessor.hasSetter()) {
return true;
}
if ((query & REMOVABLE) != 0 && JSProperty.isConfigurable(property)) {
return true;
}
return false;
} else {
assert JSProperty.isData(property);
if ((query & READABLE) != 0) {
return true;
}
if ((query & MODIFIABLE) != 0 && JSProperty.isWritable(property)) {
return true;
}
if ((query & INVOCABLE) != 0 && isCallable.executeBoolean(objectLibrary.getOrDefault(target, key, Undefined.instance))) {
return true;
}
if ((query & REMOVABLE) != 0 && JSProperty.isConfigurable(property)) {
return true;
}
return false;
}
}
@Specialization(replaces = "cachedOwnProperty")
static boolean member(DynamicObject target, String key, int query,
@Cached GetPrototypeNode getPrototype,
@Cached IsCallableNode isCallable,
@Cached IsExtensibleNode isExtensible) {
PropertyDescriptor desc = null;
boolean isProxy = false;
for (DynamicObject proto = target; proto != Null.instance; proto = getPrototype.executeJSObject(proto)) {
desc = JSObject.getOwnProperty(proto, key);
if (JSProxy.isJSProxy(proto)) {
isProxy = true;
break;
}
if (desc != null) {
break;
}
}
if (desc == null) {
if ((query & INSERTABLE) != 0 && isExtensible.executeBoolean(target)) {
return true;
}
return false;
}
boolean hasGet = desc.hasGet() && desc.getGet() != Undefined.instance;
boolean hasSet = desc.hasSet() && desc.getSet() != Undefined.instance;
boolean readable = hasGet || !hasSet;
boolean writable = hasSet || (!hasGet && desc.getIfHasWritable(true));
boolean readSideEffects = isProxy || hasGet;
boolean writeSideEffects = isProxy || hasSet;
if ((query & READABLE) != 0 && readable) {
return true;
}
if ((query & MODIFIABLE) != 0 && writable) {
return true;
}
if ((query & READ_SIDE_EFFECTS) != 0 && readSideEffects) {
return true;
}
if ((query & WRITE_SIDE_EFFECTS) != 0 && writeSideEffects) {
return true;
}
if ((query & INVOCABLE) != 0 && desc.isDataDescriptor() && isCallable.executeBoolean(desc.getValue())) {
return true;
}
if ((query & REMOVABLE) != 0 && desc.getConfigurable()) {
return true;
}
return false;
}
}