Kotlin Standard Library implementation for WebAssembly System Interface (WASI) platform providing essential I/O, time, random, UUID, and reflection capabilities.
—
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).
/**
* 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?)/**
* 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?// 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
//// 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)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)}")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
): IntOutput operations use scoped memory allocation to safely pass data to WASI:
Console output operations handle WASI-specific errors:
All WASI errors are translated to appropriate Kotlin exceptions.
Console input functions (readln() and readlnOrNull()) are not implemented because:
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")
}// Always handle potential I/O errors
try {
println("Important message")
} catch (e: Exception) {
// Log error or use alternative output method
}// For multiple outputs, prefer single println over multiple print calls
// Less efficient:
print("Hello ")
print("World")
print("!")
// More efficient:
println("Hello World!")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