views:

132

answers:

4

Recently several tools have been released such as pyjamas and Scheme2js that allow one to take code in a language such as Python or Scheme and compile it to Javascript.

But how practical is such a tool?

I could see debugging being quite painful as you would have to debug the compiled javascript code itself, and correlate any bugs in that code with the corresponding lines in the original python/scheme/etc source code. Even with an intelligent stack trace such as the pyjamas -d option provides, this still seems tedious.

Also, libraries such as jQuery make writing Javascript much more fun and productive. But for many developers Javascript is still a new language to learn.

Has anyone worked with compiled Javascript in a production environment? Any recommendations or comments on the practicality of compiling to Javascript instead of writing your code directly in Javascript?

+2  A: 

One of the more heavily used JavaScript compilers is GWT. This compiles Java to JavaScript, and is definitely used in production. The web interface to Google Wave is written in this system.

Also, Skydeck wrote Ocamljs, in order to make it easy for them to write FireFox extensions. That also worked quite well.

In summary, if you can write a good compiler, there is no showstoppers keeping you from writing a good JavaScript compiler.

Adam Goode
A: 

Google Web Toolkit does it (Java to Javascript compiling), and GWT is used widely by Google (duh) and many others, so it definitely is practical.

Since the code is autogenerated, you debug problems in Java - assumption that the problem is in your code, and not in the compiler code, is true in 99% of all cases.

MaxVT
+2  A: 

I believe GWT, based on Java, may be the most popular product of this kind, though I wouldn't describing it as "compiling Java to JS" but rather as "generating JS code". While I personally share some of your doubts, and would rather code JS directly, I have to admit that it is indeed an extremely practical as well as popular tool, entirely production-ready: I observe that, internally, many web apps that are rich and complex enough to warrant a front-end / back-end split are more and more often ending up as a Python back-end and a Java front-end -- the latter specifically to allow GWT (of course there are also plenty of Python front-ends, and plenty of Python back-ends, but I think this is a trend).

Google Wave uses GWT and is probably the most talked-about web app using it so far; together with the huge number of GWT-using web apps listed here, I think it establishes beyond any doubt that the approach is practical (as well as popular;-). Whether it's optimal (vs. writing actual javascript with a good framework in support) is a harder question to answer.

Alex Martelli
Not sure why you wouldn't call it compilation; the GWT compiler is a complete Java compiler in that it does syntactic and semantic analysis before generating (optimized) Javascript. A compiler need not target a "lower-level" language. If it was just "translation" we wouldn't expect it to perform such analysis.
Zach Snow
(Those aren't scare quotes or whatever they are called, sorry if it comes across that way!)
Zach Snow
A: 

As an another example haXe could be mentioned. Haxe is a independent language and compiles to Flash 6-10, JavaScript, NekoVM and also to c++ - source code. Why is this practical?

  • you could use features the language itself could not offer
  • recompile code on multiple platforms (e.g.: form check in JavaScript and on server side)
  • there is a remoting package for communication between the platforms, and its genius.
  • autocompletion through the compiler
  • compile time type checks

If you are interested, you could start reading here.

Hippo