package com.oracle.truffle.js.nodes.access;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.Tag;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.nodes.JavaScriptNode;
import com.oracle.truffle.js.nodes.cast.JSToBooleanNode;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.Symbol;
import com.oracle.truffle.js.runtime.builtins.JSFunction;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.JSObject;
import com.oracle.truffle.js.runtime.objects.Undefined;
import java.util.Set;
public final class WithTargetNode extends JavaScriptNode {
@Child private JavaScriptNode withVariable;
@Child private HasPropertyCacheNode withObjectHasProperty;
@Child private HasPropertyCacheNode globalObjectHasProperty;
@Child private PropertyGetNode withObjectGetUnscopables;
@Child private PropertyGetNode unscopablesGetProperty;
@Child private JSToBooleanNode toBoolean;
private final JSContext context;
private WithTargetNode(JSContext context, String propertyName, JavaScriptNode withVariable) {
this.withVariable = withVariable;
this.context = context;
this.withObjectHasProperty = HasPropertyCacheNode.create(propertyName, context);
this.globalObjectHasProperty = HasPropertyCacheNode.create(propertyName, context);
this.withObjectGetUnscopables = PropertyGetNode.create(Symbol.SYMBOL_UNSCOPABLES, false, context);
this.unscopablesGetProperty = PropertyGetNode.create(propertyName, false, context);
this.toBoolean = JSToBooleanNode.create();
}
public static JavaScriptNode create(JSContext context, String propertyName, JavaScriptNode withVariable) {
return new WithTargetNode(context, propertyName, withVariable);
}
@Override
public Object execute(VirtualFrame frame) {
Object target = withVariable.execute(frame);
if (withObjectHasProperty.hasProperty(target) && isPropertyScopable(target)) {
return target;
} else if (context.isOptionNashornCompatibilityMode() && hasNoSuchProperty(target, false) && !globalObjectHasProperty.hasProperty(GlobalObjectNode.getGlobalObject(context))) {
return target;
}
return Undefined.instance;
}
private boolean isPropertyScopable(Object target) {
if (context.getEcmaScriptVersion() >= 6) {
if (JSDynamicObject.isJSDynamicObject(target)) {
Object unscopables = withObjectGetUnscopables.getValue(target);
if (JSRuntime.isObject(unscopables)) {
boolean blocked = toBoolean.executeBoolean(unscopablesGetProperty.getValue(unscopables));
if (blocked) {
return false;
}
}
} else {
return true;
}
}
return true;
}
private boolean hasNoSuchProperty(Object thisTruffleObj, boolean isMethod) {
if (JSRuntime.isObject(thisTruffleObj)) {
DynamicObject thisObj = (DynamicObject) thisTruffleObj;
if ((!isMethod && !context.getNoSuchPropertyUnusedAssumption().isValid() && JSObject.hasOwnProperty(thisObj, JSObject.NO_SUCH_PROPERTY_NAME)) ||
(isMethod && !context.getNoSuchMethodUnusedAssumption().isValid() && JSObject.hasOwnProperty(thisObj, JSObject.NO_SUCH_METHOD_NAME))) {
return hasNoSuchPropertyImpl(thisObj, isMethod);
}
}
return false;
}
private static boolean hasNoSuchPropertyImpl(DynamicObject thisObj, boolean isMethod) {
assert JSRuntime.isObject(thisObj);
Object function = JSObject.get(thisObj, isMethod ? JSObject.NO_SUCH_METHOD_NAME : JSObject.NO_SUCH_PROPERTY_NAME);
return JSFunction.isJSFunction(function);
}
@Override
protected JavaScriptNode copyUninitialized(Set<Class<? extends Tag>> materializedTags) {
return new WithTargetNode(context, (String) withObjectHasProperty.getKey(), cloneUninitialized(withVariable, materializedTags));
}
}