or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

console-io.mdexceptions.mdindex.mdrandom.mdreflection.mdtime.mduuid.md
tile.json

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-wasm-wasi

Kotlin Standard Library implementation for WebAssembly System Interface (WASI) platform providing essential I/O, time, random, UUID, and reflection capabilities.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.kotlin/kotlin-stdlib-wasm-wasi@2.2.x

To install, run

npx @tessl/cli install tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-wasm-wasi@2.2.0

index.mddocs/

Kotlin Standard Library for WebAssembly WASI

Overview

The 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.

Package Information

  • Package Name: org.jetbrains.kotlin:kotlin-stdlib-wasm-wasi
  • Package Type: Library (Kotlin Standard Library)
  • Language: Kotlin
  • Platform: WebAssembly WASI
  • Version: 2.2.0
  • License: Apache-2.0

Installation

This 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()
        }
    }
}

Core Imports

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.*

Basic Usage

@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>()
}

Architecture

The kotlin-stdlib-wasm-wasi package is built on several key architectural components:

WASI System Call Integration

The library integrates directly with WASI system calls through WebAssembly imports:

  • clock_time_get - For time and duration operations
  • random_get - For cryptographically secure random data generation
  • fd_write - For console output operations

Platform-Specific Implementations

All APIs are actual implementations of expect declarations from the common Kotlin standard library, providing WASI-specific behavior while maintaining cross-platform compatibility.

Memory Safety

The library uses scoped memory allocators to ensure safe memory handling when interacting with WASI system calls, preventing memory leaks and buffer overflows.

Capabilities

Console I/O Operations

Basic console output functionality for WASI applications.

fun print(message: Any?)
fun println()
fun println(message: Any?)

Console I/O Documentation

Random Number Generation

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 and Duration Handling

Time source implementations and duration operations for WASI environments.

object TimeSource.Monotonic : TimeSource
object Clock.System : Clock

Time and Duration Documentation

UUID Generation

Platform-specific UUID generation using WASI secure random number generation.

@ExperimentalUuidApi
object Uuid.Companion {
    @ExperimentalUuidApi
    fun random(): Uuid
}

UUID Generation Documentation

Reflection Capabilities

Type reflection support for WASI applications.

inline fun <reified T> typeOf(): KType
inline fun <reified T> typeOfLazyInit(): KType

Reflection Documentation

Exception Handling

WASI-specific exception handling and error management.

class Throwable(
    message: String? = null,
    cause: Throwable? = null
)

Exception Handling Documentation

Platform Limitations

Not Implemented Features

  • Console Input: readln() and readlnOrNull() functions are not implemented in WASI and will throw TODO exceptions
  • File System Operations: Only console I/O is supported; general file system operations are not available
  • Network Operations: Network-related APIs are not supported in the current WASI implementation

WASI Specification Dependency

This library depends on WASI preview1 specification. Future WASI versions may require library updates for compatibility.

Error Handling

The library includes comprehensive error handling for WASI-specific scenarios:

  • WASI Error Codes: All 82 WASI error codes from the preview1 specification are mapped to appropriate Kotlin exceptions
  • System Call Failures: WASI system call failures are properly translated to Kotlin exceptions
  • Memory Management Errors: Safe memory handling prevents common WebAssembly memory issues

Performance Considerations

  • System Call Overhead: Direct WASI system call integration minimizes overhead compared to higher-level abstractions
  • Memory Efficiency: Scoped memory allocation prevents memory leaks in long-running applications
  • Cryptographic Security: Random number generation uses hardware-backed entropy when available through WASI