Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTL-1478 Swift Export in Kotlin Playground: support new target in Widget #210

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ fun main(args: Array<String>) {
</div>


You can try Kotlin export to Swift.

```html
<div class="kotlin-code" data-target-platform="swift-export" data-version="2.0.0-RC3"></div>
```
<div class="kotlin-code" data-target-platform="swift-export" data-version="2.0.0-RC3">

```kotlin
fun mul(a: Int, b: Int): Int {
return a * b
}

fun main(args: Array<String>) {
print(mul(-2, 4))
println(" + 7 =")
print(mul(-2, 4) + 7)
}
```

</div>

Use `data-target-platform` attribute with value `junit` for creating examples with tests:

<div class="kotlin-code" data-target-platform="junit">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kotlin-playground",
"version": "1.30.0",
"version": "1.31.0-alfa2",
"description": "Self-contained component to embed in websites for running Kotlin code",
"keywords": [
"kotlin",
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}/${TargetPlatforms.SWIFT_EXPORT.id}/compiler/translate?compiler=swift-export`;
Copy link
Contributor

@zoobestik zoobestik May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we use translate?compiler= is it based on jscode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's based on JS. Here's more details JetBrains/kotlin-compiler-server#723

Copy link
Contributor

@zoobestik zoobestik May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break;
default:
console.warn(`Unknown ${platform.id} , used by default JVM`)
url = `${this.server}/api/${version}/compiler/run`;
Expand Down
2 changes: 1 addition & 1 deletion src/executable-code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/js-executor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class JsExecutor {
}

async executeJsCode(jsCode, wasm, jsLibs, platform, outputHeight, theme, onError) {
if (platform === TargetPlatforms.SWIFT_EXPORT) {
return `<span class="standard-output ${theme}">${jsCode}</span>`;
}
if (platform === TargetPlatforms.CANVAS) {
this.iframe.style.display = "block";
if (outputHeight) this.iframe.style.height = `${outputHeight}px`;
Expand Down Expand Up @@ -110,7 +113,7 @@ export default class JsExecutor {
const kotlinScript = API_URLS.KOTLIN_JS + `${normalizeJsVersion(this.kotlinVersion)}/kotlin.js`;
iframeDoc.write("<script src='" + kotlinScript + "'></script>");
}
if (targetPlatform !== TargetPlatforms.WASM) {
if (targetPlatform !== TargetPlatforms.WASM && targetPlatform !== TargetPlatforms.SWIFT_EXPORT) {
for (let lib of jsLibs) {
iframeDoc.write("<script src='" + lib + "'></script>");
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/platforms/TargetPlatforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion src/utils/platforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
12 changes: 12 additions & 0 deletions src/webdemo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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));
Expand Down
Loading