KT-3008: when inlining functions from external modules, replace calls to functions in the external module via new mechanism instead of Kotlin.modules
This commit is contained in:
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.js.parser.parseFunction
|
|||||||
import org.jetbrains.kotlin.js.translate.context.Namer
|
import org.jetbrains.kotlin.js.translate.context.Namer
|
||||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
||||||
import org.jetbrains.kotlin.js.translate.reference.CallExpressionTranslator
|
import org.jetbrains.kotlin.js.translate.reference.CallExpressionTranslator
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
|
||||||
import org.jetbrains.kotlin.js.translate.utils.JsDescriptorUtils.getExternalModuleName
|
import org.jetbrains.kotlin.js.translate.utils.JsDescriptorUtils.getExternalModuleName
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||||
import org.jetbrains.kotlin.resolve.inline.InlineStrategy
|
import org.jetbrains.kotlin.resolve.inline.InlineStrategy
|
||||||
@@ -149,14 +150,18 @@ class FunctionReader(private val context: TranslationContext) {
|
|||||||
|
|
||||||
val function = parseFunction(source, offset, ThrowExceptionOnErrorReporter, JsRootScope(JsProgram("<inline>")))
|
val function = parseFunction(source, offset, ThrowExceptionOnErrorReporter, JsRootScope(JsProgram("<inline>")))
|
||||||
val moduleName = getExternalModuleName(descriptor)!!
|
val moduleName = getExternalModuleName(descriptor)!!
|
||||||
val moduleNameLiteral = context.program().getStringLiteral(moduleName)
|
val moduleReference = context.getModuleExpressionFor(descriptor) ?: getRootPackage()
|
||||||
val moduleReference = context.namer().getModuleReference(moduleNameLiteral)
|
|
||||||
|
|
||||||
val replacements = hashMapOf(moduleRootVariable[moduleName]!! to moduleReference,
|
val replacements = hashMapOf(moduleRootVariable[moduleName]!! to moduleReference,
|
||||||
moduleKotlinVariable[moduleName]!! to Namer.kotlinObject())
|
moduleKotlinVariable[moduleName]!! to Namer.kotlinObject())
|
||||||
replaceExternalNames(function, replacements)
|
replaceExternalNames(function, replacements)
|
||||||
return function
|
return function
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getRootPackage(): JsExpression {
|
||||||
|
val rootName = context.program().rootScope.declareName(Namer.getRootPackageName())
|
||||||
|
return JsAstUtils.fqnWithoutSideEffects(rootName, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val Char.isWhitespaceOrComma: Boolean
|
private val Char.isWhitespaceOrComma: Boolean
|
||||||
|
|||||||
@@ -545,10 +545,8 @@ public final class StaticContext {
|
|||||||
|
|
||||||
JsNameRef result = getQualifierForParentPackage(((PackageFragmentDescriptor) containingDescriptor).getFqName());
|
JsNameRef result = getQualifierForParentPackage(((PackageFragmentDescriptor) containingDescriptor).getFqName());
|
||||||
|
|
||||||
String moduleName = getExternalModuleName(descriptor);
|
JsExpression moduleExpression = getModuleExpressionFor(descriptor);
|
||||||
if (moduleName == null) {
|
if (moduleExpression == null) return result;
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (LibrarySourcesConfig.UNKNOWN_EXTERNAL_MODULE_NAME.equals(moduleName)) {
|
if (LibrarySourcesConfig.UNKNOWN_EXTERNAL_MODULE_NAME.equals(moduleName)) {
|
||||||
return null;
|
return null;
|
||||||
@@ -655,6 +653,23 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public JsExpression getModuleExpressionFor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
|
String moduleName = getExternalModuleName(descriptor);
|
||||||
|
if (moduleName == null) return null;
|
||||||
|
|
||||||
|
if (LibrarySourcesConfig.UNKNOWN_EXTERNAL_MODULE_NAME.equals(moduleName)) return null;
|
||||||
|
|
||||||
|
JsName moduleId = moduleName.equals(Namer.KOTLIN_LOWER_NAME) ? rootScope.declareName(Namer.KOTLIN_NAME) :
|
||||||
|
importedModules.get(moduleName);
|
||||||
|
if (moduleId == null) {
|
||||||
|
moduleId = rootScope.declareFreshName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(moduleName));
|
||||||
|
importedModules.put(moduleName, moduleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsAstUtils.fqnWithoutSideEffects(moduleId, null);
|
||||||
|
}
|
||||||
|
|
||||||
private static JsExpression applySideEffects(JsExpression expression, DeclarationDescriptor descriptor) {
|
private static JsExpression applySideEffects(JsExpression expression, DeclarationDescriptor descriptor) {
|
||||||
if (expression instanceof HasMetadata) {
|
if (expression instanceof HasMetadata) {
|
||||||
if (descriptor instanceof FunctionDescriptor ||
|
if (descriptor instanceof FunctionDescriptor ||
|
||||||
|
|||||||
@@ -544,4 +544,9 @@ public class TranslationContext {
|
|||||||
"reports true for given constructor: " + constructor);
|
"reports true for given constructor: " + constructor);
|
||||||
callSites.add(new DeferredCallSite(constructor, invocationArgs, this));
|
callSites.add(new DeferredCallSite(constructor, invocationArgs, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public JsExpression getModuleExpressionFor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
|
return staticContext.getModuleExpressionFor(descriptor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user