CtrlK
BlogDocsLog inGet started
Tessl Logo

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.

Pending
Overview
Eval results
Files

console-io.mddocs/

Console I/O Operations

Overview

The Console I/O capabilities provide basic output functionality for WASI applications. These functions use the WASI fd_write system call to write data to standard output (file descriptor 1).

API Reference

Output Functions

/**
 * Prints the given [message] to the standard output stream.
 * @param message the object to print
 */
fun print(message: Any?)

/**
 * Prints a line separator to the standard output stream.
 */
fun println()

/**
 * Prints the given [message] and a line separator to the standard output stream.
 * @param message the object to print  
 */
fun println(message: Any?)

Input Functions (Not Implemented)

/**
 * Reads a line from the standard input stream.
 * @return the line read from the input
 * @throws TODO Currently not implemented in WASI
 * @since Kotlin 1.6
 */
fun readln(): String

/**
 * Reads a line from the standard input stream, or returns null if EOF is reached.
 * @return the line read from the input, or null on EOF
 * @throws TODO Currently not implemented in WASI  
 * @since Kotlin 1.6
 */
fun readlnOrNull(): String?

Usage Examples

Basic Output

// Print simple text
print("Hello ")
print("World")
// Output: Hello World

// Print with newlines
println("First line")
println("Second line") 
println() // Empty line
// Output:
// First line
// Second line
//

Printing Different Types

// Print numbers
println(42)
println(3.14159)

// Print boolean values
println(true)
println(false)

// Print collections
val list = listOf(1, 2, 3)
println(list)

// Print null values
println(null)

Formatted Output

val name = "Alice"
val age = 30

// String interpolation
println("Name: $name, Age: $age")

// String formatting
println("Pi to 2 decimal places: ${"%.2f".format(3.14159)}")

Implementation Details

WASI System Call Integration

The console output functions are implemented using the WASI fd_write system call:

@WasmImport("wasi_snapshot_preview1", "fd_write")
private external fun wasiRawFdWrite(
    fd: Int,
    iovs: Int,
    iovsLen: Int,
    nwritten: Int
): Int

Memory Management

Output operations use scoped memory allocation to safely pass data to WASI:

  • Strings are encoded to UTF-8 bytes
  • Memory is allocated in WASI linear memory space
  • IO vectors are constructed for the fd_write call
  • Memory is automatically cleaned up after the call

Error Handling

Console output operations handle WASI-specific errors:

  • EBADF: Bad file descriptor (if stdout is not available)
  • EIO: I/O error during write operation
  • ENOSPC: No space left on device
  • EPIPE: Broken pipe (if output stream is closed)

All WASI errors are translated to appropriate Kotlin exceptions.

Platform Limitations

Input Operations Not Supported

Console input functions (readln() and readlnOrNull()) are not implemented because:

  1. WASI Specification Gaps: WASI preview1 doesn't provide robust stdin reading capabilities
  2. Blocking I/O Concerns: WebAssembly's execution model makes blocking input operations problematic
  3. Runtime Environment Variations: Different WASI runtimes handle stdin differently

These functions will throw TODO exceptions if called:

// This will throw an exception
try {
    val input = readln()
} catch (e: NotImplementedError) {
    println("Console input not available in WASI")
}

Output Limitations

  • Standard Output Only: Only stdout (fd=1) is supported for output
  • No Standard Error: stderr output is not separately handled
  • No File Output: General file I/O operations are not available through these functions

Best Practices

Error-Safe Output

// Always handle potential I/O errors
try {
    println("Important message")
} catch (e: Exception) {
    // Log error or use alternative output method
}

Performance Considerations

// For multiple outputs, prefer single println over multiple print calls
// Less efficient:
print("Hello ")
print("World")
print("!")

// More efficient:
println("Hello World!")

Alternative Input Strategies

Since console input is not available, consider these alternatives:

// Use command-line arguments for input
fun main(args: Array<String>) {
    if (args.isNotEmpty()) {
        val input = args[0]
        println("Processing: $input")
    }
}

// Use environment variables
val config = System.getenv("CONFIG_VALUE") ?: "default"
println("Configuration: $config")

Install with Tessl CLI

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

docs

console-io.md

exceptions.md

index.md

random.md

reflection.md

time.md

uuid.md

tile.json