javascript


Kotlin to Javascript

In my opinion, one of the most interesting Kotlin features is the ability to transpile into Javascript. I’m a huge fan of GWT (Java -> Javascript), a lot of my projects are working in the web with GWT (like http://chess.mobialia.com or http://12chess.com), and I need to know if Kotlin is going to be able to replace Java+GWT in the near future.

So I tried to compile the Carballo Kotlin code to JS (as a NodeJS app) in the “js” branch: https://github.com/albertoruibal/karballo/tree/js, and I couldn’t make it work yet. I found some problems that I will detail in this post.

GWT includes a reduced version of the JRE, without some classes and methods, but Kotlin is much more radical: To compile a Kotlin code to JS, it cannot reference any java.* class, so forget about java.lang.*, java.util.* or java.io.*.

No basic java types

Integer, Character, Boolean… are a Java classes and they cannot be used, so a lot of common operations must be done the “Kotlin way”, i.e.:

Integer.parseInt(myString) -> myString.toInt()
Character.toLowerCase(myChar) -> myChar.toLowerCase()
Integer.MAX_VALUE -> Int.MAX_VALUE

Emulating Java classes: System, Math, Random, Arrays, Date

These classes are not available when building to JS. To avoid changing the original code and migrate seamless from JVM to JS, I created auxiliar classes with the methods used in the code:

  • System: I implemented currentTimeMillis() (calling the JS Date object), exit(), arraycopy(), and an empty gc()
  • Math: When building for JS, the java.lang.Math class must be replaced by kotlin.js.Math, using this class avoids to change a lot of code
  • Random: Using kotlin.js.Math.random()
  • Arrays: Implementing some variants of the fill() method
  • Date: partial implementation to get the current date

Other problems

  • IntelliJ does not allow to mix the Java and the JS Kotlin stdlib in the same project, so I hat to create a different branch “js”
  • No Threads nor Coroutines support in JS jet
  • Kotlin’s readLine() (to read lines from stdin) is only available in the JVM stdlib
  • String.toBoolean() is only available in the JVM stdlib, I fixed it defining a extension function in the same package: 
    inline fun String.toBoolean(): Boolean = "true".equals(this.toLowerCase())
  • No support for asserts in the JS compiler
  • Some annotations aren’t available @JvmOverloads, @Throws…
  • No LinkedList
  • No StringBuffer, but in this case it’s better to use Kotlin’s StringBuilder
  • Missing StringBuilder.setLength() method (I sent a pull request to Kotlin implementing setLength() and substring())
  • It does not allow returns into synchronized blocks
  • No printStackTrace() on exceptions
  • No JUnit tests in JS, so I removed the tests in the “js” branch

Overall conclusion

After solving all these problems, the Kotlin2JS compiler is introducing bugs that don’t allow the karballo engine to work.

Kotlin2JS isn’t production ready, and years behind GWT.


Chromium Embedded Framework

chromiumHTML5 is a fantastic app framework, but there are many environments where you cannot rely on the features support of the browser (specially when dealing against Internet Explorer, old windows versions or environments where you cannot freely upgrade the browsers). Recently I found this problem trying to package Mobialia Chess 3D for Windows 8. Microsoft provides some tools to package HTML apps to native apps, but they will run with the Internet Explorer engine, lacking features like WebGL, WebRTC, etc.

Chromium Embedded Framework (CEF) is a library that allows to embed a Chromium webview in your native Windows or Mac desktop application. So you can convert any HTML5 to a traditional desktop app (and I bet some users to try guessing if Mobialia Chess 3D is an HTML5 app). You can also create a Windows/Mac installer to  distribute the app (I used InnoSetup, but this is another story…).

Using CEF requires a small knowledge about Windows/Mac desktop app development. I created a Windows app using Visual Studio Express, it was not very difficult, because CEF includes a “cefclient” sample project that you can use as a template to start your project development.

The main problem that I found was the size, embedding CEF will add 45Mb size to your installed application. I also found other minor problems like the lack of mp3 sound support (due to license problems) solved converting the sound files to ogg.

CEF is already used by great desktop applications like Steam, Evernote or Spotify, so it’s a great option to consider in your developments.


JavaScript as a Runtime

The future is here, and JavaScript (JS) is everywhere, but JS development is so hard that many people prefer to develop in other languages and then compile their code to JS, using JS as a universal runtime. Here are the most interesting options:

GWT

GWT stand for Google Web Toolkit, but now it’s in hands of the community and extensively used in many corporations. GWT compiles Java into JS and it’s strongly optimized. I use it a lot, and I feel very productive using an advanced IDE like Eclipse with tools like code assist, refactor, etc.

https://developers.google.com/web-toolkit/

CoffeeScript

A very compact language, inspired by Ruby and Python and that has become extremely popular in the last years. I’m not very familiar with the “Syntactic sugar” and I’m more productive with traditional languages (yes, I love curly backets! {}).

http://coffeescript.org/

Haxe

If you are an ActionScript developer (Adobe Flash), this is your language. It not only compiles to JS and ActionScript, also to PHP, C++, C#, etc. It’s becoming popular for the development of multi-platform mobile games with NME.

http://www.haxe.org/

Dart

This is a new language for the web pushed by Google. It tries to be a “modern and structured” language for the web that can be run directly into the browser, but to retain compatibility (and to run in other browsers that publicly rejected Dart), it can also be compiled to JS.

http://www.dartlang.org/

List of languages that compile to Js: http://altjs.org/


Java vs C# vs Javascript 5

*** UPDATE: This article had a HUGE mistake in the Javascript part, read below ***

I’m a Java guy but I’m always opened to other programming languages. I heard great things about Mono and the .Net platform, among those things recently I saw a Xamarin article about Android being ported over Mono in a project called XobotOS (http://blog.xamarin.com/2012/05/01/android-in-c-sharp/) and they are getting a much better performance than with Dalvik.

They did it using Sharpen: an automated tool that can convert from Java source code to C#. I was hesitated and started to port my “Carballo” chess engine to C#. Sharpen runs as an eclipse plugin (but outside eclipse). The best tutorial that I found to install sharpen is here: http://www.pauldb.me/post/14916717048/a-guide-to-sharpen-a-great-tool-for-converting-java

The new Carballo Chess Engine with a C# version is now hosted at GitGub (https://github.com/albertoruibal/carballo). You can launch the conversion tool running “ant” on the “csharp/” folder. This C# version hasn’t a graphic interface yet, but is great to make some comparisons…

I use a lot the BT2630 chess test suite, with positions to find the best move, lets see how much time needs my chess engine in Java to find the solution for the first position:

r q . . r . k . 8
. . . . . p p . 7
p . . . . . . . 6
. . . . b N P . 5
. p . . P . . P 4
. . . . . Q . . 3
P P . . . . K . 2
. . . . . R . R 1
a b c d e f g h white move
DEBUG SearchEngine - depth 1 score cp 170 nodes 176 time 42 nps 4093 pv f3b3
DEBUG SearchEngine - depth 2 score cp 136 nodes 1044 time 123 nps 8419 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score cp 131 nodes 4335 time 358 nps 12075 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score cp 111 nodes 14603 time 854 nps 17079 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score cp 115 nodes 27593 time 1311 nps 21031 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score cp 119 nodes 50536 time 1450 nps 34828 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score cp 117 nodes 110415 time 1693 nps 65180 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score cp 110 nodes 314754 time 2515 nps 125100 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score cp 112 nodes 564320 time 3580 nps 157587 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score cp 129 nodes 1219488 time 6174 nps 197487 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score cp 241 nodes 2430413 time 11373 nps 213681 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

This is 11.373 seconds at 213.681 nodes per second, my java version is “Java(TM) SE Runtime Environment (build 1.6.0_26-b03)”.

And with Mono? let’s see the result on the same machine:

DEBUG SearchEngine - depth 1 score mate 0 nodes 176 time 25 nps 6769 pv f3b3
DEBUG SearchEngine - depth 2 score mate 0 nodes 1044 time 36 nps 28216 pv f3b3 b8b7
DEBUG SearchEngine - depth 3 score mate 0 nodes 4335 time 58 nps 73474 pv f1f2 b8b6 f3b3
DEBUG SearchEngine - depth 4 score mate 0 nodes 14603 time 136 nps 106591 pv f1f2 b8b6 f3b3 e8e6
DEBUG SearchEngine - depth 5 score mate 0 nodes 27593 time 247 nps 111262 pv f1f2 b8b7 h1d1 a8d8 f2d2
DEBUG SearchEngine - depth 6 score mate 0 nodes 50536 time 450 nps 112053 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2
DEBUG SearchEngine - depth 7 score mate 0 nodes 110415 time 813 nps 135644 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3
DEBUG SearchEngine - depth 8 score mate 0 nodes 314754 time 2058 nps 152867 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3
DEBUG SearchEngine - depth 9 score mate 0 nodes 564320 time 3693 nps 152766 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6
DEBUG SearchEngine - depth 10 score mate 0 nodes 1219488 time 7673 nps 158911 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4
DEBUG SearchEngine - depth 11 score mate 0 nodes 2430413 time 15490 nps 156891 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1

That is 15.490 seconds at 156.891 nodes per second with the “Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-3)”.

I didn’t found the Sharp and Mono magic, at least in my case Mono is about a 30% slower than Java. This may be as a result of the automated code conversion…

I tested the GWT part in the GWT Developer Mode. In this mode, the code is run in the server as Java (and not in JS). In recent tests I found that the Javascript version is much more slow, probably because of the GWT Javascript Long emulation (Js does not has 64bits integers).

Well and this chess engine also has a GWT version. GWT is another magical Google product that converts your java code to Javascript. Let’s see how fast runs this test on the browser:

[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 1 score cp 170 nodes 176 time 56 nps 3087 pv f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 2 score cp 136 nodes 1044 time 186 nps 5582 pv f3b3 b8b7 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 3 score cp 131 nodes 4335 time 483 nps 8956 pv f1f2 b8b6 f3b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 4 score cp 111 nodes 14603 time 997 nps 14632 pv f1f2 b8b6 f3b3 e8e6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 5 score cp 115 nodes 27593 time 1123 nps 24548 pv f1f2 b8b7 h1d1 a8d8 f2d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 6 score cp 119 nodes 50536 time 1251 nps 40364 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 7 score cp 117 nodes 110415 time 1571 nps 70238 pv f1f2 b8b7 h1d1 a8c8 f2d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 8 score cp 110 nodes 314754 time 2648 nps 118819 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 b7c6 b2b3 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 9 score cp 112 nodes 564320 time 4063 nps 138858 pv f1f2 b8b7 h1d1 a8d8 f2d2 d8d2 d1d2 a6a5 b2b3 b7b6 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 10 score cp 129 nodes 1219488 time 7474 nps 163142 pv f1f2 b8c7 f3b3 c7b7 b3d5 b7d5 e4d5 a8d8 h1d1 e8e6 a2a4 )
[INFO] [org.vectomatic.libgwtsvgchess] - Main.info(depth 11 score cp 241 nodes 2430413 time 14216 nps 170951 pv f5g7 e8f8 g7f5 a8a7 f5h6 g8g7 f1f2 b8b5 h6f5 g7g8 f3b3 a7d7 h1c1 d7d1 )

I can’t believe my eyes : 14.216 seconds at 170.951 nodes per second with Google Chome Version 19.0.1084.52… and this is also an automated conversion process…

Yes, in my tests Javascript is faster than Sharpened Mono…