[K/Wasm] Add simple TypeScript definitions generating ^KT-65009 Fixed

This commit is contained in:
Artem Kobzar
2024-01-16 11:10:27 +00:00
committed by Space Team
parent baa0748375
commit a55c65e3e2
35 changed files with 1176 additions and 32 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.test.services.AdditionalSourceProvider
import org.jetbrains.kotlin.test.services.EnvironmentConfigurator
import org.jetbrains.kotlin.test.services.LibraryProvider
import org.jetbrains.kotlin.test.services.sourceProviders.CoroutineHelpersSourceFilesProvider
import org.jetbrains.kotlin.wasm.test.handlers.WasmDtsHandler
abstract class AbstractWasmBlackBoxCodegenTestBase<R : ResultingArtifact.FrontendOutput<R>, I : ResultingArtifact.BackendInput<I>, A : ResultingArtifact.Binary<A>>(
private val targetFrontend: FrontendKind<R>,
@@ -94,6 +95,7 @@ abstract class AbstractWasmBlackBoxCodegenTestBase<R : ResultingArtifact.Fronten
wasmArtifactsHandlersStep {
useHandlers(wasmBoxTestRunner)
useHandlers(::WasmDtsHandler)
}
}
@@ -49,6 +49,7 @@ class WasmBackendFacade(
override fun transform(module: TestModule, inputArtifact: BinaryArtifacts.KLib): BinaryArtifacts.Wasm? {
val configuration = testServices.compilerConfigurationProvider.getCompilerConfiguration(module)
val generateSourceMaps = WasmEnvironmentConfigurationDirectives.GENERATE_SOURCE_MAP in testServices.moduleStructure.allDirectives
val generateDts = WasmEnvironmentConfigurationDirectives.CHECK_TYPESCRIPT_DECLARATIONS in testServices.moduleStructure.allDirectives
// Enforce PL with the ERROR log level to fail any tests where PL detected any incompatibilities.
configuration.setupPartialLinkageConfig(PartialLinkageConfig(PartialLinkageMode.ENABLE, PartialLinkageLogLevel.ERROR))
@@ -93,12 +94,13 @@ class WasmBackendFacade(
)
val testPackage = extractTestPackage(testServices)
val (allModules, backendContext) = compileToLoweredIr(
val (allModules, backendContext, typeScriptFragment) = compileToLoweredIr(
depsDescriptors = moduleStructure,
phaseConfig = phaseConfig,
irFactory = IrFactoryImpl,
exportedDeclarations = setOf(FqName.fromSegments(listOfNotNull(testPackage, "box"))),
propertyLazyInitialization = true,
generateTypeScriptFragment = generateDts
)
val generateWat = debugMode >= DebugMode.DEBUG
val baseFileName = "index"
@@ -106,11 +108,12 @@ class WasmBackendFacade(
val compilerResult = compileWasm(
allModules = allModules,
backendContext = backendContext,
typeScriptFragment = typeScriptFragment,
baseFileName = baseFileName,
emitNameSection = true,
allowIncompleteImplementations = false,
generateWat = generateWat,
generateSourceMaps = generateSourceMaps
generateSourceMaps = generateSourceMaps,
)
val dceDumpNameCache = DceDumpNameCache()
@@ -121,11 +124,12 @@ class WasmBackendFacade(
val compilerResultWithDCE = compileWasm(
allModules = allModules,
backendContext = backendContext,
typeScriptFragment = typeScriptFragment,
baseFileName = baseFileName,
emitNameSection = true,
allowIncompleteImplementations = true,
generateWat = generateWat,
generateSourceMaps = generateSourceMaps
generateSourceMaps = generateSourceMaps,
)
return BinaryArtifacts.Wasm(
@@ -148,7 +152,8 @@ class WasmBackendFacade(
jsUninstantiatedWrapper = jsUninstantiatedWrapper,
jsWrapper = jsWrapper,
wasm = newWasm,
debugInformation = null
debugInformation = null,
dts = dts
)
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.wasm.test.handlers
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.backend.handlers.WasmBinaryArtifactHandler
import org.jetbrains.kotlin.test.directives.WasmEnvironmentConfigurationDirectives
import org.jetbrains.kotlin.test.model.BinaryArtifacts
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.moduleStructure
import org.jetbrains.kotlin.utils.fileUtils.withReplacedExtensionOrNull
class WasmDtsHandler(testServices: TestServices) : WasmBinaryArtifactHandler(testServices) {
override fun processAfterAllModules(someAssertionWasFailed: Boolean) {}
override fun processModule(module: TestModule, info: BinaryArtifacts.Wasm) {
val globalDirectives = testServices.moduleStructure.allDirectives
if (WasmEnvironmentConfigurationDirectives.CHECK_TYPESCRIPT_DECLARATIONS !in globalDirectives) return
val referenceDtsFile = module.files.first().originalFile.withReplacedExtensionOrNull(".kt", ".d.ts")
?: error("Can't find reference .d.ts file")
val generatedDts = info.compilerResult.dts
?: error("Can't find generated .d.ts file")
KotlinTestUtils.assertEqualsToFile(referenceDtsFile, generatedDts)
}
}