wasm2kt icon indicating copy to clipboard operation
wasm2kt copied to clipboard

Web Assembly to Kotlin and Java converter. Allows to compile a C or C++ program/library, and generate a Kotlin or Java program/library.

Deprecated in favour of using WASM directly in Kotlin https://github.com/korlibs/korge/tree/e617e70a6a35125e0f17a81910969ca5ea97072e/korge-core/src/korlibs/wasm

Build Status

Summary

This project allows to convert WASM (Web Assembly) from its binary format into Kotlin code. This allow for example to compile C or C++ libraries and use them in Kotlin common in any supported target including JVM, JS and Native.

The converter itself is written in kotlin.

How to use

Generate a Hello World in WASM:

echo -e "#include <stdio.h>\nint main() { printf(\"hello world\\\n\"); for (int n = 0; n < 10; n++) printf(\"%d,\", n); printf(\"\\\n\"); return 0; }" > hello.c
docker run --rm -v $(pwd):/src -t apiaryio/emcc:1.37 emconfigure emcc hello.c -o hello -O3 -s WASM=1

This will generate a hello.wasm file.

After that, you can use this project to generate a kotlin executable kt file from it:

echo '#!/usr/bin/env kscript' > hello.kt
cat src/main/kotlin/com/soywiz/wasm/WasmModule.kt >> hello.kt
./wasm2kt.kts hello.wasm >> hello.kt
echo -e "fun main(args: Array<String>) = Module.main(args)" >> hello.kt
chmod +x hello.kt

Now you can run it using kscript for example:

kscript hello.kt

Compile

wasm2kt.kts -lang kotlin -class Example a.out.wast > Example.kt
JAVA_OPTS="-Xmx2g -Xms128M" kotlinc Example.kt

What's working

  • WAST parsing
  • WASM parsing
  • generating code from WAST generated by emcc -O0 -g4
  • buggy generating code from WAST generated by emcc -O3 -g4
  • buggy generated code from wasm

TO DO

  • remaining fixes
  • implementing remaining syscalls
  • kotlin-common generator
  • migrate code to kotlin-common
    • generate a kotlin.js version of the generator and publish as a service online