From c3182205484ef34a025a7888aa487d2ae88e37da Mon Sep 17 00:00:00 2001 From: Nikolay Pachkov Date: Thu, 16 May 2024 22:14:01 +0200 Subject: [PATCH 1/4] ktl-1478 feat: support swift export --- examples.md | 21 +++++++++++++++++++++ src/config.js | 3 +++ src/executable-code/index.js | 2 +- src/js-executor/index.js | 5 ++++- src/utils/platforms/TargetPlatforms.ts | 1 + src/utils/platforms/index.ts | 3 ++- src/webdemo-api.js | 12 ++++++++++++ 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/examples.md b/examples.md index 04f7757..bfe79a2 100644 --- a/examples.md +++ b/examples.md @@ -137,6 +137,27 @@ fun main(args: Array) { +You can try Kotlin export to Swift. + +```html +
+``` +
+ +```kotlin +fun mul(a: Int, b: Int): Int { + return a * b +} + +fun main(args: Array) { + print(mul(-2, 4)) + println(" + 7 =") + print(mul(-2, 4) + 7) +} +``` + +
+ Use `data-target-platform` attribute with value `junit` for creating examples with tests:
diff --git a/src/config.js b/src/config.js index 4a05751..b6e357e 100644 --- a/src/config.js +++ b/src/config.js @@ -34,6 +34,9 @@ export const API_URLS = { case TargetPlatforms.JUNIT: url = `${this.server}/api/${version}/compiler/test`; break; + case TargetPlatforms.SWIFT_EXPORT: + url = `${this.server}/api/${version}/compiler/translate?compiler=swift-export`; + break; default: console.warn(`Unknown ${platform.id} , used by default JVM`) url = `${this.server}/api/${version}/compiler/run`; diff --git a/src/executable-code/index.js b/src/executable-code/index.js index d38b8ce..a86541c 100644 --- a/src/executable-code/index.js +++ b/src/executable-code/index.js @@ -197,7 +197,7 @@ export default class ExecutableCode { */ getJsLibraries(targetNode, platform) { if (isJsRelated(platform)) { - if (platform === TargetPlatforms.WASM) { + if (platform === TargetPlatforms.WASM || platform === TargetPlatforms.SWIFT_EXPORT) { return new Set() } const jsLibs = targetNode.getAttribute(ATTRIBUTES.JS_LIBS); diff --git a/src/js-executor/index.js b/src/js-executor/index.js index 9b4ff4e..35f373b 100644 --- a/src/js-executor/index.js +++ b/src/js-executor/index.js @@ -26,6 +26,9 @@ export default class JsExecutor { } async executeJsCode(jsCode, wasm, jsLibs, platform, outputHeight, theme, onError) { + if (platform === TargetPlatforms.SWIFT_EXPORT) { + return `${jsCode}`; + } if (platform === TargetPlatforms.CANVAS) { this.iframe.style.display = "block"; if (outputHeight) this.iframe.style.height = `${outputHeight}px`; @@ -110,7 +113,7 @@ export default class JsExecutor { const kotlinScript = API_URLS.KOTLIN_JS + `${normalizeJsVersion(this.kotlinVersion)}/kotlin.js`; iframeDoc.write(""); } - if (targetPlatform !== TargetPlatforms.WASM) { + if (targetPlatform !== TargetPlatforms.WASM && targetPlatform !== TargetPlatforms.SWIFT_EXPORT) { for (let lib of jsLibs) { iframeDoc.write(""); } diff --git a/src/utils/platforms/TargetPlatforms.ts b/src/utils/platforms/TargetPlatforms.ts index 68b1b7d..19a8025 100644 --- a/src/utils/platforms/TargetPlatforms.ts +++ b/src/utils/platforms/TargetPlatforms.ts @@ -7,6 +7,7 @@ export const TargetPlatforms = { JAVA: new TargetPlatform('java', 'JVM'), JUNIT: new TargetPlatform('junit', 'JUnit'), CANVAS: new TargetPlatform('canvas', 'JavaScript(canvas)'), + SWIFT_EXPORT: new TargetPlatform('swift-export', 'Swift export'), } as const; export type TargetPlatformsKeys = keyof typeof TargetPlatforms; diff --git a/src/utils/platforms/index.ts b/src/utils/platforms/index.ts index c60ee7d..b37a5be 100644 --- a/src/utils/platforms/index.ts +++ b/src/utils/platforms/index.ts @@ -19,7 +19,8 @@ export function isJsRelated(platform: TargetPlatform) { platform === TargetPlatforms.JS || platform === TargetPlatforms.JS_IR || platform === TargetPlatforms.CANVAS || - platform === TargetPlatforms.WASM + platform === TargetPlatforms.WASM || + platform === TargetPlatforms.SWIFT_EXPORT ); } diff --git a/src/webdemo-api.js b/src/webdemo-api.js index e1b8e27..748eb69 100644 --- a/src/webdemo-api.js +++ b/src/webdemo-api.js @@ -52,6 +52,7 @@ export default class WebDemoApi { static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies) { const MINIMAL_VERSION_IR = '1.5.0'; const MINIMAL_VERSION_WASM = '1.9.0'; + const MINIMAL_VERSION_SWIFT_EXPORT = '2.0.0'; if (platform === TargetPlatforms.JS_IR && compilerVersion < MINIMAL_VERSION_IR) { return Promise.resolve({ @@ -75,6 +76,17 @@ export default class WebDemoApi { }) } + if (platform === TargetPlatforms.SWIFT_EXPORT && compilerVersion < MINIMAL_VERSION_SWIFT_EXPORT) { + return Promise.resolve({ + output: "", + errors: [{ + severity: "ERROR", + message: `Swift export accessible only since ${MINIMAL_VERSION_SWIFT_EXPORT} version` + }], + jsCode: "" + }) + } + return executeCode(API_URLS.COMPILE(platform, compilerVersion), code, compilerVersion, platform, args, hiddenDependencies).then(function (data) { let output = ""; let errorsAndWarnings = flatten(Object.values(data.errors)); From 90d868a095a7a830bfdcf5ee0210edd5f71d1688 Mon Sep 17 00:00:00 2001 From: nikpachoo Date: Fri, 17 May 2024 10:13:04 +0200 Subject: [PATCH 2/4] ktl-1478 chore: up version to 1.31.0-alfa1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e74903d..770a81c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kotlin-playground", - "version": "1.30.0", + "version": "1.31.0-alfa1", "description": "Self-contained component to embed in websites for running Kotlin code", "keywords": [ "kotlin", From f7bdf9b779f98003060a2911b2ca4de28ef5241d Mon Sep 17 00:00:00 2001 From: Nikolay Pachkov Date: Mon, 20 May 2024 17:26:36 +0200 Subject: [PATCH 3/4] ktl-1478 chore: update swift export request --- examples.md | 4 ++-- src/config.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples.md b/examples.md index bfe79a2..baef3cb 100644 --- a/examples.md +++ b/examples.md @@ -140,9 +140,9 @@ fun main(args: Array) { You can try Kotlin export to Swift. ```html -
+
``` -
+
```kotlin fun mul(a: Int, b: Int): Int { diff --git a/src/config.js b/src/config.js index b6e357e..f091135 100644 --- a/src/config.js +++ b/src/config.js @@ -35,7 +35,7 @@ export const API_URLS = { url = `${this.server}/api/${version}/compiler/test`; break; case TargetPlatforms.SWIFT_EXPORT: - url = `${this.server}/api/${version}/compiler/translate?compiler=swift-export`; + url = `${this.server}/api/${version}/${TargetPlatforms.SWIFT_EXPORT.id}/compiler/translate?compiler=swift-export`; break; default: console.warn(`Unknown ${platform.id} , used by default JVM`) From 764a6c470c792c8b216a5da3578d705c9fccbe8d Mon Sep 17 00:00:00 2001 From: Nikolay Pachkov Date: Mon, 20 May 2024 17:27:06 +0200 Subject: [PATCH 4/4] ktl-1478 chore: up version to 1.31.0-alfa2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 770a81c..29d8fec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kotlin-playground", - "version": "1.31.0-alfa1", + "version": "1.31.0-alfa2", "description": "Self-contained component to embed in websites for running Kotlin code", "keywords": [ "kotlin",