4

Are there any converters or techniques available for converting Javascript 1.7 code with its new language features to Javascript 1.6?

I'm most interested in iterator generators and the yield keyword. The script engine I use is only 1.6, but iterator generators would make for much better code. Writing it by hand seems complicated.

I've tried writing it in C#, decompiling with ILSpy, and porting that to Javascript by hand. But that's a damn mess.

I've searched for such a convertor, but I've found nothing - perhaps due to the search terms being shared with irrelevant results in this case.

dda
  • 6,030
  • 2
  • 25
  • 34
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • I can only think of re-writing the generators by hand as lambda-functions so their variables don't pollute the scope – Paul S. Nov 17 '12 at 14:22
  • 1.6? Which environment uses that engine? The only one I know of is Firefox 1.5. – Šime Vidas Nov 17 '12 at 14:26
  • 1
    @uosɐſ How do you know it's 1.6? Since this is a Google product, I expect it to have the same JS engine as Chrome. **Actually, this entire JavaScript version numbering is in regard to Mozilla's JS implementation only.** Unless Google Apps Script implements Mozilla's JS engine, the versioning doesn't apply. – Šime Vidas Nov 17 '12 at 14:29
  • @ŠimeVidas http://code.google.com/p/google-apps-script-issues/issues/detail?id=418, http://stackoverflow.com/questions/12279357/is-there-a-complete-definition-of-the-google-app-script-syntax-somewhere – Jason Kleban Nov 17 '12 at 14:44
  • @uosɐſ I'll repeat myself. The JavaScript version numbering applies to Mozilla's JS engine exclusively. Using those version numbers in your question only adds confusion. What you want is simple: a polyfill for generators, and `yield`. – Šime Vidas Nov 17 '12 at 14:52
  • @ŠimeVidas - Tell me, which is the more likely available project: A compiler of 1.7 to 1.6 code for general backwards compatibility or specifically another language compiler that only adds to Javascript polyfill for generators and yield? – Jason Kleban Nov 17 '12 at 15:11
  • 1
    @uosɐſ More likely than both of those is a compiler of ES6 to ES5 code, since ECMAScript is the actual JavaScript standard, while "JavaScript" is only Mozilla's implementation of that standard. Notice how "JavaScript" confusingly has two meanings: (1) Mozilla's implementation of ECMAScript, and (2) ECMAScript itself. – Šime Vidas Nov 17 '12 at 15:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19685/discussion-between-uosa-and-sime-vidas) – Jason Kleban Nov 17 '12 at 18:09

1 Answers1

7

I think I've found a solution for you. Check out Traceur. It's a ES6 transpiler, i.e. it converts ES6 code (with all of the ES6 upcoming stuff like generators, classes, modules, yield) to ES5 code which is what today's JS engines are able to interpret.

Check it out here: https://github.com/google/traceur-compiler/

Language features page: https://github.com/google/traceur-compiler/wiki/LanguageFeatures

Notice how most of the stuff that Mozilla's been adding to JavaScript 1.7+ is being standardized in the upcoming edition 6 of the ECMAScript standard. The current edition is edition 5 which already contains some of that stuff (e.g. Array iteration methods, the Object Extensibility API).

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • Cool! Could you show us an example of the ES5 code that is generated for iterator/generators? – Bergi Nov 17 '12 at 15:28
  • @Bergi I've made a jsFiddle from the generators demo available on the Traceur "language features" page. I don't understand the demo-code (e.g. `function*`, `yield`, `yield*`), but the demo lays out how Traceur can be used. 1. Put ES6 code in a ` – Šime Vidas Nov 17 '12 at 18:18
  • @Bergi I've set up a playground on jsFiddle for writing and testing ES6 code: http://jsfiddle.net/8ebGb/5/ – Šime Vidas Nov 17 '12 at 18:32