-
Notifications
You must be signed in to change notification settings - Fork 74
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
3b1f866
commit 39e8501
Showing
19 changed files
with
474 additions
and
3 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
110 changes: 110 additions & 0 deletions
110
src/test/kotlin/com/compiler/server/SwiftConverterTest.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,110 @@ | ||
package com.compiler.server | ||
|
||
import com.compiler.server.base.BaseExecutorTest | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertEquals | ||
|
||
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) | ||
} | ||
|
||
@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 { | ||
stub() | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `class declaration`() = exactTest( | ||
input = "public class MyClass { public fun A() {}}", | ||
expected = """ | ||
import KotlinRuntime | ||
public final class MyClass : KotlinRuntime.KotlinBase { | ||
public override init() { | ||
stub() | ||
} | ||
public override init( | ||
__externalRCRef: Swift.UInt | ||
) { | ||
stub() | ||
} | ||
public func A() -> Swift.Void { | ||
stub() | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `simple packages`() = exactTest( | ||
input = """ | ||
package foo.bar | ||
val myProperty: Int = 42 | ||
""".trimIndent(), | ||
expected = """ | ||
@_exported import pkg | ||
public extension pkg.foo.bar { | ||
public static var myProperty: Swift.Int32 { | ||
get { | ||
stub() | ||
} | ||
} | ||
} | ||
""".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 { | ||
stub() | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
@Test | ||
fun `unsupported type declaration`() = exactTest( | ||
input = """ | ||
interface Foo | ||
fun produceFoo(): Foo = TODO() | ||
""".trimIndent(), | ||
expected = """ | ||
public func produceFoo() -> Swift.Never { | ||
stub() | ||
} | ||
""".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,32 @@ | ||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
// For Analysis API components | ||
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies") | ||
} | ||
|
||
dependencies { | ||
implementation(libs.kotlin.compiler) | ||
|
||
// Analysis API components which are required for the Swift export | ||
implementation(libs.analysis.api.standalone.`for`.ide) { isTransitive = false } | ||
implementation(libs.high.level.api.`for`.ide) { isTransitive = false } | ||
implementation(libs.high.level.api.fir.`for`.ide) { isTransitive = false } | ||
implementation(libs.high.level.api.impl.base.`for`.ide) { isTransitive = false } | ||
implementation(libs.low.level.api.fir.`for`.ide) { isTransitive = false } | ||
implementation(libs.symbol.light.classes.`for`.ide) { isTransitive = false } | ||
implementation(libs.analysis.api.platform.`interface`.`for`.ide) { isTransitive = false } | ||
implementation(libs.caffeine) | ||
|
||
// Swift export not-yet-published dependencies. | ||
implementation(libs.sir) { isTransitive = false } | ||
implementation(libs.sir.providers) { isTransitive = false } | ||
implementation(libs.sir.light.classes) { isTransitive = false } | ||
implementation(libs.sir.printer) { isTransitive = false } | ||
|
||
testImplementation("junit:junit:4.13.2") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion") | ||
} |
Oops, something went wrong.