Skip to content

Commit c318220

Browse files
committed
ktl-1478 feat: support swift export
1 parent 9be9746 commit c318220

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

examples.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ fun main(args: Array<String>) {
137137
</div>
138138

139139

140+
You can try Kotlin export to Swift.
141+
142+
```html
143+
<div class="kotlin-code" data-target-platform="swift-export"></div>
144+
```
145+
<div class="kotlin-code" data-target-platform="swift-export">
146+
147+
```kotlin
148+
fun mul(a: Int, b: Int): Int {
149+
return a * b
150+
}
151+
152+
fun main(args: Array<String>) {
153+
print(mul(-2, 4))
154+
println(" + 7 =")
155+
print(mul(-2, 4) + 7)
156+
}
157+
```
158+
159+
</div>
160+
140161
Use `data-target-platform` attribute with value `junit` for creating examples with tests:
141162

142163
<div class="kotlin-code" data-target-platform="junit">

src/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export const API_URLS = {
3434
case TargetPlatforms.JUNIT:
3535
url = `${this.server}/api/${version}/compiler/test`;
3636
break;
37+
case TargetPlatforms.SWIFT_EXPORT:
38+
url = `${this.server}/api/${version}/compiler/translate?compiler=swift-export`;
39+
break;
3740
default:
3841
console.warn(`Unknown ${platform.id} , used by default JVM`)
3942
url = `${this.server}/api/${version}/compiler/run`;

src/executable-code/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export default class ExecutableCode {
197197
*/
198198
getJsLibraries(targetNode, platform) {
199199
if (isJsRelated(platform)) {
200-
if (platform === TargetPlatforms.WASM) {
200+
if (platform === TargetPlatforms.WASM || platform === TargetPlatforms.SWIFT_EXPORT) {
201201
return new Set()
202202
}
203203
const jsLibs = targetNode.getAttribute(ATTRIBUTES.JS_LIBS);

src/js-executor/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default class JsExecutor {
2626
}
2727

2828
async executeJsCode(jsCode, wasm, jsLibs, platform, outputHeight, theme, onError) {
29+
if (platform === TargetPlatforms.SWIFT_EXPORT) {
30+
return `<span class="standard-output ${theme}">${jsCode}</span>`;
31+
}
2932
if (platform === TargetPlatforms.CANVAS) {
3033
this.iframe.style.display = "block";
3134
if (outputHeight) this.iframe.style.height = `${outputHeight}px`;
@@ -110,7 +113,7 @@ export default class JsExecutor {
110113
const kotlinScript = API_URLS.KOTLIN_JS + `${normalizeJsVersion(this.kotlinVersion)}/kotlin.js`;
111114
iframeDoc.write("<script src='" + kotlinScript + "'></script>");
112115
}
113-
if (targetPlatform !== TargetPlatforms.WASM) {
116+
if (targetPlatform !== TargetPlatforms.WASM && targetPlatform !== TargetPlatforms.SWIFT_EXPORT) {
114117
for (let lib of jsLibs) {
115118
iframeDoc.write("<script src='" + lib + "'></script>");
116119
}

src/utils/platforms/TargetPlatforms.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const TargetPlatforms = {
77
JAVA: new TargetPlatform('java', 'JVM'),
88
JUNIT: new TargetPlatform('junit', 'JUnit'),
99
CANVAS: new TargetPlatform('canvas', 'JavaScript(canvas)'),
10+
SWIFT_EXPORT: new TargetPlatform('swift-export', 'Swift export'),
1011
} as const;
1112

1213
export type TargetPlatformsKeys = keyof typeof TargetPlatforms;

src/utils/platforms/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export function isJsRelated(platform: TargetPlatform) {
1919
platform === TargetPlatforms.JS ||
2020
platform === TargetPlatforms.JS_IR ||
2121
platform === TargetPlatforms.CANVAS ||
22-
platform === TargetPlatforms.WASM
22+
platform === TargetPlatforms.WASM ||
23+
platform === TargetPlatforms.SWIFT_EXPORT
2324
);
2425
}
2526

src/webdemo-api.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default class WebDemoApi {
5252
static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies) {
5353
const MINIMAL_VERSION_IR = '1.5.0';
5454
const MINIMAL_VERSION_WASM = '1.9.0';
55+
const MINIMAL_VERSION_SWIFT_EXPORT = '2.0.0';
5556

5657
if (platform === TargetPlatforms.JS_IR && compilerVersion < MINIMAL_VERSION_IR) {
5758
return Promise.resolve({
@@ -75,6 +76,17 @@ export default class WebDemoApi {
7576
})
7677
}
7778

79+
if (platform === TargetPlatforms.SWIFT_EXPORT && compilerVersion < MINIMAL_VERSION_SWIFT_EXPORT) {
80+
return Promise.resolve({
81+
output: "",
82+
errors: [{
83+
severity: "ERROR",
84+
message: `Swift export accessible only since ${MINIMAL_VERSION_SWIFT_EXPORT} version`
85+
}],
86+
jsCode: ""
87+
})
88+
}
89+
7890
return executeCode(API_URLS.COMPILE(platform, compilerVersion), code, compilerVersion, platform, args, hiddenDependencies).then(function (data) {
7991
let output = "";
8092
let errorsAndWarnings = flatten(Object.values(data.errors));

0 commit comments

Comments
 (0)