Skip to content

Commit 5a5e0fb

Browse files
skuzmichigoriakovlev
authored andcommitted
[Wasm] Wasm target implementation
1 parent 782e22b commit 5a5e0fb

39 files changed

+827
-6
lines changed

build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != core
160160
}
161161

162162
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
163+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
163164
kotlin.sourceSets.commonMain.dependencies {
164165
api project(":$coreModule")
165166
}
@@ -370,3 +371,7 @@ if (CacheRedirector.enabled) {
370371
nodeJsExtension.nodeDownloadBaseUrl = CacheRedirector.maybeRedirect(nodeJsExtension.nodeDownloadBaseUrl)
371372
}
372373
}
374+
375+
extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
376+
it.nodeVersion = "20.2.0"
377+
}

gradle/compile-js-multiplatform.gradle

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

1414
sourceSets {
15+
jsMain {
16+
kotlin {
17+
srcDir 'jsWasm/src'
18+
}
19+
}
20+
jsTest {
21+
kotlin {
22+
srcDir 'jsWasm/test'
23+
}
24+
}
1525
jsTest.dependencies {
1626
api "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
1727
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}
19+
}
20+
wasmTest {
21+
kotlin {
22+
srcDir 'jsWasm/test'
23+
}
24+
}
25+
wasmTest.dependencies {
26+
api "org.jetbrains.kotlin:kotlin-test-wasm:$kotlin_version"
27+
}
28+
}
29+
}
30+

kotlinx-coroutines-core/build.gradle

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
2+
13
/*
24
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
35
*/
@@ -16,6 +18,7 @@ if (rootProject.ext.native_targets_enabled) {
1618
}
1719

1820
apply from: rootProject.file("gradle/compile-js-multiplatform.gradle")
21+
apply from: rootProject.file("gradle/compile-wasm-multiplatform.gradle")
1922

2023
apply from: rootProject.file('gradle/dokka.gradle.kts')
2124
apply from: rootProject.file('gradle/publish.gradle')
@@ -265,6 +268,15 @@ jvmTest {
265268
// Setup manifest for kotlinx-coroutines-core-jvm.jar
266269
jvmJar { setupManifest(it) }
267270

271+
wasmNodeTest {
272+
filter.with {
273+
excludeTest("ChannelsTest", "testIterableAsReceiveChannel")
274+
excludeTest("ChannelsTest", "testEmptyList")
275+
excludeTest("ChannelsTest", "testToList")
276+
excludeTest("ChannelBuildersFlowTest", "testConsumeAsFlowProduceBuffered")
277+
}
278+
}
279+
268280
/*
269281
* Setup manifest for kotlinx-coroutines-core.jar
270282
* This is convenient for users that pass -javaagent arg manually and also is a workaround #2619 and KTIJ-5659.
@@ -376,4 +388,4 @@ task testsJar(type: Jar, dependsOn: jvmTestClasses) {
376388

377389
artifacts {
378390
archives testsJar
379-
}
391+
}

kotlinx-coroutines-core/js/src/flow/internal/SafeCollector.kt renamed to kotlinx-coroutines-core/jsWasm/src/flow/internal/SafeCollector.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ internal actual class SafeCollector<T> actual constructor(
2929

3030
public actual fun releaseIntercepted() {
3131
}
32-
}
32+
}

kotlinx-coroutines-core/js/src/internal/CoroutineExceptionHandlerImpl.kt renamed to kotlinx-coroutines-core/jsWasm/src/internal/CoroutineExceptionHandlerImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal actual fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExce
1818

1919
internal actual fun propagateExceptionFinalResort(exception: Throwable) {
2020
// log exception
21-
console.error(exception)
21+
console.error(exception.toString())
2222
}
2323

2424
internal actual class DiagnosticCoroutineContextException actual constructor(context: CoroutineContext) :

kotlinx-coroutines-core/js/src/internal/ThreadLocal.kt renamed to kotlinx-coroutines-core/jsWasm/src/internal/ThreadLocal.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ internal actual class CommonThreadLocal<T> {
1111
actual fun set(value: T) { this.value = value }
1212
}
1313

14-
internal actual fun<T> commonThreadLocal(name: Symbol): CommonThreadLocal<T> = CommonThreadLocal()
14+
internal actual fun<T> commonThreadLocal(name: Symbol): CommonThreadLocal<T> = CommonThreadLocal()

kotlinx-coroutines-core/js/test/ImmediateDispatcherTest.kt renamed to kotlinx-coroutines-core/jsWasm/test/ImmediateDispatcherTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.coroutines
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
package kotlinx.coroutines
6+
7+
import kotlinx.coroutines.internal.*
8+
9+
internal actual abstract class CompletionHandlerBase actual constructor() : LockFreeLinkedListNode(), CompletionHandler {
10+
actual abstract override fun invoke(cause: Throwable?)
11+
}
12+
13+
internal actual inline val CompletionHandlerBase.asHandler: CompletionHandler get() = this
14+
15+
internal actual abstract class CancelHandlerBase actual constructor() : CompletionHandler {
16+
actual abstract override fun invoke(cause: Throwable?)
17+
}
18+
19+
internal actual inline val CancelHandlerBase.asHandler: CompletionHandler get() = this
20+
21+
@Suppress("NOTHING_TO_INLINE")
22+
internal actual inline fun CompletionHandler.invokeIt(cause: Throwable?) = invoke(cause)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
package kotlinx.coroutines
6+
7+
import kotlinx.coroutines.internal.*
8+
import org.w3c.dom.*
9+
import kotlin.coroutines.*
10+
11+
internal external interface JsProcess : JsAny {
12+
fun nextTick(handler: () -> Unit)
13+
}
14+
15+
internal fun tryGetProcess(): JsProcess? =
16+
js("(typeof(process) !== 'undefined' && typeof(process.nextTick) === 'function') ? process : null")
17+
18+
internal fun tryGetWindow(): Window? =
19+
js("(typeof(window) !== 'undefined' && window != null && typeof(window.addEventListener) === 'function') ? window : null")
20+
21+
internal fun createDefaultDispatcher(): CoroutineDispatcher =
22+
tryGetProcess()?.let(::NodeDispatcher)
23+
?: tryGetWindow()?.let(::WindowDispatcher)
24+
?: SetTimeoutDispatcher
25+
26+
@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI
27+
internal actual val DefaultDelay: Delay
28+
get() = Dispatchers.Default as Delay
29+
30+
public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
31+
val combined = coroutineContext + context
32+
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
33+
combined + Dispatchers.Default else combined
34+
}
35+
36+
public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {
37+
return this + addedContext
38+
}
39+
40+
// No debugging facilities on JS
41+
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
42+
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
43+
internal actual fun Continuation<*>.toDebugString(): String = toString()
44+
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on JS
45+
46+
internal actual class UndispatchedCoroutine<in T> actual constructor(
47+
context: CoroutineContext,
48+
uCont: Continuation<T>
49+
) : ScopeCoroutine<T>(context, uCont) {
50+
override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
51+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
package kotlinx.coroutines
6+
7+
internal actual val DEBUG: Boolean = false
8+
9+
internal actual val Any.hexAddress: String
10+
get() = this.hashCode().toString()
11+
12+
internal actual val Any.classSimpleName: String get() = this::class.simpleName ?: "Unknown"
13+
14+
internal actual inline fun assert(value: () -> Boolean) {}
15+
16+
internal external interface Console {
17+
fun error(s: String)
18+
}
19+
20+
internal external val console: Console

0 commit comments

Comments
 (0)