Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
/ wasm2kt Public archive

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

License

Notifications You must be signed in to change notification settings

korlibs/wasm2kt

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published