JS backend: always (explicitly) generate constructor function for classes. Use name of class as name of constructor function when it isn't special.
#KT-4115 In Progress
This commit is contained in:
@@ -139,9 +139,6 @@ class ClassTranslator private constructor(
|
|||||||
invocationArguments += JsObjectLiteral(staticProperties, true)
|
invocationArguments += JsObjectLiteral(staticProperties, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (primaryConstructor != null && primaryConstructor!!.function.body.isEmpty) {
|
|
||||||
invocationArguments[invocationArguments.indexOf(primaryConstructor!!.function)] = JsLiteral.NULL
|
|
||||||
}
|
|
||||||
this.definitionPlace = definitionPlace
|
this.definitionPlace = definitionPlace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.js.translate.reference.CallArgumentTranslator;
|
|||||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
|
||||||
import org.jetbrains.kotlin.js.translate.utils.jsAstUtils.AstUtilsKt;
|
import org.jetbrains.kotlin.js.translate.utils.jsAstUtils.AstUtilsKt;
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||||
|
import org.jetbrains.kotlin.name.Name;
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject;
|
import org.jetbrains.kotlin.psi.KtClassOrObject;
|
||||||
import org.jetbrains.kotlin.psi.KtEnumEntry;
|
import org.jetbrains.kotlin.psi.KtEnumEntry;
|
||||||
import org.jetbrains.kotlin.psi.KtParameter;
|
import org.jetbrains.kotlin.psi.KtParameter;
|
||||||
@@ -77,12 +78,22 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
|||||||
ClassDescriptor classDescriptor = getClassDescriptor(context.bindingContext(), declaration);
|
ClassDescriptor classDescriptor = getClassDescriptor(context.bindingContext(), declaration);
|
||||||
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
|
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
|
||||||
|
|
||||||
|
Name name = classDescriptor.getName();
|
||||||
|
|
||||||
|
JsFunction ctorFunction;
|
||||||
if (primaryConstructor != null) {
|
if (primaryConstructor != null) {
|
||||||
return context.getFunctionObject(primaryConstructor);
|
ctorFunction = context.getFunctionObject(primaryConstructor);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return new JsFunction(context.scope(), new JsBlock(), "fake constructor for " + classDescriptor.getName().asString());
|
ctorFunction = new JsFunction(context.scope(), new JsBlock(), "fake constructor for " + name.asString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO use name from JsName when class annotated by that
|
||||||
|
if (!name.isSpecial()) {
|
||||||
|
ctorFunction.setName(ctorFunction.getScope().declareName(name.asString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctorFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -198,9 +209,12 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addCallToSuperMethod(@NotNull List<JsExpression> arguments, JsFunction initializer) {
|
private void addCallToSuperMethod(@NotNull List<JsExpression> arguments, JsFunction initializer) {
|
||||||
JsName ref = context().scope().declareName(Namer.CALLEE_NAME);
|
if (initializer.getName() == null) {
|
||||||
initializer.setName(ref);
|
JsName ref = context().scope().declareName(Namer.CALLEE_NAME);
|
||||||
JsInvocation call = new JsInvocation(Namer.getFunctionCallRef(Namer.superMethodNameRef(ref)));
|
initializer.setName(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsInvocation call = new JsInvocation(Namer.getFunctionCallRef(Namer.superMethodNameRef(initializer.getName())));
|
||||||
call.getArguments().add(JsLiteral.THIS);
|
call.getArguments().add(JsLiteral.THIS);
|
||||||
call.getArguments().addAll(arguments);
|
call.getArguments().addAll(arguments);
|
||||||
initializerStatements.add(0, call.makeStmt());
|
initializerStatements.add(0, call.makeStmt());
|
||||||
|
|||||||
Reference in New Issue
Block a user