wasi
This module exposes a Kotlin/Wasm API inspired from WASI Preview2 in order to expose low level WASI Preview1 capabilities.
fun main() {
print()
clock()
random()
filesystem()
cli()
}
private fun print() {
Wasi.out.println("== Print ==")
Wasi.out.println("Hello, world!")
Wasi.err.println("Error")
}
private fun clock() {
Wasi.out.println("\n== Clock ==")
val now = Wasi.wallClock.now()
Wasi.out.println(now)
}
private fun filesystem() {
Wasi.out.println("\n== Filesystem ==")
Wasi.createDirectoryAt("testDir")
val descriptor = Wasi.openAt("testFile",
OpenFlags(create = true),
DescriptorFlags(read = true, write = true)
)
val content = "Hello, file!"
Wasi.write(descriptor, content.encodeToByteArray())
val readResult = Wasi.read(descriptor, content.length.toULong())
Wasi.out.println(readResult.data.decodeToString())
Wasi.readDirectory(StandardDescriptor.FIRST_PREOPEN,".").forEach { Wasi.out.println(it) }
}
private fun random() {
Wasi.out.println("\n== Random ==")
val pseudoGenerator = Wasi.seededRandom()
Wasi.out.println("Pseudo random number generator")
Wasi.out.println(pseudoGenerator.nextLong())
Wasi.out.println(pseudoGenerator.nextLong())
val secureGenerator = Wasi.secureRandom()
Wasi.out.println("Secure random number generator")
Wasi.out.println(secureGenerator.nextLong())
Wasi.out.println(secureGenerator.nextLong())
}
private fun cli() {
Wasi.out.println("\n== CLI ==")
Wasi.args.forEach { Wasi.out.println(it) }
Wasi.envVars.forEach { Wasi.out.println(it) }
}
Content copied to clipboard