Unifying your world with haXe
Posted in Uncategorized on December 23rd, 2009 by Marcelo de Moraes Serpa – Be the first to commentThe original title of this post was “Unifying the web with haXe”, however, after considering that haXe also outputs to C++, and that the other two platforms, PHP and Neko can be used beyond the web spectrum, I though I’d change it tot the current one.
Have you ever dreamed of ditching javascript, and or at least avoid the constant switching of languages when you are developing your next big web thing?
Well, the technology for that is here, and has been here for quite some time. Pre-processors, Generators and DSL’s are here to help (Take the Rails RJS engine, for example).
However, most of the solutions are either too complex or don’t offer enough flexibility/maintability.
Enter haXe.
haXe has been around for quite some time now, and has become a nice language/compiler. It generates code for many different languages, and allows you to keep one codebase and target different languages/platforms. haXe currently supports compiling for:
- The Flash Platform – compiles directly to bytecode;
- PHP – generates PHP sourcecode from haXe source;
- C++ – generates C++ sourcecode from haXe source;
- Neko – its brother, Neko, is a Virtual Machine and haXe generates bytcode for it;
- JS – generates JS code from your haXe source code;
The cross-platform compilation capabilities is what makes haXe special. Besides, it’s language is based on the ECMA-Script standard, and Nicolas Canasse, the creator, says it is a more well-though implementation of ECMA than ActionScript 3.
I never played around with haXe long enough, but this time I decided to at least try compiling a Hello World script to PHP, Flash and JS.
I leave the possibilities of such technology for you next brainstorm session!
Now, let’s get our hands dirty.
Firstly, we need swfmill. Swfmill processes xml and compiles them to swf. It can be used to construct simple swf animations or, on our case, build a swf as a library, to be used by haXe when we compile our
final SWF.
Now, get haXe. Go to www.haxe.com, and select the right installation package for your system. Installing haXe is easy, just click a button and wait a few moments.
Unzip the haxe_crowd.zip file somewhere in your filesystem, unzip it and you will see the following files:
- HelloWorld.hx – the super-powerful retro-style classical HelloWorld is back; This is the main haXe class;
- 3 build_flash.hxml – the build file for the flash version;
- build_js.hxml – the build file for the javascript version;
- build_php.hxml – the build file for the php version;
Note that the objective of the “application” is to display the haXe logo. As we are dealing with very different
platforms/technologies as output (js, flash, php) the way they do the very same thing (show the logo) varies and the API is different. Because of this, we need to use conditional compilation, and check if it is compiling for flash, php or js, and use the right code accordingly.
However, it is possible to abstract (and I’m sure it has been done already) in a way that the API is the same for different platforms (i.e, one method call would show the image for the three different platforms and the details of the conditional compilation would be hidden in the implementation of such class/library.
The point is that we have only one codebase that can target many different platforms. Of course, it takes a good developer that knows OO and haXe very well to abstract it in a way that we have minimal difference between the platforms.
However, there’s another benefit of using a technology like haXe. The remoting development is smooth. For any of you who have worked with Flash Remoting, you know that it is a pain to map the classes in the
backend, and sometimes you end up with simpler data-structures (in the case of AMFPHP).
haXe remoting solves this problem. As both platforms share the same class definition, the server/client side deal with the very same data-structure. Neat!
Ok, now go back to where you have uncompressed the zip file, and type:
$ swfmill simple assets.xml assets.swf
Now you have the assets.swf, which include the haXe logo in its library, exported for AS and with id equals “photo”.
Now, open up the build_flash.hxml in your favorite editor. Here’s what you will see:
-main HelloWorld
-swf-version 8
-swf hello_world.swf
-swf-lib assets.swf
- The -main directive specifies the class with the entry-point method;
- The swf-version specifies which Flash Player version you’d like to target;
- The swf is the actual name of the output swf
- swf-lib specifies an swf with assets that may be used by the final swf (image, sounds, other clips);
We’ve got everything we need, the assets.swf, the source code and the params for haXe to compile it. Now jump into the command-line again and type:
$ haxe flash_build.hxml
You should then see the output swf, called HelloWorld.swf. Open it through the hello_world_flash.html document.
Now, you can also compile for the php and js targets:
$ haxe php_build.hxml
For JS, you’ll need to install DomReady using haxelib (equivalent to rubygems in the Ruby world):
Firstly, setup haxelib, for a quickier setup, just press
$ haxelib setup
Then, install the DomReady library:
$ haxelib install DomReady
Now, you are ready to compile the JS version of our amazing app:
$ haxe js_build.hxml.
The flash and js builds are the ones that have more code. Flash needs the assets swf, and js needs an additional library called DomReady (used to run the code only when the whol DOM tree has been loaded).
You can see that in all the builds, lots of infra-structure code is generated, this is the way haXe abstracts its standard API across all the platforms, and some support code is needed.
For the JS versio, open the hello_world_js.html file. This file includes the hello_world.js we just compiled. The code just selects a DOM node and replaces its innerHTML with an tag with its src attribute pointing to the haxe_logo.jpg file.
For the php version, you can either setup your current webserver to browse it or just use the command-line php interpreter like this:
[php/]$ php index.php
You’ll see that the tag output in the console.
There’s also support for a virtual-machine called Neko and C++. There are even some iphone apps that have been developed with it.
haXe, despite existing for 4 years already, hasn’t grown “mainstream”, it is still a niche, albeit with a very active and exciting community. The idea behind it is amazing and has great business potential too (reuse your knowledge for multiple technolgoies, keep the same language despite of the technology).
I’m hoping that soon it get more support from more developers and companies, but that, my
friend, also depends on you too! So, grab a copy of haXe and start playing with it today!
Resources:
- Support files for this tutorial: hxllo_world.zip
- Official haXe website
- Swfmill official website
