diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfo.kt b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfo.kt index fef2a7e51dc..ab611209576 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfo.kt +++ b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfo.kt @@ -78,58 +78,6 @@ fun TranslationContext.getCallInfo(resolvedCall: ResolvedCall receiverObject - else -> thisObject - } - return CallType.SAFE.constructCall(nullableReceiverForSafeCall, object : CallType.CallConstructor { - override fun construct(receiver: JsExpression?): JsExpression { - return result - } - }, context) -} - -val VariableAccessInfo.variableDescriptor: VariableDescriptor - get() = callableDescriptor as VariableDescriptor - -val VariableAccessInfo.variableName: JsName - get() = context.getNameForDescriptor(variableDescriptor) - -fun VariableAccessInfo.isGetAccess(): Boolean = value == null - -fun VariableAccessInfo.getAccessFunctionName(): String = Namer.getNameForAccessor(variableName.getIdent()!!, isGetAccess(), false) - -fun VariableAccessInfo.constructAccessExpression(ref: JsNameRef): JsExpression { - if (isGetAccess()) { - return ref - } else { - return JsAstUtils.assignment(ref, value!!) - } -} - -val FunctionCallInfo.functionName: JsName - get() = context.getNameForDescriptor(callableDescriptor) - -fun FunctionCallInfo.hasSpreadOperator(): Boolean = argumentsInfo.isHasSpreadOperator() - private fun TranslationContext.getThisObject(receiverValue: ReceiverValue): JsExpression { assert(receiverValue.exists(), "receiverValue must be exist here") return getThisObject(getDeclarationDescriptorForReceiver(receiverValue)) diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfoExtensions.kt b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfoExtensions.kt new file mode 100644 index 00000000000..33249f13b4c --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/reference/CallInfoExtensions.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.k2js.translate.reference + +import org.jetbrains.jet.lang.descriptors.CallableDescriptor +import org.jetbrains.k2js.translate.utils.AnnotationsUtils +import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver +import org.jetbrains.jet.lang.psi.JetSuperExpression +import com.google.dart.compiler.backend.js.ast.JsExpression +import org.jetbrains.jet.lang.descriptors.VariableDescriptor +import com.google.dart.compiler.backend.js.ast.JsName +import org.jetbrains.k2js.translate.context.Namer +import com.google.dart.compiler.backend.js.ast.JsNameRef +import org.jetbrains.k2js.translate.utils.JsAstUtils +import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind.* + + +val CallInfo.callableDescriptor: CallableDescriptor + get() = resolvedCall.getResultingDescriptor().getOriginal() + +fun CallInfo.isExtension(): Boolean = receiverObject != null + +fun CallInfo.isMemberCall(): Boolean = thisObject != null + +fun CallInfo.isNative(): Boolean = AnnotationsUtils.isNativeObject(callableDescriptor) + +fun CallInfo.isSuperInvocation(): Boolean { + val thisObject = resolvedCall.getThisObject() + return thisObject is ExpressionReceiver && ((thisObject as ExpressionReceiver)).getExpression() is JetSuperExpression +} + +fun CallInfo.constructSafeCallIsNeeded(result: JsExpression): JsExpression { + if (!resolvedCall.isSafeCall()) + return result + + val nullableReceiverForSafeCall = when (resolvedCall.getExplicitReceiverKind()) { + BOTH_RECEIVERS, RECEIVER_ARGUMENT -> receiverObject + else -> thisObject + } + return CallType.SAFE.constructCall(nullableReceiverForSafeCall, object : CallType.CallConstructor { + override fun construct(receiver: JsExpression?): JsExpression { + return result + } + }, context) +} + +val VariableAccessInfo.variableDescriptor: VariableDescriptor + get() = callableDescriptor as VariableDescriptor + +val VariableAccessInfo.variableName: JsName + get() = context.getNameForDescriptor(variableDescriptor) + +fun VariableAccessInfo.isGetAccess(): Boolean = value == null + +fun VariableAccessInfo.getAccessFunctionName(): String = Namer.getNameForAccessor(variableName.getIdent()!!, isGetAccess(), false) + +fun VariableAccessInfo.constructAccessExpression(ref: JsNameRef): JsExpression { + if (isGetAccess()) { + return ref + } else { + return JsAstUtils.assignment(ref, value!!) + } +} + +val FunctionCallInfo.functionName: JsName + get() = context.getNameForDescriptor(callableDescriptor) + +fun FunctionCallInfo.hasSpreadOperator(): Boolean = argumentsInfo.isHasSpreadOperator() \ No newline at end of file