Kotlin Native and WebAssembly = Native mobile apps in browser?


#1

Afternoon!

I’m a bit confused (and perhaps thus excited) about Kotlin Native (KN) and WebAssembly (wasm).
Perhaps someone can help clarify some of my thoughts:

If KN can target wasm (i.e. run without a [J]VM), and KN can be used to write native Android (and iOS) apps, what stops native mobile apps from being executed and run in a mobile browser, not having to install them through an App Store?

Seeing as e.g. Unreal Engine and Unity can both target wasm and (successfully) run in e.g. Firefox , is it naive to believe that native mobile apps can run in the browser using wasm in a not-so-distant future?


#2

Since Kotlin Native uses LLVM, then WASM support is possible. However, since WASM doesn’t yet have GC support (it’s actively being worked on, I saw the first ML commit a few hours ago) the GC Kotlin Native uses (ref count + cycle collect?) would have to be distributed as part of the runtime as well.

I believe it should not be too far off. In fact, I believe one of the Kotlin devs maintains TeaVM which can compile JVM bytecodes to WASM (experimentally) which would include Kotlin. Granted a direct Kotlin-to-(LLVM-to-)WASM compiler would probably be more efficient than going through JVM bytecode first, but not necessarily since JVM bytecode is more specific in most cases. Also, on the LLVM front, there are a good number of optimizers that can help that Kotlin native likely takes advantage of. If they did go straight from Kotlin to WASM, they could probably benefit from better DCE, inlining, etc that comes from optimizing a high level language.

TLDR: probably wait for the next WASM cycle w/ GC unless you just want to play around.

If you want to play around, get Kotlin native to compile to LLVM IR (not sure how, didn’t look), then use s2wasm from https://github.com/WebAssembly/binaryen (pre-built bins of that+Emscripten can be found here with s2wasm in the bin folder). But you might be better off just using LLVM IR -> Emscripten since you’ll get runtime syscalls implemented in JS w/ Emscripten.

If you want to play around w/ WASM formats or see a WASM-to-JVM compiler, I wrote one in Kotlin here


#3

In fact, I believe one of the Kotlin devs maintains TeaVM which can compile JVM bytecodes to WASM (experimentally) which would include Kotlin

Yes, that’s true. Kotlin works with TeaVM WASM backend, but I tried only with simple examples.

Granted a direct Kotlin-to-(LLVM-to-)WASM compiler would probably be more efficient than going through JVM bytecode first

Remember, that WASM guys are going to include GC support. It’s build upon a thing they called “tuple”, which is similar to LLVM structures. However, structures in LLVM are not collected, nor there’s an instruction (like in WASM) that allocates such tuple. I think new features in WASM (like GC and exception handling) are the things of higher-level than LLVM. Therefore I suppose (it’s my personal opinion, we did not discuss this in JB) that there would be direct Kotlin -> WebAssembly translator, without LLVM toolchain.

GC is not the worst thing missing in WebAssembly. The worst thing is inability to debug applications. There’re no source map yet, and there’s no good tools for compiler engineers to debug WebAssembly translators. For example, you can’t conveniently display memory (like x command in gdb), you can’t install watchpoints.