Skip to content

Commit

Permalink
Refactors kotlin-js-interop module
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoengenheiro committed Mar 22, 2024
1 parent 97c9051 commit ea9ebe0
Show file tree
Hide file tree
Showing 41 changed files with 1,506 additions and 78 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- [Requests](#requests)
- [Responses](#responses)
- [Demonstration](#demonstration-1)
- [Run Server](#run-server)
- [Javascript Client](#javascript-client)
- [Android Client](#android-client)

## Kotlin Multiplatform

Expand Down Expand Up @@ -173,7 +176,7 @@ Module: [kotlin-js-interop](./kotlin-js-interop)
<tr>
<td>

[func-export.js](./kotlin-js-interop/src/main/js/func-export.mjs)
[func-export.mjs](./kotlin-js-interop/src/main/js/func-export.mjs)

</td>
<td>
Expand All @@ -184,7 +187,7 @@ Module: [kotlin-js-interop](./kotlin-js-interop)
<tr>
<td>

[file-export.js](./kotlin-js-interop/src/main/js/file-export.mjs)
[file-export.mjs](./kotlin-js-interop/src/main/js/file-export.mjs)

</td>
<td>
Expand Down Expand Up @@ -243,7 +246,8 @@ external object RandomStringFromNpm {
</table>

> [!TIP]
> To delegate default parameter value to the imported JavaScript function, use `definedExternally`.
> To delegate default parameter value to the imported JavaScript function, use `definedExternally`. Only nullable types
> can have default implementations declared.
#### Build and Run

Expand Down Expand Up @@ -441,7 +445,7 @@ More examples [at](https://ktor.io/docs/response.html).

### Demonstration

#### Server
#### Run Server

To start the server, run the following command:

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
agp = "8.2.0"
kotlin = "1.9.20"
coroutines = "1.7.3"
coroutines = "1.8.0"
ktor = "2.3.9"
nexus-publish = "2.0.0-rc-1"
android-minSdk = "24"
Expand Down
5 changes: 1 addition & 4 deletions js-app/src/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
"type": "git",
"url": "https://github.com/kresil/experiments.git"
},
"private": true,
"dependencies": {
"express": "^4.18.3"
}
"private": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import target.Target
import kotlin.test.Test
import kotlin.test.assertEquals

class JSPlatformTest {
class JsPlatformTest {

@Test
fun testTargetCorrespondsToCurrentPlatform() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import target.Platform
import target.PlatformType
import target.Target
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class JVMPlatformTest {
class JvmPlatformTest {

@Test
fun testTargetCorrespondsToCurrentPlatform() {
Expand Down
12 changes: 12 additions & 0 deletions kotlin-js-interop/js-to-kotlin/js-app/src/main/js/file-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function greet(name) {
return "Hello, " + name + "!";
}

function advocate(number) {
return "Kotlin advocate nr: " + number;
}

exports = {
greet,
advocate
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function defaultGreet(name) {
return "Single default export: Hello, " + name + "!";
}

export default defaultGreet;
6 changes: 6 additions & 0 deletions kotlin-js-interop/js-to-kotlin/js-app/src/main/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {advocate, greet} from "./file-export.mjs";
import defaultGreet from "./func-export.mjs";

console.log(greet("Kotlin"));
console.log(advocate(1));
console.log(defaultGreet("Kotlin"));
11 changes: 11 additions & 0 deletions kotlin-js-interop/js-to-kotlin/js-app/src/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "main",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
36 changes: 36 additions & 0 deletions kotlin-js-interop/js-to-kotlin/kotlin-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
alias(libs.plugins.kotlinMultiplatform)
}

repositories {
mavenCentral()
}

kotlin {
applyDefaultHierarchyTemplate()
js(compiler = IR) {
// Explicitly instructs the Kotlin compiler to emit executable .js files,
// required when using IR compiler
binaries.executable()
nodejs {

}
// useCommonJs()
useEsModules()
}

sourceSets {
jsMain {
dependencies {
implementation(kotlin("stdlib-js"))
implementation(npm("randomstring", "1.3.0"))
implementation(npm("is-sorted", "1.0.5"))
}
}
jsTest {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit ea9ebe0

Please sign in to comment.