22.08.10

Incompatible ES5 change: literal getter and setter functions must now have exactly zero or one arguments

ECMAScript accessor syntax in SpiderMonkey

For quite some time SpiderMonkey and Mozilla-based browsers have supported user-defined getter and setter functions (collectively, accessors), both programmatically and syntactically. The syntaxes for accessors were once legion, but SpiderMonkey has pared them back almost to the syntax recently codified in ES5 (and added new syntax where required by ES5).

// All valid in ES5
var a = { get x() { } };
var b = { get "y"() { } };
var c = { get 2() { } };

var e = { set x(v) { } };
var f = { set "y"(v) { } };
var g = { set 2(v) { } };

SpiderMonkey has historically parsed literal accessors using a slightly-tweaked version of its function parsing code. Therefore, as previously explained SpiderMonkey would accept essentially anything which could follow function in a function expression as valid accessor syntax in object literals.

ES5 requires accessors have exact numbers of arguments

A consequence of parsing accessors using generalized function parsing is that SpiderMonkey accepted some nonsensicalities, such as no-argument setters or multiple-argument getters or setters:

var o1 = { get p(a, b, c, d, e, f, g) { /* why have any arguments? */ } };
var o2 = { set p() { /* to what value? */ } };
var o3 = { set p(a, b, c) { /* why more than one? */ } };

ES5 accessor syntax sensibly deems such constructs errors: a conforming ES5 implementation would reject all of the above statements.

SpiderMonkey is changing to follow ES5: getters require no arguments, setters require one argument

SpiderMonkey has now been changed to follow ES5. There seemed little to no gain in continuing to support bizarre numbers of arguments when the spec counseled otherwise, and any code which does end up broken is easily fixed.

As always, you can experiment with a version of Firefox with these changes to accessor syntax by downloading a nightly from nightly.mozilla.org. (Don’t forget to use the profile manager if you want to keep the settings you use with your primary Firefox installation pristine.)

3 Comments »

  1. Updated the dev-doc under: set Operator and get Operator and linked to your blog entry.

    Comment by Fabian — 22.08.10 @ 18:20

  2. Yeah, it makes sense. Though, the spec possibly needs a slight wording errata.

    PropertySetParameterList :
    Identifier

    This production describes that PropertySetParameterList is the only Identifier.

    But later is written:

    with parameters specified by PropertySetParameterList

    Also the production name itself then ambiguous — the word “List” means plural thing (though, can be a list with one element).

    And this is only for that declarative form of accessors’ definition. I.e. just a helper for a programmer which can help and parsing stage to inform about that. It is still possible to define an accessor function with some arguments using Object.definePropert(y/ies).

    Dmitry.

    Comment by Dmitry A. Soshnikov — 23.08.10 @ 11:23

  3. Of course, as I’m sure you already know, the spec nitpicks are best off raised on the es5-discuss mailing list. I have too many other concerns at the moment to mention this there, not when, technically speaking, there’s nothing wrong with the specification beyond aesthetics.

    I’m pretty sure almost everyone would agree it would be way overkill to require Object.defineProperty to require that accessor functions have the correct number of arguments. 🙂

    Comment by Jeff — 23.08.10 @ 13:37

RSS feed for comments on this post. TrackBack URI

Leave a comment

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>