The Complete Magazine on Open Source

Haxe: Write Once, Convert to Many Languages

, / 324 0

Hax visual

The proliferation of a wide variety of platforms has become a major challenge for developers when it comes to porting their application to different environments. Haxe assists developers to face this challenge. This article is an eye opener to the world of cross-compilation based programming using the Haxe toolkit.

One of the major challenges for software developers today is dealing with the variety of software environments or platforms. Applications can enjoy the fullest potential of the platforms only when they are built using native codebase. To harness this advantage, organisations need to have developers who are skilled in those individual development techniques. Typically, teams address this with the help of coders fluent in specific programming languages to port an application to a specific platform. Now, instead of this traditional approach, there are special multi-platform or multi-paradigm cross-compiler based languages.

Multi-platform languages
Here, the word ‘multi-platform’ is not used to imply that the language will work on different platforms, but rather that the source code of these languages can be compiled into the source code of other programming languages. This can also be called ‘source-to-source compilation’. Two such popular languages are Haxe and Monkey. The code written in these programming languages is compiled into specific target languages, which can again be handled as usual programs written in those target programming languages. Due to this source-to-source translation methodology, all that a developer needs to do is to write the Haxe code and convert it into the target language, so that the platform-specific features can be harnessed fully.

Figure 1

Figure 1: Haxe components

The Haxe toolkit
Haxe is a popular multi-platform toolkit that has five major components:

  • The Haxe programming language
  • A cross-compiler
  • The Haxe library
  • Haxe based frameworks
  • Additional tools

Haxe is a high-level programming language. With respect to data types, Haxe is strictly a typed language. The effort required to learn the syntax of Haxe is very minimal if you already know any of the modern day programming languages like Java, PHP, etc.

Why Haxe?
Though there are other alternatives to Haxe in the multi-platform programming world, Haxe possesses the following advantages:

  • Haxe is available for download with an open source licence.
  • It can be configured in various operating systems without any major glitches.
  • The Haxe programming language is fully featured.
  • It has the capability to target both programming languages and virtual machines like Neko.
  • Haxe has support for modern programming languages like C#, Python, etc.
  • It can be used to compile into client side scripts like JavaScript and server side scripts like PHP.
Figure 2

Figure 2: The evolution of Haxe

The evolution of Haxe
Development on the Haxe project was started in 2005 by Nicolas Cannasse. Initially, it was named as ‘haXe’. The milestones in Haxe’s development are listed below:

  • The first beta version was released in February 2006. The targets it supported then were AVM bytecode and Neko, which was the virtual machine developed by Cannasse himself.
  • The version 1.0 of Haxe was released during May 2006. It had support for JavaScript.
  • In August 2006, the ‘haxelib’ tool was added as a component to the Haxe Toolkit.
  • ActionScript 3 was added as a target in March 2007.
  • In July 2008, Haxe version 2.0 was released with support for PHP.
  • In July 2009, C++ support was added to Haxe with the release 2.04.
  • In January 2011, version 2.07 was launched, which supported ‘macros’.
  • The versions 2.08 and 2.09 focused on the improvements in the JavaScript target.
  • In 2012 , support for Java and C# was added in Haxe 2.10.
  • Haxe 3 was launched in May 2013 with the establishment of the Haxe Foundation to deal with future developments and maintenance.
  • The latest version of Haxe is 3.20, which was released this year.

Haxe installation
The latest Haxe version can be downloaded from which provides both the installer and binaries for Windows, Linux and Mac based computers. A typical Haxe installation on a Windows PC is easy (similar is the case with other OSs like Linux). Just download the executable installer file and run it using a double click. Being a small file, the installation will get completed in a few minutes. During the installation, you will be given an option to select or remove the Neko virtual machine too.

The Haxe cross-compiler
The Haxe compiler is capable of compiling the Haxe source code into the source code of the target language. The available programming language targets for the Haxe compiler are given in Table 1.

Table 1

Table 1: Haxe’s programming language targets

Table 2

Table 2: Haxe virtual machine targets

Apart from the above specified source-to-source compilation, Haxe supports source-to-bytecode compilation as well, as shown in Table 2.

It is interesting to know that Neko has both programming and virtual machine components.

The Haxe ‘Hello World’
The syntax of the Haxe programming language would look very familiar to most programmers as it is more oriented towards ECMAScript Standards (the scripting language specification standardised by ECMA International in ECMA-262 and ISO/IEC 16262). The detailed documentation on Haxe programming is available for download on The Haxe source code can be entered using any text editor. Once the program is entered, it can be compiled into the available target programming languages/bytecode using the Haxe cross-compiler. A sample Hello World program in Haxe is as shown below:

class HelloWorld {
static public function main() {
trace(“Welcome to Haxe Programming”);

This code should be saved as HelloWorld.hx. Then, from the command prompt it will be cross-compiled into JavaScript using the following command:

haxe -main HelloWorld -js HelloWorld.js

The compilation will result in the creation of the JavaScript source file HelloWorld.js, the contents of which are shown below:

(function (console) { “use strict”;
var HelloWorld = function() { };
HelloWorld.main = function() {
console.log(“Welcome to Haxe Programming”);
})(typeof console != “undefined” ? console : {log:function(){}});

The same Haxe code will be compiled into Python using the corresponding option in the Haxe compiler, as follows:

haxe -main HelloWorld -python

This would generate the Python source code as output in the file, the contents of which are shown below:

class HelloWorld:
def main():
print(“Welcome to Haxe Programming”)

With the default installation of Haxe in the Windows environment, when you try to target certain programming languages like C#, there will be an error message like:

“Error:Library hxcs is not installed”.

This can be rectified by using the following command:

haxelib install hxcs

After this installation, the source program will be compiled into the C# target with the following command:

haxe -main HelloWorld -cs HelloWorldCS

The point to be noted here is that ‘HelloWorldCS’ is a folder name, in contrast to the file names supplied for Python and JavaScript compilation. The above command creates a folder and places all the necessary files and sub-directories for the C# project. Another interesting feature is that the generated C# code is also compiled and an ‘exe’ in the name ‘HelloWorld.exe’ is built in the ‘bin’ folder, which will be readily executed.

Figure 3

Figure 3: The categories in the Haxe standard library

The Haxe standard library
The standard library provided with Haxe has support for various commonly used tasks. It has three major categories as shown in Figure 3.

  • The general-purpose component of the standard library covers features like arrays, strings, regular expressions and XML. It also covers advanced features like various encryption algorithms (haxe.crypto, haxe.JSON, etc).
  • The system component has features like DB manipulation, file handling, process handling, etc.
  • The target-specific library component covers features specific to the corresponding target languages.

Haxe based frameworks and tools
Haxe has good support for various domains as shown
in Figure 4. There are various Haxe based tools which assist in the above-specified application domains:

  • OpenFL
  • Flambe
  • Nape Physics Engine

The OpenFL framework is built using Haxe and provides a Flash API for various mobile platforms. Flambe is a good gaming framework with support for mobile browsers. Nape is a physics engine fully written in Haxe. Similarly, there are various provisions for desktop application development, mobile application development and command line based applications.

Figure 4

Figure 4: Haxe applications

Figure 5

Figure 5: HaxeUI

HaxeUI provides a good set of user interface elements, which can be used for building cross-platform interfaces. A sample ( UI using HaxeUI is shown in Figure 5.
Overall, Haxe provides a rich experience to developers in terms of its language support and wider array of target languages. The reliability of Haxe can be understood from the list of popular companies (Prezi, BBC, Toyota, etc) using it. This is listed in the official website of Haxe. To conclude, if you are a developer wanting to develop applications for a spectrum of platforms, you should definitely give Haxe a try.