43

After reading Peter's article on JavaScript I noticed

Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers.

There are currently two popular compilers with some vague ES:Harmony compliance:

Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end.

Tracuer seems to be sticking more rigorously to the ES:Harmony specification but I don't know whether it intends to become a full ES:Harmony compiler.

Since the aim is to to compile ES6 down to ES3 it would also need to support ES5 features (and probably a switch whether to compile ES5 to ES3 or ES6 to ES3).

  1. Are there currently any other projects aiming to create a full ES:Harmony to ES3 compiler?
  2. Is it wise to start writing such a compiler knowing that the standard is young / unstable / in flux.
  3. Are there currently any ES5 -> ES3 compilers?

I've left a question on the Traceur mailing list.

The aim of such a compiler would be backwards compatibility with ES3. Not full emulation of ES5 and ES6 in ES3.

leech
  • 7,950
  • 6
  • 60
  • 72
Raynos
  • 156,883
  • 55
  • 337
  • 385
  • Good question. Googlers at least want Traceur to be a playground that supports pretty much everywhere, but whether they intent to make it a compiler with switches, I bet we have to wait for a team member to confirm. On a side note, there are features in ES6/5 that cannot be simply compiled back to ES3 so you will never get full compilation support. – Tower Jul 02 '11 at 19:45
  • @rfactor depends what you mean by features, we can't preserve the big-O complexity of all the new API / syntax but we can fully emulate it all. – Raynos Jul 02 '11 at 20:02
  • 3
    Your quote from Brendan Eich is not about the goal of Harmony being a good language to compile into ES3/5. The quote is about Harmony being a better **target** for *other languages* to compile **to**. There are a lot of languages that compile to JavaScript. A goal of Harmony is to become a base-level better language, e.g. the "assembly of the web". – Nathan Wall Jan 17 '13 at 02:14

8 Answers8

14

(shameless but relevant plug below)

Caja is reworking its ES5 support via ES5/3 and will do the same for ES harmony. So our structure would be implemented as a Harmony to ES3 layer which can be skipped for real harmony implementations, and then a separable loader that preserves the security properties that concern caja.

Like Traceur, members of the Caja team are part of TC39 (the committee defining ES Harmony).

I don't know about Coffeescript's plans, but it was mentioned during discussions of Harmony modules. Module loaders will likely have the ability to intercept loaded source code (via eval hooks) and rewrite it before module initialization, so if a module is written in CoffeeScript, a runtime CoffeeScript rewriter could be invoked at initialization time. This would allow apps to be composed of modules written in multiple languages that compile down to Harmony at load time.

One thing to note is that not everything in Harmony can be implemented easily via translation. For example, implementing weak maps correctly would require implementing your own garbage collector in JavaScript and even if you did that you would probably just reintroduce the host object/native object cycle problem.

Ry-
  • 199,309
  • 51
  • 404
  • 420
Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
  • 1
    My interest lie in a compiler that only compiles correct ES5/ES:Harmony to ES3. Something that adheres to the specification and doesn't give you any extras out of the box. This way everyone can write ES:Harmony but still support IE9/10, FF4/5, Saf5, Op11, etc. – Raynos Jun 28 '11 at 18:05
  • 1
    @Raynos, That is also what I'm talking about with Caja. Caja currently implements ES5 strict mode on top of ES3 separate from its security work. When Harmony is closer to completion we will likely take the same tack for forwards compatibility. – Mike Samuel Jun 28 '11 at 18:39
  • @MikeSamuel Your ES5/3 implementation does some extra thing as well either by design or due to browser support limitations. If it's the former then we have different goals, if it's the latter then you might want to mention it on the page :) – Raynos Jun 28 '11 at 18:41
  • @Raynos, we are looking at ways to shrink the difference between ES5/3 and native ES5. One thing that's unlikely to go away is the "strict only" restriction because we have no clients for non-strict mode. Some differences like primitive `this` coercion are due to browser limitations/performance requirements. – Mike Samuel Jun 28 '11 at 18:46
  • @MikeSamuel your goal is still defence ;) Exposing `eval` and `Function` safely is a huge task where-as my goal of providing a down compiler supports `eval` and `Function` because there is a one-to-one mapping. There are also various features like "strict-mode" which don't have to be supported in the compiler because the aim is backwards compatibility not full emulation. Asides from that I was hoping to write the compiler in JavaScript ;) – Raynos Jun 28 '11 at 19:18
  • @Raynos, you are right that our priorities are different. Our goal is to provide a subset of the language that allows proving of useful security properties, and to allow security policies to be expressed in code while preserving APIs. This produces significant differences in ES5/3, but we have been working with TC39 and they have been very receptive to our arguments to base Harmony on ES5 strict mode and to our proxy, weakmap, and quasi proposals so I think the difference between ES Harmony and the SES Harmony (secure ecmascript) will be smaller than ES5 - SES5. – Mike Samuel Jun 28 '11 at 19:24
  • @Raynos, in particular, we do plan to support `eval` in SES Harmony, and are still looking at ways to do that efficiently in SES5. – Mike Samuel Jun 28 '11 at 19:25
  • `WeakMap` *can* be shimmed - e.g. https://github.com/drses/weak-map. Proxy and subclassing is impossible to shim/transpile correctly. – Sheepy Nov 30 '15 at 02:30
11

Check out TypeScript, Microsoft's new language based on ES6.

orad
  • 12,746
  • 18
  • 69
  • 107
  • 3
    Works great for many structural ES6 features, excellent if you throw in a few shims the missing ES6 methods and objects (as TypeScript adds almost no runtime presence). – Bartvds Nov 27 '13 at 19:09
  • TS is more like ES6 than CoffeeScript. The cs is more like Ruby than ES6. – Martin Jun 10 '14 at 08:40
8

Continuum has implemented most of the relevant features and should run in es3 browsers (like older IEs).

kybernetikos
  • 7,500
  • 1
  • 43
  • 52
4

As of the time of typing, we now have Babel. It integrates with many different build tools/systems, and will transpile ES6+ in order to support legacy browsers (it doesn't state which version it targets, but it does say that it targets IE9+).

To install it type npm install babel -g.

Note that it has rather a lot of dependencies and when installed it is ~23.4 MB (2888 files).

starbeamrainbowlabs
  • 4,842
  • 6
  • 38
  • 68
4

Mascara is probably what you're looking for.

Eli Grey
  • 32,712
  • 13
  • 69
  • 92
  • 3
    As nice as that is, it does not follow the ES6 standard, it's just based on it. I don't want a "Better JavaScript" -> JavaScript compiler. I want a faithful ES:Harmony -> ES3 compiler and it's supposed to be open source! – Raynos Jul 01 '11 at 08:57
  • 1
    "it's supposed to be open source" - I don't recall you mentioning that in your post. Not everything good is open source, Raynos. Also, you can decompile the .pyc files if you really need to make a change. Not to mention that Traceur is only *based* on ES Harmony too, not an implementation of it. – Eli Grey Jul 01 '11 at 19:29
  • 2
    that's true. But at least it's open source so it can be forked to be fully and only es harmony compliant. A compiler this should really be an OS community project. – Raynos Jul 01 '11 at 20:14
  • 1
    @Raynos: ES:Harmony/ES6 is not yet a finished standard (like ES5 is), it is still just a set of proposals under consideration. So none if the alternatives can be faithful at this point. – JacquesB Mar 08 '12 at 07:47
  • 1
    @JacquesB they can at least try to be faithful ;) there is a difference between an es:h transpiler and a X -> JS compiler thats _inspired_ by es:h – Raynos Mar 08 '12 at 13:53
3

Google Closure Compiler (Github) is a great tool for ES6 compilation. It's a simple Java jar that is used from the command line. There are other options such as API services and GUIs, but I find that it was best to set up an automatic build system hooking into the Java JAR. It can transpile your ES6 code into ES5 compatible code. I started using it for compressing and obfuscating code, but it can also do error checking and the ES6 transpilation as I mentioned.

Note that the ES6 features are marked as experimental. But I'm planning on using them in production soon, since my tests were rock solid.

Avindra Goolcharan
  • 3,288
  • 36
  • 37
  • Hi, could you please tell which command to use, to convert ES 6 to ES 5? – Ned Aug 21 '15 at 07:06
  • 1
    ```java -jar compiler.jar --language_in ECMASCRIPT6 --language_out ECMASCRIPT5 --js_output_file=app.min.js app.js``` To see all the options it has, check ```java -jar compiler.jar --help``` – Avindra Goolcharan Aug 21 '15 at 18:53
1

There's also https://github.com/matthewrobb/six

Six is a language super-set of JavaScript that enables new syntactic features from the 6th edition of ECMAScript to be used, through a transpiler, in your scripts today.

WARNING: Still in a very early state, proceed with caution.

Community
  • 1
  • 1
Ilia Choly
  • 16,442
  • 12
  • 75
  • 145
  • 2
    Project suspended, last commit 8 months ago as of now. – Radek Jul 09 '13 at 23:14
  • I think the market for "better javascript" has been pretty much saturated by CoffeeScript, though. Even with Microsoft and Google creating "better javascripts" (Dart/TypeScript), nobody seems to care. I even say this as a non-CoffeeScripter (for me plain js is good enough). – Camilo Martin Aug 05 '14 at 22:28
0

I'm not sure in what instance compilation back to ES3 would valuable as opposed to ES5, seeing that implementation changes are limited to array and object helper functions, and ES5 support is so prevalent.

So for completeness, another ES6 to ES5 compiler is the esnext project by Square. It is a collection of a number of modules designed to polyfill various ES6 features provided in one package. Here is the list modules included: https://github.com/square/esnext#available

Andrew Odri
  • 6,983
  • 4
  • 38
  • 52
  • The only place I can think of immediately is on Windows, when using .js files with Windows Scripting Host, which uses ES3, and has not plans upgrading. – zumalifeguard Sep 17 '14 at 11:18