package com.oracle.truffle.js.nodes.function;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.DirectCallNode;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.js.nodes.access.IsObjectNode;
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.JavaScriptRootNode;
import com.oracle.truffle.js.runtime.builtins.JSFunction;
import com.oracle.truffle.js.runtime.builtins.JSFunctionData;
import com.oracle.truffle.js.runtime.objects.Undefined;
public final class ConstructorRootNode extends JavaScriptRootNode {
private final JSFunctionData functionData;
private final CallTarget callTarget;
@Child private DirectCallNode callNode;
@Child private SpecializedNewObjectNode newObjectNode;
@Child private IsObjectNode isObjectNode;
private final ConditionProfile isObject = ConditionProfile.createBinaryProfile();
private final ConditionProfile isNotUndefined = ConditionProfile.createBinaryProfile();
private final boolean newTarget;
protected ConstructorRootNode(JSFunctionData functionData, CallTarget callTarget, boolean newTarget) {
super(functionData.getContext().getLanguage(), ((RootCallTarget) callTarget).getRootNode().getSourceSection(), null);
this.functionData = functionData;
this.callTarget = callTarget;
this.newTarget = newTarget;
}
public static ConstructorRootNode create(JSFunctionData functionData, CallTarget callTarget, boolean newTarget) {
return new ConstructorRootNode(functionData, callTarget, newTarget);
}
private Object allocateThisObject(VirtualFrame frame, Object[] arguments) {
Object thisObject;
if (!getFunctionData().isDerived()) {
Object functionObject = newTarget ? arguments[2] : arguments[1];
thisObject = newObjectNode.execute(frame, (DynamicObject) functionObject);
} else {
thisObject = JSFunction.CONSTRUCT;
}
arguments[0] = thisObject;
return thisObject;
}
private Object filterConstructorResult(Object thisObject, Object result) {
if (isObject.profile(isObjectNode.executeBoolean(result))) {
return result;
}
if (getFunctionData().isDerived() && isNotUndefined.profile(result != Undefined.instance)) {
throw Errors.createTypeError("constructor result not as expected");
}
assert thisObject != JSFunction.CONSTRUCT;
return thisObject;
}
@Override
public Object execute(VirtualFrame frame) {
if (callNode == null || newObjectNode == null || isObjectNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
initialize();
}
Object[] arguments = frame.getArguments();
Object thisObject = allocateThisObject(frame, arguments);
Object result = callNode.call(arguments);
return filterConstructorResult(thisObject, result);
}
private void initialize() {
this.callNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget));
this.newObjectNode = insert(SpecializedNewObjectNode.create(functionData));
this.isObjectNode = insert(IsObjectNode.create());
}
private JSFunctionData getFunctionData() {
return functionData;
}
@Override
public boolean isCloningAllowed() {
return true;
}
@Override
protected boolean isCloneUninitializedSupported() {
return true;
}
@Override
public boolean isInternal() {
return true;
}
@Override
protected JavaScriptRootNode cloneUninitialized() {
return new ConstructorRootNode(functionData, callTarget, newTarget);
}
@Override
@TruffleBoundary
public String toString() {
String callTargetName = ((RootCallTarget) callTarget).getRootNode().toString();
return JSConfig.DetailedCallTargetNames ? JSRuntime.stringConcat("[Construct]", callTargetName) : callTargetName;
}
}