package jdk.nashorn.internal.runtime;
import static jdk.nashorn.internal.lookup.Lookup.MH;
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.SwitchPoint;
import jdk.dynalink.CallSiteDescriptor;
import jdk.dynalink.NamedOperation;
import jdk.dynalink.Operation;
import jdk.dynalink.StandardOperation;
import jdk.dynalink.linker.GuardedInvocation;
import jdk.dynalink.linker.LinkRequest;
import jdk.nashorn.api.scripting.AbstractJSObject;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
import jdk.nashorn.internal.runtime.linker.NashornGuards;
public final class WithObject extends Scope {
private static final MethodHandle WITHEXPRESSIONGUARD = findOwnMH("withExpressionGuard", boolean.class, Object.class, PropertyMap.class, SwitchPoint[].class);
private static final MethodHandle WITHEXPRESSIONFILTER = findOwnMH("withFilterExpression", Object.class, Object.class);
private static final MethodHandle WITHSCOPEFILTER = findOwnMH("withFilterScope", Object.class, Object.class);
private static final MethodHandle BIND_TO_EXPRESSION_OBJ = findOwnMH("bindToExpression", Object.class, Object.class, Object.class);
private static final MethodHandle BIND_TO_EXPRESSION_FN = findOwnMH("bindToExpression", Object.class, ScriptFunction.class, Object.class);
private final ScriptObject expression;
WithObject(final ScriptObject scope, final ScriptObject expression) {
super(scope, null);
this.expression = expression;
setIsInternal();
}
@Override
public boolean delete(final Object key, final boolean strict) {
final ScriptObject self = expression;
final String propName = JSType.toString(key);
final FindProperty find = self.findProperty(propName, true);
if (find != null) {
return self.delete(propName, strict);
}
return false;
}
@Override
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
if (request.isCallSiteUnstable()) {
return super.lookup(desc, request);
}
GuardedInvocation link = null;
final Operation op = desc.getOperation();
assert op instanceof NamedOperation;
final String name = ((NamedOperation)op).getName().toString();
FindProperty find = expression.findProperty(name, true);
if (find != null) {
link = expression.lookup(desc, request);
if (link != null) {
return fixExpressionCallSite(desc, link);
}
}
final ScriptObject scope = getProto();
find = scope.findProperty(name, true);
if (find != null) {
return fixScopeCallSite(scope.lookup(desc, request), name, find.getOwner());
}
final String fallBack;
final Operation firstOp = NashornCallSiteDescriptor.getBaseOperation(desc);
if (firstOp == StandardOperation.GET) {
if (NashornCallSiteDescriptor.isMethodFirstOperation(desc)) {
fallBack = NO_SUCH_METHOD_NAME;
} else {
fallBack = NO_SUCH_PROPERTY_NAME;
}
} else {
fallBack = null;
}
if (fallBack != null) {
find = expression.findProperty(fallBack, true);
if (find != null) {
if (NO_SUCH_METHOD_NAME.equals(fallBack)) {
link = expression.noSuchMethod(desc, request).addSwitchPoint(getProtoSwitchPoint(name));
} else if (NO_SUCH_PROPERTY_NAME.equals(fallBack)) {
link = expression.noSuchProperty(desc, request).addSwitchPoint(getProtoSwitchPoint(name));
}
}
}
if (link != null) {
return fixExpressionCallSite(desc, link);
}
link = scope.lookup(desc, request);
if (link != null) {
return fixScopeCallSite(link, name, null);
}
return null;
}
@Override
protected FindProperty findProperty(final Object key, final boolean deep, final boolean isScope, final ScriptObject start) {
final FindProperty exprProperty = expression.findProperty(key, true, false, expression);
if (exprProperty != null) {
return exprProperty;
}
return super.findProperty(key, deep, isScope, start);
}
@Override
protected Object invokeNoSuchProperty(final Object key, final boolean isScope, final int programPoint) {
final FindProperty find = expression.findProperty(NO_SUCH_PROPERTY_NAME, true);
if (find != null) {
final Object func = find.getObjectValue();
if (func instanceof ScriptFunction) {
final ScriptFunction sfunc = (ScriptFunction)func;
final Object self = isScope && sfunc.isStrict()? UNDEFINED : expression;
return ScriptRuntime.apply(sfunc, self, key);
}
}
return getProto().invokeNoSuchProperty(key, isScope, programPoint);
}
@Override
public void setSplitState(final int state) {
((Scope) getNonWithParent()).setSplitState(state);
}
@Override
public int getSplitState() {
return ((Scope) getNonWithParent()).getSplitState();
}
@Override
public void addBoundProperties(final ScriptObject source, final Property[] properties) {
getNonWithParent().addBoundProperties(source, properties);
}
private ScriptObject getNonWithParent() {
ScriptObject proto = getProto();
while (proto != null && proto instanceof WithObject) {
proto = proto.getProto();
}
return proto;
}
private static GuardedInvocation fixReceiverType(final GuardedInvocation link, final MethodHandle filter) {
final MethodType invType = link.getInvocation().type();
final MethodType newInvType = invType.changeParameterType(0, filter.type().returnType());
return link.asType(newInvType);
}
private static GuardedInvocation fixExpressionCallSite(final CallSiteDescriptor desc, final GuardedInvocation link) {
if (NashornCallSiteDescriptor.getBaseOperation(desc) != StandardOperation.GET || !NashornCallSiteDescriptor.isMethodFirstOperation(desc)) {
return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
}
final MethodHandle linkInvocation = link.getInvocation();
final MethodType linkType = linkInvocation.type();
final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
return link.replaceMethods(
MH.foldArguments(
linkReturnsFunction ?
BIND_TO_EXPRESSION_FN :
BIND_TO_EXPRESSION_OBJ,
filterReceiver(
linkInvocation.asType(
linkType.changeReturnType(
linkReturnsFunction ?
ScriptFunction.class :
Object.class).
changeParameterType(
0,
Object.class)),
WITHEXPRESSIONFILTER)),
filterGuardReceiver(link, WITHEXPRESSIONFILTER));
}
private GuardedInvocation fixScopeCallSite(final GuardedInvocation link, final String name, final ScriptObject owner) {
final GuardedInvocation newLink = fixReceiverType(link, WITHSCOPEFILTER);
final MethodHandle expressionGuard = expressionGuard(name, owner);
final MethodHandle filteredGuard = filterGuardReceiver(newLink, WITHSCOPEFILTER);
return link.replaceMethods(
filterReceiver(
newLink.getInvocation(),
WITHSCOPEFILTER),
NashornGuards.combineGuards(
expressionGuard,
filteredGuard));
}
private static MethodHandle filterGuardReceiver(final GuardedInvocation link, final MethodHandle receiverFilter) {
final MethodHandle test = link.getGuard();
if (test == null) {
return null;
}
final Class<?> receiverType = test.type().parameterType(0);
final MethodHandle filter = MH.asType(receiverFilter,
receiverFilter.type().changeParameterType(0, receiverType).
changeReturnType(receiverType));
return filterReceiver(test, filter);
}
private static MethodHandle filterReceiver(final MethodHandle mh, final MethodHandle receiverFilter) {
return MH.filterArguments(mh, 0, receiverFilter.asType(receiverFilter.type().changeReturnType(mh.type().parameterType(0))));
}
public static Object withFilterExpression(final Object receiver) {
return ((WithObject)receiver).expression;
}
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
if (fn instanceof ScriptFunction) {
return bindToExpression((ScriptFunction) fn, receiver);
} else if (fn instanceof ScriptObjectMirror) {
final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
if (mirror.isFunction()) {
return new AbstractJSObject() {
@Override
public Object call(final Object thiz, final Object... args) {
return mirror.call(withFilterExpression(receiver), args);
}
};
}
}
return fn;
}
private static Object bindToExpression(final ScriptFunction fn, final Object receiver) {
return fn.createBound(withFilterExpression(receiver), ScriptRuntime.EMPTY_ARRAY);
}
private MethodHandle expressionGuard(final String name, final ScriptObject owner) {
final PropertyMap map = expression.getMap();
final SwitchPoint[] sp = expression.getProtoSwitchPoints(name, owner);
return MH.insertArguments(WITHEXPRESSIONGUARD, 1, map, sp);
}
@SuppressWarnings("unused")
private static boolean withExpressionGuard(final Object receiver, final PropertyMap map, final SwitchPoint[] sp) {
return ((WithObject)receiver).expression.getMap() == map && !hasBeenInvalidated(sp);
}
private static boolean hasBeenInvalidated(final SwitchPoint[] switchPoints) {
if (switchPoints != null) {
for (final SwitchPoint switchPoint : switchPoints) {
if (switchPoint.hasBeenInvalidated()) {
return true;
}
}
}
return false;
}
public static Object withFilterScope(final Object receiver) {
return ((WithObject)receiver).getProto();
}
public ScriptObject getExpression() {
return expression;
}
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
return MH.findStatic(MethodHandles.lookup(), WithObject.class, name, MH.type(rtype, types));
}
}