Skip to content

Commit dce81cb

Browse files
committed
[Wasm] Wasm target implementation
1 parent a79db37 commit dce81cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+981
-165
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ See [Contributing Guidelines](CONTRIBUTING.md).
215215
[Dispatchers.IO]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-i-o.html
216216
[asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html
217217
[Promise.await]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/await.html
218-
[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/promise.html
218+
[promise]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/[js]promise.html
219219
[Window.asCoroutineDispatcher]: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/as-coroutine-dispatcher.html
220220

221221
<!--- INDEX kotlinx.coroutines.flow -->

build.gradle

+19
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != core
157157
apply from: rootProject.file("gradle/compile-native-multiplatform.gradle")
158158
}
159159

160+
apply from: rootProject.file("gradle/compile-jsAndWasmShared-multiplatform.gradle")
161+
160162
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
163+
164+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
165+
161166
kotlin.sourceSets.commonMain.dependencies {
162167
api project(":$coreModule")
163168
}
@@ -368,3 +373,17 @@ if (CacheRedirector.enabled) {
368373
nodeJsExtension.nodeDownloadBaseUrl = CacheRedirector.maybeRedirect(nodeJsExtension.nodeDownloadBaseUrl)
369374
}
370375
}
376+
377+
// Drop this configuration when the Node.JS version in KGP will support wasm gc milestone 4
378+
// check it here:
379+
// https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsRootExtension.kt
380+
extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
381+
// canary nodejs that supports recent Wasm GC changes
382+
it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
383+
it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
384+
}
385+
386+
// Drop this when node js version become stable
387+
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask).configureEach {
388+
args.add("--ignore-engines")
389+
}

buildSrc/src/main/kotlin/KotlinVersion.kt

-14
This file was deleted.

gradle/compile-js-multiplatform.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ kotlin {
1212
}
1313

1414
sourceSets {
15+
jsMain {
16+
dependsOn(jsAndWasmSharedMain)
17+
}
18+
jsTest {
19+
dependsOn(jsAndWasmSharedTest)
20+
}
21+
1522
jsTest.dependencies {
1623
api "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
1724
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
kotlin {
6+
sourceSets {
7+
jsAndWasmSharedMain {
8+
dependsOn(commonMain)
9+
}
10+
jsAndWasmSharedTest {
11+
dependsOn(commonTest)
12+
}
13+
}
14+
}
15+
16+
// Disable intermediate sourceSet compilation because we do not need js-wasmJs artifact
17+
tasks.configureEach {
18+
if (name == 'compileJsAndWasmSharedMainKotlinMetadata') {
19+
enabled = false
20+
}
21+
}

gradle/compile-native-multiplatform.gradle

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import static KotlinVersion.isKotlinVersionAtLeast
2-
31
/*
42
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
53
*/
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
kotlin {
6+
wasmJs {
7+
moduleName = project.name
8+
nodejs()
9+
compilations['main']?.dependencies {
10+
api "org.jetbrains.kotlinx:atomicfu-wasm-js:$atomicfu_version"
11+
}
12+
}
13+
14+
sourceSets {
15+
wasmJsMain {
16+
dependsOn(jsAndWasmSharedMain)
17+
}
18+
wasmJsTest {
19+
dependsOn(jsAndWasmSharedTest)
20+
}
21+
22+
wasmJsTest.dependencies {
23+
api "org.jetbrains.kotlin:kotlin-test-wasm-js:$kotlin_version"
24+
}
25+
}
26+
}

gradle/dokka.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ tasks.withType(DokkaTaskPartial::class).configureEach {
4545
}
4646
}
4747

48+
val kotlin_version: String by project
49+
4850
if (project.name == "kotlinx-coroutines-core") {
4951
// Custom configuration for MPP modules
5052
tasks.withType(DokkaTaskPartial::class).configureEach {
@@ -64,6 +66,10 @@ if (project.name == "kotlinx-coroutines-core") {
6466
val jvmMain by getting {
6567
makeLinkMapping(project.file("jvm"))
6668
}
69+
70+
val wasmJsMain by getting {
71+
makeLinkMapping(project.file("wasm"))
72+
}
6773
}
6874
}
6975
}

integration-testing/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,8 @@ compileKotlin {
179179
jvmTarget = "1.8"
180180
}
181181
}
182+
183+
// Drop this when node js version become stable
184+
tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask.class).configureEach {
185+
it.args.add("--ignore-engines")
186+
}

integration-testing/smokeTest/build.gradle

+18-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ plugins {
33
}
44

55
repositories {
6-
// Coroutines from the outer project are published by previous CI buils step
7-
mavenLocal()
86
mavenCentral()
97
maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
8+
// Coroutines from the outer project are published by previous CI buils step
9+
mavenLocal()
1010
}
1111

1212
kotlin {
1313
jvm()
1414
js(IR) {
1515
nodejs()
1616
}
17+
wasmJs() {
18+
nodejs()
19+
}
1720

1821
sourceSets {
1922
commonMain {
@@ -34,6 +37,11 @@ kotlin {
3437
implementation kotlin('test-js')
3538
}
3639
}
40+
wasmJsTest {
41+
dependencies {
42+
implementation kotlin('test-wasm-js')
43+
}
44+
}
3745
jvmTest {
3846
dependencies {
3947
implementation kotlin('test')
@@ -50,3 +58,11 @@ kotlin {
5058
}
5159
}
5260

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

kotlinx-coroutines-core/build.gradle

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ if (rootProject.ext.native_targets_enabled) {
1919
apply from: rootProject.file("gradle/compile-native-multiplatform.gradle")
2020
}
2121

22+
apply from: rootProject.file("gradle/compile-jsAndWasmShared-multiplatform.gradle")
23+
2224
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
2325

26+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
27+
2428
apply from: rootProject.file('gradle/dokka.gradle.kts')
2529
apply from: rootProject.file('gradle/publish.gradle')
2630
/* ==========================================================================
2731
Configure source sets structure for kotlinx-coroutines-core:
2832
2933
TARGETS SOURCE SETS
3034
------- ----------------------------------------------
31-
32-
js -----------------------------------------------------+
33-
|
35+
wasmJs \----------> jsAndWasmShared --------------------+
36+
js / |
3437
V
3538
jvmCore\ --------> jvm ---------> concurrent -------> common
3639
jdk8 / ^
@@ -242,7 +245,7 @@ kotlin.sourceSets {
242245

243246
kotlin.sourceSets.configureEach {
244247
// Do not apply 'ExperimentalForeignApi' where we have allWarningsAsErrors set
245-
if (it.name in ["jvmMain", "jvmCoreMain", "jsMain", "concurrentMain", "commonMain"]) return
248+
if (it.name in ["jvmMain", "jvmCoreMain", "jsMain", 'wasmJsMain', 'jsAndWasmSharedMain', "concurrentMain", "commonMain"]) return
246249
languageSettings {
247250
optIn('kotlinx.cinterop.ExperimentalForeignApi')
248251
optIn('kotlin.experimental.ExperimentalNativeApi')

kotlinx-coroutines-core/js/src/CoroutineContext.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private external val navigator: dynamic
1212
private const val UNDEFINED = "undefined"
1313
internal external val process: dynamic
1414

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

0 commit comments

Comments
 (0)