Skip to content

Commit

Permalink
WASM target implementation (Kotlin#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
igoriakovlev committed Dec 1, 2023
1 parent 17bdf4a commit 85afa72
Show file tree
Hide file tree
Showing 53 changed files with 981 additions and 165 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ See [Contributing Guidelines](CONTRIBUTING.md).
[Dispatchers.IO]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-i-o.html
[asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html
[Promise.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await.html
[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.html
[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/[js]promise.html
[Window.asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html

<!--- INDEX kotlinx.coroutines.flow -->
Expand Down
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != core
apply from: rootProject.file("gradle/compile-native-multiplatform.gradle")
}

apply from: rootProject.file("gradle/compile-jsAndWasmShared-multiplatform.gradle")

apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")

apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")

kotlin.sourceSets.commonMain.dependencies {
api project(":$coreModule")
}
Expand Down Expand Up @@ -335,3 +340,17 @@ if (CacheRedirector.enabled) {
nodeJsExtension.nodeDownloadBaseUrl = CacheRedirector.maybeRedirect(nodeJsExtension.nodeDownloadBaseUrl)
}
}

// Drop this configuration when the Node.JS version in KGP will support wasm gc milestone 4
// check it here:
// https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsRootExtension.kt
extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
// canary nodejs that supports recent Wasm GC changes
it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

// Drop this when node js version become stable
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask).configureEach {
args.add("--ignore-engines")
}
14 changes: 0 additions & 14 deletions buildSrc/src/main/kotlin/KotlinVersion.kt

This file was deleted.

7 changes: 7 additions & 0 deletions gradle/compile-js-multiplatform.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ kotlin {
}

sourceSets {
jsMain {
dependsOn(jsAndWasmSharedMain)
}
jsTest {
dependsOn(jsAndWasmSharedTest)
}

jsTest.dependencies {
api "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
}
Expand Down
21 changes: 21 additions & 0 deletions gradle/compile-jsAndWasmShared-multiplatform.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

kotlin {
sourceSets {
jsAndWasmSharedMain {
dependsOn(commonMain)
}
jsAndWasmSharedTest {
dependsOn(commonTest)
}
}
}

// Disable intermediate sourceSet compilation because we do not need js-wasmJs artifact
tasks.configureEach {
if (name == 'compileJsAndWasmSharedMainKotlinMetadata') {
enabled = false
}
}
2 changes: 0 additions & 2 deletions gradle/compile-native-multiplatform.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import static KotlinVersion.isKotlinVersionAtLeast

/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
Expand Down
26 changes: 26 additions & 0 deletions gradle/compile-wasm-multiplatform.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

kotlin {
wasmJs {
moduleName = project.name
nodejs()
compilations['main']?.dependencies {
api "org.jetbrains.kotlinx:atomicfu-wasm-js:$atomicfu_version"
}
}

sourceSets {
wasmJsMain {
dependsOn(jsAndWasmSharedMain)
}
wasmJsTest {
dependsOn(jsAndWasmSharedTest)
}

wasmJsTest.dependencies {
api "org.jetbrains.kotlin:kotlin-test-wasm-js:$kotlin_version"
}
}
}
6 changes: 6 additions & 0 deletions gradle/dokka.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ tasks.withType(DokkaTaskPartial::class).configureEach {
}
}

val kotlin_version: String by project

if (project.name == "kotlinx-coroutines-core") {
// Custom configuration for MPP modules
tasks.withType(DokkaTaskPartial::class).configureEach {
Expand All @@ -64,6 +66,10 @@ if (project.name == "kotlinx-coroutines-core") {
val jvmMain by getting {
makeLinkMapping(project.file("jvm"))
}

val wasmJsMain by getting {
makeLinkMapping(project.file("wasm"))
}
}
}
}
5 changes: 5 additions & 0 deletions integration-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,8 @@ compileKotlin {
jvmTarget = "1.8"
}
}

// Drop this when node js version become stable
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
it.args.add("--ignore-engines")
}
20 changes: 18 additions & 2 deletions integration-testing/smokeTest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ plugins {
}

repositories {
// Coroutines from the outer project are published by previous CI buils step
mavenLocal()
mavenCentral()
maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
// Coroutines from the outer project are published by previous CI buils step
mavenLocal()
}

kotlin {
jvm()
js(IR) {
nodejs()
}
wasmJs() {
nodejs()
}

sourceSets {
commonMain {
Expand All @@ -34,6 +37,11 @@ kotlin {
implementation kotlin('test-js')
}
}
wasmJsTest {
dependencies {
implementation kotlin('test-wasm-js')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
Expand All @@ -50,3 +58,11 @@ kotlin {
}
}

// Drop this configuration when the Node.JS version in KGP will support wasm gc milestone 4
// check it here:
// https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsRootExtension.kt
rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
// canary nodejs that supports recent Wasm GC changes
it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}
11 changes: 7 additions & 4 deletions kotlinx-coroutines-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ if (rootProject.ext.native_targets_enabled) {
apply from: rootProject.file("gradle/compile-native-multiplatform.gradle")
}

apply from: rootProject.file("gradle/compile-jsAndWasmShared-multiplatform.gradle")

apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")

apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")

apply from: rootProject.file('gradle/dokka.gradle.kts')
apply from: rootProject.file('gradle/publish.gradle')
/* ==========================================================================
Configure source sets structure for kotlinx-coroutines-core:
TARGETS SOURCE SETS
------- ----------------------------------------------
js -----------------------------------------------------+
|
wasmJs \----------> jsAndWasmShared --------------------+
js / |
V
jvmCore\ --------> jvm ---------> concurrent -------> common
jdk8 / ^
Expand Down Expand Up @@ -242,7 +245,7 @@ kotlin.sourceSets {

kotlin.sourceSets.configureEach {
// Do not apply 'ExperimentalForeignApi' where we have allWarningsAsErrors set
if (it.name in ["jvmMain", "jvmCoreMain", "jsMain", "concurrentMain", "commonMain"]) return
if (it.name in ["jvmMain", "jvmCoreMain", "jsMain", 'wasmJsMain', 'jsAndWasmSharedMain', "concurrentMain", "commonMain"]) return
languageSettings {
optIn('kotlinx.cinterop.ExperimentalForeignApi')
optIn('kotlin.experimental.ExperimentalNativeApi')
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/js/src/CoroutineContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private external val navigator: dynamic
private const val UNDEFINED = "undefined"
internal external val process: dynamic

internal fun createDefaultDispatcher(): CoroutineDispatcher = when {
internal actual fun createDefaultDispatcher(): CoroutineDispatcher = when {
// Check if we are running under jsdom. WindowDispatcher doesn't work under jsdom because it accesses MessageEvent#source.
// It is not implemented in jsdom, see https://github.com/jsdom/jsdom/blob/master/Changelog.md
// "It's missing a few semantics, especially around origins, as well as MessageEvent source."
Expand Down
Loading

0 comments on commit 85afa72

Please sign in to comment.