-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Wasm wasi #4064
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
Merged
Merged
Wasm wasi #4064
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
kotlinx-coroutines-core/jsAndWasmShared/src/CoroutineContext.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 distingishWasmWasi
target fromWasmJs
orJS
targets there.