BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Javascript as Compiler Target: Clamato, GWT Smalltalk, Python, Scheme

Javascript as Compiler Target: Clamato, GWT Smalltalk, Python, Scheme

This item in japanese

Despite Javascript's ubiquity, it's a hard language to like. Until the advent of VMs like Google's V8, Apple/WebKit's Nitro/SquirrelFish Extreme and Firefox' TraceMonkey, Javascript's performance was abysmal.
Incompatible implementations across browser make development painful as well. Libraries like jQuery and many others offer convenient abstractions and allow to program to one API and even out the incompatibilities.

Another approach to solving the problems is to hide Javascript completely and use it as a compilation target. Google's GWT is a mature example that takes Java code, written to a subset of the Java API, and compiles it into browser specific Javascript. One example of a real world application of GWT is the Google Wave client. Recently, a few more language implementations have joined the trend.

Avi Bryant, creator of the Smalltalk Seaside web framework, has created Clamato, a Smalltalk dialect that compiles to idiomatic Javascript. Avi described the design principles of Clamato in the InfoQ interview with him taped at FutureRuby.

The tutorial page for Clamato allows to open a Clamato (Smalltalk) class browser in the web browser and inspect and edit the Clamato source code. To look for classes, use the text field in the left upper corner of the browser. Interesting bits are the selfhosting PEG-based Clamato parser (enter "PP" in the textfield to see the classes for the parser and the various combinators).
A certain Seaside influence is visible in the Seaside-style HTML builder (the HTMLCanvas class in the Clamato browser). Visual components can use it to build their GUIs, for instance, the counter component from the tutorial:

 
renderOn: html
  html h3 with: @counter.
  html button
    with: '+';
    onClick: [@counter := @counter + 1. self reload].
  html button
    with: '-';
    onClick: [@counter := @counter - 1. self reload].

The renderOn: html line defines a new method and the rest of the sample is the method body, which builds the HTML with a header and two buttons, which are set up with event handlers.

As it happens, another browser-based Smalltalk has appeared recently: GWT Smalltalk. The creator Peter Fisk has previously created the .NET and alternatively ActionScript-based Vista Smalltalk.  GWTSmalltalk is written using GWT which generates Javascript that runs in the browser.
GWT Smalltalk is in an early stage right now but progresses quickly. It has support for using XMPP, which puts it in a good place to interact with with Google Wave (XMPP is an essential part of Google Wave). More details are available in the Industry Misinterpretations podcast interview with Peter Fisk and on the GWT Smalltalk blog.

Stepping outside the Smalltalk sphere, Python also has its Javascript compiler in pyjamas:

pyjamas is a stand-alone python to javascript compiler, an AJAX framework / library and a Widget set API

The best place to start is the online pyjamas book.

Another language that compiles to Javascript is Moby Scheme. What makes Moby-Scheme stand out is the way it's targeting mobile devices, for the moment Android, by compiling to Javascript instead. This is possible because of PhoneGap and because Android ships an efficient Javascript VM with its WebKit based browser. PhoneGap is a library that allows Javascript to access native device functionality, such as accelerometers, GPS, cameras, as well as other features such as address books. The creators of PhoneGap built it to bridge all the disparate smartphone devices which have very different programming environments, yet share one thing: WebKit and fast Javascript VMs. By giving Javascript access to device features, cross platform apps are possible for smartphones. See InfoQ's interview with the PhoneGap creators for a thorough overview of PhoneGap.

Rate this Article

Adoption
Style

BT