A very cool new utility has just been released by Olav Junker Kjær called Mascara which is an ECMAScript 4 to JavaScript translator (written in Python).
Using this utility you can begin writing ECMAScript 4 code today, receiving its full benefits (such as IDE integration and compile-time type-checking) while still being able to run the resulting code in all modern browsers.
A simple demonstration is up on the Mascara web site which provides a view into the translated source code, along with run-time (and inline) error messages.
A full break-down of the features currently supported are:
- Type verification
- Classes and inheritance, constructors, super initializers
- Static members
- Type inference from initialization
- Parameterized types, Map and Vector
- Union types
- Structural types
- Getters/setters
- Namespaces
- Nullable types
Definitely be aware, however, that while the above feature list is more than enough to get started there are still a a few items left to be implemented.
The most important question, when looking at the above code, should be: How do I begin using this today? Olav went ahead and made it easy to do IDE integration – even setting up instructions on how to integrate with Eclipse on Windows (he currently shows how to integrate using the JScript command-line interface, but it can easily be expanded). I’m quite excited by this prospect – having the ability to do compile-time type-checking and be able to succinctly write classes with inheritance will be a major boon to development.
Additionally, he provides information on how to do automatic CGI translation (naturally, you would want to do this only in a development environment).
He’s continuing to provide frequent updates on the Mascara blog – be sure to follow along if you’re interested in starting to use ECMAScript 4 sooner, rather than later.
Eran (June 10, 2008 at 9:26 am)
Question is, do you consider this to be useful for anything more than intellectual curiosity? would you trust such translated code in a real project on a production machine?
John Resig (June 10, 2008 at 9:38 am)
@Eran: I would absolutely argue that it’s useful beyond intellectual curiosity. Considering that you’re getting the majority of the developmental benefits of ECMAScript 4 along with the ubiquitous cross-browser support of JavaScript it seems like a win-win situation. I can’t answer the “trust” question of the translated code – the project is still quite new and I haven’t poured through the results yet – but it definitely seems like it’s off to a really effective start.
zproxy (June 10, 2008 at 9:43 am)
Alternatively you can use jsc to recompile a .net dll to JavaScript or ActionScript.
C# -> msil -> ActionScript
C# -> msil -> JavaScript
VB.Net -> msil -> ActionScript
VB.Net -> msil -> JavaScript
Visit project homepage: http://jsc.sf.net
mvalente (June 10, 2008 at 9:50 am)
Intelectual curiosity would be my (unfinished) Python(bytecode)-to-Javascript converter…
http://coffeepie.dyndns.ws/
— MV
Tom (June 10, 2008 at 10:39 am)
I wouldn’t start building a big project in Mascara. As they themselves admit, the ES4 spec isn’t stabilized. I think compiling various languages to ES3 is fine, however, so long as you have a debugging strategy.
John Resig (June 10, 2008 at 11:04 am)
@Tom: Sure – although there are people already developing using ECMAScript 4 like the OpenLaszlo guys. There are parts of the spec that are quite solid at this point (in that they’ve been universally implemented, or are in the process of being universally implemented).
As to the debugging strategy – I don’t think it’ll be that hard – looking at the compiled source it’s virtually identical to normal JavaScript. The important part to realize is that the majority of the features that are being implemented here are designed to help development (there are some exceptions but, obviously it’ll be hard to implement performance-related language improvements when you’re confined by the constructs of the older language). As far as script conversion goes it appears as if the class and inheritance has the greatest amount of conversion occurring but even then the result is still rather obvious.
Tom (June 10, 2008 at 1:46 pm)
Thanks for the feedback.
Tom (June 10, 2008 at 1:48 pm)
Actually, if you know the answer and don’t mind explaining quickly, has Apple said why they hate everything? Is it mostly just the same issue as the ES3.1 folks (just not quite as extreme)? Or is it a very different stance?
timothy (June 10, 2008 at 2:28 pm)
This could be great for cases where I maintain essentially the same source in JavaScript and ActionScript.
John Resig (June 10, 2008 at 3:08 pm)
@Tom: I’m not sure exactly why, but they’re working on a whitepaper to explain their position.
Tom (June 10, 2008 at 6:40 pm)
Thanks for the link.
Boszkowo (June 14, 2008 at 2:05 pm)
Thank you from Poland
Metal Hurlant (June 16, 2008 at 4:08 pm)
Talking about playing with ES4 today, I’ve been hacking at a little related project, that wraps the Tamarin ESC compiler in a flash movie with a little bit of glue:
http://screamingdonkey.hurlant.com/
If you’re on firefox, you can see it in action running something familiar:
http://screamingdonkey.hurlant.com/demo/processing/
If you look at the source of that page, it may look perfectly normal, until you see the “type” attribute on some of the tags.