Forwards-compatible syntax proposal

Lars Hansen lhansen at adobe.com
Mon May 12 11:06:29 PDT 2008


> From: es4-discuss-bounces at mozilla.org 
> [mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Ian Hickson
> Sent: 8. mai 2008 20:18
> To: es4-discuss at mozilla.org
> Subject: Forwards-compatible syntax proposal
>
> One of the problems with ES4 relative to ES3 is that the new 
> syntax means that a script using ES4 features doesn't work in 
> ES3 compilers.

To be precise, what you're saying is that there are ES4 source
fragments that can't be parsed by ES3 compilers.

I have various reactions to this statement, but among the top
contenders are :

  * rejecting syntactically invalid scripts is a feature, 
    not a bug

  * a quiet fallback is going to make web developers' lives even
    worse, having to content not just with incompatible
    implementations of a known language but implementations that
    may censor content in scripts

    (As syntax evolves across multiple language versions, different
    parts of the language are censored by this mechanism, so
    an ES5 program running on an ES3 implementation and an ES4
    implementation would have two different behaviors.)

  * this looks like compatibility handling that applies to a 
    particular environment, and like handling <!-- syntax it
    seems like something browser vendors might try to agree
    on outside the forum of ECMAScript

Another reaction is that there are ES4 source fragments that
/can/ be parsed by ES3 compilers, but whose meaning will silently
be something completely different in the two versions of the
language.  Here are some:

  // a generator expression in ES4, a loop calling a function
  // in ES3

  function f(x) {
      for ( i=0 ; i < 10 ; i++ )
          yield (f(), i)
  }

  // (artificial) three statements in ES3, two in ES4

  x = this function 
  f(x)
  {  x++ }

Finally, I'm worried that accepting your proposal will prevent a
language processor from reporting syntax errors.  This seems like
it has important usability problems.

Summing up, the effect of your proposal is that some (many? most?) 
new scripts are rejected late rather than early, and some new 
scripts just compute different results in old implementations.
My gut feeling is that both are bad ideas, but in any case still
that this is a case for a web standards group to deal with.

I know, those are not technical arguments.  As an exercise we
should apply your proposal to the ES3-ES4 migration to see how
it would have applied there.  What we're looking for are plausible
programs that are not valid ES3 but for which a valid ES3
implementation using your fallback would terminate normally with
an erroneous answer.

All programs using exceptions for control flow are candidates:

   function trampolineLoop(f) {
       for (;;)
           try {
               return f();
           }
           catch (x) {
               f = x
           }
   }

   function f1() { throw function () { return f2(...a) } }  // splat is
ES4 syntax

   trampolineLoop(f1)

This program loops forever if the ES4 body is replaced by 
code that throws a syntax error.

There's more to be said here, presumably, and despite the fact that I'm
sympathetic to the notion of reporting errors as late as possible in
some
deployment situations, I'm skeptical as to whether this kind of a
fallback
is right for a computational language like ES, as opposed to a
declarative
-- and non-looping -- language like HTML, and I'm fairly sure this kind
of fallback does not really belong in the ES spec proper.

--lars

> -----Original Message-----
> From: es4-discuss-bounces at mozilla.org 
> [mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Ian Hickson
> Sent: 8. mai 2008 20:18
> To: es4-discuss at mozilla.org
> Subject: Forwards-compatible syntax proposal
> 
> 
> One of the problems with ES4 relative to ES3 is that the new 
> syntax means that a script using ES4 features doesn't work in 
> ES3 compilers.
> 
> There's not much we can do in the ES3-ES4 language migration 
> about this. 
> But we _can_ prevent this problem from existing again in ES5 and up.
> 
> I propose that ES4 clients, when they hit a syntax error, 
> back up to the start of the current block, and then look for 
> the end of the block, which is the "}" corresponding to the 
> last token in this grammar:
> 
>    block = "{" tokens* "}"
>    tokens = block | string | comment | regexp | other
>    string = single-quote [ no-backslash | escape ]* [ 
> single-quote | end of line ]
>    string = double-quote [ no-backslash | escape ]* [ 
> double-quote | end of line ]
>    string = (triple-quoted string)
>    no-backslash = anything but "\"
>    escape = "\" followed by anything
>    comment = "/*" ... "*/"
>    comment = "//" ... end of line
>    comment = "<!--" ... end of line
>    regexp = "/" [ no-backslash | escape ]* [ "/" | end of line ]
>    other = anything else
> 
> ...and then compile the whole block to one "throw 
> SyntaxError" statement.
> 
> I may have missed some things we should include, and the 
> grammar above may be too simple and may have to have changes 
> made, but hopefully it conveys the basic idea.
> 
> Comments?
> 
> -- 
> Ian Hickson               U+1047E                
> )\._.,--....,'``.    fL
> http://ln.hixie.ch/       U+263A                /,   _.. \   
> _\  ;`._ ,.
> Things that are impossible just take longer.   
> `._.-(,_..'--(,_..'`-.;.'
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
> 



More information about the Es4-discuss mailing list