Skip to content

Commit

Permalink
Update to the latest Swift export version
Browse files Browse the repository at this point in the history
  • Loading branch information
sbogolepov committed Apr 29, 2024
1 parent d331d72 commit 0641af2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
systemProp.kotlinVersion=2.0.0-RC1
systemProp.kotlinIdeVersion=1.9.20-506
systemProp.kotlinIdeVersionSuffix=IJ8109.175
systemProp.swiftExportVersion=2.0.20-dev-1552
systemProp.swiftExportVersion=2.0.20-dev-2553
systemProp.policy=executor.policy
systemProp.indexes=indexes.json
systemProp.indexesJs=indexesJs.json
Expand Down
20 changes: 9 additions & 11 deletions src/test/kotlin/com/compiler/server/SwiftConverterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ class SwiftConverterTest : BaseExecutorTest() {
fun `class declaration`() = exactTest(
input = "public class MyClass { public fun A() {}}",
expected = """
import KotlinBridges
import KotlinRuntime
public class MyClass {
public init() {
public class MyClass : KotlinRuntime.KotlinBase {
public override init() {
fatalError()
}
public func A() -> Swift.Void {
Expand All @@ -67,9 +66,6 @@ class SwiftConverterTest : BaseExecutorTest() {
val myProperty: Int = 42
""".trimIndent(),
expected = """
import KotlinBridges
import KotlinRuntime
public extension Playground.foo.bar {
public static var myProperty: Swift.Int32 {
get {
Expand All @@ -87,15 +83,17 @@ class SwiftConverterTest : BaseExecutorTest() {
@Test
fun `invalid code`() = exactTest(
input = "abracadabra",
// For now, we unconditionally import a few modules. Will be gone in the future versions
expected = """
import KotlinBridges
import KotlinRuntime
""".trimIndent()
)

@Test
fun `more invalid code`() = shouldFailTest(
fun `more invalid code`() = exactTest(
input = "fun foo(): Bar = error()",
expected = """
public func foo() -> ERROR_TYPE {
fatalError()
}
""".trimIndent()
)
}
19 changes: 8 additions & 11 deletions swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.sir.providers.SirSession
import org.jetbrains.kotlin.sir.providers.SirTypeProvider
import org.jetbrains.kotlin.sir.providers.impl.*
import org.jetbrains.sir.lightclasses.SirDeclarationFromKtSymbolProvider

internal class PlaygroundSirSession(
private val ktAnalysisSession: KtAnalysisSession,
ktModule: KtModule,
) : SirSession {
override val declarationNamer = SirDeclarationNamerImpl()
override val enumGenerator = SirEnumGeneratorImpl()
override val moduleProvider = SirSingleModuleProvider("Playground", "KotlinBridges")
override val moduleProvider = SirSingleModuleProvider("Playground")
override val declarationProvider = CachingSirDeclarationProvider(
declarationsProvider = SirDeclarationFromKtSymbolProvider(
ktAnalysisSession = ktAnalysisSession,
ktModule = ktModule,
sirSession = sirSession,
)
)
override val parentProvider = SirParentProviderImpl(
ktAnalysisSession = ktAnalysisSession,
sirSession = sirSession,
)
override val typeProvider = SirTypeProviderImpl(
ktAnalysisSession = ktAnalysisSession,
sirSession = sirSession,
)
override val visibilityChecker = SirVisibilityCheckerImpl(
ktAnalysisSession = ktAnalysisSession,
errorTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType,
unsupportedTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType,
sirSession = sirSession,
)
override val visibilityChecker = SirVisibilityCheckerImpl()
override val childrenProvider = SirDeclarationChildrenProviderImpl(
ktAnalysisSession = ktAnalysisSession,
sirSession = sirSession,
)
}
10 changes: 5 additions & 5 deletions swift-export-playground/src/main/kotlin/Runner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ fun runSwiftExport(
): String {
val (ktModule, sources) = collectModuleAndSources(sourceFile, "Playground", stdlibPath)

val sirModule: SirModule = analyze(ktModule) {
val sirSession = PlaygroundSirSession(analysisSession)
with(sirSession) {
return analyze(ktModule) {
val sirSession = PlaygroundSirSession(ktModule)
val sirModule: SirModule = with(sirSession) {
ktModule.sirModule().also {
sources.flatMap { file ->
file.getFileSymbol().getFileScope().extractDeclarations()
file.getFileSymbol().getFileScope().extractDeclarations(analysisSession)
}.forEach { topLevelDeclaration ->
val parent = topLevelDeclaration.parent as? SirMutableDeclarationContainer
?: error("top level declaration can contain only module or extension to package as a parent")
parent.addChild { topLevelDeclaration }
}
}
}
SirAsSwiftSourcesPrinter.print(sirModule, stableDeclarationsOrder = true, renderDocComments = true)
}
return SirAsSwiftSourcesPrinter.print(sirModule, stableDeclarationsOrder = true, renderDocComments = true)
}

@OptIn(KtAnalysisApiInternals::class)
Expand Down
19 changes: 16 additions & 3 deletions swift-export-playground/src/test/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,25 @@ class SwiftExportTests {
fun foo(): Int = 5
""",
"""
import KotlinBridges
import KotlinRuntime
public func foo() -> Swift.Int32 {
fatalError()
}
"""
)

@Test
fun `class declaration`() = testSources(
"""
class A
""".trimIndent(),
"""
import KotlinRuntime
public class A : KotlinRuntime.KotlinBase {
public override init() {
fatalError()
}
}
""".trimIndent()
)
}

0 comments on commit 0641af2

Please sign in to comment.