Wednesday, December 30, 2009

ECMAScript 262 Ed. 3 allows extensions

That ECMAScript allows other extensions is no surprise to me. As Chapter 16 states:

An implementation may provide additional types, values, objects, properties, and functions beyond those described in this specification. This may cause constructs (such as looking up a variable in the global scope) to have implementation-defined behaviour instead of throwing an error (such as ReferenceError).

But what surprised me is the reply I got to this bugreport:

The extension in SpiderMonkey is old and it serves to satisfy the conditional compilation use-case:
if (DEBUG) {
  function maybe() {...}
}
if DEBUG evaluates to falsy, no function maybe is defined. More, this means the binding of maybe depends on evaluation of the sub-statement containing it. Same goes for the testcase in comment 0. ECMA-262 Ed. 3 (chapter 16) allows syntactic extensions, which the function as sub-statement definitely is an example of. That we differ from other browsers is a tolerable consequence of ES3 chapter 16 allowing syntactic extension. Note that for ES4, function in a block will be block-scoped, but hoiste to top of the block, so the testcase in comment 0 will work.

So basically all Mozilla implementations of ES3 (SpiderMonkey, Rhino, Tamarin?) have been extended so that if changes the behaviour described in ES3 10.1.3. Functions (not variables) are not added to the Scope Chain if they are wrapped in an if which doesn't evaluate to true.

Doesn't seem much of a problem though as there are only two bug reports about it and an only one comment explaining why it is the way it is.