[JS IR] tie together IC code in tests and K2JsIrCompiler

This commit is contained in:
Anton Bannykh
2021-11-12 13:59:41 +03:00
committed by TeamCityServer
parent ff02ea5840
commit 75368a2c06
7 changed files with 121 additions and 23 deletions
@@ -126,15 +126,16 @@ fun lowerPreservingTags(modules: Iterable<IrModuleFragment>, context: JsIrBacken
@Suppress("UNUSED_PARAMETER")
fun generateJsFromAst(
mainModule: String,
caches: Map<String, ModuleCache>
mainModuleName: String,
moduleKind: ModuleKind,
caches: Map<String, ModuleCache>,
): CompilerResult {
val deserializer = JsIrAstDeserializer()
val fragments = JsIrProgram(caches.values.map { JsIrModule(it.name, it.name, it.asts.values.mapNotNull { it.ast?.let { deserializer.deserialize(ByteArrayInputStream(it))} }) })
val fragments = JsIrProgram(caches.values.map { JsIrModule(it.name, it.name, it.asts.values.sortedBy { it.name }.mapNotNull { it.ast?.let { deserializer.deserialize(ByteArrayInputStream(it))} }) })
return CompilerResult(
generateSingleWrappedModuleBody(
"main",
ModuleKind.PLAIN,
mainModuleName,
moduleKind,
fragments.modules.flatMap { it.fragments },
sourceMapsInfo = null,
generateScriptModule = false,
@@ -191,7 +191,7 @@ private fun buildCacheForModule(
}
// TODO: actual way of building a cache could change in future
buildCacheForModuleFiles(irModule, dependencies, deserializer, configuration, dirtyFiles, cacheConsumer)
buildCacheForModuleFiles(irModule, dependencies, deserializer, configuration, dirtyFiles, cacheConsumer, emptySet(), null) // TODO: main arguments?
cacheConsumer.commitLibraryPath(libraryPath)
}
@@ -434,6 +434,8 @@ fun rebuildCacheForDirtyFiles(
dirtyFiles: Collection<String>?,
cacheConsumer: PersistentCacheConsumer,
irFactory: IrFactory,
exportedDeclarations: Set<FqName>,
mainArguments: List<String>?,
) {
val loadedModules = loadModules(configuration.languageVersionSettings, dependencyGraph)
@@ -462,7 +464,16 @@ fun rebuildCacheForDirtyFiles(
val currentIrModule = irModules.find { it.second == library }?.first!!
buildCacheForModuleFiles(currentIrModule, irModules.map { it.first }, jsIrLinker, configuration, dirtyFiles, cacheConsumer)
buildCacheForModuleFiles(
currentIrModule,
irModules.map { it.first },
jsIrLinker,
configuration,
dirtyFiles,
cacheConsumer,
exportedDeclarations,
mainArguments
)
}
@Suppress("UNUSED_PARAMETER")
@@ -472,14 +483,17 @@ private fun buildCacheForModuleFiles(
deserializer: JsIrLinker,
configuration: CompilerConfiguration,
dirtyFiles: Collection<String>?, // if null consider the whole module dirty
cacheConsumer: PersistentCacheConsumer
cacheConsumer: PersistentCacheConsumer,
exportedDeclarations: Set<FqName>,
mainArguments: List<String>?,
) {
compileWithIC(
currentModule,
configuration = configuration,
deserializer = deserializer,
dependencies = dependencies,
exportedDeclarations = setOf(FqName("box")),
mainArguments = mainArguments,
exportedDeclarations = exportedDeclarations,
filesToLower = dirtyFiles?.toSet(),
cacheConsumer = cacheConsumer,
)
@@ -489,3 +503,16 @@ private fun buildCacheForModuleFiles(
// val dirtyS = if (dirtyFiles == null) "[ALL]" else dirtyFiles.joinToString(",", "[", "]") { it }
// println("Dirty files -> $dirtyS")
}
fun loadModuleCaches(icCachePaths: Collection<String>): Map<String, ModuleCache> {
val icCacheMap: Map<ModulePath, String> = loadCacheInfo(icCachePaths)
return icCacheMap.entries.associate { (lib, cache) ->
val provider = createCacheProvider(cache)
val files = provider.filePaths()
lib to ModuleCache(lib, files.associate { f ->
f to FileCache(f, provider.binaryAst(f), provider.dts(f), provider.sourceMap(f))
})
}
}
@@ -50,6 +50,8 @@ interface PersistentCacheProvider {
fun sourceMap(path: String): ByteArray?
fun filePaths(): Iterable<String>
companion object {
val EMPTY = object : PersistentCacheProvider {
override fun fileFingerPrint(path: String): Hash {
@@ -83,6 +85,8 @@ interface PersistentCacheProvider {
override fun sourceMap(path: String): ByteArray? {
return null
}
override fun filePaths(): Iterable<String> = emptyList()
}
}
}
@@ -178,6 +182,15 @@ class PersistentCacheProviderImpl(private val cachePath: String) : PersistentCac
override fun sourceMap(path: String): ByteArray? {
return readBytesFromCacheFile(path, fileSourceMap)
}
override fun filePaths(): Iterable<String> {
return File(cachePath).listFiles()!!.filter { it.isDirectory }.mapNotNull { f ->
val fileInfo = File(f, fileInfoFile)
if (fileInfo.exists()) {
fileInfo.readLines()[0]
} else null
}
}
}
interface PersistentCacheConsumer {