Cool Tools for Compiling to JavaScript

Bring your code to the Web with surprising ease and few compromises

By Peter Wayner, 14th April 2015.  
http://www.arnnet.com.au/article/572526/cool-tools-compiling-javascript/


Every programmer has a favorite language or two. JavaScript lovers are the 
luckiest these days because their language is taking over the Internet and the 
Internet is taking over the world. Those whose hearts reside elsewhere in the 
programming language world, however, are stuck. They can either stay on the 
sidelines and curse the relentless juggernaut of HTML, CSS, JavaScript, and 
Node.js, or they can find a way to love it.

Luckily, there is a third way that allows you to enjoy the pleasures of your 
favorite language while still deploying your code to the ever-expanding world 
of JavaScript: Simply convert your code, which can be surprisingly easy. 
Performance may suffer a bit, but often much less than you might imagine. Then 
you can ship your code to browsers and quit trying to get people to install 
executable files.

The steadfast will feel like this is abject capitulation, a bitter retreat from 
the principles that bind you to your favorite syntax. Some may even feel it's a 
bit of a betrayal, an act so traitorous that you must hide it from your 
colleagues. Others will suggest, quite correctly, that it's not so simple. 
Getting the code to run is one thing. Gluing the parts together and creating a 
UI is plenty of additional work.

You're welcome to wallow in your pity, but there are plenty of rationalizations 
that make the idea more palatable. First, JavaScript engines run much, much 
faster than they did in the past. Second, crafting a Web UI has never been 
easier, thanks to frameworks and ample HTML/CSS design talent. Third, 
JavaScript is becoming a bit of a lingua franca. If you can convert all of 
these languages to JavaScript, and the list is surprisingly long, you can also 
link them all together.

Here we take a look at the wealth of little languages that enable some of the 
most popular programming languages to compile to JavaScript. A future article 
will explore how fading languages are being reborn by bringing them to the 
browser. There's no reason to be bogged down in pity or hatred. These 
techniques let you enjoy your favorite language and run wherever JavaScript 
does.

Ruby

There are a number of options that let you think like a Ruby programmer while 
running in a JavaScript environment.

RubyJS, for instance, is a JavaScript library that adds many of the basic 
primitives in one JavaScript object. What you write is technically JavaScript, 
but the special Ruby object behaves like Ruby code most of the time. Strings, 
Numbers, Iterators, and Enumerators are waiting for you.

If writing JavaScript that works like Ruby isn't enough, Opal will translate 
Ruby source code directly into JavaScript. It often behaves like a Ruby VM, but 
sometimes it doesn't. Ruby's mutable Strings, for instance, are converted 
directly to JavaScript's immutable ones, which shouldn't be a problem for some 
applications but could drive a few people insane. Other little effects like 
this can lead to rude surprises in edge cases.

For those who want more, HotRuby offers a more complete solution, a JavaScript 
virtual machine that churns through Ruby op codes. The code base is getting a 
bit old, but it offers true believers another option.

Java

It's hard to know what inspired Google to create the Google Web Toolkit, a 
preprocessor that converts Java into JavaScript. Perhaps the manager loved Java 
and didn't want it to die. Perhaps they had extra Java geniuses sitting around, 
waiting to run the Web.

Whatever the reason, they did it and they often use it for their most 
sophisticated Web products. It's a great gift for anyone with a pile of Java 
and no time to rewrite it. The guts of the language are all there, but some of 
the less common classes like BigInteger are missing; that said, you can usually 
find a way to add them. The user interface framework is largely borrowed from 
Swing, so Swing developers will feel right at home. Others won't find it too 
hard to learn.

Google Web Toolkit is far from the only choice for Java programmers. 
Java2Script is fully integrated with Eclipse, and GrooScript converts Java's 
kissing cousin, Groovy.

There are several tools that run JVM byte code with JavaScript, a clever idea 
that lets you deploy JAR files even if you don't have the Java source. Some, 
like Doppio and Node-jvm, are interpreters; others, like TeaVM or Dragome, will 
convert the byte code into JavaScript permanently.

Erlang

Erlang lovers have several options. One solution is to use Erjang, a tool that 
runs Erlang on the JVM, which is then used to run Java byte code with one of 
Java options above. It sounds simple, right?

Shen is a compiler that converts Erlang and its cousins, like Elixir, Joxa, and 
Lol, into JavaScript. If you want to run this code in Node.js, there's also a 
package erlang-shen-js.

A third option is to use LuvvieScript, a strict subset of Erlang that's been 
given hooks to access all of the DOM objects. You use the Erlang structure you 
love, and it translates your instructions into something the DOM understands. 
It's not exactly the same, but it will do.

C

Many people are surprised to find they can use C or C-like languages with 
JavaScript. Sure, the basic JavaScript syntax is quite similar to C, but the 
guts are different. C lets you touch memory directly, but JavaScript hides all 
these details. C lets you manipulate pointers, but JavaScript protects you from 
their dangerous power. Yet these differences are surmountable with a bit of 
clever hacking.

It may not be fair to call LLJS a version of C, but this version of JavaScript 
offers statically typed variables and programmer control of the memory -- well, 
not the memory per se, but a JavaScript version of it. The documentation likes 
to promise pause-free execution because there's no garbage collection.

If you want to work with standard C, Clue will convert C into JavaScript and a 
variety of other scripting languages like Perl or Lua. The developers even 
claim that some code will actually run faster in the JIT for these dynamic 
languages than it will when compiled into native binaries. The secret is that 
the JITs can notice things during runtime that the basic compilers couldn't 
because they can watch the program run.

The biggest name may be Emscripten, a modded version of LLVM that is rewired to 
spit out instructions for asm.js instead of machine code. The secret is that 
asm.js is a narrow subset of JavaScript designed to be easily optimized by the 
latest JavaScript engines, like SpiderMonkey. The results are impressive, and 
some of the best proof comes from the gaming community. Both the Unity and 
Unreal engines can run games in HTML5-compatible browsers.

Python

Python is another popular dynamic language that maps easily to JavaScript. Many 
of the internal features are similar, and some of the biggest differences are 
in the syntax.

Simpler options, like RapydScript and PyvaScript, merely offer Python-like 
syntax that's translated fairly directly into JavaScript. They will do a few 
transformations, like insert curly brackets to match the whitespace-indented 
blocks, and voilĂ  -- it runs in a browser. These are more for programmers who 
think in JavaScript but want to type Pythonically, as the language lovers say.

More complex versions, like PYXC-PJ and Pyjs, will actively convert Python into 
JavaScript, often creating something that's quite readable -- or at least as 
readable as the original code. Pyjs also comes with a widget toolkit making it 
fairly similar to Google Web Toolkit.

The most fun, though, may be PyPy, an incredible feat of software engineering 
with almost Rube Goldbergian proportions. Python goes in and runs on a Python 
interpreter written in RPython, a subset of Python designed to be easily 
compiled. This RPython is then compiled down to something that looks like C 
that can be fed into Emscripten. The developers claim that they can show some 
Python benchmarks running faster in SpiderMonkey than CPython.

If they can do that with Python and C, you can too.


JavaScript, in another costume

Of course, even when it comes to JavaScript, you have alternatives. After all, 
some people like punctuation marks and others don't. CoffeeScript is for the 
people who don't. If you need to program in JavaScript but resent typing so 
many semicolons or curly brackets, then CoffeeScript is for you.

The guts of CoffeeScript are the same as JavaScript because it's not really a 
language. It's a preprocessor that adds in the semicolons and curly brackets, 
so you don't have to. You type out your program in Ruby-like simplicity, and 
CoffeeScript converts it into a minified version of JavaScript.

This isn't the same as programming in another language because the variables 
and functions you define will still behave like JavaScript variables and 
functions. The variables will still be dynamically typed, and all the little 
aggravations will still be there. The math and the squirrelly, overloaded 
behavior of the plus operator will still wear you down, but you'll save some 
time typing.

The CoffeeScript world is remarkably diverse. Once the world realized it could 
preprocess its code, many got into the game. Iced CoffeeScript, for instance, 
is like regular CoffeeScript but with a few extra constructs that make 
asynchronous calls a bit cleaner and simpler to type and read. There may be at 
least a dozen cousins offering to simplify your particular style of programming.

--

Cheers,
Stephen


_______________________________________________
Link mailing list
[email protected]
http://mailman.anu.edu.au/mailman/listinfo/link

Reply via email to