package com.oracle.truffle.js.nodes.access;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.NodeCost;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.nodes.function.JSFunctionCallNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSArguments;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.builtins.JSArray;
import com.oracle.truffle.js.runtime.builtins.JSFunction;
import com.oracle.truffle.js.runtime.builtins.JSProxy;
import com.oracle.truffle.js.runtime.interop.JSInteropUtil;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.Undefined;
@NodeInfo(cost = NodeCost.NONE)
@ImportStatic({JSProxy.class, JSArguments.class})
public abstract class JSProxyCallNode extends JavaScriptBaseNode {
private final JSContext context;
@Child private GetMethodNode trapGetter;
@Child private JSFunctionCallNode callNode;
@Child private JSFunctionCallNode callTrapNode;
protected final boolean isNew;
protected final boolean isNewTarget;
private final ConditionProfile pxTrapFunProfile = ConditionProfile.createBinaryProfile();
private final BranchProfile errorBranch = BranchProfile.create();
protected JSProxyCallNode(JSContext context, boolean isNew, boolean isNewTarget) {
this.callNode = (isNew || isNewTarget) ? JSFunctionCallNode.createNewTarget() : JSFunctionCallNode.createCall();
this.callTrapNode = JSFunctionCallNode.createCall();
this.trapGetter = GetMethodNode.create(context, null, isNewTarget || isNew ? JSProxy.CONSTRUCT : JSProxy.APPLY);
this.context = context;
this.isNew = isNew;
this.isNewTarget = isNewTarget;
}
public abstract Object execute(Object[] arguments);
public static JSProxyCallNode create(JSContext context, boolean isNew, boolean isNewTarget) {
return JSProxyCallNodeGen.create(context, isNew, isNewTarget);
}
@Specialization(guards = {"!isNew", "!isNewTarget"})
protected Object doCall(Object[] arguments) {
Object thisObj = JSArguments.getThisObject(arguments);
Object function = JSArguments.getFunctionObject(arguments);
assert JSProxy.isJSProxy(function);
DynamicObject proxy = (DynamicObject) function;
if (!JSRuntime.isCallableProxy(proxy)) {
errorBranch.enter();
throw Errors.createTypeErrorNotAFunction(function, this);
} else {
DynamicObject pxHandler = JSProxy.getHandlerChecked(proxy, errorBranch);
Object pxTarget = JSProxy.getTarget(proxy);
Object pxTrapFun = trapGetter.executeWithTarget(pxHandler);
Object[] proxyArguments = JSArguments.extractUserArguments(arguments);
if (pxTrapFunProfile.profile(pxTrapFun == Undefined.instance)) {
return callNode.executeCall(JSArguments.create(thisObj, pxTarget, proxyArguments));
}
Object[] trapArgs = new Object[]{pxTarget, thisObj, JSArray.createConstant(context, proxyArguments)};
return callTrapNode.executeCall(JSArguments.create(pxHandler, pxTrapFun, trapArgs));
}
}
@Specialization(guards = {"isNew || isNewTarget"})
protected Object doConstruct(Object[] arguments) {
Object function = JSArguments.getFunctionObject(arguments);
assert JSProxy.isJSProxy(function);
DynamicObject proxy = (DynamicObject) function;
if (!JSRuntime.isConstructorProxy(proxy)) {
errorBranch.enter();
throw Errors.createTypeErrorNotAFunction(function, this);
} else {
DynamicObject pxHandler = JSProxy.getHandlerChecked(proxy, errorBranch);
Object pxTarget = JSProxy.getTarget(proxy);
Object pxTrapFun = trapGetter.executeWithTarget(pxHandler);
Object newTarget = isNewTarget ? JSArguments.getNewTarget(arguments) : proxy;
Object[] constructorArguments = JSArguments.extractUserArguments(arguments, isNewTarget ? 1 : 0);
if (pxTrapFunProfile.profile(pxTrapFun == Undefined.instance)) {
if (JSDynamicObject.isJSDynamicObject(pxTarget)) {
return callNode.executeCall(JSArguments.createWithNewTarget(JSFunction.CONSTRUCT, pxTarget, newTarget, constructorArguments));
} else {
return JSInteropUtil.construct(pxTarget, constructorArguments);
}
}
Object[] trapArgs = new Object[]{pxTarget, JSArray.createConstant(context, constructorArguments), newTarget};
Object result = callTrapNode.executeCall(JSArguments.create(pxHandler, pxTrapFun, trapArgs));
if (!JSRuntime.isObject(result)) {
errorBranch.enter();
throw Errors.createTypeErrorNotAnObject(result, this);
}
return result;
}
}
}