Friday, December 10, 2010

S2JS: Scala to Closure-annotated Javascript

For the last few months I've been working on a project called S2JS. The source code for the project is currently hosted at github:

S2JS is a Scala compiler plugin that can turn a subset of Scala code into Closure-annotated Javascript Code.
Currently it is a work-in-progress, although I am currently using it for some of the features on

Closure compiler/library

For those not familiar with the closure library/compiler, it allows you to annotate your javascript with type annotations, turning Javascript into a typed language. With those annotations in place (plus the equivalent of imports) you can run the closure-compiler to perform TONS of optimizations like inlining and removal of dead code.
After using it for a while I came to the conclusion that it is has a lot of performance benefits, but it is extremely verbose and repetitive.

Scala / Javascript

As you probably know, Scala is a VERY nice statically-typed language that compiles to the JVM. At first glance it seems very different from Javascript, but it turns out it is not so different from Closure-annotated-javascript: They are both statically-typed and they both have classes, singleton objects, anonymous functions, structural types, generics, etc. So it made sense to create a way to translate Scala code (terse and expressive) to Closure-annotated Javascript (very verbose).

Other Approaches

I have seen other projects that aim to turn Scala code into Javascript code, but I think that specifically targeting the closure compiler yields the most benefits. Most of the type information that the closure compiler wants (as annotations) is readily available within the AST of a Scala program.

No comments:

Post a Comment