JS: minor refactoring
This commit is contained in:
@@ -69,7 +69,7 @@ class CoroutineTransformer : JsVisitorWithContextImpl() {
|
||||
}
|
||||
}
|
||||
|
||||
fun transformCoroutines(fragments: List<JsProgramFragment>) {
|
||||
fun transformCoroutines(fragments: Iterable<JsProgramFragment>) {
|
||||
val coroutineTransformer = CoroutineTransformer()
|
||||
for (fragment in fragments) {
|
||||
val scope = ProgramFragmentInliningScope(fragment)
|
||||
|
||||
@@ -57,8 +57,7 @@ private val SPECIAL_FUNCTION_PATTERN = Regex("var\\s+($JS_IDENTIFIER)\\s*=\\s*($
|
||||
|
||||
class FunctionReader(
|
||||
private val reporter: JsConfig.Reporter,
|
||||
private val config: JsConfig,
|
||||
private val currentModuleName: JsName
|
||||
private val config: JsConfig
|
||||
) {
|
||||
/**
|
||||
* fileContent: .js file content, that contains this module definition.
|
||||
@@ -198,7 +197,7 @@ class FunctionReader(
|
||||
fragment: JsProgramFragment
|
||||
): FunctionWithWrapper {
|
||||
val tag = Namer.getFunctionTag(descriptor, config)
|
||||
val moduleReference = fragment.inlineModuleMap[tag]?.deepCopy() ?: currentModuleName.makeRef()
|
||||
val moduleReference = fragment.inlineModuleMap[tag]?.deepCopy() ?: fragment.scope.declareName("_").makeRef()
|
||||
val allDefinedNames = collectDefinedNamesInAllScopes(fn.function)
|
||||
val replacements = hashMapOf(
|
||||
info.moduleVariable to moduleReference,
|
||||
|
||||
+1
-1
@@ -114,7 +114,7 @@ object LabeledBlockToDoWhileTransformation {
|
||||
}
|
||||
}
|
||||
|
||||
fun transformLabeledBlockToDoWhile(fragments: List<JsProgramFragment>) {
|
||||
fun transformLabeledBlockToDoWhile(fragments: Iterable<JsProgramFragment>) {
|
||||
for (fragment in fragments) {
|
||||
LabeledBlockToDoWhileTransformation.apply(fragment.declarationBlock)
|
||||
LabeledBlockToDoWhileTransformation.apply(fragment.initializerBlock)
|
||||
|
||||
@@ -38,7 +38,7 @@ class FunctionContext(
|
||||
|
||||
val functionsByFunctionNodes = HashMap<JsFunction, FunctionWithWrapper>()
|
||||
|
||||
fun scopeForFragment(fragment: JsProgramFragment) = if (fragment in newFragments) {
|
||||
fun scopeForFragment(fragment: JsProgramFragment) = if (fragment in inliner.translationResult.newFragments) {
|
||||
inliningScopeCache.computeIfAbsent(fragment) {
|
||||
loadFragment(fragment)
|
||||
ProgramFragmentInliningScope(fragment)
|
||||
@@ -82,7 +82,7 @@ class FunctionContext(
|
||||
return lookUpFunctionDirect(call) ?: lookUpFunctionIndirect(call, scope) ?: lookUpFunctionExternal(call, scope.fragment)
|
||||
}
|
||||
|
||||
private val functionReader = FunctionReader(inliner.reporter, inliner.config, inliner.translationResult.innerModuleName)
|
||||
private val functionReader = FunctionReader(inliner.reporter, inliner.config)
|
||||
|
||||
private data class FragmentInfo(
|
||||
val functions: Map<JsName, FunctionWithWrapper>,
|
||||
@@ -132,7 +132,7 @@ class FunctionContext(
|
||||
else -> null
|
||||
}?.let {
|
||||
InlineFunctionDefinition(it, null).also { definition ->
|
||||
if (scope.fragment in newFragments) {
|
||||
if (scope.fragment in inliner.translationResult.newFragments) {
|
||||
inliner.process(definition, call, scope)
|
||||
}
|
||||
}
|
||||
@@ -141,9 +141,6 @@ class FunctionContext(
|
||||
|
||||
private val inliningScopeCache = mutableMapOf<JsProgramFragment, ProgramFragmentInliningScope>()
|
||||
|
||||
private val newFragments = inliner.translationResult.newFragments.toIdentitySet()
|
||||
|
||||
|
||||
private fun loadFragment(fragment: JsProgramFragment) {
|
||||
fragmentInfo.computeIfAbsent(fragment) {
|
||||
FragmentInfo(
|
||||
|
||||
@@ -139,7 +139,7 @@ class K2JSTranslator @JvmOverloads constructor(
|
||||
|
||||
// Global phases
|
||||
|
||||
val program = translationResult.buildProgram()
|
||||
val (program, importedModules) = translationResult.buildProgram()
|
||||
|
||||
program.resolveTemporaryNames()
|
||||
checkCanceled()
|
||||
@@ -152,7 +152,7 @@ class K2JSTranslator @JvmOverloads constructor(
|
||||
files,
|
||||
program,
|
||||
diagnostics,
|
||||
translationResult.importedModuleList.map { it.externalName },
|
||||
importedModules,
|
||||
moduleDescriptor,
|
||||
bindingTrace.bindingContext
|
||||
)
|
||||
|
||||
+4
-11
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.js.backend.ast.*
|
||||
import org.jetbrains.kotlin.js.config.JsConfig
|
||||
import org.jetbrains.kotlin.js.facade.TranslationUnit
|
||||
import org.jetbrains.kotlin.js.inline.util.toIdentitySet
|
||||
import org.jetbrains.kotlin.protobuf.CodedInputStream
|
||||
import org.jetbrains.kotlin.serialization.js.ast.JsAstDeserializer
|
||||
import org.jetbrains.kotlin.serialization.js.ast.JsAstProtoBuf
|
||||
@@ -36,15 +37,7 @@ class AstGenerationResult(
|
||||
config: JsConfig
|
||||
) {
|
||||
|
||||
// TODO remove
|
||||
val newFragments = translatedSourceFiles.values.map { it.fragment }
|
||||
|
||||
// Only available after the merge
|
||||
val importedModuleList: List<JsImportedModule>
|
||||
get() = merger.importedModules
|
||||
|
||||
val innerModuleName: JsName
|
||||
get() = merger.internalModuleName
|
||||
val newFragments = translatedSourceFiles.values.map { it.fragment }.toSet()
|
||||
|
||||
private val cache = mutableMapOf<TranslationUnit.BinaryAst, DeserializedFileTranslationResult>()
|
||||
|
||||
@@ -68,10 +61,10 @@ class AstGenerationResult(
|
||||
}
|
||||
}
|
||||
|
||||
fun buildProgram(): JsProgram {
|
||||
fun buildProgram(): Pair<JsProgram, List<String>> {
|
||||
val fragments = units.map { translate(it).fragment }
|
||||
fragments.forEach { merger.addFragment(it) }
|
||||
return merger.buildProgram()
|
||||
return merger.buildProgram() to merger.importedModules.map { it.externalName }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.js.translate.context.Namer
|
||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
||||
import java.util.*
|
||||
|
||||
fun expandIsCalls(fragments: List<JsProgramFragment>) {
|
||||
fun expandIsCalls(fragments: Iterable<JsProgramFragment>) {
|
||||
val visitor = TypeCheckRewritingVisitor()
|
||||
for (fragment in fragments) {
|
||||
visitor.accept(fragment.declarationBlock)
|
||||
|
||||
Reference in New Issue
Block a user