JS backend: don't create local var if open class is not referenced from another open class.

(cherry picked from commit fdf650f)
This commit is contained in:
develar
2013-08-14 14:56:41 +04:00
committed by Zalim Bashorov
parent 43db01d7ce
commit 39ccf418c0
@@ -107,6 +107,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
private final ClassDescriptor descriptor; private final ClassDescriptor descriptor;
private final JetClass declaration; private final JetClass declaration;
private final JsNameRef label; private final JsNameRef label;
private boolean referencedFromOpenClass = false;
private OpenClassInfo(JetClass declaration, ClassDescriptor descriptor, JsNameRef label) { private OpenClassInfo(JetClass declaration, ClassDescriptor descriptor, JsNameRef label) {
this.descriptor = descriptor; this.descriptor = descriptor;
@@ -129,7 +130,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
if (vars.isEmpty()) { if (vars.isEmpty()) {
if (!propertyInitializers.isEmpty()) { if (!propertyInitializers.isEmpty()) {
classesVar.setInitExpression(new JsObjectLiteral(propertyInitializers)); classesVar.setInitExpression(new JsObjectLiteral(propertyInitializers, true));
} }
return; return;
} }
@@ -156,6 +157,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
continue; continue;
} }
item.referencedFromOpenClass = true;
parents.add(item); parents.add(item);
} }
@@ -171,8 +173,17 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
OpenClassInfo item = it.next(); OpenClassInfo item = it.next();
JsExpression translatedDeclaration = JsExpression translatedDeclaration =
new ClassTranslator(item.declaration, item.descriptor, classDescriptorToLabel, context()).translate(); new ClassTranslator(item.declaration, item.descriptor, classDescriptorToLabel, context()).translate();
vars.add(new JsVar(item.label.getName(), translatedDeclaration));
propertyInitializers.add(new JsPropertyInitializer(item.label, item.label)); JsExpression value;
if (item.referencedFromOpenClass) {
vars.add(new JsVar(item.label.getName(), translatedDeclaration));
value = item.label;
}
else {
value = translatedDeclaration;
}
propertyInitializers.add(new JsPropertyInitializer(item.label, value));
} }
} }