From a414cd64c5056cc512672c67a8193d8ecbf76811 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 10 Feb 2017 19:02:07 +0300 Subject: [PATCH] JS: support module imports in new pipeline --- .../js/translate/context/StaticContext.java | 30 +++++++++---------- .../translate/context/TranslationContext.java | 5 ---- .../kotlin/js/translate/general/Merger.kt | 20 +++++++++++-- .../js/translate/general/Translation.java | 7 ++--- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index 7b103129427..740dc18eed1 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -121,8 +121,6 @@ public final class StaticContext { @NotNull private final Map importedModules = new LinkedHashMap<>(); - private Collection readOnlyImportedModules; - @NotNull private final JsScope rootPackageScope; @@ -153,8 +151,7 @@ public final class StaticContext { rootPackageScope = new JsObjectScope(rootScope, ""); JsName kotlinName = rootScope.declareName(Namer.KOTLIN_NAME); - importedModules.put(new JsImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null), - new JsImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName, null)); + createImportedModule(new JsImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null), Namer.KOTLIN_LOWER_NAME, kotlinName, null); classModelGenerator = new ClassModelGenerator(this); } @@ -189,14 +186,6 @@ public final class StaticContext { return namer; } - @NotNull - public Collection getImportedModules() { - if (readOnlyImportedModules == null) { - readOnlyImportedModules = Collections.unmodifiableCollection(importedModules.values()); - } - return readOnlyImportedModules; - } - @NotNull public JsScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) { if (descriptor instanceof ModuleDescriptor) { @@ -526,7 +515,12 @@ public final class StaticContext { public ObjectInstanceNameGenerator() { addRule(descriptor -> { String suggested = getSuggestedName(descriptor) + Namer.OBJECT_INSTANCE_FUNCTION_SUFFIX; - return localOrImportedName(descriptor, suggested); + JsName result = JsScope.declareTemporaryName(suggested); + String tag = SignatureUtilsKt.generateSignature(descriptor); + if (tag != null) { + fragment.getNameBindings().add(new JsNameBinding("object:" + tag, result)); + } + return result; }); } } @@ -681,12 +675,18 @@ public final class StaticContext { JsImportedModule module = importedModules.get(key); if (module == null) { JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName)); - module = new JsImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null); - importedModules.put(key, module); + module = createImportedModule(key, baseName, internalName, plainName != null ? pureFqn(plainName, null) : null); } return module; } + private JsImportedModule createImportedModule(JsImportedModuleKey key, String baseName, JsName internalName, JsExpression plainName) { + JsImportedModule module = new JsImportedModule(baseName, internalName, plainName); + importedModules.put(key, module); + fragment.getImportedModules().add(module); + return module; + } + @NotNull private String getPlainId(@NotNull DeclarationDescriptor declaration) { SuggestedName suggestedName = nameSuggestion.suggest(declaration); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java index 437507eadaf..6db6177d11d 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/TranslationContext.java @@ -125,11 +125,6 @@ public class TranslationContext { return null; } - @NotNull - public Collection getImportedModules() { - return staticContext.getImportedModules(); - } - @Nullable public UsageTracker usageTracker() { return usageTracker; diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Merger.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Merger.kt index f7b75a6304c..062780e4c4c 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Merger.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Merger.kt @@ -18,19 +18,20 @@ package org.jetbrains.kotlin.js.translate.general import org.jetbrains.kotlin.js.backend.ast.* import org.jetbrains.kotlin.js.backend.ast.metadata.coroutineMetadata -import org.jetbrains.kotlin.js.inline.clean.resolveTemporaryNames import org.jetbrains.kotlin.js.translate.context.Namer import org.jetbrains.kotlin.js.translate.utils.JsAstUtils class Merger(private val rootFunction: JsFunction, val internalModuleName: JsName) { // Maps unique signature (see generateSignature) to names private val nameTable = mutableMapOf() + private val importedModuleTable = mutableMapOf() private val importBlock = JsGlobalBlock() private val declarationBlock = JsGlobalBlock() private val initializerBlock = JsGlobalBlock() private val exportBlock = JsGlobalBlock() private val declaredImports = mutableSetOf() private val classes = mutableMapOf() + private val importedModulesImpl = mutableListOf() // Add declaration and initialization statements from program fragment to resulting single program fun addFragment(fragment: JsProgramFragment) { @@ -49,6 +50,9 @@ class Merger(private val rootFunction: JsFunction, val internalModuleName: JsNam exportBlock.statements += fragment.exportBlock } + val importedModules: List + get() = importedModulesImpl + private fun Map.rename(name: JsName): JsName = getOrElse(name) { name } // Builds mapping to map names from different fragments into single name when they denote single declaration @@ -61,6 +65,19 @@ class Merger(private val rootFunction: JsFunction, val internalModuleName: JsNam } fragment.scope.findName(Namer.getRootPackageName())?.let { nameMap[it] = internalModuleName } + for (importedModule in fragment.importedModules) { + nameMap[importedModule.internalName] = importedModuleTable.getOrPut(importedModule.key) { + val scope = rootFunction.scope + val newName = importedModule.internalName.let { + if (it.isTemporary) scope.declareTemporaryName(it.ident) else scope.declareName(it.ident) + } + newName.also { + importedModulesImpl += JsImportedModule(importedModule.externalName, it, importedModule.plainReference) + it.copyMetadataFrom(importedModule.internalName) + } + } + } + return nameMap } @@ -99,7 +116,6 @@ class Merger(private val rootFunction: JsFunction, val internalModuleName: JsNam this += declarationBlock.statements this += initializerBlock.statements } - rootFunction.body.resolveTemporaryNames() } private fun addClassPrototypes(statements: MutableList) { diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java index e3f44cb22f3..5757ef7aadc 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java @@ -292,14 +292,13 @@ public final class Translation { // Invoke function passing modules as arguments // This should help minifier tool to recognize references to these modules as local variables and make them shorter. - List importedModuleList = new ArrayList<>(); + List importedModuleList = merger.getImportedModules(); - /* - for (JsImportedModule importedModule : staticContext.getImportedModules()) { + for (JsImportedModule importedModule : importedModuleList) { rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName())); - importedModuleList.add(importedModule); } + /* if (mainCallParameters.shouldBeGenerated()) { JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments()); if (statement != null) {