JS IR: per-module .js generation support

This commit is contained in:
Anton Bannykh
2020-06-03 18:41:52 +03:00
parent deb5dc1057
commit da79f93c61
38 changed files with 1358 additions and 124 deletions
@@ -129,6 +129,7 @@ abstract class BasicBoxTest(
val expectActualLinker = EXPECT_ACTUAL_LINKER.matcher(fileContent).find()
val skipDceDriven = SKIP_DCE_DRIVEN.matcher(fileContent).find()
val splitPerModule = SPLIT_PER_MODULE.matcher(fileContent).find()
TestFileFactoryImpl(coroutinesPackage).use { testFactory ->
val inputFiles = TestFiles.createTestFiles(
@@ -173,7 +174,7 @@ abstract class BasicBoxTest(
testFactory.tmpDir,
file.parent, module, outputFileName, dceOutputFileName, pirOutputFileName, dependencies, allDependencies, friends, modules.size > 1,
!SKIP_SOURCEMAP_REMAPPING.matcher(fileContent).find(), outputPrefixFile, outputPostfixFile,
actualMainCallParameters, testPackage, testFunction, needsFullIrRuntime, isMainModule, expectActualLinker, skipDceDriven
actualMainCallParameters, testPackage, testFunction, needsFullIrRuntime, isMainModule, expectActualLinker, skipDceDriven, splitPerModule
)
when {
@@ -395,7 +396,8 @@ abstract class BasicBoxTest(
needsFullIrRuntime: Boolean,
isMainModule: Boolean,
expectActualLinker: Boolean,
skipDceDriven: Boolean
skipDceDriven: Boolean,
splitPerModule: Boolean
) {
val kotlinFiles = module.files.filter { it.fileName.endsWith(".kt") }
val testFiles = kotlinFiles.map { it.fileName }
@@ -419,7 +421,7 @@ abstract class BasicBoxTest(
val incrementalData = IncrementalData()
translateFiles(
psiFiles.map(TranslationUnit::SourceFile), outputFile, dceOutputFile, pirOutputFile, config, outputPrefixFile, outputPostfixFile,
mainCallParameters, incrementalData, remap, testPackage, testFunction, needsFullIrRuntime, isMainModule, skipDceDriven
mainCallParameters, incrementalData, remap, testPackage, testFunction, needsFullIrRuntime, isMainModule, skipDceDriven, splitPerModule
)
if (incrementalCompilationChecksEnabled && module.hasFilesToRecompile) {
@@ -471,7 +473,7 @@ abstract class BasicBoxTest(
translateFiles(
translationUnits, recompiledOutputFile, recompiledOutputFile, recompiledOutputFile, recompiledConfig, outputPrefixFile, outputPostfixFile,
mainCallParameters, incrementalData, remap, testPackage, testFunction, needsFullIrRuntime, false, true
mainCallParameters, incrementalData, remap, testPackage, testFunction, needsFullIrRuntime, false, true, false
)
val originalOutput = FileUtil.loadFile(outputFile)
@@ -544,7 +546,8 @@ abstract class BasicBoxTest(
testFunction: String,
needsFullIrRuntime: Boolean,
isMainModule: Boolean,
skipDceDriven: Boolean
skipDceDriven: Boolean,
splitPerModule: Boolean
) {
val translator = K2JSTranslator(config, false)
val translationResult = translator.translateUnits(ExceptionThrowingReporter, units, mainCallParameters)
@@ -929,6 +932,7 @@ abstract class BasicBoxTest(
private val KJS_WITH_FULL_RUNTIME = Pattern.compile("^// *KJS_WITH_FULL_RUNTIME *\$", Pattern.MULTILINE)
private val EXPECT_ACTUAL_LINKER = Pattern.compile("^// EXPECT_ACTUAL_LINKER *$", Pattern.MULTILINE)
private val SKIP_DCE_DRIVEN = Pattern.compile("^// *SKIP_DCE_DRIVEN *$", Pattern.MULTILINE)
private val SPLIT_PER_MODULE = Pattern.compile("^// *SPLIT_PER_MODULE *$", Pattern.MULTILINE)
@JvmStatic
protected val runTestInNashorn = getBoolean("kotlin.js.useNashorn")
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.js.facade.MainCallParameters
import org.jetbrains.kotlin.js.facade.TranslationUnit
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.parsing.parseBoolean
import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.utils.fileUtils.withReplacedExtensionOrNull
import java.io.File
@@ -56,6 +57,8 @@ abstract class BasicIrBoxTest(
val runEs6Mode: Boolean = getBoolean("kotlin.js.ir.es6", false)
val perModule: Boolean = getBoolean("kotlin.js.ir.perModule")
private val osName: String = System.getProperty("os.name").toLowerCase()
// TODO Design incremental compilation for IR and add test support
@@ -63,8 +66,11 @@ abstract class BasicIrBoxTest(
private val compilationCache = mutableMapOf<String, String>()
private val cachedDependencies = mutableMapOf<String, Collection<String>>()
override fun doTest(filePath: String, expectedResult: String, mainCallParameters: MainCallParameters, coroutinesPackage: String) {
compilationCache.clear()
cachedDependencies.clear()
super.doTest(filePath, expectedResult, mainCallParameters, coroutinesPackage)
}
@@ -86,7 +92,8 @@ abstract class BasicIrBoxTest(
testFunction: String,
needsFullIrRuntime: Boolean,
isMainModule: Boolean,
skipDceDriven: Boolean
skipDceDriven: Boolean,
splitPerModule: Boolean
) {
val filesToCompile = units.map { (it as TranslationUnit.SourceFile).file }
@@ -135,18 +142,13 @@ abstract class BasicIrBoxTest(
exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, testFunction))),
generateFullJs = true,
generateDceJs = runIrDce,
es6mode = runEs6Mode
es6mode = runEs6Mode,
multiModule = splitPerModule || perModule
)
val wrappedCode =
wrapWithModuleEmulationMarkers(compiledModule.jsCode!!, moduleId = config.moduleId, moduleKind = config.moduleKind)
outputFile.write(wrappedCode)
compiledModule.jsCode!!.writeTo(outputFile, config)
compiledModule.dceJsCode?.let { dceJsCode ->
val dceWrappedCode =
wrapWithModuleEmulationMarkers(dceJsCode, moduleId = config.moduleId, moduleKind = config.moduleKind)
dceOutputFile.write(dceWrappedCode)
}
compiledModule.dceJsCode?.writeTo(dceOutputFile, config)
if (generateDts) {
val dtsFile = outputFile.withReplacedExtensionOrNull("_v5.js", ".d.ts")
@@ -166,11 +168,9 @@ abstract class BasicIrBoxTest(
mainArguments = mainCallParameters.run { if (shouldBeGenerated()) arguments() else null },
exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, testFunction))),
dceDriven = true,
es6mode = runEs6Mode
).jsCode!!.let { pirCode ->
val pirWrappedCode = wrapWithModuleEmulationMarkers(pirCode, moduleId = config.moduleId, moduleKind = config.moduleKind)
pirOutputFile.write(pirWrappedCode)
}
es6mode = runEs6Mode,
multiModule = splitPerModule || perModule
).jsCode!!.writeTo(pirOutputFile, config)
}
} else {
generateKLib(
@@ -188,6 +188,23 @@ abstract class BasicIrBoxTest(
}
}
private fun JsCode.writeTo(outputFile: File, config: JsConfig) {
val wrappedCode =
wrapWithModuleEmulationMarkers(mainModule, moduleId = config.moduleId, moduleKind = config.moduleKind)
outputFile.write(wrappedCode)
val dependencyPaths = mutableListOf<String>()
dependencies.forEach { (moduleId, code) ->
val wrappedCode = wrapWithModuleEmulationMarkers(code, config.moduleKind, moduleId)
val dependencyPath = outputFile.absolutePath.replace("_v5.js", "-${moduleId}_v5.js")
dependencyPaths += dependencyPath
File(dependencyPath).write(wrappedCode)
}
cachedDependencies[outputFile.absolutePath] = dependencyPaths
}
override fun dontRunOnSpecificPlatform(targetBackend: TargetBackend): Boolean {
if (targetBackend != TargetBackend.JS_IR_ES6) return false
if (!runEs6Mode) return false
@@ -210,7 +227,8 @@ abstract class BasicIrBoxTest(
// TODO: should we do anything special for module systems?
// TODO: return list of js from translateFiles and provide then to this function with other js files
testChecker.check(jsFiles, testModuleName, testPackage, testFunction, expectedResult, withModuleSystem)
val allFiles = jsFiles.flatMap { file -> cachedDependencies[file]?.let { deps -> deps + file } ?: listOf(file) }
testChecker.check(allFiles, testModuleName, testPackage, testFunction, expectedResult, withModuleSystem)
}
}
@@ -797,6 +797,104 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test {
}
}
@TestMetadata("js/js.translator/testData/box/crossModuleRefIR")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CrossModuleRefIR extends AbstractIrBoxJsES6Test {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath);
}
public void testAllFilesPresentInCrossModuleRefIR() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/crossModuleRefIR"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("callableObjectRef.kt")
public void testCallableObjectRef() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/callableObjectRef.kt");
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/constructor.kt");
}
@TestMetadata("export.kt")
public void testExport() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/export.kt");
}
@TestMetadata("inheritance.kt")
public void testInheritance() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inheritance.kt");
}
@TestMetadata("inlineJsModule.kt")
public void testInlineJsModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModule.kt");
}
@TestMetadata("inlineJsModuleNonIdentifier.kt")
public void testInlineJsModuleNonIdentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModuleNonIdentifier.kt");
}
@TestMetadata("inlineJsModulePackage.kt")
public void testInlineJsModulePackage() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModulePackage.kt");
}
@TestMetadata("inlineModule.kt")
public void testInlineModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModule.kt");
}
@TestMetadata("inlineModuleNonIndentifier.kt")
public void testInlineModuleNonIndentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModuleNonIndentifier.kt");
}
@TestMetadata("lambda.kt")
public void testLambda() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/lambda.kt");
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/object.kt");
}
@TestMetadata("objectInInlineClosure.kt")
public void testObjectInInlineClosure() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectInInlineClosure.kt");
}
@TestMetadata("objectIsObject.kt")
public void testObjectIsObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectIsObject.kt");
}
@TestMetadata("topLevelExtension.kt")
public void testTopLevelExtension() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelExtension.kt");
}
@TestMetadata("topLevelFunction.kt")
public void testTopLevelFunction() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelFunction.kt");
}
@TestMetadata("topLevelMutableProperty.kt")
public void testTopLevelMutableProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelMutableProperty.kt");
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelProperty.kt");
}
}
@TestMetadata("js/js.translator/testData/box/dataClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -797,6 +797,104 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
}
}
@TestMetadata("js/js.translator/testData/box/crossModuleRefIR")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CrossModuleRefIR extends AbstractIrBoxJsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath);
}
public void testAllFilesPresentInCrossModuleRefIR() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/crossModuleRefIR"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("callableObjectRef.kt")
public void testCallableObjectRef() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/callableObjectRef.kt");
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/constructor.kt");
}
@TestMetadata("export.kt")
public void testExport() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/export.kt");
}
@TestMetadata("inheritance.kt")
public void testInheritance() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inheritance.kt");
}
@TestMetadata("inlineJsModule.kt")
public void testInlineJsModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModule.kt");
}
@TestMetadata("inlineJsModuleNonIdentifier.kt")
public void testInlineJsModuleNonIdentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModuleNonIdentifier.kt");
}
@TestMetadata("inlineJsModulePackage.kt")
public void testInlineJsModulePackage() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModulePackage.kt");
}
@TestMetadata("inlineModule.kt")
public void testInlineModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModule.kt");
}
@TestMetadata("inlineModuleNonIndentifier.kt")
public void testInlineModuleNonIndentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModuleNonIndentifier.kt");
}
@TestMetadata("lambda.kt")
public void testLambda() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/lambda.kt");
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/object.kt");
}
@TestMetadata("objectInInlineClosure.kt")
public void testObjectInInlineClosure() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectInInlineClosure.kt");
}
@TestMetadata("objectIsObject.kt")
public void testObjectIsObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectIsObject.kt");
}
@TestMetadata("topLevelExtension.kt")
public void testTopLevelExtension() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelExtension.kt");
}
@TestMetadata("topLevelFunction.kt")
public void testTopLevelFunction() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelFunction.kt");
}
@TestMetadata("topLevelMutableProperty.kt")
public void testTopLevelMutableProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelMutableProperty.kt");
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelProperty.kt");
}
}
@TestMetadata("js/js.translator/testData/box/dataClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -797,6 +797,104 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
}
}
@TestMetadata("js/js.translator/testData/box/crossModuleRefIR")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CrossModuleRefIR extends AbstractBoxJsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath);
}
public void testAllFilesPresentInCrossModuleRefIR() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/crossModuleRefIR"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS, true);
}
@TestMetadata("callableObjectRef.kt")
public void testCallableObjectRef() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/callableObjectRef.kt");
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/constructor.kt");
}
@TestMetadata("export.kt")
public void testExport() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/export.kt");
}
@TestMetadata("inheritance.kt")
public void testInheritance() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inheritance.kt");
}
@TestMetadata("inlineJsModule.kt")
public void testInlineJsModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModule.kt");
}
@TestMetadata("inlineJsModuleNonIdentifier.kt")
public void testInlineJsModuleNonIdentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModuleNonIdentifier.kt");
}
@TestMetadata("inlineJsModulePackage.kt")
public void testInlineJsModulePackage() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineJsModulePackage.kt");
}
@TestMetadata("inlineModule.kt")
public void testInlineModule() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModule.kt");
}
@TestMetadata("inlineModuleNonIndentifier.kt")
public void testInlineModuleNonIndentifier() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/inlineModuleNonIndentifier.kt");
}
@TestMetadata("lambda.kt")
public void testLambda() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/lambda.kt");
}
@TestMetadata("object.kt")
public void testObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/object.kt");
}
@TestMetadata("objectInInlineClosure.kt")
public void testObjectInInlineClosure() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectInInlineClosure.kt");
}
@TestMetadata("objectIsObject.kt")
public void testObjectIsObject() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/objectIsObject.kt");
}
@TestMetadata("topLevelExtension.kt")
public void testTopLevelExtension() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelExtension.kt");
}
@TestMetadata("topLevelFunction.kt")
public void testTopLevelFunction() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelFunction.kt");
}
@TestMetadata("topLevelMutableProperty.kt")
public void testTopLevelMutableProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelMutableProperty.kt");
}
@TestMetadata("topLevelProperty.kt")
public void testTopLevelProperty() throws Exception {
runTest("js/js.translator/testData/box/crossModuleRefIR/topLevelProperty.kt");
}
}
@TestMetadata("js/js.translator/testData/box/dataClass")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)