diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/NameTables.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/NameTables.kt index 09fb29e1ce7..4c7b1370dbb 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/NameTables.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/NameTables.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -26,24 +26,27 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty import java.util.* -fun mapToKey(declaration: T): String { +// TODO remove direct usages of [mapToKey] from [NameTable] & co and move it to scripting & REPL infrastructure. Review usages. +private fun mapToKey(declaration: T): String { return with(JsManglerIr) { if (declaration is IrDeclaration && isPublic(declaration)) { declaration.hashedMangle.toString() } else if (declaration is Signature) { declaration.toString().hashMangle.toString() - } else "key_have_not_generated" + } else { + error("Key is not generated for " + declaration?.let { it::class.simpleName }) + } } } -fun JsManglerIr.isPublic(declaration: IrDeclaration) = +private fun JsManglerIr.isPublic(declaration: IrDeclaration) = declaration.isExported() && declaration !is IrScript && declaration !is IrVariable && declaration !is IrValueParameter class NameTable( val parent: NameTable<*>? = null, val reserved: MutableSet = mutableSetOf(), val sanitizer: (String) -> String = ::sanitizeName, - val mappedNames: MutableMap = mutableMapOf() + val mappedNames: MutableMap? = null ) { var finished = false val names = mutableMapOf() @@ -59,7 +62,7 @@ class NameTable( assert(!finished) names[declaration] = name reserved.add(name) - mappedNames[mapToKey(declaration)] = name + mappedNames?.set(mapToKey(declaration), name) } fun declareFreshName(declaration: T, suggestedName: String): String { @@ -151,7 +154,7 @@ class NameTables( packages: List, reservedForGlobal: MutableSet = mutableSetOf(), reservedForMember: MutableSet = mutableSetOf(), - val mappedNames: MutableMap = mutableMapOf() + val mappedNames: MutableMap? = null ) { val globalNames: NameTable private val memberNames: NameTable @@ -172,8 +175,6 @@ class NameTables( mappedNames = mappedNames ) - mappedNames.addAllIfAbsent(mappedNames) - val classDeclaration = mutableListOf() for (p in packages) { for (declaration in p.declarations) { @@ -249,7 +250,7 @@ class NameTables( this += other.filter { it.key !in this } } - private fun packagesAdded() = mappedNames.isEmpty() + private fun packagesAdded() = mappedNames.isNullOrEmpty() fun merge(files: List, additionalPackages: List) { val packages = mutableListOf().also { it.addAll(files) } @@ -311,13 +312,17 @@ class NameTables( parent = parent.parent } - return mappedNames[mapToKey(declaration)] - ?: error("Can't find name for declaration ${declaration.render()}") + + mappedNames?.get(mapToKey(declaration))?.let { + return it + } + + error("Can't find name for declaration ${declaration.render()}") } fun getNameForMemberField(field: IrField): String { val signature = fieldSignature(field) - val name = memberNames.names[signature] ?: mappedNames[mapToKey(signature)] + val name = memberNames.names[signature] ?: mappedNames?.get(mapToKey(signature)) // TODO investigate if (name == null) { @@ -329,7 +334,7 @@ class NameTables( fun getNameForMemberFunction(function: IrSimpleFunction): String { val signature = jsFunctionSignature(function) - val name = memberNames.names[signature] ?: mappedNames[mapToKey(signature)] + val name = memberNames.names[signature] ?: mappedNames?.get(mapToKey(signature)) // TODO Add a compiler flag, which enables this behaviour // TODO remove in DCE diff --git a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstBinaries.kt b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstBinaries.kt index 2ebaad90d8c..3c90fdc4ce2 100644 --- a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstBinaries.kt +++ b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstBinaries.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -23,7 +23,7 @@ class JsReplTestAgainstBinaries : AbstractJsReplTest() { private val dependencies = readLibrariesFromConfiguration(environment.configuration) init { - val nameTable = NameTables(emptyList()) + val nameTable = NameTables(emptyList(), mappedNames = mutableMapOf()) val compiler = JsScriptDependencyCompiler(environment.configuration, nameTable, createSymbolTable()) val runtimeBinary = compiler.compile(dependencies) diff --git a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstKlib.kt b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstKlib.kt index 6d0239c18c4..6c33f486da7 100644 --- a/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstKlib.kt +++ b/libraries/scripting/js-test/test/org/jetbrains/kotlin/scripting/repl/js/test/JsReplTestAgainstKlib.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -21,7 +21,7 @@ class JsReplTestAgainstKlib : AbstractJsReplTest() { private var dependencyCode: String? = null override fun createCompilationState(): JsReplCompilationState { - val nameTables = NameTables(emptyList()) + val nameTables = NameTables(emptyList(), mappedNames = mutableMapOf()) val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl) val dependencyCompiler = JsScriptDependencyCompiler(environment.configuration, nameTables, symbolTable) val dependencies = readLibrariesFromConfiguration(environment.configuration) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJsCompilerImpls.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJsCompilerImpls.kt index 87a021b7a70..b0fe99601aa 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJsCompilerImpls.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJsCompilerImpls.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.scripting.repl.js.readLibrariesFromConfiguration import kotlin.script.experimental.api.* class JsScriptCompilerWithDependenciesProxy(private val environment: KotlinCoreEnvironment) : ScriptCompilerProxy { - private val nameTables = NameTables(emptyList()) + private val nameTables = NameTables(emptyList(), mappedNames = mutableMapOf()) private val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl) private val dependencies: List = readLibrariesFromConfiguration(environment.configuration) private val compiler = JsCoreScriptingCompiler(environment, nameTables, symbolTable, dependencies) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplCompiler.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplCompiler.kt index 49553c54c31..ea537e441af 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplCompiler.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplCompiler.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -21,7 +21,7 @@ class JsReplCompiler(private val environment: KotlinCoreEnvironment) : ReplCompi override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> { return JsReplCompilationState( lock, - NameTables(emptyList()), + NameTables(emptyList(), mappedNames = mutableMapOf()), readLibrariesFromConfiguration(environment.configuration), ReplCodeAnalyzerBase.ResettableAnalyzerState(), SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplUtils.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplUtils.kt index d9da0c9b29a..fb73899385c 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplUtils.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/repl/js/JsReplUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -172,7 +172,7 @@ class DependencyLoader { fun writeNames(nameTables: NameTables): ByteArray { val result = StringBuilder() - for (entry in nameTables.mappedNames) { + for (entry in nameTables.mappedNames.orEmpty()) { result.append("${entry.key} ${entry.value}" + System.lineSeparator()) } return result.toString().toByteArray(Charset.defaultCharset())