From 7a8ba9c7338bf7e109e8c8b4159c7adc5c62a9d1 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Thu, 2 Oct 2014 15:46:03 +0400 Subject: [PATCH] JS backend refactor: refactored LiteralFunctionTranslator after code review --- .../expression/LiteralFunctionTranslator.kt | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/expression/LiteralFunctionTranslator.kt b/js/js.translator/src/org/jetbrains/k2js/translate/expression/LiteralFunctionTranslator.kt index c5c748de0a2..dd619123dc4 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/expression/LiteralFunctionTranslator.kt +++ b/js/js.translator/src/org/jetbrains/k2js/translate/expression/LiteralFunctionTranslator.kt @@ -128,8 +128,7 @@ private fun moveCapturedLocalInside(capturingFunction: JsFunction, capturedName: when (localFunAlias) { is JsNameRef -> { /** Local inline function does not capture anything, so just move alias inside */ - capturedName.setStaticRef(localFunAlias) - capturingFunction.getInnerFunction()?.addDeclaration(capturedName, localFunAlias) + declareAliasInsideFunction(capturingFunction, capturedName, localFunAlias) CapturedArgsParams() } is JsInvocation -> @@ -157,28 +156,35 @@ private fun moveCapturedLocalInside(capturingFunction: JsFunction, capturedName: val capturedArgs = localFunAlias.getArguments() val scope = capturingFunction.getInnerFunction()?.getScope()!! - val names = capturedArgs.map {(it as JsNameRef).getName()} - val freshNames = getFreshNamesInScope(scope, names) + val freshNames = getFreshNamesInScope(scope, capturedArgs) val aliasCallArguments = freshNames.map { it.makeRef() } val alias = JsInvocation(localFunAlias.getQualifier(), aliasCallArguments) - - capturedName.setStaticRef(alias) - capturingFunction.getInnerFunction()?.addDeclaration(capturedName, alias) + declareAliasInsideFunction(capturingFunction, capturedName, alias) val capturedParameters = freshNames.map {JsParameter(it)} return CapturedArgsParams(capturedArgs, capturedParameters) } -private fun getFreshNamesInScope(scope: JsScope, suggested: List): List { - val suggestedNames = suggested.stream().filterNotNull() - val suggestedIdents = suggestedNames.map { it.getIdent() } - val freshNames = suggestedIdents.map { scope.declareFreshName(it) } - - return freshNames.toList() +private fun declareAliasInsideFunction(function: JsFunction, name: JsName, alias: JsExpression) { + name.setStaticRef(alias) + function.getInnerFunction()?.addDeclaration(name, alias) } +private fun getFreshNamesInScope(scope: JsScope, suggested: List): List { + val freshNames = arrayListOf() + for (suggestion in suggested) { + if (suggestion !is JsNameRef) { + throw AssertionError("Expected suggestion to be JsNameRef") + } + + val ident = suggestion.getIdent() + val name = scope.declareFreshName(ident) + freshNames.add(name) + } + + return freshNames } private fun JsFunction.addDeclaration(name: JsName, value: JsExpression?) { @@ -194,4 +200,4 @@ private fun isLocalInlineDeclaration(descriptor: CallableDescriptor): Boolean { return descriptor is FunctionDescriptor && descriptor.getVisibility() == Visibilities.LOCAL && InlineUtil.getInlineType(descriptor).isInline() -} \ No newline at end of file +}