-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor hack: use wasm stdlib to avoid downloading the whole K/N distribution
- Loading branch information
1 parent
50e6205
commit 9a58408
Showing
18 changed files
with
405 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/compiler/server/compiler/components/SwiftExportTranslator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.compiler.server.compiler.components | ||
|
||
import com.compiler.server.model.CompilerDiagnostics | ||
import com.compiler.server.model.SwiftExportResult | ||
import com.compiler.server.model.toExceptionDescriptor | ||
import component.KotlinEnvironment | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.springframework.stereotype.Component | ||
import runSwiftExport | ||
import java.nio.file.Path | ||
|
||
@Component | ||
class SwiftExportTranslator( | ||
private val kotlinEnvironment: KotlinEnvironment, | ||
) { | ||
fun translate(files: List<KtFile>): SwiftExportResult = try { | ||
usingTempDirectory { tempDirectory -> | ||
val ioFiles = files.writeToIoFiles(tempDirectory) | ||
val stdlib = kotlinEnvironment.WASM_LIBRARIES.singleOrNull { "stdlib" in it } | ||
val swiftCode = runSwiftExport( | ||
sourceFile = ioFiles.first(), | ||
stdlibPath = stdlib?.let { Path.of(it) }, | ||
) | ||
SwiftExportResult( | ||
compilerDiagnostics = CompilerDiagnostics(emptyMap()), | ||
swiftCode = swiftCode | ||
) | ||
} | ||
} catch (e: Exception) { | ||
SwiftExportResult(swiftCode = "", exception = e.toExceptionDescriptor()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.compiler.server | ||
|
||
import com.compiler.server.base.BaseExecutorTest | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class SwiftConverterTest : BaseExecutorTest() { | ||
|
||
private fun exactTest(input: String, expected: String) { | ||
val actual = translateToSwift(input) | ||
assertEquals(expected, actual.swiftCode.trimEnd()) | ||
} | ||
|
||
private fun containsTest(input: String, expected: String) { | ||
val actual = translateToSwift(input) | ||
assertContains(actual.swiftCode.trimEnd(), expected) | ||
} | ||
|
||
private fun shouldFailTest(input: String) { | ||
val actual = translateToSwift(input) | ||
assertTrue(actual.hasErrors()) | ||
} | ||
|
||
@Test | ||
fun basicSwiftExportTest() = containsTest( | ||
input = """ | ||
fun main() {} | ||
""".trimIndent(), | ||
expected = "public func main() -> Swift.Void" | ||
) | ||
|
||
@Test | ||
fun `use stdlib declaration`() = containsTest( | ||
input = "fun foo(): UInt = 42", | ||
expected = """ | ||
public func foo() -> Swift.UInt32 { | ||
fatalError() | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `class declaration`() = exactTest( | ||
input = "public class MyClass { public fun A() {}}", | ||
expected = """ | ||
import KotlinRuntime | ||
public class MyClass : KotlinRuntime.KotlinBase { | ||
public override init() { | ||
fatalError() | ||
} | ||
public func A() -> Swift.Void { | ||
fatalError() | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `simple packages`() = exactTest( | ||
input = """ | ||
package foo.bar | ||
val myProperty: Int = 42 | ||
""".trimIndent(), | ||
expected = """ | ||
public extension Playground.foo.bar { | ||
public static var myProperty: Swift.Int32 { | ||
get { | ||
fatalError() | ||
} | ||
} | ||
} | ||
public enum foo { | ||
public enum bar { | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `invalid code`() = exactTest( | ||
input = "abracadabra", | ||
expected = """ | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `more invalid code`() = exactTest( | ||
input = "fun foo(): Bar = error()", | ||
expected = """ | ||
public func foo() -> ERROR_TYPE { | ||
fatalError() | ||
} | ||
""".trimIndent() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
An implementation of Swift export for Kotlin Playground. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
// For Analysis API components | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies") | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/swift-export-experimental") | ||
} | ||
|
||
val kotlinVersion = rootProject.properties["systemProp.kotlinVersion"] | ||
val swiftExportVersion = rootProject.properties["systemProp.swiftExportVersion"] | ||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion") | ||
// For K/N Distribution class | ||
implementation("org.jetbrains.kotlin:kotlin-native-utils:$kotlinVersion") | ||
|
||
// Analysis API components which are required for the Swift export | ||
implementation("org.jetbrains.kotlin:analysis-api-standalone-for-ide:$kotlinVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:high-level-api-for-ide:$kotlinVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:high-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:high-level-api-impl-base-for-ide:$kotlinVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:low-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:symbol-light-classes-for-ide:$kotlinVersion") { isTransitive = false } | ||
|
||
// Swift export not-yet-published dependencies. | ||
implementation("org.jetbrains.kotlin:sir:$swiftExportVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:sir-providers:$swiftExportVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:sir-light-classes:$swiftExportVersion") { isTransitive = false } | ||
implementation("org.jetbrains.kotlin:sir-printer:$swiftExportVersion") { isTransitive = false } | ||
|
||
testImplementation("junit:junit:4.13.2") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion") | ||
} |
31 changes: 31 additions & 0 deletions
31
swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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( | ||
ktModule: KtModule, | ||
) : SirSession { | ||
override val declarationNamer = SirDeclarationNamerImpl() | ||
override val enumGenerator = SirEnumGeneratorImpl() | ||
override val moduleProvider = SirSingleModuleProvider("Playground") | ||
override val declarationProvider = CachingSirDeclarationProvider( | ||
declarationsProvider = SirDeclarationFromKtSymbolProvider( | ||
ktModule = ktModule, | ||
sirSession = sirSession, | ||
) | ||
) | ||
override val parentProvider = SirParentProviderImpl( | ||
sirSession = sirSession, | ||
) | ||
override val typeProvider = SirTypeProviderImpl( | ||
errorTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType, | ||
unsupportedTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType, | ||
sirSession = sirSession, | ||
) | ||
override val visibilityChecker = SirVisibilityCheckerImpl() | ||
override val childrenProvider = SirDeclarationChildrenProviderImpl( | ||
sirSession = sirSession, | ||
) | ||
} |
Oops, something went wrong.