Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm wasi #4064

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/AuxBuildConfiguration.kt
Expand Up @@ -19,7 +19,6 @@ object AuxBuildConfiguration {
}

CacheRedirector.configureJsPackageManagers(rootProject)
CacheRedirector.configureWasmNodeRepositories(rootProject)

// Sigh, there is no BuildScanExtension in classpath when there is no --scan
rootProject.extensions.findByName("buildScan")?.withGroovyBuilder {
Expand Down
17 changes: 0 additions & 17 deletions buildSrc/src/main/kotlin/CacheRedirector.kt
Expand Up @@ -138,23 +138,6 @@ object CacheRedirector {
project.configureYarnAndNodeRedirects()
}

/**
* Temporary repositories to depend on until GC milestone 4 in KGP
* and stable Node release. Safe to remove when its removal does not break WASM tests.
*/
@JvmStatic
fun configureWasmNodeRepositories(project: Project) {
val extension = project.extensions.findByType<NodeJsRootExtension>()
if (extension != null) {
extension.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
extension.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

project.tasks.withType<KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}
}

@JvmStatic
fun maybeRedirect(url: String): String {
if (!cacheRedirectorEnabled) return url
Expand Down
Expand Up @@ -63,6 +63,18 @@ kotlin {
api("org.jetbrains.kotlinx:atomicfu-wasm-js:${version("atomicfu")}")
}
}
@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
wasmWasi {
nodejs()
compilations["main"]?.dependencies {
api("org.jetbrains.kotlinx:atomicfu-wasm-wasi:${version("atomicfu")}")
}
compilations.configureEach {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't do this in configure-compilation-conventions.gradle.kts because there is no way to distingish WasmWasi target from WasmJs or JS targets there.

compilerOptions.configure {
optIn.add("kotlin.wasm.internal.InternalWasmApi")
}
}
}
applyDefaultHierarchyTemplate()
sourceSets {
commonTest {
Expand Down Expand Up @@ -101,15 +113,26 @@ kotlin {
api("org.jetbrains.kotlin:kotlin-test-wasm-js:${version("kotlin")}")
}
}
groupSourceSets("jsAndWasmShared", listOf("js", "wasmJs"), listOf("common"))
val wasmWasiMain by getting {
}
val wasmWasiTest by getting {
dependencies {
api("org.jetbrains.kotlin:kotlin-test-wasm-wasi:${version("kotlin")}")
}
}
groupSourceSets("jsAndWasmJsShared", listOf("js", "wasmJs"), emptyList())
groupSourceSets("jsAndWasmShared", listOf("jsAndWasmJsShared", "wasmWasi"), listOf("common"))
}
}

// Disable intermediate sourceSet compilation because we do not need js-wasmJs artifact
// Disable intermediate sourceSet compilation because we do not need js-wasm common artifact
tasks.configureEach {
if (name == "compileJsAndWasmSharedMainKotlinMetadata") {
enabled = false
}
if (name == "compileJsAndWasmJsSharedMainKotlinMetadata") {
enabled = false
}
}

tasks.named("jvmTest", Test::class) {
Expand Down
5 changes: 5 additions & 0 deletions integration-testing/smokeTest/build.gradle
Expand Up @@ -48,6 +48,11 @@ kotlin {
implementation kotlin('test-wasm-js')
}
}
wasmWasiTest {
dependencies {
implementation kotlin('test-wasm-wasi')
}
}
jvmTest {
dependencies {
implementation kotlin('test')
Expand Down
@@ -1,5 +1,5 @@
// Klib ABI Dump
// Targets: [androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86, iosArm64, iosSimulatorArm64, iosX64, js, linuxArm64, linuxX64, macosArm64, macosX64, mingwX64, tvosArm64, tvosSimulatorArm64, tvosX64, wasmJs, watchosArm32, watchosArm64, watchosDeviceArm64, watchosSimulatorArm64, watchosX64]
// Targets: [androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86, iosArm64, iosSimulatorArm64, iosX64, js, linuxArm64, linuxX64, macosArm64, macosX64, mingwX64, tvosArm64, tvosSimulatorArm64, tvosX64, wasmJs, wasmWasi, watchosArm32, watchosArm64, watchosDeviceArm64, watchosSimulatorArm64, watchosX64]
// Alias: native => [androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86, iosArm64, iosSimulatorArm64, iosX64, linuxArm64, linuxX64, macosArm64, macosX64, mingwX64, tvosArm64, tvosSimulatorArm64, tvosX64, watchosArm32, watchosArm64, watchosDeviceArm64, watchosSimulatorArm64, watchosX64]
// Rendering settings:
// - Signature version: 2
Expand Down
11 changes: 7 additions & 4 deletions kotlinx-coroutines-core/build.gradle.kts
Expand Up @@ -18,10 +18,13 @@ apply(plugin = "pub-conventions")
Configure source sets structure for kotlinx-coroutines-core:
TARGETS SOURCE SETS
------- ----------------------------------------------
wasmJs \----------> jsAndWasmShared --------------------+
js / |
V
------------------------------------------------------------
wasmJs \------> jsAndWasmJsShared ----+
js / |
V
wasmWasi --------------------> jsAndWasmShared ----------+
|
V
jvmCore\ --------> jvm ---------> concurrent -------> common
jdk8 / ^
|
Expand Down
16 changes: 15 additions & 1 deletion kotlinx-coroutines-core/common/test/flow/VirtualTime.kt
Expand Up @@ -22,7 +22,21 @@ internal class VirtualTimeDispatcher(enclosingScope: CoroutineScope) : Coroutine
val delayNanos = ThreadLocalEventLoop.currentOrNull()?.processNextEvent()
?: error("Event loop is missing, virtual time source works only as part of event loop")
if (delayNanos <= 0) continue
if (delayNanos > 0 && delayNanos != Long.MAX_VALUE) error("Unexpected external delay: $delayNanos")
if (delayNanos > 0 && delayNanos != Long.MAX_VALUE) {
if (usesSharedEventLoop) {
val targetTime = currentTime + delayNanos
while (currentTime < targetTime) {
val nextTask = heap.minByOrNull { it.deadline } ?: break
if (nextTask.deadline > targetTime) break
heap.remove(nextTask)
currentTime = nextTask.deadline
nextTask.run()
}
currentTime = maxOf(currentTime, targetTime)
} else {
error("Unexpected external delay: $delayNanos")
}
}
val nextTask = heap.minByOrNull { it.deadline } ?: return@launch
heap.remove(nextTask)
currentTime = nextTask.deadline
Expand Down
29 changes: 0 additions & 29 deletions kotlinx-coroutines-core/js/src/CoroutineContext.kt
@@ -1,8 +1,6 @@
package kotlinx.coroutines

import kotlinx.browser.*
import kotlinx.coroutines.internal.*
import kotlin.coroutines.*

private external val navigator: dynamic
private const val UNDEFINED = "undefined"
Expand All @@ -28,30 +26,3 @@ private fun isJsdom() = jsTypeOf(navigator) != UNDEFINED &&
jsTypeOf(navigator.userAgent) != UNDEFINED &&
jsTypeOf(navigator.userAgent.match) != UNDEFINED &&
navigator.userAgent.match("\\bjsdom\\b")

@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI
internal actual val DefaultDelay: Delay
get() = Dispatchers.Default as Delay

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
val combined = coroutineContext + context
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
combined + Dispatchers.Default else combined
}

public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {
return this + addedContext
}

// No debugging facilities on JS
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on JS

internal actual class UndispatchedCoroutine<in T> actual constructor(
context: CoroutineContext,
uCont: Continuation<T>
) : ScopeCoroutine<T>(context, uCont) {
override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
}
31 changes: 31 additions & 0 deletions kotlinx-coroutines-core/jsAndWasmShared/src/CoroutineContext.kt
@@ -0,0 +1,31 @@
package kotlinx.coroutines

import kotlinx.coroutines.internal.ScopeCoroutine
import kotlin.coroutines.*

@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI
internal actual val DefaultDelay: Delay
get() = Dispatchers.Default as Delay

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
val combined = coroutineContext + context
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
combined + Dispatchers.Default else combined
}

public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {
return this + addedContext
}

// No debugging facilities on Wasm and JS
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on Wasm and JS

internal actual class UndispatchedCoroutine<in T> actual constructor(
context: CoroutineContext,
uCont: Continuation<T>
) : ScopeCoroutine<T>(context, uCont) {
override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
}
29 changes: 0 additions & 29 deletions kotlinx-coroutines-core/wasmJs/src/CoroutineContext.kt
@@ -1,8 +1,6 @@
package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import org.w3c.dom.*
import kotlin.coroutines.*

internal external interface JsProcess : JsAny {
fun nextTick(handler: () -> Unit)
Expand All @@ -18,30 +16,3 @@ internal actual fun createDefaultDispatcher(): CoroutineDispatcher =
tryGetProcess()?.let(::NodeDispatcher)
?: tryGetWindow()?.let(::WindowDispatcher)
?: SetTimeoutDispatcher

@PublishedApi // Used from kotlinx-coroutines-test via suppress, not part of ABI
internal actual val DefaultDelay: Delay
get() = Dispatchers.Default as Delay

public actual fun CoroutineScope.newCoroutineContext(context: CoroutineContext): CoroutineContext {
val combined = coroutineContext + context
return if (combined !== Dispatchers.Default && combined[ContinuationInterceptor] == null)
combined + Dispatchers.Default else combined
}

public actual fun CoroutineContext.newCoroutineContext(addedContext: CoroutineContext): CoroutineContext {
return this + addedContext
}

// No debugging facilities on Wasm
internal actual inline fun <T> withCoroutineContext(context: CoroutineContext, countOrElement: Any?, block: () -> T): T = block()
internal actual inline fun <T> withContinuationContext(continuation: Continuation<*>, countOrElement: Any?, block: () -> T): T = block()
internal actual fun Continuation<*>.toDebugString(): String = toString()
internal actual val CoroutineContext.coroutineName: String? get() = null // not supported on Wasm

internal actual class UndispatchedCoroutine<in T> actual constructor(
context: CoroutineContext,
uCont: Continuation<T>
) : ScopeCoroutine<T>(context, uCont) {
override fun afterResume(state: Any?) = uCont.resumeWith(recoverResult(state, uCont))
}
10 changes: 10 additions & 0 deletions kotlinx-coroutines-core/wasmWasi/src/Debug.kt
@@ -0,0 +1,10 @@
package kotlinx.coroutines

internal actual val DEBUG: Boolean = false

internal actual val Any.hexAddress: String
get() = this.hashCode().toString()

internal actual val Any.classSimpleName: String get() = this::class.simpleName ?: "Unknown"

internal actual inline fun assert(value: () -> Boolean) {}
120 changes: 120 additions & 0 deletions kotlinx-coroutines-core/wasmWasi/src/EventLoop.kt
@@ -0,0 +1,120 @@
@file:OptIn(UnsafeWasmMemoryApi::class)
package kotlinx.coroutines

import kotlin.coroutines.CoroutineContext
import kotlin.wasm.*
import kotlin.wasm.unsafe.*

@WasmImport("wasi_snapshot_preview1", "poll_oneoff")
private external fun wasiPollOneOff(ptrToSubscription: Int, eventPtr: Int, nsubscriptions: Int, resultPtr: Int): Int

@WasmImport("wasi_snapshot_preview1", "clock_time_get")
private external fun wasiRawClockTimeGet(clockId: Int, precision: Long, resultPtr: Int): Int

private const val CLOCKID_MONOTONIC = 1

internal actual fun createEventLoop(): EventLoop = DefaultExecutor

internal actual fun nanoTime(): Long = withScopedMemoryAllocator { allocator: MemoryAllocator ->
val ptrTo8Bytes = allocator.allocate(8)
val returnCode = wasiRawClockTimeGet(
clockId = CLOCKID_MONOTONIC,
precision = 1,
resultPtr = ptrTo8Bytes.address.toInt()
)
check(returnCode == 0) { "clock_time_get failed with the return code $returnCode" }
ptrTo8Bytes.loadLong()
}

private fun sleep(nanos: Long, ptrTo32Bytes: Pointer, ptrTo8Bytes: Pointer, ptrToSubscription: Pointer) {
//__wasi_timestamp_t timeout;
(ptrToSubscription + 24).storeLong(nanos)
val returnCode = wasiPollOneOff(
ptrToSubscription = ptrToSubscription.address.toInt(),
eventPtr = ptrTo32Bytes.address.toInt(),
nsubscriptions = 1,
resultPtr = ptrTo8Bytes.address.toInt()
)
check(returnCode == 0) { "poll_oneoff failed with the return code $returnCode" }
}

internal actual object DefaultExecutor : EventLoopImplBase() {
override fun shutdown() {
// don't do anything: on WASI, the event loop is the default executor, we can't shut it down
}

override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle =
scheduleInvokeOnTimeout(timeMillis, block)

actual override fun enqueue(task: Runnable) {
if (kotlin.wasm.internal.onExportedFunctionExit == null) {
kotlin.wasm.internal.onExportedFunctionExit = ::runEventLoop
}
super.enqueue(task)
}
}

internal actual abstract class EventLoopImplPlatform : EventLoop() {
protected actual fun unpark() {
// do nothing: in WASI, no external callbacks can be invoked while `poll_oneoff` is running,
// so it is both impossible and unnecessary to unpark the event loop
}

protected actual fun reschedule(now: Long, delayedTask: EventLoopImplBase.DelayedTask) {
// throw; on WASI, the event loop is the default executor, we can't shut it down or reschedule tasks
// to anyone else
throw UnsupportedOperationException("runBlocking event loop is not supported")
}
}

internal actual inline fun platformAutoreleasePool(crossinline block: () -> Unit) = block()

internal fun runEventLoop() {
withScopedMemoryAllocator { allocator ->
val ptrToSubscription = initializeSubscriptionPtr(allocator)
val ptrTo32Bytes = allocator.allocate(32)
val ptrTo8Bytes = allocator.allocate(8)
val eventLoop = DefaultExecutor
eventLoop.incrementUseCount()
try {
while (true) {
val parkNanos = eventLoop.processNextEvent()
if (parkNanos == Long.MAX_VALUE) {
// no more events
break
}
if (parkNanos > 0) {
// sleep until the next event
sleep(
parkNanos,
ptrTo32Bytes = ptrTo32Bytes,
ptrTo8Bytes = ptrTo8Bytes,
ptrToSubscription = ptrToSubscription
)
}
}
} finally { // paranoia
eventLoop.decrementUseCount()
}
}
}

private fun initializeSubscriptionPtr(allocator: MemoryAllocator): Pointer {
val ptrToSubscription = allocator.allocate(48)
//userdata
ptrToSubscription.storeLong(0)
//uint8_t tag;
(ptrToSubscription + 8).storeByte(0) //EVENTTYPE_CLOCK
//__wasi_clockid_t id;
(ptrToSubscription + 16).storeInt(CLOCKID_MONOTONIC) //CLOCKID_MONOTONIC
//__wasi_timestamp_t timeout;
//(ptrToSubscription + 24).storeLong(timeout)
//__wasi_timestamp_t precision;
(ptrToSubscription + 32).storeLong(0)
//__wasi_subclockflags_t
(ptrToSubscription + 40).storeShort(0) //ABSOLUTE_TIME=1/RELATIVE=0

return ptrToSubscription
}

internal actual fun createDefaultDispatcher(): CoroutineDispatcher = DefaultExecutor