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()
|
val coroutineTransformer = CoroutineTransformer()
|
||||||
for (fragment in fragments) {
|
for (fragment in fragments) {
|
||||||
val scope = ProgramFragmentInliningScope(fragment)
|
val scope = ProgramFragmentInliningScope(fragment)
|
||||||
|
|||||||
@@ -57,8 +57,7 @@ private val SPECIAL_FUNCTION_PATTERN = Regex("var\\s+($JS_IDENTIFIER)\\s*=\\s*($
|
|||||||
|
|
||||||
class FunctionReader(
|
class FunctionReader(
|
||||||
private val reporter: JsConfig.Reporter,
|
private val reporter: JsConfig.Reporter,
|
||||||
private val config: JsConfig,
|
private val config: JsConfig
|
||||||
private val currentModuleName: JsName
|
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* fileContent: .js file content, that contains this module definition.
|
* fileContent: .js file content, that contains this module definition.
|
||||||
@@ -198,7 +197,7 @@ class FunctionReader(
|
|||||||
fragment: JsProgramFragment
|
fragment: JsProgramFragment
|
||||||
): FunctionWithWrapper {
|
): FunctionWithWrapper {
|
||||||
val tag = Namer.getFunctionTag(descriptor, config)
|
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 allDefinedNames = collectDefinedNamesInAllScopes(fn.function)
|
||||||
val replacements = hashMapOf(
|
val replacements = hashMapOf(
|
||||||
info.moduleVariable to moduleReference,
|
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) {
|
for (fragment in fragments) {
|
||||||
LabeledBlockToDoWhileTransformation.apply(fragment.declarationBlock)
|
LabeledBlockToDoWhileTransformation.apply(fragment.declarationBlock)
|
||||||
LabeledBlockToDoWhileTransformation.apply(fragment.initializerBlock)
|
LabeledBlockToDoWhileTransformation.apply(fragment.initializerBlock)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class FunctionContext(
|
|||||||
|
|
||||||
val functionsByFunctionNodes = HashMap<JsFunction, FunctionWithWrapper>()
|
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) {
|
inliningScopeCache.computeIfAbsent(fragment) {
|
||||||
loadFragment(fragment)
|
loadFragment(fragment)
|
||||||
ProgramFragmentInliningScope(fragment)
|
ProgramFragmentInliningScope(fragment)
|
||||||
@@ -82,7 +82,7 @@ class FunctionContext(
|
|||||||
return lookUpFunctionDirect(call) ?: lookUpFunctionIndirect(call, scope) ?: lookUpFunctionExternal(call, scope.fragment)
|
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(
|
private data class FragmentInfo(
|
||||||
val functions: Map<JsName, FunctionWithWrapper>,
|
val functions: Map<JsName, FunctionWithWrapper>,
|
||||||
@@ -132,7 +132,7 @@ class FunctionContext(
|
|||||||
else -> null
|
else -> null
|
||||||
}?.let {
|
}?.let {
|
||||||
InlineFunctionDefinition(it, null).also { definition ->
|
InlineFunctionDefinition(it, null).also { definition ->
|
||||||
if (scope.fragment in newFragments) {
|
if (scope.fragment in inliner.translationResult.newFragments) {
|
||||||
inliner.process(definition, call, scope)
|
inliner.process(definition, call, scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,9 +141,6 @@ class FunctionContext(
|
|||||||
|
|
||||||
private val inliningScopeCache = mutableMapOf<JsProgramFragment, ProgramFragmentInliningScope>()
|
private val inliningScopeCache = mutableMapOf<JsProgramFragment, ProgramFragmentInliningScope>()
|
||||||
|
|
||||||
private val newFragments = inliner.translationResult.newFragments.toIdentitySet()
|
|
||||||
|
|
||||||
|
|
||||||
private fun loadFragment(fragment: JsProgramFragment) {
|
private fun loadFragment(fragment: JsProgramFragment) {
|
||||||
fragmentInfo.computeIfAbsent(fragment) {
|
fragmentInfo.computeIfAbsent(fragment) {
|
||||||
FragmentInfo(
|
FragmentInfo(
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ class K2JSTranslator @JvmOverloads constructor(
|
|||||||
|
|
||||||
// Global phases
|
// Global phases
|
||||||
|
|
||||||
val program = translationResult.buildProgram()
|
val (program, importedModules) = translationResult.buildProgram()
|
||||||
|
|
||||||
program.resolveTemporaryNames()
|
program.resolveTemporaryNames()
|
||||||
checkCanceled()
|
checkCanceled()
|
||||||
@@ -152,7 +152,7 @@ class K2JSTranslator @JvmOverloads constructor(
|
|||||||
files,
|
files,
|
||||||
program,
|
program,
|
||||||
diagnostics,
|
diagnostics,
|
||||||
translationResult.importedModuleList.map { it.externalName },
|
importedModules,
|
||||||
moduleDescriptor,
|
moduleDescriptor,
|
||||||
bindingTrace.bindingContext
|
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.backend.ast.*
|
||||||
import org.jetbrains.kotlin.js.config.JsConfig
|
import org.jetbrains.kotlin.js.config.JsConfig
|
||||||
import org.jetbrains.kotlin.js.facade.TranslationUnit
|
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.protobuf.CodedInputStream
|
||||||
import org.jetbrains.kotlin.serialization.js.ast.JsAstDeserializer
|
import org.jetbrains.kotlin.serialization.js.ast.JsAstDeserializer
|
||||||
import org.jetbrains.kotlin.serialization.js.ast.JsAstProtoBuf
|
import org.jetbrains.kotlin.serialization.js.ast.JsAstProtoBuf
|
||||||
@@ -36,15 +37,7 @@ class AstGenerationResult(
|
|||||||
config: JsConfig
|
config: JsConfig
|
||||||
) {
|
) {
|
||||||
|
|
||||||
// TODO remove
|
val newFragments = translatedSourceFiles.values.map { it.fragment }.toSet()
|
||||||
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
|
|
||||||
|
|
||||||
private val cache = mutableMapOf<TranslationUnit.BinaryAst, DeserializedFileTranslationResult>()
|
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 }
|
val fragments = units.map { translate(it).fragment }
|
||||||
fragments.forEach { merger.addFragment(it) }
|
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 org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun expandIsCalls(fragments: List<JsProgramFragment>) {
|
fun expandIsCalls(fragments: Iterable<JsProgramFragment>) {
|
||||||
val visitor = TypeCheckRewritingVisitor()
|
val visitor = TypeCheckRewritingVisitor()
|
||||||
for (fragment in fragments) {
|
for (fragment in fragments) {
|
||||||
visitor.accept(fragment.declarationBlock)
|
visitor.accept(fragment.declarationBlock)
|
||||||
|
|||||||
Reference in New Issue
Block a user