JS backend: Use new CallTranslator for build main and test calls

This commit is contained in:
Erokhin Stanislav
2013-12-27 17:25:13 +04:00
parent 39f3a6ec4c
commit 2d50858289
3 changed files with 23 additions and 7 deletions
@@ -38,7 +38,7 @@ import org.jetbrains.k2js.translate.declaration.PackageDeclarationTranslator;
import org.jetbrains.k2js.translate.expression.ExpressionVisitor; import org.jetbrains.k2js.translate.expression.ExpressionVisitor;
import org.jetbrains.k2js.translate.expression.FunctionTranslator; import org.jetbrains.k2js.translate.expression.FunctionTranslator;
import org.jetbrains.k2js.translate.expression.PatternTranslator; import org.jetbrains.k2js.translate.expression.PatternTranslator;
import org.jetbrains.k2js.translate.reference.CallBuilder; import org.jetbrains.k2js.translate.reference.ReferencePackage;
import org.jetbrains.k2js.translate.test.JSTestGenerator; import org.jetbrains.k2js.translate.test.JSTestGenerator;
import org.jetbrains.k2js.translate.test.JSTester; import org.jetbrains.k2js.translate.test.JSTester;
import org.jetbrains.k2js.translate.utils.JsAstUtils; import org.jetbrains.k2js.translate.utils.JsAstUtils;
@@ -46,6 +46,7 @@ import org.jetbrains.k2js.translate.utils.dangerous.DangerousData;
import org.jetbrains.k2js.translate.utils.dangerous.DangerousTranslator; import org.jetbrains.k2js.translate.utils.dangerous.DangerousTranslator;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.plugin.JetMainDetector.getMainFunction; import static org.jetbrains.jet.plugin.JetMainDetector.getMainFunction;
@@ -174,9 +175,7 @@ public final class Translation {
return null; return null;
} }
FunctionDescriptor functionDescriptor = getFunctionDescriptor(context.bindingContext(), mainFunction); FunctionDescriptor functionDescriptor = getFunctionDescriptor(context.bindingContext(), mainFunction);
return CallBuilder.build(context) JsArrayLiteral argument = new JsArrayLiteral(toStringLiteralList(arguments, context.program()));
.args(new JsArrayLiteral(toStringLiteralList(arguments, context.program()))) return ReferencePackage.buildFakeCall(context, functionDescriptor, Collections.singletonList(argument), null).makeStmt();
.descriptor(functionDescriptor)
.translate().makeStmt();
} }
} }
@@ -49,6 +49,22 @@ fun TranslationContext.buildSet(resolvedCall: ResolvedCall<out VariableDescripto
return variableAccessCases.translate(variableAccessInfo) return variableAccessCases.translate(variableAccessInfo)
} }
fun TranslationContext.buildFakeCall(functionDescriptor: FunctionDescriptor, args: List<JsExpression>, thisObject: JsExpression?): JsExpression {
val fakeCallInfo = object: CallInfo {
override val context: TranslationContext = this@buildFakeCall
override val resolvedCall: ResolvedCall<out CallableDescriptor>
get() {
throw UnsupportedOperationException("Resolved call for direct call unsupported")
}
override val callableDescriptor: CallableDescriptor = functionDescriptor
override val thisObject: JsExpression? = thisObject
override val receiverObject: JsExpression? = null
override val nullableReceiverForSafeCall: JsExpression? = null
}
val fakeFunctionInfo = FunctionCallInfo(fakeCallInfo, CallArgumentTranslator.ArgumentsInfo(args, false, null));
return DefaultCallCase(fakeFunctionInfo).translate()
}
private fun TranslationContext.buildCall(resolvedCall: ResolvedCall<out FunctionDescriptor>, receiver1: JsExpression?, receiver2: JsExpression?): JsExpression { private fun TranslationContext.buildCall(resolvedCall: ResolvedCall<out FunctionDescriptor>, receiver1: JsExpression?, receiver2: JsExpression?): JsExpression {
if (resolvedCall is VariableAsFunctionResolvedCall) { if (resolvedCall is VariableAsFunctionResolvedCall) {
assert(receiver2 == null, "receiver2 for VariableAsFunctionResolvedCall must be null") // TODO: add debug info assert(receiver2 == null, "receiver2 for VariableAsFunctionResolvedCall must be null") // TODO: add debug info
@@ -25,11 +25,12 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.JetTestFunctionDetector; import org.jetbrains.k2js.translate.general.JetTestFunctionDetector;
import org.jetbrains.k2js.translate.reference.CallBuilder; import org.jetbrains.k2js.translate.reference.ReferencePackage;
import org.jetbrains.k2js.translate.reference.ReferenceTranslator; import org.jetbrains.k2js.translate.reference.ReferenceTranslator;
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils; import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
//TODO: use method object instead of static functions //TODO: use method object instead of static functions
@@ -59,7 +60,7 @@ public final class JSTestGenerator {
@NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) { @NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) {
JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context); JsExpression expression = ReferenceTranslator.translateAsFQReference(classDescriptor, context);
JsNew testClass = new JsNew(expression); JsNew testClass = new JsNew(expression);
JsExpression functionToTestCall = CallBuilder.build(context).descriptor(functionDescriptor).receiver(testClass).translate(); JsExpression functionToTestCall = ReferencePackage.buildFakeCall(context, functionDescriptor, Collections.<JsExpression>emptyList(), testClass);
JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName()); JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
tester.constructTestMethodInvocation(functionToTestCall, testName); tester.constructTestMethodInvocation(functionToTestCall, testName);
} }