Skip to content

Commit 2a385fd

Browse files
committed
Support kotlin 1.9.20+
1 parent 086658f commit 2a385fd

File tree

6 files changed

+81
-19
lines changed

6 files changed

+81
-19
lines changed

build.gradle

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension
1212
import org.jetbrains.kotlin.konan.target.HostManager
1313
import org.jetbrains.dokka.gradle.DokkaTaskPartial
1414

15+
import static KotlinVersion.isKotlinVersionAtLeast
1516
import static Projects.*
1617

1718
apply plugin: 'jdk-convention'
@@ -160,7 +161,15 @@ configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != core
160161
}
161162

162163
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
163-
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
164+
165+
// TODO: Wasm target names have been changed since 1.9.20, remove this after migration to 1.9.20
166+
def isNewWasmTargetEnabled = isKotlinVersionAtLeast(ext.kotlin_version, 1, 9, 20)
167+
if (isNewWasmTargetEnabled) {
168+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
169+
} else {
170+
apply from: rootProject.file("gradle/compile-wasm-multiplatform19.gradle")
171+
}
172+
164173
kotlin.sourceSets.commonMain.dependencies {
165174
api project(":$coreModule")
166175
}

gradle/compile-wasm-multiplatform.gradle

+9-8
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
*/
44

55
kotlin {
6-
wasm {
6+
wasmJs {
77
moduleName = project.name
88
nodejs()
99
compilations['main']?.dependencies {
10-
api "org.jetbrains.kotlinx:atomicfu-wasm:$atomicfu_version"
10+
api "org.jetbrains.kotlinx:atomicfu-wasm-js:$atomicfu_version"
1111
}
1212
}
1313

1414
sourceSets {
15-
wasmMain {
15+
wasmJsMain {
1616
kotlin {
1717
srcDir 'jsWasm/src'
18+
srcDir 'wasm/src'
1819
}
1920
}
20-
wasmTest {
21+
wasmJsTest {
2122
kotlin {
2223
srcDir 'jsWasm/test'
24+
srcDir 'wasm/test'
2325
}
2426
}
25-
wasmTest.dependencies {
26-
api "org.jetbrains.kotlin:kotlin-test-wasm:$kotlin_version"
27+
wasmJsTest.dependencies {
28+
api "org.jetbrains.kotlin:kotlin-test-wasm-js:$kotlin_version"
2729
}
2830
}
29-
}
30-
31+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
kotlin {
6+
wasm {
7+
moduleName = project.name
8+
nodejs()
9+
compilations['main']?.dependencies {
10+
api "org.jetbrains.kotlinx:atomicfu-wasm:$atomicfu_version"
11+
}
12+
}
13+
14+
sourceSets {
15+
wasmMain {
16+
kotlin {
17+
srcDir 'jsWasm/src'
18+
srcDir 'wasm/src'
19+
}
20+
}
21+
wasmTest {
22+
kotlin {
23+
srcDir 'jsWasm/test'
24+
srcDir 'wasm/test'
25+
}
26+
}
27+
wasmTest.dependencies {
28+
api "org.jetbrains.kotlin:kotlin-test-wasm:$kotlin_version"
29+
}
30+
}
31+
}

kotlinx-coroutines-core/build.gradle

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
2+
import static KotlinVersion.isKotlinVersionAtLeast
23

34
/*
45
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
@@ -18,7 +19,14 @@ if (rootProject.ext.native_targets_enabled) {
1819
}
1920

2021
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
21-
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
22+
23+
// TODO: Wasm target names have been changed since 1.9.20, remove this after migration to 1.9.20
24+
def isNewWasmTargetEnabled = isKotlinVersionAtLeast(ext.kotlin_version, 1, 9, 20)
25+
if (isNewWasmTargetEnabled) {
26+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
27+
} else {
28+
apply from: rootProject.file("gradle/compile-wasm-multiplatform19.gradle")
29+
}
2230

2331
apply from: rootProject.file('gradle/dokka.gradle.kts')
2432
apply from: rootProject.file('gradle/publish.gradle')
@@ -268,12 +276,20 @@ jvmTest {
268276
// Setup manifest for kotlinx-coroutines-core-jvm.jar
269277
jvmJar { setupManifest(it) }
270278

271-
wasmNodeTest {
272-
filter.with {
273-
excludeTest("ChannelsTest", "testIterableAsReceiveChannel")
274-
excludeTest("ChannelsTest", "testEmptyList")
275-
excludeTest("ChannelsTest", "testToList")
276-
excludeTest("ChannelBuildersFlowTest", "testConsumeAsFlowProduceBuffered")
279+
void addIngoreTestsForWasm(org.gradle.api.tasks.testing.TestFilter filter) {
280+
filter.excludeTest("ChannelsTest", "testIterableAsReceiveChannel")
281+
filter.excludeTest("ChannelsTest", "testEmptyList")
282+
filter.excludeTest("ChannelsTest", "testToList")
283+
filter.excludeTest("ChannelBuildersFlowTest", "testConsumeAsFlowProduceBuffered")
284+
}
285+
286+
if (isNewWasmTargetEnabled) {
287+
wasmJsNodeTest {
288+
addIngoreTestsForWasm(filter)
289+
}
290+
} else {
291+
wasmNodeTest {
292+
addIngoreTestsForWasm(filter)
277293
}
278294
}
279295

kotlinx-coroutines-core/wasm/src/JSDispatcher.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,24 @@ private open class ClearTimeout(protected val handle: Int) : CancelHandler(), Di
7878
}
7979

8080
@Suppress("UNUSED_PARAMETER")
81-
private fun toJsAnyCallback(handle: () -> Unit): JsAny = js("handle")
81+
private fun setTimeout(
82+
window: WindowOrWorkerGlobalScope,
83+
handler: () -> Unit,
84+
timeout: Int,
85+
): Int = js("window.setTimeout(handler, timeout)")
8286

8387
internal class WindowDispatcher(private val window: Window) : CoroutineDispatcher(), Delay {
8488
private val queue = WindowMessageQueue(window)
8589

8690
override fun dispatch(context: CoroutineContext, block: Runnable) = queue.enqueue(block)
8791

8892
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
89-
val handle = window.setTimeout(toJsAnyCallback { with(continuation) { resumeUndispatched(Unit) } }, delayToInt(timeMillis))
93+
val handle = setTimeout(window, { with(continuation) { resumeUndispatched(Unit) } }, delayToInt(timeMillis))
9094
continuation.invokeOnCancellation(handler = WindowClearTimeout(handle).asHandler)
9195
}
9296

9397
override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
94-
val handle = window.setTimeout(toJsAnyCallback(block::run), delayToInt(timeMillis))
98+
val handle = setTimeout(window, block::run, delayToInt(timeMillis))
9599
return WindowClearTimeout(handle)
96100
}
97101

kotlinx-coroutines-core/wasm/test/TestBase.kt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public actual typealias TestResult = Promise<JsAny?>
1515

1616
public actual val isNative = false
1717

18+
@Suppress("NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS") // Counterpart for @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
1819
public actual open class TestBase actual constructor() {
1920
public actual val isBoundByJsTestTimeout = true
2021
private var actionIndex = 0

0 commit comments

Comments
 (0)