JS backend: fix translation for extension property

This commit is contained in:
Michael Nedzelsky
2014-12-03 16:12:53 +03:00
parent 5bf1d73199
commit 7c9ea16d8e
3 changed files with 21 additions and 10 deletions
@@ -45,6 +45,8 @@ import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind.*
import com.google.dart.compiler.backend.js.ast.JsLiteral import com.google.dart.compiler.backend.js.ast.JsLiteral
import org.jetbrains.k2js.translate.utils.TranslationUtils import org.jetbrains.k2js.translate.utils.TranslationUtils
import org.jetbrains.k2js.translate.context.TranslationContext import org.jetbrains.k2js.translate.context.TranslationContext
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor
import org.jetbrains.jet.lang.resolve.descriptorUtil.isExtension
val CallInfo.callableDescriptor: CallableDescriptor val CallInfo.callableDescriptor: CallableDescriptor
@@ -58,7 +60,7 @@ fun CallInfo.isNative(): Boolean = AnnotationsUtils.isNativeObject(callableDescr
fun CallInfo.isSuperInvocation(): Boolean { fun CallInfo.isSuperInvocation(): Boolean {
val dispatchReceiver = resolvedCall.getDispatchReceiver() val dispatchReceiver = resolvedCall.getDispatchReceiver()
return dispatchReceiver is ExpressionReceiver && ((dispatchReceiver as ExpressionReceiver)).getExpression() is JetSuperExpression return dispatchReceiver is ExpressionReceiver && dispatchReceiver.getExpression() is JetSuperExpression
} }
val VariableAccessInfo.variableDescriptor: VariableDescriptor val VariableAccessInfo.variableDescriptor: VariableDescriptor
@@ -69,7 +71,16 @@ val VariableAccessInfo.variableName: JsName
fun VariableAccessInfo.isGetAccess(): Boolean = value == null fun VariableAccessInfo.isGetAccess(): Boolean = value == null
fun VariableAccessInfo.getAccessFunctionName(): String = Namer.getNameForAccessor(variableName.getIdent()!!, isGetAccess(), false) fun VariableAccessInfo.getAccessFunctionName(): String {
val descriptor = variableDescriptor
if (descriptor is PropertyDescriptor && descriptor.isExtension) {
val propertyAccessorDescriptor = if (isGetAccess()) descriptor.getGetter() else descriptor.getSetter()
return context.getNameForDescriptor(propertyAccessorDescriptor).getIdent()
}
else {
return Namer.getNameForAccessor(variableName.getIdent()!!, isGetAccess(), false)
}
}
fun VariableAccessInfo.constructAccessExpression(ref: JsExpression): JsExpression { fun VariableAccessInfo.constructAccessExpression(ref: JsExpression): JsExpression {
if (isGetAccess()) { if (isGetAccess()) {
@@ -290,14 +290,11 @@ public final class StaticContext {
} }
return declarePropertyOrPropertyAccessorName(descriptor, propertyName, false); return declarePropertyOrPropertyAccessorName(descriptor, propertyName, false);
} else { } else {
if (descriptor instanceof PropertyDescriptor) { assert !(descriptor instanceof PropertyDescriptor) : "descriptor should not be instance of PropertyDescriptor: " + descriptor;
return declarePropertyOrPropertyAccessorName(descriptor, propertyName, false);
} else { boolean isGetter = descriptor instanceof PropertyGetterDescriptor;
String propertyJsName = getNameForDescriptor(propertyDescriptor).getIdent(); String accessorName = Namer.getNameForAccessor(propertyName, isGetter, false);
boolean isGetter = descriptor instanceof PropertyGetterDescriptor; return declarePropertyOrPropertyAccessorName(descriptor, accessorName, false);
String accessorName = Namer.getNameForAccessor(propertyJsName, isGetter, false);
return declarePropertyOrPropertyAccessorName(descriptor, accessorName, false);
}
} }
} }
}; };
@@ -33,6 +33,7 @@ import org.jetbrains.k2js.translate.utils.ast.*
import org.jetbrains.k2js.translate.utils.TranslationUtils.* import org.jetbrains.k2js.translate.utils.TranslationUtils.*
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isExtension import org.jetbrains.jet.lang.resolve.DescriptorUtils.isExtension
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall
import org.jetbrains.jet.lang.resolve.descriptorUtil.isExtension
/** /**
* Translates single property /w accessors. * Translates single property /w accessors.
@@ -120,6 +121,7 @@ private class PropertyTranslator(
return generateDelegatedGetterFunction(getterDescriptor, delegatedCall) return generateDelegatedGetterFunction(getterDescriptor, delegatedCall)
} }
assert(!descriptor.isExtension, "Unexpected extension property $descriptor}")
val scope = context().getScopeForDescriptor(getterDescriptor.getContainingDeclaration()) val scope = context().getScopeForDescriptor(getterDescriptor.getContainingDeclaration())
val result = backingFieldReference(context(), descriptor) val result = backingFieldReference(context(), descriptor)
val body = JsBlock(JsReturn(result)) val body = JsBlock(JsReturn(result))
@@ -172,6 +174,7 @@ private class PropertyTranslator(
(delegatedJsCall as JsInvocation).getArguments().set(0, receiver.makeRef()) (delegatedJsCall as JsInvocation).getArguments().set(0, receiver.makeRef())
} }
} else { } else {
assert(!descriptor.isExtension, "Unexpected extension property $descriptor}")
val assignment = assignmentToBackingField(withAliased, descriptor, valueParameter.makeRef()) val assignment = assignmentToBackingField(withAliased, descriptor, valueParameter.makeRef())
function.addStatement(assignment.makeStmt()) function.addStatement(assignment.makeStmt())
} }