JS backend: support object declaration in other declarations.
#KT-2696 fixed (cherry picked from commits 8931d13, 627b100 and f6b8686)
This commit is contained in:
@@ -41,12 +41,10 @@ public final class ObjectTest extends SingleFileTranslationTest {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
//TODO: KT-2696
|
||||
public void TODO_testObjectInObject() throws Exception {
|
||||
public void testObjectInObject() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
|
||||
public void testObjectInheritingFromATrait() throws Exception {
|
||||
fooBoxTest();
|
||||
}
|
||||
|
||||
+28
-18
@@ -26,7 +26,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassKind;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetClassOrObject;
|
||||
import org.jetbrains.jet.lang.psi.JetObjectLiteralExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
|
||||
import org.jetbrains.jet.lang.psi.JetParameter;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.k2js.translate.LabelGenerator;
|
||||
@@ -41,7 +41,8 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*;
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getClassDescriptorForType;
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isNotAny;
|
||||
import static org.jetbrains.k2js.translate.expression.LiteralFunctionTranslator.createPlace;
|
||||
import static org.jetbrains.k2js.translate.initializer.InitializerUtils.createPropertyInitializer;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor;
|
||||
@@ -65,26 +66,39 @@ public final class ClassTranslator extends AbstractTranslator {
|
||||
private final ClassAliasingMap aliasingMap;
|
||||
|
||||
@NotNull
|
||||
public static JsExpression generateClassCreation(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
|
||||
return new ClassTranslator(classDeclaration, null, context).translate(context);
|
||||
public static JsInvocation generateClassCreation(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
|
||||
return new ClassTranslator(classDeclaration, null, context).translate();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsExpression generateClassCreation(@NotNull JetClassOrObject classDeclaration,
|
||||
public static JsInvocation generateClassCreation(@NotNull JetClassOrObject classDeclaration,
|
||||
@NotNull ClassDescriptor descriptor,
|
||||
@NotNull TranslationContext context) {
|
||||
return new ClassTranslator(classDeclaration, descriptor, null, context).translate(context);
|
||||
return new ClassTranslator(classDeclaration, descriptor, null, context).translate();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsExpression generateObjectLiteral(@NotNull JetObjectLiteralExpression objectLiteralExpression,
|
||||
@NotNull TranslationContext context) {
|
||||
return new ClassTranslator(objectLiteralExpression.getObjectDeclaration(), null, context).translateObjectLiteralExpression();
|
||||
public static JsExpression generateObjectLiteral(
|
||||
@NotNull JetObjectDeclaration objectDeclaration,
|
||||
@NotNull TranslationContext context
|
||||
) {
|
||||
return new ClassTranslator(objectDeclaration, null, context).translateObjectLiteralExpression();
|
||||
}
|
||||
|
||||
ClassTranslator(@NotNull JetClassOrObject classDeclaration,
|
||||
@NotNull
|
||||
public static JsExpression generateObjectLiteral(
|
||||
@NotNull JetObjectDeclaration objectDeclaration,
|
||||
@NotNull ClassDescriptor descriptor,
|
||||
@NotNull TranslationContext context
|
||||
) {
|
||||
return new ClassTranslator(objectDeclaration, descriptor, null, context).translateObjectLiteralExpression();
|
||||
}
|
||||
|
||||
ClassTranslator(
|
||||
@NotNull JetClassOrObject classDeclaration,
|
||||
@Nullable ClassAliasingMap aliasingMap,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context
|
||||
) {
|
||||
this(classDeclaration, getClassDescriptor(context.bindingContext(), classDeclaration), aliasingMap, context);
|
||||
}
|
||||
|
||||
@@ -108,21 +122,17 @@ public final class ClassTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression translate() {
|
||||
public JsInvocation translate() {
|
||||
return translate(context());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression translate(@NotNull TranslationContext declarationContext) {
|
||||
public JsInvocation translate(@NotNull TranslationContext declarationContext) {
|
||||
JsInvocation createInvocation = context().namer().classCreateInvocation(descriptor);
|
||||
translate(createInvocation, declarationContext);
|
||||
return createInvocation;
|
||||
}
|
||||
|
||||
public void translate(@NotNull JsInvocation createInvocation) {
|
||||
translate(createInvocation, context());
|
||||
}
|
||||
|
||||
private void translate(@NotNull JsInvocation createInvocation, @NotNull TranslationContext context) {
|
||||
addSuperclassReferences(createInvocation);
|
||||
addClassOwnDeclarations(createInvocation.getArguments(), context);
|
||||
@@ -284,7 +294,7 @@ public final class ClassTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private JsExpression getClassReference(@NotNull ClassDescriptor superClassDescriptor) {
|
||||
private JsNameRef getClassReference(@NotNull ClassDescriptor superClassDescriptor) {
|
||||
// aliasing here is needed for the declaration generation step
|
||||
if (aliasingMap != null) {
|
||||
JsNameRef name = aliasingMap.get(superClassDescriptor, descriptor);
|
||||
|
||||
+6
@@ -106,6 +106,12 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, @NotNull TranslationContext context) {
|
||||
// parsed it in initializer visitor => no additional actions are needed
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitNamedFunction(@NotNull JetNamedFunction expression, @NotNull TranslationContext context) {
|
||||
FunctionDescriptor descriptor = getFunctionDescriptor(context.bindingContext(), expression);
|
||||
|
||||
+1
-1
@@ -191,7 +191,7 @@ final class NamespaceTranslator extends AbstractTranslator {
|
||||
|
||||
@Override
|
||||
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, @NotNull TranslationContext context) {
|
||||
InitializerUtils.generate(declaration, initializerStatements, result, context);
|
||||
InitializerUtils.generate(declaration, initializerStatements, context);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -414,7 +414,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
@NotNull
|
||||
public JsNode visitObjectLiteralExpression(@NotNull JetObjectLiteralExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
return ClassTranslator.generateObjectLiteral(expression, context);
|
||||
return ClassTranslator.generateObjectLiteral(expression.getObjectDeclaration(), context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-10
@@ -65,18 +65,14 @@ public final class InitializerUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void generate(@NotNull JetObjectDeclaration declaration,
|
||||
public static void generate(
|
||||
@NotNull JetObjectDeclaration declaration,
|
||||
@NotNull List<JsStatement> initializers,
|
||||
@Nullable List<JsPropertyInitializer> definitions,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context
|
||||
) {
|
||||
ClassDescriptor descriptor = getClassDescriptor(context.bindingContext(), declaration);
|
||||
JsExpression value = ClassTranslator.generateClassCreation(declaration, descriptor, context);
|
||||
if (definitions != null && value instanceof JsLiteral) {
|
||||
definitions.add(createPropertyInitializer(descriptor, value, context));
|
||||
}
|
||||
else {
|
||||
initializers.add(create(descriptor, value, context));
|
||||
}
|
||||
JsExpression value = ClassTranslator.generateObjectLiteral(declaration, descriptor, context);
|
||||
initializers.add(create(descriptor, value, context));
|
||||
}
|
||||
|
||||
public static JsStatement create(Named named, JsExpression value, TranslationContext context) {
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ public final class InitializerVisitor extends TranslatorVisitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visitObjectDeclaration(@NotNull JetObjectDeclaration declaration, @NotNull TranslationContext context) {
|
||||
InitializerUtils.generate(declaration, result, null, context);
|
||||
InitializerUtils.generate(declaration, result, context);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,13 @@ object B {
|
||||
}
|
||||
|
||||
class C {
|
||||
fun ov() = "d"
|
||||
class object {
|
||||
fun ov() = "d"
|
||||
}
|
||||
object query {val status = "complete" + ov()}
|
||||
}
|
||||
|
||||
fun box() = A.query.status == "complete" && B.query.status == "completed" && C().query.status == "completed"
|
||||
fun box() = A.query.status == "complete" && B.query.status == "completed"
|
||||
// todo fix after KT-3868 will be fixed
|
||||
// && C.query.status == "completed"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user