[K/JS] Add the ability to turn off polyfills generating

This commit is contained in:
Artem Kobzar
2022-10-19 12:52:23 +00:00
committed by Space Team
parent dbf0ba442a
commit 7b9aed25da
12 changed files with 48 additions and 16 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.RuntimeDiagnostic
import org.jetbrains.kotlin.name.FqName
import java.io.File
@@ -110,6 +111,7 @@ fun compileIr(
): LoweredIr {
val moduleDescriptor = moduleFragment.descriptor
val irFactory = symbolTable.irFactory
val shouldGeneratePolyfills = configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)
val allModules = when (mainModule) {
is MainModule.SourceFiles -> dependencyModules + listOf(moduleFragment)
@@ -141,7 +143,10 @@ fun compileIr(
irLinker.checkNoUnboundSymbols(symbolTable, "at the end of IR linkage process")
allModules.forEach { module ->
collectNativeImplementations(context, module)
if (shouldGeneratePolyfills) {
collectNativeImplementations(context, module)
}
moveBodilessDeclarationsToSeparatePlace(context, module)
}
@@ -160,7 +165,9 @@ fun generateJsCode(
moduleFragment: IrModuleFragment,
nameTables: NameTables
): String {
collectNativeImplementations(context, moduleFragment)
if (context.configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)) {
collectNativeImplementations(context, moduleFragment)
}
moveBodilessDeclarationsToSeparatePlace(context, moduleFragment)
jsPhases.invokeToplevel(PhaseConfig(jsPhases), context, listOf(moduleFragment))
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.moveBodilessDeclarationsToSepara
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.*
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi2ir.descriptors.IrBuiltInsOverDescriptors
@@ -53,8 +54,12 @@ class JsIrCompilerWithIC(
dirtyFiles: Collection<IrFile>,
mainArguments: List<String>?
): List<JsIrFragmentAndBinaryAst> {
val shouldGeneratePolyfills = context.configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)
allModules.forEach {
collectNativeImplementations(context, it)
if (shouldGeneratePolyfills) {
collectNativeImplementations(context, it)
}
moveBodilessDeclarationsToSeparatePlace(context, it)
}
@@ -5,22 +5,17 @@
package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
fun collectNativeImplementations(context: JsIrBackendContext, moduleFragment: IrModuleFragment) =
CollectNativeImplementationsVisitor(context).let { collector ->
moduleFragment.files.forEach { it.accept(collector, null) }
}
class CollectNativeImplementationsVisitor(private val context: JsIrBackendContext) : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {}
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.js.sourceMap.SourceMap3Builder
import org.jetbrains.kotlin.js.sourceMap.SourceMapBuilderConsumer
import org.jetbrains.kotlin.js.util.TextOutputImpl
import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.addToStdlib.runIf
import java.io.File
import java.util.*
@@ -45,6 +46,7 @@ class IrModuleToJsTransformer(
private val moduleToName: Map<IrModuleFragment, String> = emptyMap(),
private val removeUnusedAssociatedObjects: Boolean = true
) {
private val shouldGeneratePolyfills = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
fun generateModule(modules: Iterable<IrModuleFragment>): CompilerResult {
@@ -148,7 +150,8 @@ class IrModuleToJsTransformer(
globalNameScope = namer.globalNames
)
val polyfillStatements = generatePolyfillStatements(modules)
val polyfillStatements = runIf(shouldGeneratePolyfills) { generatePolyfillStatements(modules) }
val (importStatements, importedJsModules) =
generateImportStatements(
getNameForExternalDeclaration = { staticContext.getNameForStaticDeclaration(it) },
@@ -168,7 +171,7 @@ class IrModuleToJsTransformer(
if (generateScriptModule) {
with(program.globalBlock) {
statements.addWithComment("block: polyfills", polyfillStatements)
polyfillStatements?.let { statements.addWithComment("block: polyfills", it) }
statements.addWithComment("block: imports", importStatements + crossModuleImports)
statements += moduleBody
statements.addWithComment("block: exports", exportStatements + crossModuleExports)
@@ -189,7 +192,8 @@ class IrModuleToJsTransformer(
}
}
program.globalBlock.statements.addWithComment("block: polyfills", polyfillStatements)
polyfillStatements?.let { program.globalBlock.statements.addWithComment("block: polyfills", it) }
program.globalBlock.statements += ModuleWrapperTranslation.wrap(
exportedModule.name,
rootFunction,
@@ -13,8 +13,6 @@ import org.jetbrains.kotlin.ir.backend.js.lower.StaticMembersLowering
import org.jetbrains.kotlin.ir.backend.js.utils.*
import org.jetbrains.kotlin.ir.backend.js.utils.serialization.JsIrAstSerializer
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.js.backend.JsToStringGenerationVisitor
@@ -90,6 +88,7 @@ class IrModuleToJsTransformerTmp(
private val moduleToName: Map<IrModuleFragment, String> = emptyMap(),
private val removeUnusedAssociatedObjects: Boolean = true,
) {
private val shouldGeneratePolyfills = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_POLYFILLS)
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
private val mainModuleName = backendContext.configuration[CommonConfigurationKeys.MODULE_NAME]!!
@@ -233,7 +232,9 @@ class IrModuleToJsTransformerTmp(
)
val result = JsIrProgramFragment(file.fqName.asString()).apply {
polyfills.statements += backendContext.polyfills.getAllPolyfillsFor(file)
if (shouldGeneratePolyfills) {
polyfills.statements += backendContext.polyfills.getAllPolyfillsFor(file)
}
}
val internalModuleName = ReservedJsNames.makeInternalModuleName().takeIf { !isEsModules }
@@ -241,7 +241,10 @@ class Merger(
if (generateScriptModule) {
with(program.globalBlock) {
statements.addWithComment("block: polyfills", polyfillDeclarationBlock.statements)
polyfillDeclarationBlock.statements
.takeIf { it.isNotEmpty() }
?.let { statements.addWithComment("block: polyfills", it) }
statements.addWithComment("block: imports", importStatements)
statements += moduleBody
statements.addWithComment("block: exports", exportStatements)
@@ -265,7 +268,10 @@ class Merger(
}
}
program.globalBlock.statements.addWithComment("block: polyfills", polyfillDeclarationBlock.statements)
polyfillDeclarationBlock.statements
.takeIf { it.isNotEmpty() }
?.let { program.globalBlock.statements.addWithComment("block: polyfills", it) }
program.globalBlock.statements += ModuleWrapperTranslation.wrap(
moduleName,
rootFunction,