KT-11030 Repair support of enums. Minor refactoring of ClassTranslator
This commit is contained in:
+1
-8
@@ -62,9 +62,6 @@ class ClassTranslator private constructor(
|
||||
|
||||
private val descriptor = getClassDescriptor(context.bindingContext(), classDeclaration)
|
||||
|
||||
// TODO: eliminate
|
||||
private fun translateObjectLiteralExpression() = translate(context())
|
||||
|
||||
private fun translate(declarationContext: TranslationContext = context()): JsInvocation {
|
||||
return JsInvocation(context().namer().classCreateInvocation(descriptor), getClassCreateInvocationArguments(declarationContext))
|
||||
}
|
||||
@@ -103,7 +100,7 @@ class ClassTranslator private constructor(
|
||||
}
|
||||
|
||||
if (isEnumClass(descriptor)) {
|
||||
val enumEntries = JsObjectLiteral(bodyVisitor.getEnumEntryList(), true)
|
||||
val enumEntries = JsObjectLiteral(bodyVisitor.enumEntryList, true)
|
||||
val function = simpleReturnFunction(context.getScopeForDescriptor(descriptor), enumEntries)
|
||||
invocationArguments.add(function)
|
||||
}
|
||||
@@ -295,10 +292,6 @@ class ClassTranslator private constructor(
|
||||
return ClassTranslator(objectDeclaration, context).translateObjectInsideClass(context)
|
||||
}
|
||||
|
||||
@JvmStatic fun generateObjectDeclaration(objectDeclaration: KtObjectDeclaration, context: TranslationContext): JsExpression {
|
||||
return ClassTranslator(objectDeclaration, context).translateObjectLiteralExpression()
|
||||
}
|
||||
|
||||
private fun generateSecondaryConstructor(constructor: KtSecondaryConstructor, context: TranslationContext): JsPropertyInitializer {
|
||||
val constructorDescriptor = BindingUtils.getDescriptorForElement(context.bindingContext(), constructor) as ConstructorDescriptor
|
||||
val classDescriptor = constructorDescriptor.containingDeclaration
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visitObjectDeclaration(@NotNull KtObjectDeclaration declaration, TranslationContext context) {
|
||||
JsExpression object = ClassTranslator.generateObjectDeclaration(declaration, context);
|
||||
JsExpression object = ClassTranslator.generateClassCreation(declaration, context);
|
||||
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(context.bindingContext(), declaration);
|
||||
JsName objName = context.getNameForDescriptor(descriptor);
|
||||
staticResult.add(new JsPropertyInitializer(objName.makeRef(), object));
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ class FileDeclarationVisitor(
|
||||
context!!
|
||||
|
||||
// TODO: avoid duplication with superclass
|
||||
val obj = ClassTranslator.generateObjectDeclaration(declaration, context)
|
||||
val obj = ClassTranslator.generateClassCreation(declaration, context)
|
||||
val descriptor = BindingUtils.getClassDescriptor(context.bindingContext(), declaration)
|
||||
val objName = context.getNameForDescriptor(descriptor)
|
||||
result.add(JsPropertyInitializer(objName.makeRef(), obj))
|
||||
|
||||
+28
-5
@@ -257,16 +257,39 @@ var Kotlin = {};
|
||||
for (var entryName in enumEntryList) {
|
||||
if (enumEntryList.hasOwnProperty(entryName)) {
|
||||
var entryObject = enumEntryList[entryName];
|
||||
values.push(entryObject);
|
||||
entryObject.ordinal$ = i++;
|
||||
entryObject.name$ = entryName;
|
||||
cls[entryName] = entryObject;
|
||||
values.push(entryName);
|
||||
if (typeof entryObject === 'function' && entryObject.type === Kotlin.TYPE.INIT_FUN) {
|
||||
entryObject.className = entryName;
|
||||
Object.defineProperty(cls, entryName, {
|
||||
get: function(ordinal, name, obj) {
|
||||
return function() {
|
||||
var result = obj.apply(this);
|
||||
result.ordinal$ = ordinal;
|
||||
result.name$ = name;
|
||||
return result;
|
||||
}
|
||||
}(i++, entryName, entryObject),
|
||||
configurable: true
|
||||
})
|
||||
}
|
||||
else {
|
||||
entryObject.ordinal$ = i++;
|
||||
entryObject.name$ = entryName;
|
||||
cls[entryName] = entryObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
enumEntryList.values$ = values;
|
||||
cls.valuesNames$ = values;
|
||||
cls.values$ = null;
|
||||
};
|
||||
|
||||
staticProperties.values = function () {
|
||||
if (this.values$ == null) {
|
||||
this.values$ = [];
|
||||
for (var i = 0; i < this.valuesNames$.length; ++i) {
|
||||
this.values$.push(this[this.valuesNames$[i]])
|
||||
}
|
||||
}
|
||||
return this.values$;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user