<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Where&#039;s Walden? &#187; js</title>
	<atom:link href="http://whereswalden.com/tag/js/feed/" rel="self" type="application/rss+xml" />
	<link>http://whereswalden.com</link>
	<description>Mozilla, politics, economics, law, backpacking, cycling, and other random desiderata</description>
	<lastBuildDate>Sat, 19 Jun 2010 05:13:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SpiderMonkey change du jour: the special __parent__ property has been removed</title>
		<link>http://whereswalden.com/2010/05/07/spidermonkey-change-du-jour-the-special-__parent__-property-has-been-removed/</link>
		<comments>http://whereswalden.com/2010/05/07/spidermonkey-change-du-jour-the-special-__parent__-property-has-been-removed/#comments</comments>
		<pubDate>Fri, 07 May 2010 23:22:52 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[caja]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[fbjs]]></category>
		<category><![CDATA[getGlobalForObject]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[__parent__]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1679</guid>
		<description><![CDATA[tl;dr
The special __parent__ property has been removed from SpiderMonkey and is no longer in nightly builds.  If you don&#8217;t use __parent__ or don&#8217;t know what the property does, you didn&#8217;t miss much.
If you use __parent__, you have a couple replacements.  If you were using it to determine the global object for another object, [...]]]></description>
			<content:encoded><![CDATA[<h2><abbr title="too long; didn't read">tl;dr</abbr></h2>
<p>The special <code>__parent__</code> property has been removed from SpiderMonkey and is no longer in nightly builds.  If you don&#8217;t use <code>__parent__</code> or don&#8217;t know what the property does, you didn&#8217;t miss much.</p>
<p>If you use <code>__parent__</code>, you have a couple replacements.  If you were using it to determine the global object for another object, use <a href="https://developer.mozilla.org/en/Components.utils.getGlobalForObject"><code>Components.utils.getGlobalForObject</code></a> instead.  If you were using it only to test its value against an expected value, use <code>nsIDOMWindowUtils.getParent</code> instead (but do note that its semantics are not absolutely identical to those of <code>__parent__</code>).  If you were using it from unprivileged web scripts as a potential vector for security exploits, I feel your pain and will take no steps to assuage it.  If you were using it some other way, comment and we&#8217;ll figure something out for your use case.</p>
<p>If you think you understood the <code>__parent__</code> property (you probably don&#8217;t), or if you&#8217;re interested in the nitty-gritty details of JavaScript semantics, read on for the details of this esoteric property and the reasons for its removal.</p>
<h2>Scoping in JavaScript</h2>
<p>In the following example code, when the function <code>g</code> is invoked, what stores the <code>v</code> variable?</p>
<pre class="code" data-language="javascript">
function f(a)
{
  var v = a;
  function g() { return v; }
  return g;
}

var fun = f(2);
fun();
</pre>
<p>The variable is accessed in an enclosing <dfn>scope</dfn>, created when the enclosing function was invoked.  In the ECMAScript standard, the location of such variables is an object, stored as an internal <code>[[Scope]]</code> property of the function being called.  In <abbr title="ECMAScript 3rd edition">ES3</abbr> this object was a standard JavaScript object; <abbr title="ECMAScript 5th edition">ES5</abbr> tightens semantics slightly and uses a simpler structure, but the idea&#8217;s the same.</p>
<h2>Meet the <code>__parent__</code></h2>
<p>It&#8217;s not possible to access the object stored as <code>[[Scope]]</code> in ECMAScript proper, but it has been possible to access it in SpiderMonkey and Mozilla-based browsers.  The magical <code>__parent__</code> property can often provide access to this value:</p>
<pre class="code" data-language="javascript">
this.toString = function() { return "global"; }
var q = 17;
function foo() { return q; }
print(foo.__parent__); // prints "global"
</pre>
<h2>&#8220;Often&#8221;?  When does <code>__parent__</code> not expose this value?</h2>
<p><code>__parent__</code> doesn&#8217;t always reflect <code>[[Scope]]</code> because ECMAScript requires that certain objects not be made available to scripts.  Among such objects are what SpiderMonkey refers to as With objects, Call objects, and Block objects.</p>
<h3>With objects</h3>
<p>SpiderMonkey creates With objects to handle the esoteric, non-lexical name lookup required by the semantics of the <code>with</code> statement in JavaScript.  These semantics require that a name, depending on the runtime object used in the <code>with</code>, refer to a property of that object <em>or</em> to a variable in an enclosing scope.  It&#8217;s impossible to know in general which will be the case before runtime, and therefore it&#8217;s impossible to speed up a lot of script that runs inside a <code>with</code>.  (Incidentally, this is why you should <em>never ever ever</em> [<em>ever</em>] use <code>with</code> in performance-critical code.  Use a two-letter abbreviation variable to save typing, if verbosity is your concern.)  We can&#8217;t simply use the <code>with</code> block&#8217;s object as the scope, because if we miss on a lookup there we want to fall back to the normal scope; therefore we introduce a With object.  Here&#8217;s an example of a situation where a With object is created:</p>
<pre class="code" data-language="javascript">
with ({ toString: function() { return "with object"; } })
{
  print(function() { return toString; }.__parent__); // prints "with object"
}
</pre>
<p><code>__parent__</code> doesn&#8217;t actually give you the With object, because it&#8217;s a carefully-tuned internal value.  With objects have behavior and functionality optimized for their particular purpose, and if we simply exposed them to scripts it would probably be possible to do Very Bad Things.  Consequently, if you try to access <code>__parent__</code> in a situation where you &#8220;should&#8221; get a With object, we instead give you back the object where the With object begins its search: the object used for the <code>with</code> block.  This may be what a developer superficially familiar with <code>__parent__</code> might expect, but it is nevertheless a lie.</p>
<h3>Call objects</h3>
<p>SpiderMonkey&#8217;s Call objects represent the local variables of a function call.  Therefore, Call objects correspond to the <code>[[Scope]]</code> of functions.  Returning to the original example, reproduced below, <code>v</code> is stored in a Call object, which is the <code>[[Scope]]</code> of the function <code>g</code>:</p>
<pre class="code" data-language="javascript">
function f(a)
{
  var v = a;
  function g() { return v; }
  return g;
}

var fun = f(2);
print(fun.__parent__);
</pre>
<p>When you attempt to access <code>g</code>&#8217;s <code>__parent__</code> property, SpiderMonkey censors the <code>[[Scope]]</code> and returns <code>null</code>, because ECMAScript requires that Call objects not be exposed.  This case, which is far more common than the <code>with</code> case, completely prevents access to <code>[[Scope]]</code> at all (rather than exposing a half-representation of the value).  This situation is even more pervasive in light of the modern JavaScript encapsulation practice of enclosing libraries in closures for pseudo-namespacing purposes.  In many libraries <code>__parent__</code> on many functions of interest gives no value whatsoever.</p>
<h3>Block objects</h3>
<p>One of the first things every JavaScript developer learns is that <em>JavaScript variables are not scoped to blocks</em>.  Language tutorials usually emphasize this point, because it differs from most other languages (and in particular, it differs from the C-ish family of languages, the syntax of which JavaScript borrows to a fair degree).  The conventional wisdom is that names in functions are always scoped to enclosing functions or to the global scope.  If you want a locally-defined name, you have to wrap it up in a function.</p>
<p><em>The conventional wisdom is wrong.</em></p>
<p>Here&#8217;s proof: what does this example print?</p>
<pre class="code" data-language="javascript">
var v = "global";
function test(frob)
{
  try
  {
    if (frob === 0)
      return v + " try";
    if (frob === 1)
      throw "local";
  }
  catch (v)
  {
    return v + " catch";
  }
  finally
  {
    if (frob === 2)
      return v + " finally";
  }
  return v + " after finally";
}

print("not throwing, in try:          " + test(0));
print("throwing, in catch:            " + test(1));
print("not throwing, in finally:      " + test(2));
print("not throwing, after finally:   " + test(3));
</pre>
<p>Behavior depends on whether the binding for <code>v</code> as introduced by the <code>catch</code> is scoped to the function or to something else.  JavaScript specifies that <code>v</code>, referring to the value potentially thrown while executing the <code>try</code> block, is scoped <em>only</em> to the <code>catch</code> block.  Thus <code>v</code> in the <code>catch</code> refers to the thrown value; all other uses of <code>v</code> are outside the <code>catch</code> block and refer to the global variable <code>v</code>.  Therefore the output is this:</p>
<pre class="code">
not throwing, in try:          global try
throwing, in catch:            local catch
not throwing, in finally:      global finally
not throwing, after finally:   global after finally
</pre>
<p>Any function which referred to <code>v</code> inside the <code>test</code> function would have a Call object as its <code>[[Scope]]</code> &mdash; except for a function defined inside the <code>catch</code> block.  Such a function at that location would have to capture the thrown value, so there must be something else on the scope chain before the Call object.  SpiderMonkey refers to these objects as Block objects, because they implement traditional block-level scoping.</p>
<p>Block objects have the same issues as Call objects, so SpiderMonkey censors them to <code>null</code> as well.  For optimization purposes we&#8217;d like to &#8220;boil them away&#8221; whenever possible, so as not to create an obnoxious little one-property object when we catch an exception and define a function that captures it.  Exposing such an object directly prevents these optimizations, and would likely expose some security vulnerabilities.</p>
<p>But <code>__parent__</code>&#8217;s issues don&#8217;t stop merely at those induced by complex language semantics.  Even perfectly simple code &mdash; simpler even than nested functions &mdash; has potential for unpredictability.</p>
<h2>If certain optimizations are implemented, <code>__parent__</code> may be a lie</h2>
<p>Consider this script in the global scope:</p>
<pre class="code" data-language="javascript">
function fun() { return 17; }

print(fun.__parent__);
</pre>
<p>Does <code>fun</code>&#8217;s <code>[[Scope]]</code> need to be the global object?</p>
<blockquote><p>It&#8217;s OK to cheat if you never get caught.</p>
</blockquote>
<div class="attribution">Smalltalk implementer maxim</div>
<p>ECMAScript provides no way for script to access <code>[[Scope]]</code>.  It&#8217;s a construct used to ease specification, and it need not even exist in implementations.  <code>fun</code> never uses any values from its enclosing scope, so there&#8217;s no reason that <code>[[Scope]]</code> must be global object.  An implementation that carried around <code>[[Scope]]</code> with each function might simply make <code>[[Scope]]</code> be <code>null</code>, which could potentially speed up some <a href="http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29">garbage collection</a> algorithms.  Similarly, a function which refers only to never-modified variables in enclosing scopes might simply copy those variables&#8217; values and, again, make <code>[[Scope]]</code> lie.  Should a specification-artifact internal property constrain implementations looking to improve performance, or code size, or any number of other measures of desirability?  Given where JavaScript use is heading now, the answer must be no.</p>
<p>For certain types of functions, there are very good reasons, even in simple cases, to make <code>[[Scope]]</code> a lie.  SpiderMonkey implements some optimizations along these lines, but those optimizations don&#8217;t affect the value we store for <code>[[Scope]]</code> at the present.  If it were advantageous to change that, we would do so in a heartbeat.  Therefore, while <code>__parent__</code> may have a reliable value now, it&#8217;s possible it would not in the future.</p>
<h2><code>__parent__</code> is available even where you think it isn&#8217;t</h2>
<p>Oddly enough, <code>__parent__</code>, despite its representing <code>[[Scope]]</code>, isn&#8217;t just applicable to function objects.  Instead, its value is generalized to <em>all</em> objects, in a way even more difficult to describe than that above.  (Very roughly, <code>__parent__</code> on non-function objects corresponds to the object which would be the <code>__parent__</code> of a function created in the same context.  I think.  Mostly the value is used for security checks; improper setting of <code>__parent__</code> is a common cause of implementation-caused <abbr title="cross-site scripting">XSS</abbr> vulnerabilities.)  This generalization and its semantics have even less support in the specification, so its meaning is even less clear than for functions.</p>
<h2><code>__parent__</code> doesn&#8217;t expose anything useful</h2>
<p><code>__parent__</code>, beyond having unintuitive behavior, doesn&#8217;t tell the developer anything he&#8217;d care to know.  Why would you need to access the enclosing scope of a function, as an object, in the first place?  The very definition of the problem is obscure; it is almost a prerequisite to have read the ECMA-262 specification to be able to ask the question.</p>
<p>In the searching of various codebases that we&#8217;ve done, we&#8217;ve found only these use cases for <code>__parent__</code>:</p>
<dl>
<dt>Testing</dt>
<dd>A small number of Mozilla tests use <code>__parent__</code> to verify proper scoping of objects.  Since the value as we use it has security implications, this use case is reasonable &mdash; but it has extremely limited applicability.  This use case can be supported by creating an alternate means of accessing an object&#8217;s parent, a means exposed through <abbr title="Cross-platform component-object model">XPCOM</abbr>, not through JavaScript itself, and certainly not by <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#45610">polluting every object with the functionality</a>.  We have implemented <code>nsIDOMWindowUtils.getParent(obj)</code> to support this testing-oriented use case.  Beware: <em>this method doesn&#8217;t censor like <code>__parent__</code> does</em>!  Tread cautiously when using it where a With, Call, or Block object might be exposed, and don&#8217;t use the returned object except in immediate strict equality (<code>===</code>) checks.  (<abbr title="Nota bene, note well" lang="la">NB</abbr>: we&#8217;ve implemented it this way out of expediency; if it turns out to be too great a responsibility for typical testers, we&#8217;ll likely change it to censor.)</dd>
<dt>Determining the global context (global object) where an object was created</dt>
<dd>Since <code>__parent__</code> often (for any function not nested in another function) is simply the global object, some people have used <code>__parent__</code> to retrieve the global object associated with an arbitrary object.  By walking from <code>__parent__</code> to <code>__parent__</code> you can often reach the global object this way.  However, since the <code>__parent__</code> value is sometimes a lie, it&#8217;s not as simple as <code>var global = obj; while (global.__parent__) global = global.__parent__;</code>.  Instead, you have to be careful to start from an object with a known <code>__parent__</code>, one guaranteed not to be a nested function or similar.  The easiest way to do this is to first walk up the prototype chain to eventually bottom out at <code>Object.prototype</code>, then to walk the parent chain.  This method is baroque and non-obvious, and it requires careful tiptoeing around incorrect <code>__parent__</code> values.  If you know the object whose global you want to retrieve is a <abbr title="document object model">DOM</abbr> node, the fully-standard <code>node.ownerDocument.defaultView</code> property also provides global access.  Otherwise, the use case would be better addressed by exposing a method somewhere to directly retrieve the corresponding global object.  We have <a href="http://whereswalden.com/2010/05/05/new-mozilla-developer-feature-components-utils-getglobalforobjectobj/">recently</a> added such a method to support this use case: <a href="https://developer.mozilla.org/en/Components.utils.getGlobalForObject"><code>Components.utils.getGlobalForObject(obj)</code></a>.</dd>
<dt>Being evil to Firefox</dt>
<dd>A fair number of security bugs reported against SpiderMonkey in recent years have worked by successfully confusing the engine into assigning a bad parent to an object.  This incorrect security context can then be used as a vector to run code in the context of another website (resulting in an XSS hole, possibly usable against any website), or in some cases, in the context of the browser &#8220;chrome&#8221; itself (providing the capability for arbitrary code execution &mdash; the worst-case scenario as far as exploits are concerned).  <code>__parent__</code> is immutable and can&#8217;t itself be used to confuse the engine (at least in the absence of bugs, and we have no way to demonstrate such).  However, it provides greater visibility into the engine, and it can sometimes expose values to script which wouldn&#8217;t (and shouldn&#8217;t) be accessible any other way.</dd>
<dt>Being evil to sandboxing mechanisms</dt>
<dd><code>__parent__</code> exposes security holes in some websites as well as in the browser directly.  Such websites are those which purport to &#8220;safely&#8221; execute scripts provided by other users, through sandboxing or other mechanisms.  For example, <a href="http://facebook.com/">Facebook</a> uses <a href="http://wiki.developers.facebook.com/index.php/FBJS"><abbr title="Facebook JavaScript">FBJS</abbr></a>, a script-rewriting plus runtime-check mechanism, to allow applications to include interactivity.  <a href="http://google.com/">Google</a>&#8217;s <a href="http://code.google.com/p/google-caja/">Caja</a> system provides a rigorous capabilities-providing system (also through script-rewriting plus runtime checks) to do the same, and it too is used in public sites these days.  One requirement of such systems is that the global object must never be directly exposed: if you have the global object, you have unfettered access to the document, you have <code>eval</code>, you have <code>Function</code>, and you are utterly and thoroughly hosed.  <code>__parent__</code> provides easy access to this, so all these systems must censor access to <code>__parent__</code>, both statically (<code>obj.__parent__</code>) and dynamically (<code>var p = "__parent__"; obj[p]</code>).  <a href="http://stuff.mit.edu/iap/2008/facebook/">Not everyone knows, or remembers, that this is necessary.</a>  Removing <code>__parent__</code> reduces attack surface in JavaScript sandboxes.</dd>
</dl>
<p>The former two use cases are better addressed in other ways.  The latter two are holes best removed entirely.</p>
<h2><code>__parent__</code> is being removed</h2>
<p><code>__parent__</code> is being removed from SpiderMonkey.  The value it purports to expose is esoteric and has semantics defined by a specification which JavaScript developers shouldn&#8217;t really have to read to understand it.  The identification of that value is non-intuitive.  Sometimes the value it exposes is a lie.  There are plausible reasons why it might be made to lie if potential performance-improving ideas were implemented.  It&#8217;s exposed even in places where it makes little sense.  The use cases for it can be and are better served in other ways &mdash; or explicitly not served, when it serves as a vector for security vulnerabilities.  In sum <code>__parent__</code> doesn&#8217;t pass muster, so it&#8217;s being removed.</p>
<p>You can experiment with a version of Firefox without support for <code>__parent__</code> by downloading a nightly from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a>.  (Don’t forget to <a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep the settings you use with your primary Firefox installation pristine.)  The next release is many months away, which should provide plenty of time for extension developers to update their extensions to not use <code>__parent__</code>, if by chance they had managed to discover it and use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/05/07/spidermonkey-change-du-jour-the-special-__parent__-property-has-been-removed/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>New Mozilla developer feature: Components.utils.getGlobalForObject(obj)</title>
		<link>http://whereswalden.com/2010/05/05/new-mozilla-developer-feature-components-utils-getglobalforobjectobj/</link>
		<comments>http://whereswalden.com/2010/05/05/new-mozilla-developer-feature-components-utils-getglobalforobjectobj/#comments</comments>
		<pubDate>Wed, 05 May 2010 21:17:57 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[getGlobalForObject]]></category>
		<category><![CDATA[global object]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[window]]></category>
		<category><![CDATA[xpcom]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1749</guid>
		<description><![CDATA[Suppose you&#8217;re an extension developer implementing some sort of event listener-like interface corresponding to browser windows.  You&#8217;d like listeners to stick around as long as the original browser window is open, so when the browser window&#8217;s unload event fires when the browser window is closed, you want to remove the listener.  Further, for [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you&#8217;re an extension developer implementing some sort of event listener-like interface corresponding to browser windows.  You&#8217;d like listeners to stick around as long as the original browser window is open, so when the browser window&#8217;s <code>unload</code> event fires when the browser window is closed, you want to remove the listener.  Further, for simplicity, you&#8217;d like to be able to reuse the same interface as the <abbr title="document object model">DOM</abbr> uses: <code>EventTarget.addEventListener(eventName, listener, bubbles)</code>.  But if you do that, how do you know what browser window to associate with a listener?  One possibility is that you could associate event listeners with windows if you could determine the global object that corresponded to the event listener in question.  (Assume <span lang="la">arguendo</span> that you control all use of listeners, so every listener is straightforwardly created in the window with which it&#8217;s associated &mdash; no shenanigans passing listeners across windows.)  This isn&#8217;t the only situation where one might wish to know the global object, but it does happen to be a somewhat common one.</p>
<p>Is it possible to determine the global object corresponding to an arbitrary object?  You can through a convoluted sequence of actions involving prototype hopping using <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object/GetPrototypeOf"><code>Object.getPrototypeOf</code></a> and walking the &#8220;scope chain&#8221; for the object (using an obscure Mozilla-specific feature whose details I omit).  More simply, if the object in question is a DOM node, you could use <code>node.ownerDocument.defaultView</code>, which, while quite understandable, is still DOM-specific.  But wouldn&#8217;t it be much better if there were some simpler, universal way to determine this value, rather than skirting the edges of feature intentionality?</p>
<p>Firefox nightlies now implement support for a new method available to extensions and other privileged code: <a href="https://developer.mozilla.org/en/Components.utils.getGlobalForObject"><code>Components.utils.getGlobalForObject(obj)</code></a>.  It&#8217;s designed to support the specific need of determining the window where an object was created.  (<abbr title="cross-platform component-object model">XPCOM</abbr> components of course have a global object that isn&#8217;t a window, and that global object will be returned by the method in those circumstances.)  Its functionality needs little explanation:</p>
<pre class="code" data-language="javascript">
/* in the global scope */
var global = this;
var obj = {};
function foo() { }
assertEq(Components.utils.getGlobalForObject(foo), global);
assertEq(Components.utils.getGlobalForObject(obj), global);
</pre>
<p>If you&#8217;re using the previously-noted method involving prototype- and scope-chain-hopping, you should change your code to use <code>Components.utils.getGlobalForObject(obj)</code> instead.  This new method is much simpler and clearer, and upcoming changes mean that the old method will no longer work in future nightlies and releases.  The next release is still several months out, so you should have plenty of time to adjust.</p>
<p>As always, you can experiment with a bleeding-edge version of Firefox that supports <code>Components.utils.getGlobalForObject(obj)</code> by downloading a nightly from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a>. (Don’t forget to <a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep the settings you use with your primary Firefox installation pristine.)</p>
<p>A last note: the method as currently implemented in nightlies suffers from a small bug when used with objects from unprivileged code &mdash; objects from scripts in web pages, that sort of thing.  It&#8217;s fixed in the TraceMonkey repository, and the adjustment should make its way into the mozilla-central repository (and thus into nightlies) in short order.  If you only use the method on objects from privileged scripts, I don&#8217;t believe you&#8217;ll encounter any problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/05/05/new-mozilla-developer-feature-components-utils-getglobalforobjectobj/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>More SpiderMonkey changes: ancient, esoteric, very rarely used syntax for creating getters and setters is being removed</title>
		<link>http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/</link>
		<comments>http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 21:17:42 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[accessor]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[getter]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[setter]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1643</guid>
		<description><![CDATA[tl;dr
We&#8217;ve removed support for a handful of obsolete getter/setter syntaxes in SpiderMonkey and Mozilla.  This does not include { get property() { return "value"; }, set property(v) { } }, which is widely used and which is part of the latest standard.  If you don&#8217;t get any syntax errors with your code, you [...]]]></description>
			<content:encoded><![CDATA[<h2><abbr title="too long, didn't read">tl;dr</abbr></h2>
<p>We&#8217;ve removed support for a handful of obsolete getter/setter syntaxes in SpiderMonkey and Mozilla.  This does <em>not</em> include <code>{ get property() { return "value"; }, set property(v) { } }</code>, which is widely used and which is part of the latest standard.  If you don&#8217;t get any syntax errors with your code, you don&#8217;t need to worry about this.  If you do, <a href="#getter-setter-updating-for-removals">skip to the end</a> for details on how to adjust your code to cope.  But really, you should read it all for the sheer joy of learning about all sorts of awful syntax you didn&#8217;t even know existed before it went away.  [Or at least empathize with us liberated SpiderMonkey hackers.  <img src='http://whereswalden.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> ])</p>
<h2>Properties in JavaScript and ECMAScript 3</h2>
<p>The fundamental data structure in JavaScript is the <dfn>object</dfn>: a container mapping names to values through <dfn>properties</dfn>.  You can add, remove, or change the value associated with any property, so long as the property may be modified.  All user-defined properties are infinitely modifiable in any of these ways; only a few properties defined by ECMAScript (the standard on which JavaScript is based) are not fully modifiable.</p>
<pre class="code" data-language="javascript">
var obj1 = {};
obj.property = 17; // add
var obj2 = { property: 42 }; // add from birth
obj2.property = 17; // change it
delete obj2.property; // remove it
</pre>
<p>Properties which store values are useful, but what if you want properties which can <em>do</em> things when you interact with them?  What if you want to have properties which map strings to <em>lazily-computed</em> values?  Or what if you want setting a property to have side effects (as, for example, setting an array&#8217;s length to 0 removes all elements in it)?</p>
<h2>Properties with getters and setters in JavaScript</h2>
<p>If you want properties which have functionality beyond just holding a value, you need <dfn>getters</dfn> and <dfn>setters</dfn>, stored within <dfn>accessor properties</dfn>.  (Properties which hold values are called <dfn>data properties</dfn>.)  JavaScript has long included extensions to ECMAScript to create accessor properties, both syntactic:</p>
<pre class="code" data-language="javascript">
var o1 =
  {
    get property() { print("gotten!"); return "get"; },
    set property(v) { print("sotten!  " + v); }
  };
var v1 = o1.property; // prints "gotten!", v1 === "get"
o1.property = "new"; // prints "sotten!"
</pre>
<p>&#8230;and programmatic:</p>
<pre class="code" data-language="javascript">
var o2 = {};
o2.__defineGetter__("property", function() { print("gotten!"); return "get"; });
o2.__defineSetter__("property", function() { print("sotten!"); });
var v2 = o2.property; // prints "gotten!", v2 === "get"
o2.property = "new"; // prints "sotten!"
</pre>
<p>Getters and setters are now part of <abbr title="ECMAScript 5th edition">ES5</abbr>.  The syntax demonstrated above is valid ES5; a different API, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/defineProperty"><code>Object.defineProperty</code></a>, provides more flexible support for specifying getters and setters dynamically.  Developers using the old-school APIs should begin updating to use the new API as browsers make new releases supporting it.  Firefox in particular will include support for <code>Object.defineProperty</code> in its next major release, likely to occur in the latter half of the year.</p>
<h2>Examining antediluvian accessor syntax</h2>
<p>Unbeknownst to the vast majority of web developers, extension developers, and even Mozilla developers, in the past JavaScript has included other getter and setter syntaxes.</p>
<h3>Named ES5-like getters and setters</h3>
<p>If you look up the function that acts as the getter given ES5-standard getter syntax, what&#8217;s the name of that function?</p>
<pre class="code" data-language="javascript">
var o = { get property() { return "get"; } };
print(Object.getOwnPropertyDescriptor(o, "property").get.name);
</pre>
<p>There are a couple plausible answers here: &#8220;<code>anonymous</code>&#8220;, &#8220;<code>property</code>&#8221; (the property name), or &#8220;&#8221; (the empty string) might be reasonable.   JavaScript and ES5 arbitrarily create the getter or setter as a function whose name is the empty string.  What if you <em>wanted</em> to <em>name</em> that function?  (Bear with me for a moment and pretend this is a compelling need, and that adding a named getter or setter programmatically is absolutely unacceptable.)</p>
<p>Solely by accident of implementation, in the past SpiderMonkey has parsed the following syntax to assign names to getter and setter functions:</p>
<pre class="code" data-language="javascript">
var o = { get property getter() { return "get"; } };
// Prints "Name: getter" in past versions of SpiderMonkey (or would if
// Object.getOwnPropertyDescriptor existed; __lookupGetter__ is a
// simple workaround); previous line is syntax error elsewhere
print("Name: " + Object.getOwnPropertyDescriptor(o, "property").get.name);
</pre>
<p>SpiderMonkey internally implemented the parsing of literal getters and setters by parsing them as though the start of a function expression had just been parsed:</p>
<pre class="code" data-language="javascript">
// Faked-up parser state when parsing normal getters/setters
var o = { get property () { } };
                       &uarr;
function () { }
         &uarr;
</pre>
<p>Function expressions may be named or unnamed, but this wasn&#8217;t originally considered, so in the above example <code>getter</code> is treated as the name of the function created to correspond to the getter:</p>
<pre class="code" data-language="javascript">
// Faked-up parser state when parsing named-getter-function syntax
var o = { get property getter() { } };
                       &uarr;
function getter() { }
         &uarr;
</pre>
<p>No other <abbr title="JavaScript">JS</abbr> engine accepts this unintentional accessor-method name token.</p>
<h3>Getters and setters in object literals</h3>
<p>Possibly the best-known additional syntax is for specifying getters and setters in object literals.  This syntax was the original Netscape invention for getters and setters; in practice it was superseded by the newer, more function-looking syntax.  SpiderMonkey is again the only engine to implement it.</p>
<pre class="code" data-language="javascript">
function g() { print("gotten!"); return "get"; }
var o1 =
  {
    property getter: g,
    property setter: function(v) { print("sotten!  " + v); }
  };
var v1 = o1.property; // prints "gotten!", v1 === "get"
o1.property = "new"; // prints "sotten!"
</pre>
<p>This accessor property syntax has one large advantage over the more-common syntax previously demonstrated (and even over the unintentional named-accessor mistake shown in the previous section).  Where you see <code>property</code> in the object literal above, you could instead see a numeric literal, or a string literal &mdash; just as you might see either in any object literal without getters or setters, <abbr title="exempli gratia, for example" lang="la">e.g.</abbr> <code>{ 1: "value", "o": "hai" }</code>.  Historically, in <code>get property() { ... }</code>, <code>property</code> was required to be an identifier, thus excluding numbers and non-identifier accessor properties from representation.  The syntax here had the further advantage of allowing serialization to &#8220;source&#8221; (more accurately, a reasonable but not always equivalent facsimile) of objects containing non-identifier-named accessor properties, through another Netscape extension in JavaScript.</p>
<p>This syntax also has a few disadvantages.  Since the <code>getter</code> and <code>setter</code> contextual keywords follow the property name, the eye must scan past the property name to determine whether a portion of a literal represents a data property or an accessor property.  This special-case check also complicates parsing, because now the parser has to check for something beyond just a colon at such locations.  (To be sure, this problem exists with <code>get foo() { }</code>, but it&#8217;s restricted to the single leading token <code>get</code>, not to all leading tokens.)  Since the value assigned to the getter is parsed as an arbitrary expression, there&#8217;s no guarantee the value must be a function &mdash; that must be checked at runtime.</p>
<h3>Assigning getters and setters to properties</h3>
<p>This accessor syntax provides the same functionality as <code>Object.defineProperty(obj, propname, { get: fun, enumerable: true, configurable: true })</code> (<span lang="la">mutatis mutandis</span> for setters), except as part of the language syntax rather than as part of its standard library.  Again, no other engine has implemented this syntax.</p>
<pre class="code" data-language="javascript">
var o = {};
o.property getter = function() { print("gotten!"); return "get" };
o.property setter = function() { print("sotten!"); };
var v = o.property; // prints "gotten!", v === "get"
o.property = "new"; // prints "sotten!"
</pre>
<p>This syntax is also obscure: outside SpiderMonkey source and test files, only a single file in the Mozilla source code uses it.  Strangely, a trawl through <a href="https://addons.mozilla.org/"><abbr title="addons.mozilla.org">AMO</abbr></a> shows half a dozen extensions have managed to discover this syntax, despite its near-complete disuse in Mozilla itself.</p>
<h3>Assigning getters and setters to names rather than properties</h3>
<p>Syntactically, this is just a different flavor of the previous example:</p>
<pre class="code" data-language="javascript">
varname getter = function() { return "get"; };
var q = varname; // "get"
</pre>
<p>Semantically, however, it&#8217;s a rather different beast.  The problem is that not all names are alike in SpiderMonkey.  While ECMAScript specifies all name accesses in terms of objects (pure-JS objects in ES3, tighter spec-internal artifacts in ES5), most if not all JS engines out there optimize name access based on the type of the name.  Local and enclosing variable access may be some number of pointer jumps, comparisons, and an offset, rather than some sort of hash table lookup in a more general case.  Global variable access can in many circumstances skip lookups in enclosing scopes, going to the global object directly.  (Last and certainly least, variable access inside <code>with</code> almost necessarily must be essentially unoptimized and dog-slow.  Friends don&#8217;t let friends use <code>with</code>!)  These sorts of optimizations rely on names always being plain old values, not accessors (except in the global case, where the type of optimizations implemented are qualitatively quite different).  Slowing down local or enclosing variable accesses just to support this very rare case would be insane.</p>
<p>SpiderMonkey actually hasn&#8217;t supported this syntax for awhile.  I only mention it because SpiderMonkey includes code specifically to exclude it.  If this syntax is seen and <code>varname</code> can be resolved to a <code>var</code>, it&#8217;s a compile-time syntax error.  Otherwise, if <code>varname</code> resolves to a <code>var</code> at runtime (possible in the presence of <code>with</code> or <code>eval</code>), it&#8217;s a runtime <code>TypeError</code>.  Last, if it doesn&#8217;t, it &#8220;works&#8221; &mdash; and you are most likely <a href="http://squarefree.com/">Jesse</a>, combining syntax and features in obscure and evil ways solely to make SpiderMonkey developers&#8217; lives hard.  <img src='http://whereswalden.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   In sufficiently old versions of Firefox where these restrictions weren&#8217;t in place, it&#8217;s entirely conceivable this syntax may have resulted in security vulnerabilities (one large factor in its removal from SpiderMonkey).</p>
<h3>Prefixed function expressions</h3>
<p>Perhaps the most bizarre getter/setter syntax is a modification of the syntax for function expressions and statements.  As with all the others, this syntax has only been implemented by SpiderMonkey.</p>
<pre class="code" data-language="javascript">
getter function foo() { return "foo getter"; };
var v = foo; // "foo getter"
var q = setter function bar(v) { };
</pre>
<p>When the prefixed function is a statement in the global scope, the syntax is equivalent to <code>Object.defineProperty(globalObject, "foo", { get: function foo() { /* ... */ }, enumerable: true, configurable: true })</code> (<span lang="la">mutatis mutandis</span> for <code>setter</code>).  If it&#8217;s a statement in a function scope or an expression that&#8217;s not a statement, the prefix serves no purpose that I can discern, except that it affects <code>Function.prototype.toString()</code>&#8217;s behavior by including the prefix in the returned string.</p>
<h2>None of these old getter/setter syntaxes provide value</h2>
<p>Now that ES5 has codified The One True Syntax and The One True Programmatic API, these older syntaxes bring little to the table.</p>
<ul>
<li>The mistaken ES5-like named accessor <code>get property funname() { }</code> syntax doesn&#8217;t satisfy a compelling need.</li>
<li><code>property getter:</code> in object literals provides one compelling feature: the ability to have non-identifier-named properties.  As ES5&#8217;s <code>get property() { }</code> syntax includes these further extensions beyond what engines have already implemented, this advantage no longer exists:
<pre class="code" data-language="javascript">
var o =
  {
    get name() { return "names valid"; },
    set break() { this.x = "keywords too"; },
    set 1() { this.y = "numeric literals also accepted"; },
    get "custom string"() { return "arbitrary string literals too!"; }
  };
</pre>
<p>(<code>property getter:</code> has a final advantage with respect to an ancient Netscape extension, but given that extension&#8217;s dubious future I will omit the details.  Suffice it to say the use case is highly esoteric, and reasonably graceful degradation is possible without <code>property getter:</code>.)</li>
<li><code>getter =</code> and <code>getter function</code> are fully subsumed by <code>Object.defineProperty</code>.</li>
<li><code>varname getter =</code> was already gone.</li>
</ul>
<p>In sum: these syntaxes make some things slightly easier, but they don&#8217;t provide anything you can&#8217;t do with ES5&#8217;s standardized accessor support.</p>
<h2>These syntaxes were the source of numerous bugs</h2>
<p>In addition to not being particularly useful, these syntaxes imposed notable costs on development.  Supporting so many different getter and setter syntaxes isn&#8217;t easy, and the relevant code paths are quite complicated, attempting to decide when which syntax is correct and when not (particularly as far as <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">object serialization</a> is concerned).  This has resulted in a multitude of accessor bugs usually found by <a href="https://www.squarefree.com/">Jesse</a>&#8217;s fuzz-testing and almost never by real-world scripts: bugs which, in C or C++, can often lead to memory-unsafety and, in the extreme, arbitrary code execution.  By my count SpiderMonkey has sixteen separate tests (corresponding to the same number of bugs) dedicated to edge cases and corner behaviors with these syntaxes: syntaxes no one uses, syntaxes superseded by newer and better ones, and syntaxes which no other JS engine currently supports, nor ever will support.</p>
<p>These syntaxes continue to impose costs on development.  Not all related bugs have been fixed, and changes to nearby code do have to take account of this syntax.  We have had at least one long-standing (but believed &#8220;mostly harmless&#8221;, in that a sanity-check fails but surrounding defensive code completely contains the problem) bug involving this syntax, which due to its relative harmlessness has gone unfixed for nearly three years (and, almost as bad, undiscovered for two of them).  Recent implementation work on ES5&#8217;s strict mode support required adjustments to the area of parsing object literals (for ES5&#8217;s strict mode rejection of duplicate property names), adjustments required to work around support for these syntaxes.</p>
<p>In short, <abbr title="There ain't no such thing as a free lunch">TANSTAAFL</abbr>.  We&#8217;ve paid a large cost to keep these syntaxes around, and we continue to pay to keep them around &mdash; sometimes directly, sometimes indirectly, but unavoidably if support is worthwhile.</p>
<h2>Support for all non-ES5 accessor syntaxes has been removed from SpiderMonkey</h2>
<p>But for the many reasons previously given, support for these obsolete syntaxes is not worthwhile, so we have removed them from SpiderMonkey.  <code>get property funname() { }</code> was an error from the start that no one will miss.  SpiderMonkey has recently implemented support for ES5 numeric- and string-literal accessor property names (support for keyword names already exists), so the remaining important use case for <code>property getter:</code> has been eliminated.  The <code>getter =</code> and <code>getter function</code> syntaxes never provided extra value, so they too have been removed without qualms.</p>
<p>To give an idea of the complexity eliminated by removing these syntaxes, the <a href="http://hg.mozilla.org/mozilla-central/rev/e47d2506e0ad">patch to remove them</a> added 116 lines of code but <em>removed</em> 313 lines of code.  Outside of code changes (that is, adjusting or removing tests which used these features), it added 133 lines but removed <em>1213</em> lines.  It&#8217;s always great <a href="http://blog.mozilla.com/tglek/2010/03/31/how-to-get-reviews-fast-delete-code/">deleting code</a> like this.  <img src='http://whereswalden.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2 id="getter-setter-updating-for-removals">Updating existing code to adapt to these removals</h2>
<p>One nice feature of removing syntax is that the failure mode when that syntax is encountered is blindingly obvious: the script will fail to parse.  Parse errors show up in the JavaScript console, so it&#8217;s easy to tell when this is the problem; SpiderMonkey&#8217;s excellent error messages should point directly at the offending location.</p>
<p>If by chance you do actually use any of these syntaxes, the necessary fixes are simple.  Suppose the existence of these helper functions:</p>
<pre class="code" data-language="javascript">
function accessorDescriptor(field, fun)
{
  var desc = { enumerable: true, configurable: true };
  desc[field] = fun;
  return desc;
}

function defineGetter(obj, prop, get)
{
  if (Object.defineProperty)
    return Object.defineProperty(obj, prop, accessorDescriptor("get", get));
  if (Object.prototype.__defineGetter__)
    return obj.__defineGetter__(prop, get);

  throw new Error("browser does not support getters");
}

function defineSetter(obj, prop, set)
{
  if (Object.defineProperty)
    return Object.defineProperty(obj, prop, accessorDescriptor("set", set));
  if (Object.prototype.__defineSetter__)
    return obj.__defineSetter__(prop, set);

  throw new Error("browser does not support setters");
}
</pre>
<p>Here&#8217;s how you can update each old syntax to work again:</p>
<dl>
<dt><code>get property funname() { }</code></dt>
<dd>
<pre class="code" data-language="javascript">
var o = defineGetter({}, "property", function funname() { });
</pre>
</dd>
<dt><code>property setter: fun</code></dt>
<dd>
<pre class="code" data-language="javascript">
var o = defineSetter({}, "property", fun);
</pre>
</dd>
<dt><code>obj.prop getter = fun</code></dt>
<dd>
<pre class="code" data-language="javascript">
defineGetter(obj, "prop", fun);
</pre>
</dd>
<dt><code>setter function prop() { }</code> (when at global scope; otherwise just remove the <code>setter</code> prefix)</dt>
<dd>
<pre class="code" data-language="javascript">
defineSetter(obj, "prop", fun);
</pre>
</dd>
</dl>
<p>You can experiment with a version of Firefox with support for these obsolete syntaxes removed by downloading a nightly from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a>.  (Don’t forget to <a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep the settings you use with your primary Firefox installation pristine.)</p>
<h2>A brief word on <code>__defineGetter__</code> and <code>__defineSetter__</code></h2>
<p>As you may have noticed, all examples here use <code>Object.defineProperty</code> in preference to either <code>__defineGetter__</code> or <code>__defineSetter__</code>, using the latter two only as fallback when the former is absent.  While many browsers support these methods, not all do.  <code>Object.defineProperty</code> is the future, and it is the standard; Microsoft has even <a href="http://blogs.msdn.com/ie/archive/2009/01/13/responding-to-change-updated-getter-setter-syntax-in-ie8-rc-1.aspx">gone on the record</a> to say that they will not implement <code>__defineGetter__</code> or <code>__defineSetter__</code> in <abbr title="Internet Explorer">IE</abbr> given the existence of the standardized method (props to them for that choice, by the way).  For greatest forward compatibility with all browsers, you should use <code>Object.defineProperty</code> if it exists, and only fall back to <code>__define{G,S}etter__</code> if it does not.</p>
<p>In a distant future we would like to remove support for <code>__defineGetter__</code> and <code>__defineSetter__</code>, after ES5 adoption has taken off, so as not to distract from the standardized support.  The less new web developers have to know about legacy extensions superseded by standardized alternatives, the better.  This action is at least several years in the future, likely longer; being able to make the change will require preparation and adjustment in anticipation of that time.  Given upcoming releases of browsers supporting ES5 functionality, there&#8217;s no better time than the present to start gradually, and gracefully, adopting standardized methods over legacy alternatives.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>More changes coming to SpiderMonkey: the magical __count__ property is being removed</title>
		<link>http://whereswalden.com/2010/04/06/more-changes-coming-to-spidermonkey-the-magical-__count__-property-of-objects-is-being-removed/</link>
		<comments>http://whereswalden.com/2010/04/06/more-changes-coming-to-spidermonkey-the-magical-__count__-property-of-objects-is-being-removed/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 00:17:05 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[__count__]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1549</guid>
		<description><![CDATA[Meet __count__
SpiderMonkey for some time has included a little-known, little-publicized, and little-used property named __count__ on all objects.  This property stored the count of the number of enumerable properties directly on the object.  For example,

assertEqual({ 1: 1 }.__count__, 1);
assertEqual([].__count__, 0);
assertEqual([1].__count__, 1);
assertEqual([1, /* hole */, 2, 3].__count__, 3);

It&#8217;s sort of a convenient way to [...]]]></description>
			<content:encoded><![CDATA[<h2>Meet <code>__count__</code></h2>
<p>SpiderMonkey for some time has included a little-known, little-publicized, and little-used property named <code>__count__</code> on all objects.  This property stored the count of the number of enumerable properties directly on the object.  For example,</p>
<pre class="code" data-language="javascript">
assertEqual({ 1: 1 }.__count__, 1);
assertEqual([].__count__, 0);
assertEqual([1].__count__, 1);
assertEqual([1, /* hole */, 2, 3].__count__, 3);
</pre>
<p>It&#8217;s sort of a convenient way to check property counts, avoiding a verbose <code>for (var p in o)</code> loop.  For example, you could use it to determine how many mappings you had in a hash.</p>
<p>Unfortunately, <code>__count__</code> has a number of problems.</p>
<h2>What&#8217;s wrong with <code>__count__</code></h2>
<p>First, and most notably for web developers, <code>__count__</code> is non-standard.  To the best of my knowledge, no other JavaScript engine supports it.  Developers must write scripts under the assumption it doesn&#8217;t exist, so it provides no brevity bonus.  (I recognize extensions and Mozilla-based applications are a special case.  For the further reasons below, it&#8217;s still worth removing even if some code could assume its existence.)</p>
<p>Second, the special <code>__count__</code> property contributes to the problem that you can&#8217;t actually use an object as a string-value hash.  The reason is that you have to be careful that your string-valued keys don&#8217;t conflict with special properties, because special properties can&#8217;t be overwritten with a custom property.  This breaks the nicest feature of using objects for hashes: you can&#8217;t just use normal <code>[]</code> property access to set and retrieve mappings.  If someone inserts a mapping of, say, <code>"__count__"</code> &rarr; <code>"special"</code>, the association with <code>"special"</code> won&#8217;t be preserved; <code>obj.__count__ = 5</code> is actually a no-op.  (<abbr title="Nota bene, note well" lang="la">NB</abbr>: Even ignoring <code>__count__</code> other special properties prevent this from working, but we&#8217;re slowly working toward getting rid of them.  [Some much more slowly than others, I hasten to note!  You don't need to worry about <code>__proto__</code> being removed any time in the near future, although you should use <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Object/GetPrototypeOf"><code>Object.getPrototypeOf(obj)</code></a> with a compatibility shim to determine <code>obj</code>'s prototype in new code.])  A further wrinkle is that <code>__count__</code> is implemented in such a way that an object literal with <code>"__count__"</code> as a named property functions <em>differently</em> from an object to which the property is later added by assignment:</p>
<pre class="code" data-language="javascript">
var o = { __count__: 17 };
assertEqual(o.__count__, 17);

// ...but...
var o2 = {};
o2.__count__ = 17;
assertEqual(o2.__count__, 17); // fails: __count__ is 0
</pre>
<p>Third, in supporting <code>__count__</code> we&#8217;ve incurred special-case deoptimization code in SpiderMonkey&#8217;s script-to-bytecode compiler.  This extra complication, for a feature not often used, does nothing for code readability, complexity, or quality.</p>
<p>Fourth, <code>__count__</code> doesn&#8217;t work the way you <em>might</em> think it works: it&#8217;s not uniformly fast for all objects.  Property access generally has a syntactic assumption of constant-time speediness.  This assumption isn&#8217;t valid in languages with getters and setters, but since it&#8217;s usually the case, it&#8217;s not a horribly inaccurate one.  Thus, one might expect that evaluating <code>obj.__count__</code> is an uncomplicated <code>O(1)</code> operation which doesn&#8217;t allocate memory and just looks up the size of an idealized hash table.  It might be possible to make that true, but in fact it never has been true: generally, computing <code>__count__</code> is <code>O(n)</code> in the number of properties on the object.  Further, because <code>__count__</code> reuses the same enumeration mechanism as <code>for</code> loops, it usually requires a memory allocation, which can be slow.  <code>__count__</code> has no asymptotic advantage over manual enumeration of the object&#8217;s properties.</p>
<p>In sum, <code>__count__</code> has problems that mean it doesn&#8217;t give you much more than a <code>for (var p in o)</code> loop would.  If that loop were placed in a function, it would be almost identical in code size to use of the property &mdash; and it would have the advantage of being completely cross-browser.</p>
<h2><code>__count__</code> is being removed</h2>
<p>We have removed support for <code>__count__</code> from SpiderMonkey.  As a consequence <code>__count__</code> will also be removed from the next version of Firefox based on trunk Mozilla code.  (And, of course, future versions of other Mozilla-based products like SeaMonkey will pick the change up when they produce releases based on trunk Mozilla code.)  For the above reasons <code>__count__</code> doesn&#8217;t make much sense to keep around, and it imposes real development costs.  You should have no difficulty updating your code to implement alternative functionality to <code>__count__</code>.  Here&#8217;s one example of how you might do this:</p>
<pre class="code" data-language="javascript">
function count(o)
{
  var n = 0;
  for (var p in o)
    n += Object.prototype.hasOwnProperty.call(o, p);
  return n;
}

assertEqual(count({ 1: 1 }), 1);
assertEqual(count([]), 0);
assertEqual(count([1]), 1);
assertEqual(count([1, /* hole */, 2, 3]), 3);
</pre>
<p>If you use <code>__count__</code> and need to test changes to remove that use, you can experiment with a version of Firefox with support for <code>__count__</code> removed by downloading a nightly from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a>.  (Don&#8217;t forget to <a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep the settings you use with your primary Firefox installation pristine.)</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/04/06/more-changes-coming-to-spidermonkey-the-magical-__count__-property-of-objects-is-being-removed/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ECMA-262 quote of the day</title>
		<link>http://whereswalden.com/2010/02/28/ecma-262-quote-of-the-day/</link>
		<comments>http://whereswalden.com/2010/02/28/ecma-262-quote-of-the-day/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 05:47:31 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1420</guid>
		<description><![CDATA[
If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the behaviour of sort is implementation-defined.
[...]
A function comparefn is a consistent comparison function for a set of values S if all of the requirements below are met for all values a, b, and c (possibly [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<p>If <var>comparefn</var> is not <code>undefined</code> and is not a consistent comparison function for the elements of this array (see below), the behaviour of <code>sort</code> is implementation-defined.</p>
<p>[...]</p>
<p>A function <var>comparefn</var> is a consistent comparison function for a set of values <var>S</var> if all of the requirements below are met for all values <var>a</var>, <var>b</var>, and <var>c</var> (possibly the same value) in the set <var>S</var>: The notation <var>a</var> &lt;<sub>CF</sub> <var>b</var> means <var>comparefn</var>(<var>a</var>,<var>b</var>) &lt; 0; <var>a</var> =<sub>CF</sub> <var>b</var> means <var>comparefn</var>(<var>a</var>,<var>b</var>) = 0 (of either sign); and a &gt;<sub>CF</sub> <var>b</var> means <var>comparefn</var>(<var>a</var>,<var>b</var>) &gt; 0.</p>
<ul>
<li>Calling <var>comparefn</var>(<var>a</var>,<var>b</var>) always returns the same value <var>v</var> when given a specific pair of values <var>a</var> and <var>b</var> as its two arguments. Furthermore, Type(<var>v</var>) is Number, and <var>v</var> is not <code>NaN</code>. Note that this implies that exactly one of <var>a</var> &lt;<sub>CF</sub> <var>b</var>, <var>a</var> =<sub>CF</sub> <var>b</var>, and <var>a</var> &gt;<sub>CF</sub> <var>b</var> will be true for a given pair of <var>a</var> and <var>b</var>.</li>
<li>Calling <var>comparefn</var>(<var>a</var>,<var>b</var>) does not modify the <code>this</code> object.</li>
<li><var>a</var> =<sub>CF</sub> <var>a</var> (reflexivity)</li>
<li>If <var>a</var> =<sub>CF</sub> <var>b</var>, then <var>b</var> =<sub>CF</sub> <var>a</var> (symmetry)
<li>If <var>a</var> =<sub>CF</sub> <var>b</var> and <var>b</var> =<sub>CF</sub> <var>c</var>, then <var>a</var> =<sub>CF</sub> c (transitivity of =<sub>CF</sub>)</li>
<li>If <var>a</var> &lt;<sub>CF</sub> <var>b</var> and <var>b</var> &lt;<sub>CF</sub> <var>c</var>, then <var>a</var> &lt;<sub>CF</sub> <var>c</var> (transitivity of &lt;<sub>CF</sub>)</li>
<li>If <var>a</var> &gt;<sub>CF</sub> <var>b</var> and <var>b</var> &gt;<sub>CF</sub> <var>c</var>, then <var>a</var> &gt;<sub>CF</sub> <var>c</var> (transitivity of &gt;<sub>CF</sub>)</li>
</ul>
</blockquote>
<div class="attribution"><a href="http://www.mozilla.org/js/language/E262-3.pdf">ECMA-262 3rd edition</a> or <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf">ECMA-262 5th edition</a>, 15.4.4.11 Array.prototype.sort (comparefn)</div>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/02/28/ecma-262-quote-of-the-day/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Working on the JS engine, Episode III: offered without comment</title>
		<link>http://whereswalden.com/2010/02/26/working-on-the-js-engine-episode-iii-offered-without-comment/</link>
		<comments>http://whereswalden.com/2010/02/26/working-on-the-js-engine-episode-iii-offered-without-comment/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 03:05:56 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[offered without comment]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[__LINE__]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1410</guid>
		<description><![CDATA[#define WORKAROUND(cx_, saver_)                                               \
    Workaround WORKAROUND_PASTE(w_, [...]]]></description>
			<content:encoded><![CDATA[<pre class="code" data-language="c++">#define WORKAROUND(cx_, saver_)                                               \
    Workaround WORKAROUND_PASTE(w_, __LINE__)((cx_), (saver_))
#define WORKAROUND_PASTE(a_, b_) WORKAROUND_PASTE2(a_, b_)
#define WORKAROUND_PASTE2(a_, b_) a_ ## b_ /* having fun yet? */
#define SAVER(cx_, saver_)                                                    \
    AutoValueRooter saver_(cx_);                                              \
    WORKAROUND((cx_), (saver_));</pre>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/02/26/working-on-the-js-engine-episode-iii-offered-without-comment/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Brief talk on ES5 and Mozilla support for it</title>
		<link>http://whereswalden.com/2010/02/08/brief-talk-on-es5-and-mozilla-support-for-it/</link>
		<comments>http://whereswalden.com/2010/02/08/brief-talk-on-es5-and-mozilla-support-for-it/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 20:32:42 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1352</guid>
		<description><![CDATA[I gave a three-minute not-actually-lightning-talk-but-let&#8217;s-call-it-that-anyway on ECMA-262 5th edition, what&#8217;s in it, and the state of Mozilla&#8217;s support for it at the Mozilla weekly meeting this week.  It&#8217;s probably old hat if you&#8217;ve been following the standard closely, but if you haven&#8217;t it gives a short and sweet overview of what&#8217;s new; there&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a three-minute <a href="http://whereswalden.com/files/mozilla/es5-lightning/">not-actually-lightning-talk-but-let&#8217;s-call-it-that-anyway</a> on ECMA-262 5th edition, what&#8217;s in it, and the state of Mozilla&#8217;s support for it at the Mozilla weekly meeting this week.  It&#8217;s probably old hat if you&#8217;ve been following the standard closely, but if you haven&#8217;t it gives a short and sweet overview of what&#8217;s new; there&#8217;s a three-minute video of the actual talk <a href="https://wiki.mozilla.org/WeeklyUpdates/2010-02-08">on the meeting page</a> (start at around 7:00 into the complete video).  If you&#8217;re strapped for time, view <a href="http://whereswalden.com/files/mozilla/es5-lightning/">the slides</a> and turn off stylesheets (View &gt; Page Style &gt; No Style in Firefox) to see notes on what roughly accompanied each slide.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/02/08/brief-talk-on-es5-and-mozilla-support-for-it/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>More ES5 backwards-incompatible changes: regular expressions now evaluate to a new object, not the same object, each time they&#8217;re encountered</title>
		<link>http://whereswalden.com/2010/01/15/more-es5-incompatible-changes-regular-expressions-now-evaluate-to-a-new-object-not-the-same-object-each-time-theyre-encountered/</link>
		<comments>http://whereswalden.com/2010/01/15/more-es5-incompatible-changes-regular-expressions-now-evaluate-to-a-new-object-not-the-same-object-each-time-theyre-encountered/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 16:37:36 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1265</guid>
		<description><![CDATA[(preemptive clarification: coming in Firefox 3.7 and not Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)
Disjunction: is /foo/ the same object, or a new object, each time it&#8217;s evaluated in ES3?
According to ECMA-262 3rd edition, what should this code print?

function getRegEx() { return /regex/; }
print("getRegEx() [...]]]></description>
			<content:encoded><![CDATA[<p>(preemptive clarification: coming in <strong>Firefox 3.7</strong> and <em>not</em> Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)</p>
<h2>Disjunction: is <code>/foo/</code> the same object, or a new object, each time it&#8217;s evaluated in <abbr title="ECMA-262 3rd edition">ES3</abbr>?</h2>
<p>According to ECMA-262 3rd edition, what should this code print?</p>
<pre class="code" data-language="javascript">
function getRegEx() { return /regex/; }
print("getRegEx() === getRegEx(): " + (getRegEx() === getRegEx()));
</pre>
<p>The answer depends upon this question: when a JavaScript regular expression literal is evaluated, does it create a new <code>RegExp</code> object each time, or does it evaluate to the exact same <code>RegExp</code> object each time it&#8217;s evaluated?  Let&#8217;s look at a few examples and make a guess.</p>
<h2>I sense a pattern</h2>
<pre class="code" data-language="javascript">
var tests =
  [
   function getNull() { return null; },
   function getNumber() { return 1; },
   function getString() { return "a"; },
   function getBoolean() { return false; },
   function getObject() { return {}; },
   function getArray() { return []; },
   function getFunction() { return function f(){}; },
  ];

for (var i = 0, sz = tests.length; i &lt; sz; i++)
{
  var t = tests[i];
  print(t.name + "() === " + t.name + "(): " + (t() === t()));
}
</pre>
<p>If you test that code, you&#8217;ll see that the first four results are true, and the rest are false, all per ECMA-262 3rd edition.  (Okay, technically, and bizarrely, ES3 permitted <em>either</em> result for the function case, but no browser ever implemented a result of true; <abbr title="ECMA-262 5th edition">ES5</abbr> acknowledges reality and mandates that the result be false.)  The first four functions return primitive values; the last three return objects.  There&#8217;s only a single instance of any primitive value &mdash; or, alternately, you might say, equality doesn&#8217;t distinguish between different instances of the same primitive.  Therefore it doesn&#8217;t really matter whether primitive literals evaluate to new instances or the same instance.  On the other hand, objects compare equal only if they&#8217;re the <em>same</em> object.  Since the object cases didn&#8217;t compare identically, they must be new objects each time.  This makes sense: if this were not the case, what would happen in the following example?</p>
<pre class="code" data-language="javascript">
function makePoint(x, y)
{
  var pt = {};
  pt.x = x;
  pt.y = y;
  return pt;
}

var pt1 = makePoint(1, 2);
var pt2 = makePoint(3, 4);
</pre>
<p>It would be complete nonsense if the object literal above evaluated to the same object every time it were encountered; the next two lines would blow away the previous point, and we would have <code>pt1.x ===3 &#038;&#038; pt1.y === 4</code>.</p>
<h2>Plausible assertion: regular expression literals evaluate to new objects when encountered?</h2>
<p>Returning to the original question, then, what <em>does</em> ES3 say this code should print?</p>
<pre class="code" data-language="javascript">
function getRegEx() { return /regex/; }
print("getRegEx() === getRegEx(): " + (getRegEx() === getRegEx()));
</pre>
<p>A regular expression is an object.  If you don&#8217;t want to get weird property-poisoning of the sort just suggested, regular expression literals must evaluate to different objects each time they&#8217;re encountered, right?</p>
<h2>Alternative: ES3 says <code>/foo/</code> is the same object every time</h2>
<p>Wrong.  According to ES3, there&#8217;s only a single object for each regular expression literal that&#8217;s returned each time the literal is encountered:</p>
<blockquote><p>A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is scanned.  The object is created before evaluation of the containing program or function begins.  Evaluation of the literal produces a reference to that object; it does not create a new object.</p>
</blockquote>
<div class="attribution">ECMA-262, 3rd ed. 7.8.5 Regular Expression Literals</div>
<p>This was originally a dubious optimization in the standard to avoid the &#8220;costly&#8221; creation of a regular expression object every time a literal would be encountered.  It&#8217;s perhaps a little surprising that the same object is returned each time, but does it make a difference in real programs not written to demonstrate the quirk?  Often it doesn&#8217;t matter.  As a simple example, <code>if (/^\d+$/.test(str)) { /* ... */ }</code> executes identically either way, assuming <code>RegExp.prototype.test</code> is unmodified.  The <code>RegExp</code> never escapes, and its use doesn&#8217;t depend on mutable state, so creating new objects each time doesn&#8217;t make a difference (other than negligibly, in speed).</p>
<p>Sometimes, however, the shared-object misoptimization does matter meaningfully: when a <code>RegExp</code> with mutable state is used in ways that depend on that state.  Most regular expressions don&#8217;t store any state, so if the same <code>RegExp</code> object is used twice it&#8217;s no big deal.  However, it can matter a lot for regular expressions specified with the <code>global</code> flag:</p>
<pre class="code" data-language="javascript">
var s = "abcddeeefffffgggggggghhhhhhhhhhhhh";
function next(s)
{
  var r = /(.)\1*/g;
  r.exec(s);
  return r.lastIndex;
}

var r = [];
for (var i =0; i &lt; 8; i++)
  r.push(next(s));
print(r.join(", "));
</pre>
<p>Each time a regular expression with the <code>global</code> flag is used, its <code>lastIndex</code> property is updated with the index of the location in the matched string where matching should resume when the regular expression is next used.  Thus, in this example we have mutable state, and if <code>next</code> is called multiple times we have uses which will depend on that mutable state.  Let&#8217;s see what happens in engines which implemented regular expression literals per ES3.  If you <a href="https://developer.mozilla.org/devnews/index.php/2010/01/10/firefox-3-6-release-candidate-is-now-available-for-download/">download the Firefox 3.6 release candidate</a> and test the above code in it (adjusting the implied <code>print</code> to <code>alert</code>), the printed result will be this:</p>
<pre class="code">
1, 2, 3, 5, 8, 13, 21, 34
</pre>
<h2>ES5: an escape to sanity</h2>
<p>Is ES3&#8217;s behavior what you&#8217;d expect?  No, it isn&#8217;t.  In fact, ES3&#8217;s behavior, which Mozilla and SpiderMonkey implement, is <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=98409">the second-most duplicated bug filed against Mozilla&#8217;s JavaScript engine</a>.  SpiderMonkey and (strangely enough) v8 are the only notable JavaScript engines out there that implement ES3&#8217;s behavior.  ES3&#8217;s behavior is rarely what web developers expect, and it doesn&#8217;t provide any real value, so ES5 is changing to the behavior you&#8217;d expect: evaluating a regular expression literal creates a new object every time.</p>
<p>Starting with Firefox 3.7, Firefox will implement what ES5 specifies.  Download a Firefox nightly from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a> and test it out as above (<a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep your current Firefox settings and install untouched).  Instead of the Fibonacci sequence you&#8217;ll get this:</p>
<pre class="code">
1, 1, 1, 1, 1, 1, 1, 1
</pre>
<h2>The bottom line</h2>
<p><strong>Starting with Firefox 3.7, evaluating a regular expression literal like <code>/foo/</code> will create a new <code>RegExp</code> object, just as evaluating <code>{}</code> or <code>[]</code> currently creates a new object or array.</strong>  The optimization ES3 specified has resulted in clear developer confusion and was misguided and inconsistent with respect to other object literal syntax in JavaScript.</p>
<p>Again, as with <a href="http://whereswalden.com/2010/01/12/more-es5-backwards-incompatible-changes-the-global-properties-undefined-nan-and-infinity-are-now-immutable/">my previous post</a>, we doubt this change will affect many scripts (in this case, except for the better).  The fact that few browsers implemented ES3&#8217;s semantics means that most sites have to cope with either choice of semantics, so the semantics in ES5, implemented by Mozilla for Firefox 3.7, are likely already handled.  Still, it&#8217;s possible that this change might break some sites (particularly those which include browser-specific code), so we&#8217;re giving a heads-up as early as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/01/15/more-es5-incompatible-changes-regular-expressions-now-evaluate-to-a-new-object-not-the-same-object-each-time-theyre-encountered/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>More ES5 backwards-incompatible changes: the global properties undefined, NaN, and Infinity are now immutable</title>
		<link>http://whereswalden.com/2010/01/12/more-es5-backwards-incompatible-changes-the-global-properties-undefined-nan-and-infinity-are-now-immutable/</link>
		<comments>http://whereswalden.com/2010/01/12/more-es5-backwards-incompatible-changes-the-global-properties-undefined-nan-and-infinity-are-now-immutable/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 19:41:44 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[Infinity]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[NaN]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[undefined]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1244</guid>
		<description><![CDATA[(preemptive clarification: coming in Firefox 3.7 and not Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)
JavaScript and the undefined, Infinity, and NaN &#8220;keywords&#8221;
Consider the following JavaScript program: what do you think it does?

print("undefined before: " + undefined);
undefined = 17;
print("undefined after:  " + undefined);

The above [...]]]></description>
			<content:encoded><![CDATA[<p>(preemptive clarification: coming in <strong>Firefox 3.7</strong> and <em>not</em> Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)</p>
<h2>JavaScript and the <code>undefined</code>, <code>Infinity</code>, and <code>NaN</code> &#8220;keywords&#8221;</h2>
<p>Consider the following JavaScript program: what do you think it does?</p>
<pre class="code" data-language="javascript">
print("undefined before: " + undefined);
undefined = 17;
print("undefined after:  " + undefined);
</pre>
<p>The above program will print this output:</p>
<pre class="code" data-language="javascript">
undefined before: undefined
undefined after:  17
</pre>
<h2>Surely you can&#8217;t be serious!</h2>
<p>A sane person might think that this program isn&#8217;t even a program.  Doesn&#8217;t <code>undefined</code> always refer to the primitive value <code>undefined</code>?  After all, this &#8220;program&#8221; isn&#8217;t one, nor would be the same one for <code>true</code> or <code>false</code>, <span lang="la">mutatis mutandis</span>:</p>
<pre class="code" data-language="javascript">
print("null before: " + null);
null = 17; // !!! NullLiteral is not a LeftHandSideExpression
print("null after:  " + null);
</pre>
<h2>I am serious&#8230;and don&#8217;t call me Shirley</h2>
<p>Curiously, the program that assigns to <code>undefined</code> is a valid JavaScript program, but programs that assign to <code>null</code>, <code>true</code>, and <code>false</code> are not.  Why not?  The latter are all <dfn>keywords</dfn> with intrinsic meaning within the language; <code>undefined</code>, on the other hand, is just a normal property of the global object.  According to ECMA-262 3rd edition, if you assign a different value to <code>undefined</code>, that different value becomes the new value of <code>undefined</code>.</p>
<p>This is a clear botch in <abbr title="ECMA-262 3rd edition">ES3</abbr>.  <code>undefined</code> should have been a keyword in JavaScript from the beginning; similarly, the global properties <code>Infinity</code> and <code>NaN</code> probably should have been keywords as well (or perhaps the properties should not have existed, given that <code>Math.Infinity</code> and <code>Math.NaN</code> exist and are immutable).  ECMA-262 5th edition doesn&#8217;t quite go so far as to change these three properties into keywords due to backwards compatibility concerns (making that change would be guaranteed to break any programs that even tried to assign to those names, regardless whether the program relied on that assignment for correctness).  Instead, it changes these properties to be read-only, in the same way that the various numeric properties on the <code>Math</code> object are read-only.  Assigning to these properties in <abbr title="ECMA-262 5th edition">ES5</abbr> won&#8217;t do anything (unless you opt into strict mode, in which case a <code>TypeError</code> exception will be thrown after we fix <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=537873">bug 537873</a>), but at least it won&#8217;t <em>definitely</em> and <em>completely</em> break existing programs that relied on this.</p>
<p>We&#8217;ve made this change in SpiderMonkey, and it is now in trunk builds of Firefox, slated for the eventual <strong>Firefox 3.7</strong> release.  Download a nightly build from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a> and test out the change for yourself (<a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep your current Firefox settings and install untouched).  This change should have no effect on the vast, vast majority of web developers who don&#8217;t try to change the values of these properties; as for the [civility and my religion require I redact this description] developers who <em>did</em> change the value of the global <code>undefined</code>, <code>NaN</code>, or <code>Infinity</code> properties, well&#8230;<a href="http://www.youtube.com/watch?v=pDRN3umyXTk">you had it coming</a>.</p>
<h2>The bottom line</h2>
<p><strong>The global properties <code>undefined</code>, <code>Infinity</code>, and <code>NaN</code> will be read-only and immutable in Firefox 3.7</strong>.  Assigning to these properties will do nothing (except in strict mode where a <code>TypeError</code> exception will be thrown once we fix <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=537873">a bug</a>) rather than changing their values.  This shouldn&#8217;t break the vast, vast, vast majority of scripts out there &mdash; but there&#8217;s no way to guarantee it will break no one, so we think it&#8217;s worth announcing this backwards-incompatible change as proactively as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/01/12/more-es5-backwards-incompatible-changes-the-global-properties-undefined-nan-and-infinity-are-now-immutable/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ECMA-262 ed. 5 backwards-incompatible change coming to SpiderMonkey and to Gecko-based browsers</title>
		<link>http://whereswalden.com/2009/12/21/ecma-262-ed-5-backwards-incompatible-change-coming-to-spidermonkey-and-to-gecko-based-browsers/</link>
		<comments>http://whereswalden.com/2009/12/21/ecma-262-ed-5-backwards-incompatible-change-coming-to-spidermonkey-and-to-gecko-based-browsers/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 19:41:57 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[getter]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[setter]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[strict mode]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=1138</guid>
		<description><![CDATA[(preemptive clarification: coming in Firefox 3.7 and not Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)
ES5 and compatibility
The fifth edition of ECMA-262, the next iteration of the JavaScript language, is broadly backwards-compatible with existing JavaScript code.  Generally, code that worked in past browsers that [...]]]></description>
			<content:encoded><![CDATA[<p>(preemptive clarification: coming in <strong>Firefox 3.7</strong> and <em>not</em> Firefox 3.6, which is to say, a good half year away from now rather than Real Soon Now)</p>
<h2><abbr title="ECMAScript-262, fifth edition">ES5</abbr> and compatibility</h2>
<p>The fifth edition of ECMA-262, the next iteration of the JavaScript language, is broadly backwards-compatible with existing JavaScript code.  Generally, code that worked in past browsers that implemented the specification will continue to work in new browsers as they implement the new edition of the specification.  However, there are a few exceptions.  The most obvious one is ES5&#8217;s strict mode, where specially-tagged scripts and functions will cause parsing and execution of their contents to occur under stricter requirements than in the past.  For example, this code would have executed &#8220;as intended&#8221; in ES3, but in ES5 the definition of a variable named &#8220;arguments&#8221; inside a function <em>in strict mode</em> is a syntax error (none of the code even executes):</p>
<pre class="code" data-language="javascript">
function strictModeError()
{
  "use strict";
  var arguments = 17; // stupid, but permissible, in ES3
  return arguments;
}
if (strictModeError() !== 17)
  throw new Error("up is down");
</pre>
<p>The above isn&#8217;t more than a theoretical problem as it is expected old code wouldn&#8217;t have accidentally opted into strict mode.  Not all of ES5&#8217;s incompatible changes, however, are so benign.</p>
<h2>ES5 compatibility with ES3 extensions</h2>
<p>One unusual area of compatibility concerns not ES3, but <em>extensions</em> to ES3.  One of the more profound changes in ES5 is the introduction of <dfn>getters and setters</dfn>, in which what appears syntactically to be a property when used actually will invoke function calls &#8220;under the hood&#8221;.  Most major JS engines support this extension to ES3:</p>
<pre class="code" data-language="javascript">
var o =
  {
    get field() { return this._field; },
    set field(f)
    {
      if (typeof f != "number")
        throw new Error("not a number");
      this._field = f;
    },
    _field: 0
  };
print(o.field); // 0
o.field = 5;
print(o.field); // 5
try { o.field = "0"; } catch (e) { /* throws: not a number */ }
print(o.field); // 5
o.field = 17;
print(o.field); // 17
</pre>
<p>This syntax is in ES5, partly because it addresses a need in a reasonable way but mostly because many developers will already be familiar with it.  (Getters and setters were also available programmatically; ES5&#8217;s solution is different but more flexible.)  However, not all aspects of getters and setters are present in ES5 in the same way they were in extensions to ES3 engines.</p>
<h2>Assigning to getter-only properties in ES5</h2>
<p>Consider the previous example, slightly tweaked:</p>
<pre class="code" data-language="javascript">
var o =
  {
    get field() { return this._field; },
    _field: 17
  };
print(o.field); // 17
o.field = 5;  // ???
print(o.field); // ???
</pre>
<p>In this case the <code>field</code> property is read-only: you could analogize it to <code>element.childNodes.length</code>, which has a value which it makes no sense to attempt to change.  What should happen, then, if you attempt to change it?  Current browsers throw a <code>TypeError</code> when you try this.  ES5, however, chooses to remain (arguably) more faithful to ES3 and instead makes setting a property that only has a getter do nothing &mdash; except in strict mode, where a <code>TypeError</code> exception will be thrown.</p>
<p>SpiderMonkey, the JavaScript engine embedded in Gecko browsers, has just made the switch from its previous always-throw behavior to ES5&#8217;s only-throw-if-in-strict-mode behavior when an attempt is made to set a property that only has a getter.  (This will also apply to DOM properties like the aforementioned <code>element.childNodes.length</code>.)  In the future, if you try to set a property that only has a getter, no exception will be thrown unless you&#8217;ve opted into strict mode.  This change is now in trunk builds of Firefox; download a nightly build from <a href="http://nightly.mozilla.org/">nightly.mozilla.org</a> and test out the change for yourself (<a href="http://support.mozilla.com/en-US/kb/Managing+profiles">use the profile manager</a> if you want to keep your current Firefox settings and install untouched).  If your site relies on an exception being thrown, this change could break it, and we&#8217;re hoping that an extended period of time to test the change will help developers iron out any reliance on this non-standard behavior.  <strong>This change will first appear in Firefox 3.7</strong>, which probably won&#8217;t be released until the second half of 2010 or so.  Firefox 3.6 preserves current behavior where an exception is always thrown, so you should have plenty of time to update your site in response to this change.</p>
<h2>The bottom line</h2>
<p>Firefox 3.6 and earlier throw an exception whenever you attempt to set a property represented only by a getter (this includes DOM properties defined as readonly).  <strong>Firefox 3.7 will only throw a <code>TypeError</code> when assigning to a property represented by only a getter if the assignment occurs in ES5 strict mode code.  This change will also apply to attempts to set readonly DOM properties like <code>element.childNodes.length</code>.</strong>  If you&#8217;re relying on an exception being thrown in either case, change the assignment location code so that it works when no <code>TypeError</code> exception is thrown.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2009/12/21/ecma-262-ed-5-backwards-incompatible-change-coming-to-spidermonkey-and-to-gecko-based-browsers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
