Kotlin to WebAssembly
Explore how to compile Kotlin to WebAssembly using Kotlin/Native, set up a Node.js lightweight server, and create Kotlin code to draw on an HTML5 canvas. Understand the use of jsinterop for JavaScript interop and how to run your WebAssembly code in modern browsers to enhance web performance and safety.
We'll cover the following...
WebAssembly (WASM) is poised to be the next major disruptive technology. In the past, JavaScript was the only predominant viable option for executing code within browsers. With WebAssembly, you can compile code written in many different languages—including C, C++, Rust, Go, C#, Python, Java, and Kotlin—to the WASM binary instruction format, for a stack-based virtual machine that runs within modern browsers. WASM brings many benefits, including speed of development using higher-level languages, high runtime performance, enhanced safety, and code that’s easier to test and debug and that interoperates with code written in different languages.
Kotlin to WebAssembly is currently in initial stages at the time of this writing. Treat the material in this appendix as experimental.
To target Kotlin to WASM, use the Kotlin/Native compiler with the -target wasm32 command-line option. If you’ve not had a chance to install the Kotlin/Native distribution in Kotlin/Native, do so before you continue.