Kotlin Standard Library implementation for WebAssembly System Interface (WASI) platform providing essential I/O, time, random, UUID, and reflection capabilities.
npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-wasm-wasi@2.2.0The Kotlin Standard Library for WebAssembly WASI (kotlin-stdlib-wasm-wasi) provides platform-specific implementations of the Kotlin standard library for the WebAssembly System Interface (WASI) specification. This library enables Kotlin applications to run in WASI-compliant WebAssembly runtimes, providing essential functionality for I/O operations, time handling, random number generation, UUID creation, and reflection capabilities tailored specifically for the WASI environment.
As part of the Kotlin Multiplatform ecosystem, this library serves as the bridge between Kotlin's high-level programming model and low-level WebAssembly system interfaces, enabling developers to write Kotlin applications for server-side WASI environments, edge computing platforms, and other systems supporting the WASI specification.
org.jetbrains.kotlin:kotlin-stdlib-wasm-wasiThis library is automatically included when targeting Kotlin/WASM with WASI in a Kotlin Multiplatform project. Add the WASI target to your Kotlin Multiplatform configuration:
kotlin {
wasm {
wasi {
nodejs()
}
}
}The APIs in this library are accessed through standard Kotlin imports. Most functionality is available through the common Kotlin standard library packages:
// I/O operations
import kotlin.io.*
// Random number generation
import kotlin.random.*
// Time and duration handling
import kotlin.time.*
// UUID generation (experimental)
import kotlin.uuid.*
import kotlin.uuid.ExperimentalUuidApi
// Reflection capabilities
import kotlin.reflect.*@OptIn(ExperimentalUuidApi::class)
fun main() {
// Console output
println("Hello WASI World!")
print("Output without newline")
// Random number generation
val randomInt = Random.nextInt(1, 100)
val secureRandom = Random.nextBytes(16)
// UUID generation (experimental API)
val uuid = Uuid.random()
// Time operations
val now = Clock.System.now()
val duration = measureTime {
// Some operation
}
// Type reflection
val type = typeOf<String>()
}The kotlin-stdlib-wasm-wasi package is built on several key architectural components:
The library integrates directly with WASI system calls through WebAssembly imports:
clock_time_get - For time and duration operationsrandom_get - For cryptographically secure random data generationfd_write - For console output operationsAll APIs are actual implementations of expect declarations from the common Kotlin standard library, providing WASI-specific behavior while maintaining cross-platform compatibility.
The library uses scoped memory allocators to ensure safe memory handling when interacting with WASI system calls, preventing memory leaks and buffer overflows.
Basic console output functionality for WASI applications.
fun print(message: Any?)
fun println()
fun println(message: Any?)Cryptographically secure random number generation using WASI's random_get system call.
object Random {
fun nextInt(): Int
fun nextInt(until: Int): Int
fun nextInt(from: Int, until: Int): Int
fun nextBytes(size: Int): ByteArray
// ... other random functions
}Random Number Generation Documentation
Time source implementations and duration operations for WASI environments.
object TimeSource.Monotonic : TimeSource
object Clock.System : ClockTime and Duration Documentation
Platform-specific UUID generation using WASI secure random number generation.
@ExperimentalUuidApi
object Uuid.Companion {
@ExperimentalUuidApi
fun random(): Uuid
}Type reflection support for WASI applications.
inline fun <reified T> typeOf(): KType
inline fun <reified T> typeOfLazyInit(): KTypeWASI-specific exception handling and error management.
class Throwable(
message: String? = null,
cause: Throwable? = null
)Exception Handling Documentation
readln() and readlnOrNull() functions are not implemented in WASI and will throw TODO exceptionsThis library depends on WASI preview1 specification. Future WASI versions may require library updates for compatibility.
The library includes comprehensive error handling for WASI-specific scenarios: