CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.

Pending
Overview
Eval results
Files

coroutines.mddocs/

Coroutines

JavaScript-specific coroutine support with generator-based execution and event loop integration for asynchronous programming. This module provides the runtime support needed for Kotlin coroutines to work efficiently in JavaScript environments.

Capabilities

Core Coroutine Intrinsics

Low-level coroutine creation and execution functions.

/**
 * Starts coroutine without dispatching, returns COROUTINE_SUSPENDED if suspended
 */
fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(completion: Continuation<T>): Any?

/**
 * Creates unintercepted coroutine without starting it
 */
fun <T> (suspend () -> T).createCoroutineUnintercepted(completion: Continuation<T>): Continuation<Unit>

/**
 * Intercepts continuation with current continuation interceptor
 */
fun <T> Continuation<T>.intercepted(): Continuation<T>

/**
 * Continuation interface for coroutine execution
 */
interface Continuation<in T> {
    val context: CoroutineContext
    fun resumeWith(result: Result<T>)
}

Usage Examples:

import kotlin.coroutines.*

// Create and start a coroutine
suspend fun fetchData(): String {
    // Simulate async operation
    return "Data loaded"
}

fun startCoroutine() {
    val continuation = object : Continuation<String> {
        override val context = EmptyCoroutineContext
        
        override fun resumeWith(result: Result<String>) {
            result.fold(
                onSuccess = { data -> console.log("Success: $data") },
                onFailure = { error -> console.error("Error: $error") }
            )
        }
    }
    
    // Start the coroutine
    ::fetchData.startCoroutineUninterceptedOrReturn(continuation)
}

Coroutine Cancellation

Exception-based cancellation mechanism for coroutines.

/**
 * Exception thrown when coroutine is cancelled
 */
class CancellationException : IllegalStateException {
    constructor()
    constructor(message: String?)
    constructor(message: String?, cause: Throwable?)
    constructor(cause: Throwable?)
}

Usage Examples:

import kotlin.coroutines.cancellation.CancellationException

suspend fun cancellableOperation(): String {
    // Check for cancellation
    if (/* some cancellation condition */) {
        throw CancellationException("Operation was cancelled")
    }
    
    return "Operation completed"
}

// Handle cancellation
try {
    val result = cancellableOperation()
    console.log("Result: $result")
} catch (e: CancellationException) {
    console.log("Operation was cancelled: ${e.message}")
}

JavaScript-Specific Implementation

Internal implementation details optimized for JavaScript runtime.

/**
 * JavaScript-specific continuation implementation
 */
internal class ContinuationImpl<in T>(
    completion: Continuation<Any?>?,
    private val _context: CoroutineContext
) : Continuation<T>

/**
 * Generator-based coroutine implementation for JavaScript
 */
internal class GeneratorCoroutineImpl<in T>(
    private val resultContinuation: Continuation<T>
) : Continuation<Any?>

/**
 * Safe continuation wrapper for JavaScript environments
 */
internal class SafeContinuation<in T> : Continuation<T>

Coroutine Context

Context elements for coroutine execution.

/**
 * Empty coroutine context
 */
object EmptyCoroutineContext : CoroutineContext {
    override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E? = null
    override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R = initial
    override fun plus(context: CoroutineContext): CoroutineContext = context
    override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext = this
}

/**
 * Coroutine context interface
 */
interface CoroutineContext {
    operator fun <E : Element> get(key: Key<E>): E?
    fun <R> fold(initial: R, operation: (R, Element) -> R): R
    operator fun plus(context: CoroutineContext): CoroutineContext
    fun minusKey(key: Key<*>): CoroutineContext
    
    interface Element : CoroutineContext
    interface Key<E : Element>
}

Usage Examples:

import kotlin.coroutines.*

// Create coroutine with empty context
suspend fun simpleCoroutine(): Int {
    return 42
}

// Custom continuation with context
val continuation = object : Continuation<Int> {
    override val context: CoroutineContext = EmptyCoroutineContext
    
    override fun resumeWith(result: Result<Int>) {
        result.fold(
            onSuccess = { value -> console.log("Got value: $value") },
            onFailure = { error -> console.error("Error: $error") }
        )
    }
}

// Start coroutine
::simpleCoroutine.startCoroutineUninterceptedOrReturn(continuation)

Types

// Core coroutine types
interface Continuation<in T> {
    val context: CoroutineContext
    fun resumeWith(result: Result<T>)
}

interface CoroutineContext {
    operator fun <E : Element> get(key: Key<E>): E?
    fun <R> fold(initial: R, operation: (R, Element) -> R): R
    operator fun plus(context: CoroutineContext): CoroutineContext
    fun minusKey(key: Key<*>): CoroutineContext
    
    interface Element : CoroutineContext
    interface Key<E : Element>
}

// Cancellation support
class CancellationException : IllegalStateException

// Context implementations
object EmptyCoroutineContext : CoroutineContext

// Internal implementation classes
internal class ContinuationImpl<in T> : Continuation<T>
internal class GeneratorCoroutineImpl<in T> : Continuation<Any?>
internal class SafeContinuation<in T> : Continuation<T>

// Result type for continuation resumption
class Result<out T> {
    fun <R> fold(onSuccess: (T) -> R, onFailure: (Throwable) -> R): R
    fun getOrThrow(): T
    fun getOrNull(): T?
    fun exceptionOrNull(): Throwable?
    val isSuccess: Boolean
    val isFailure: Boolean
}

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

docs

browser-integration.md

collections.md

coroutines.md

index.md

io-encoding.md

javascript-interop.md

math-time.md

reflection.md

uuid.md

w3c-dom-apis.md

tile.json