<?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; ecma-262</title>
	<atom:link href="http://whereswalden.com/tag/ecma-262/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>Thu, 03 May 2012 09:13:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>I feel the need&#8230;the need for JSON parsing correctness and speed!</title>
		<link>http://whereswalden.com/2011/06/06/i-feel-the-need-the-need-for-json-parsing-correctness-and-speed/</link>
		<comments>http://whereswalden.com/2011/06/06/i-feel-the-need-the-need-for-json-parsing-correctness-and-speed/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 23:17:34 +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[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json.parse]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=3149</guid>
		<description><![CDATA[JSON and SpiderMonkey JSON is a handy serialization format for passing data between servers and browsers and between independent, cooperating web pages. It&#8217;s increasingly the format of choice for website APIs. ECMAScript 5 (the standard underlying JavaScript) includes built-in support for producing and parsing JSON. SpiderMonkey has included such support since before ES5 added it. [...]]]></description>
			<content:encoded><![CDATA[<h2><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON"><abbr title="JavaScript Object Notation">JSON</abbr></a> and SpiderMonkey</h2>
<p>JSON is a handy <a href="http://www.ietf.org/rfc/rfc4627.txt">serialization format</a> for passing data between servers and browsers and between independent, cooperating web pages.  It&#8217;s increasingly <a href="http://blog.programmableweb.com/2011/05/25/1-in-5-apis-say-bye-xml/">the format of choice</a> for website <abbr title="application programming interfaces">APIs</abbr>.</p>
<p>ECMAScript 5 (the standard underlying JavaScript) includes built-in support for producing and parsing JSON.  SpiderMonkey has included such support since before <abbr title="ECMAScript 5">ES5</abbr> added it.</p>
<p>SpiderMonkey&#8217;s support, because it predated ES5, hasn&#8217;t always agreed with ES5.  Also, because JSON support was added before it became ubiquitous on the web, it wasn&#8217;t written with raw speed in mind.</p>
<h2>Improving <code>JSON.parse</code></h2>
<p>We&#8217;ve now improved JSON parsing in Firefox 5 to be fast and fully conformant with ES5.  For awhile we&#8217;ve made <a href="http://whereswalden.com/2010/09/08/spidermonkey-json-change-trailing-commas-no-longer-accepted/">improvements</a> to JSON by piecemeal change.  This worked for small bug fixes, and it probably would have worked to fix the remaining conformance bugs.  But performance is different: to improve performance we needed to parse in a fundamentally different way.  It was time for a <a href="http://www.joelonsoftware.com/articles/fog0000000069.html">rewrite</a>.</p>
<h2>What parsing bugs got fixed?</h2>
<p>The bugs the new parser fixes are quite small and generally shouldn&#8217;t affect sites, in part because other browsers overwhelmingly don&#8217;t have these bugs.  We&#8217;ve had no compatibility reports for these fixes in the month and a half they&#8217;ve been in the tree:</p>
<ul>
<li>The number syntax is properly stricter:
<ul>
<li>Octal numbers are now syntax errors.</li>
<li>Numbers containing a decimal point must now include a fractional component (<abbr title="id est, that is" lang="la">i.e.</abbr> <code>1.</code> is no longer accepted).</li>
</ul>
</li>
<li><code>JSON.parse("this")</code> now throws a <code>SyntaxError</code> rather than evaluate to <code>true</code>, due to a mistake reusing our keyword parser.  (Hysterically, because we used our JSON parser to optimize <code>eval</code> in certain cases, this change means that <code>eval("(this)")</code> will no longer evaluate to <code>true</code>.)</li>
<li>Strings can&#8217;t contain tab characters: <code>JSON.parse('"\t"')</code> now properly throws a <code>SyntaxError</code>.</li>
</ul>
<p>This list of changes <em>should</em> be complete, but it&#8217;s possible I&#8217;ve missed others.  Parsing might be a solved problem in the compiler literature, but it&#8217;s still pretty complicated.  I could have missed lurking bugs in the old parser, and it&#8217;s possible (although I think less likely) that I&#8217;ve introduced bugs in the new parser.</p>
<h2>What about speed?</h2>
<p>The new parser is much faster than the old one.  Exactly how fast depends on the data you&#8217;re parsing.  For example, on Opera&#8217;s <a href="http://testsuites.opera.com/JSON/performance/001.html">simple parse</a> test, I get around 156000 times/second in Firefox 4, but in Firefox 5 with the new JSON parser I get around 339000 times/second (bigger is better).  On a second testcase, <a href="http://krakenbenchmark.mozilla.org/">Kraken&#8217;s</a> <code>JSON.parse</code> test (json-parse-financial, to be precise), I get a 4.0 time of around 140ms and a 5.0 time of around 100ms (smaller is better).  (In both cases I&#8217;m comparing builds containing far more JavaScript changes than just the new parser, to be sure.  But I&#8217;m pretty sure the bulk of the performance improvements in these two cases are due to the new parser.)  The new JSON parser puts us solidly in the center of the browser pack.</p>
<p>It&#8217;ll only get better in the future as we wring even more speed out of SpiderMonkey.  After all, on the same system used to generate the above numbers, IE gets around 510000 times/second.  I expect further speedup will happen during more generalized performance improvements: improving the speed of defining new properties, improving the speed with which objects are allocated, improving the speed of creating a property name from a string, and so on.  As we perform such streamlining, we&#8217;ll parse JSON even faster.</p>
<h2>Side benefit: better error messages</h2>
<p>The parser rewrite also gives <code>JSON.parse</code> better error messages.  With the old parser it would have been difficult to provide useful feedback, but in the new parser it&#8217;s easy to briefly describe the reason for syntax errors.</p>
<pre>
js&gt; JSON.parse('{ foo: 17 }'); // unquoted property name
(old) typein:1: SyntaxError: JSON.parse
(new) typein:1: SyntaxError: JSON.parse: expected property name or '}'
</pre>
<p>We can definitely <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=507998">do more here</a>, perhaps by including context for the error from the provided string, but this is nevertheless a marked improvement over the old parser&#8217;s error messages.</p>
<h2>Bottom line</h2>
<p><code>JSON.parse</code> in Firefox 5 is faster, follows the spec, and tells you what went wrong if you give it bad data.  &#8217;nuff said.</p>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2011/06/06/i-feel-the-need-the-need-for-json-parsing-correctness-and-speed/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>SpiderMonkey JSON change: trailing commas no longer accepted</title>
		<link>http://whereswalden.com/2010/09/08/spidermonkey-json-change-trailing-commas-no-longer-accepted/</link>
		<comments>http://whereswalden.com/2010/09/08/spidermonkey-json-change-trailing-commas-no-longer-accepted/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 05:57:01 +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[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[trailing comma]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=2200</guid>
		<description><![CDATA[Historically, SpiderMonkey&#8217;s JSON implementation has accepted input containing trailing commas: JSON.parse('[1, 2, 3, ]'); JSON.parse('{ "1": 2, }"); We did so because the JSON RFC permitted implementations to accept extensions, and trailing commas are nice for humans to be able to use. The down side is that accepting extra syntax like this makes interoperability harder: [...]]]></description>
			<content:encoded><![CDATA[<p>Historically, SpiderMonkey&#8217;s <abbr title="JavaScript object notation">JSON</abbr> implementation has accepted input containing trailing commas:</p>
<pre class="code" data-language="javascript">
JSON.parse('[1, 2, 3, ]');
JSON.parse('{ "1": 2, }");
</pre>
<p>We did so because the <a href="http://www.ietf.org/rfc/rfc4627.txt">JSON <abbr title="request for comments">RFC</abbr></a> permitted implementations to accept extensions, and trailing commas are nice for humans to be able to use.  The down side is that accepting extra syntax like this makes interoperability harder: implementations which don&#8217;t implement the same extension, for reasons every bit as valid as those of implementations allowing the extension, are disadvantaged.  <abbr title="ECMAScript, 5th edition">ES5</abbr> weighed these concerns and chose to precisely specify permissible JSON syntax, putting everyone on the same page: trailing commas are not permitted.  Therefore, the examples above should throw a <code>SyntaxError</code> per ES5.</p>
<p>SpiderMonkey has now been changed to conform to ES5 on this point: trailing commas are syntax errors.  If you still need to accept trailing commas, you should use a custom implementation that accepts them &mdash; but best would be for you to adjust the processes that produce JSON strings including trailing commas to not include them.  (If you are an extension or are in privileged code, for the moment you can use <code>nsIJSON.legacyDecode</code> to continue to accept trailing commas.  However, note that we have added it only to accommodate legacy code in the process of being updated to no longer generate faulty input, and it will be removed sometime in the future.)</p>
<p>You can experiment with a version of Firefox with this change by downloading a <a href="http://nightly.mozilla.org/js-preview.html">TraceMonkey branch nightly</a>; this change should also make its way into <a href="http://nightly.mozilla.org/">mozilla-central nightlies</a> shortly, if you&#8217;d rather stick to trunk builds.  (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>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/09/08/spidermonkey-json-change-trailing-commas-no-longer-accepted/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>New ES5 strict mode support: now with poison pills!</title>
		<link>http://whereswalden.com/2010/09/08/new-es5-strict-mode-support-now-with-poison-pills/</link>
		<comments>http://whereswalden.com/2010/09/08/new-es5-strict-mode-support-now-with-poison-pills/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 05:56:33 +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[firefox]]></category>
		<category><![CDATA[introspection]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[programming language]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[strict]]></category>
		<category><![CDATA[strict mode]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=2150</guid>
		<description><![CDATA[tl;dr Don&#8217;t try to use the arguments or caller properties of functions created in strict mode code. Don&#8217;t try to use the callee or caller properties of arguments objects corresponding to invocations of functions in strict mode code. Don&#8217;t try to use the caller property of a function if it might be called from strict [...]]]></description>
			<content:encoded><![CDATA[<h2><abbr title="too long; didn't read">tl;dr</abbr></h2>
<p>Don&#8217;t try to use the <code>arguments</code> or <code>caller</code> properties of functions created in strict mode code.  Don&#8217;t try to use the <code>callee</code> or <code>caller</code> properties of <code>arguments</code> objects corresponding to invocations of functions in strict mode code.  Don&#8217;t try to use the <code>caller</code> property of a function if it might be called from strict mode code.  You are in for an unpleasant surprise (a thrown <code>TypeError</code>) if you do.</p>
<pre data-language="javascript" class="code">
function strict() { "use strict"; return arguments; }
function outer() { "use strict"; return inner(); }
function inner() { return inner.caller; }

strict.caller;    // !!! BAD IDEA
strict.arguments; // !!! BAD IDEA
strict().caller;  // !!! BAD IDEA
strict().callee;  // !!! BAD IDEA
outer();          // !!! BAD IDEA
</pre>
<p>Really, it&#8217;s best not to access the caller of a function, the current function (except by naming it), or the arguments for a given function (except via <code>arguments</code> or by use of the named parameter) at all.</p>
<h2><abbr title="ECMAScript 5th edition">ES5</abbr> strict mode: self-limitation, not wish fulfillment</h2>
<p>ES5 introduces the curious concept of strict mode.  Strict mode, whose name and concept derive from the similar feature in Perl, is a new feature in ES5 whose purpose is to deliberately <em>reduce</em> the things you can do in JavaScript.  Instead of a feature, it&#8217;s really more the <em>absence</em> of several features, within the scope of the strict-annotated code: <code>with</code>, particularly intrusive forms of <code>eval</code>, silent failure of writes to non-writable properties, silent failure of deletions of non-configurable properties, implicit creation of global-object properties, and so on.  The goal of these removals is to simplify both the reasoning necessary to understand such code and the implementation of it in JavaScript engines: to sand away some rough edges in the language.</p>
<h2>Magical stack-introspective properties of functions and arguments</h2>
<p>Consider this code, and note the expected behavior (expected per the web, but not as part of <abbr title="ECMAScript 3rd edition">ES3</abbr>):</p>
<pre data-language="javascript" class="code">
function getSelf() { return arguments.callee; }
assertEq(getSelf(), getSelf); // arguments.callee is the enclosing function

function outer()
{
  function inner() { return arguments.callee.caller; } // inner.caller === outer
  return inner();
}
assertEq(outer(), outer); // fun.caller === nearest function in stack that called fun, or null

function args2()
{
  return args2.arguments;
}
assertEq(args2(17)[0], 17); // fun.arguments === arguments object for nearest call to fun in stack, or null
</pre>
<p>Real-world JavaScript implementations take many shortcuts for top performance.  These shortcuts are not (supposed to be) observable, except by timing the relevant functionality.  Two common optimizations are <dfn>function inlining</dfn> and <dfn>avoiding creating an <code>arguments</code> object</dfn>.  The above &#8220;features&#8221; play havoc with both of these optimizations (as well as others, one of which will be the subject of a forthcoming post).</p>
<p>Inlining a function should conceptually be equivalent to splatting the function&#8217;s contents in that location in the calling function and doing some <a href="http://en.wikipedia.org/wiki/AlphaRenaming">α-renaming</a> to ensure no names in the splatted body conflict with the surrounding code.  The ability to access the calling function defeats this: there&#8217;s no function being invoked any more, so what does it even mean to ask for the function&#8217;s caller?  (Don&#8217;t simply say you&#8217;d hard-code the surrounding function: how do you know which property lookups in the inlined code will occur upon precisely the function being called, looking for precisely the <code>caller</code> property?)  It is also possible to access a function&#8217;s arguments through <code>fun.arguments</code>.  While the &#8220;proper&#8221; behavior here is more obvious, implementing it would be a large hassle: either the arguments would have to be created when the function was inlined (in the general case where you can&#8217;t be sure the function will never be used this way), or you&#8217;d have to inline code in such a way as to be able to &#8220;work backward&#8221; to the argument values.</p>
<p>Speaking of <code>arguments</code>, in offering access to the corresponding function via <code>arguments.callee</code> it has the same problems as <code>fun.caller</code>.  It also presents one further problem: in (some, mostly old) engines, <code>arguments.caller</code> provides access to the variables declared <em>within</em> that function when it was most recently called.  (If you&#8217;re thinking <a href="http://stuff.mit.edu/iap/2008/facebook/">security/integrity/optimization hazard</a>, well, you now know why engines no longer support it.)</p>
<p>In sum these features are bad for optimization.  Further, since they&#8217;re a form of dynamic scoping, they&#8217;re basically bad style in many other languages already.</p>
<h2>Per ES5, SpiderMonkey no longer supports this stack-inspecting magic when it interacts with strict mode</h2>
<p>As of the most recent Firefox nightly, SpiderMonkey now rejects code like that given above when it occurs in strict mode (more or less).  (The properties in question are now generally implemented through a so-called &#8220;poison pill&#8221; mechanism, an immutable accessor property which throws a <code>TypeError</code> when retrieved or set.)  The specific scenarios which we reject are as follows.</p>
<p>First, attempts to access the caller or arguments (except by directly naming the object) of a strict mode function throw a <code>TypeError</code>, because these properties are poison pills:</p>
<pre data-language="javascript" class="code">
function strict()
{
  "use strict";
  strict.caller;    // !!! TypeError
  strict.arguments; // !!! TypeError
  return arguments; // direct name: perfectly cromulent
}
strict();
</pre>
<p>Second, attempts to access the enclosing function or caller variables via the <code>arguments</code> of a strict mode function throw a <code>TypeError</code>.  These properties too are poison pills:</p>
<pre data-language="javascript" class="code">
function strict()
{
  "use strict";
  arguments.callee; // !!! TypeError
  arguments.caller; // !!! TypeError
}
strict();
</pre>
<p>Third (and most trickily, because non-strict code is affected), attempts to access a function&#8217;s caller when that caller is in strict mode will throw a <code>TypeError</code>.  This isn&#8217;t a poison pill, because if the <code>"use strict"</code> directive weren&#8217;t there it would still &#8220;work&#8221;:</p>
<pre data-language="javascript" class="code">
function outer()
{
  "use strict";
  return inner();
}
function inner()
{
  return inner.caller; // !!! TypeError
}
outer();
</pre>
<p>But if there&#8217;s no strict mode in sight, nothing will throw exceptions, and what worked before will still work:</p>
<pre data-language="javascript" class="code">
function fun()
{
  assertEq(fun.caller, null); // global scope
  assertEq(fun.arguments, arguments);
  assertEq(arguments.callee, fun);
  arguments.caller; // won't throw, won't do anything special
  return arguments;
}
fun();
</pre>
<h2>Conclusion</h2>
<p>With these changes, which are required by ES5, stack inspection is slowly going the way of the dodo.  Don&#8217;t use it or rely on it!  Even if you never use strict mode, beware the third change, for it still might affect you if you provide methods for other code to call.  (But don&#8217;t expect to be able to avoid strict mode forever: I expect all JavaScript libraries will adopt strict mode in short order, given its benefits.)</p>
<p>(For those curious about <code>new Error().stack</code>, we&#8217;re still considering what to do about it.  Regrettably, we may need to kill it for information-privacy reasons too, or at least censor it to something less useful.  Nothing&#8217;s certain yet; stay tuned for further announcements should we make changes.)</p>
<p>You can experiment with a version of Firefox with these changes by downloading a <a href="http://nightly.mozilla.org/js-preview.html">TraceMonkey branch nightly</a>; these changes should also make their way into <a href="http://nightly.mozilla.org/">mozilla-central nightlies</a> shortly, if you&#8217;d rather stick to trunk builds.  (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>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/09/08/new-es5-strict-mode-support-now-with-poison-pills/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Now in SpiderMonkey and Firefox: ES5&#8216;s Function.prototype.bind</title>
		<link>http://whereswalden.com/2010/09/07/now-in-spidermonkey-and-firefox-es5s-function-prototype-bind/</link>
		<comments>http://whereswalden.com/2010/09/07/now-in-spidermonkey-and-firefox-es5s-function-prototype-bind/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 17:28:10 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[ecma-262]]></category>
		<category><![CDATA[ecmascript]]></category>
		<category><![CDATA[es5]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=2212</guid>
		<description><![CDATA[This is just a brief note to point out that, as of the August 29th Firefox nightly (and I think as of the latest beta, but don&#8217;t quote me), SpiderMonkey (and Firefox) now implements ES5&#8216;s new Function.prototype.bind method &#8212; native support for creating functions bound to a pre-specified this value: var property = 42; var [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a brief note to point out that, as of the August 29th Firefox nightly (and I think as of the latest beta, but don&#8217;t quote me), SpiderMonkey (and Firefox) now implements <abbr title="ECMAScript 5th edition">ES5</abbr>&#8216;s new <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind"><code>Function.prototype.bind</code></a> method &mdash; native support for creating functions bound to a pre-specified <code>this</code> value:</p>
<pre class="code" data-language="javascript">
var property = 42;
var obj =
  {
    property: 17,
    method: function() { return this.property; }
  };

var bound = obj.method.bind(obj);
assertEq(bound(), 17);
</pre>
<p>&#8230;or with pre-specified leading arguments:</p>
<pre class="code" data-language="javascript">
function multiply()
{
  var product = 1;
  for (var i = 0, sz = arguments.length; i &lt; sz; i++)
    product *= arguments[i];
  return product;
}

var productTimesFive = multiply.bind(null /* this */, 5);
assertEq(productTimesFive(4, 3, 2, 1), 120);
</pre>
<p>&#8230;and, in a feature present only in the ES5 <code>bind</code> implementation (and not in any of the numerous precursors), they even work with <code>new</code>:</p>
<pre class="code" data-language="javascript">
function Vector()
{
  var args = arguments;
  this.length = arguments.length;
  this.get = function(i) { return args[i]; };
  this.set = function(i, v) { args[i] = v; };
}

var PartialVector = Vector.bind(null /* this, ignored with new */, 3, 7);

var threeSevenTen = new PartialVector(10); // new Vector(3, 7, 10)
</pre>
<p>For more information, see the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind">article on <code>Function.prototype.bind</code> on MDC</a>.  For the utmost information, see the <a href="http://ecma262-5.com/ELS5_HTML.htm#Section_15.3.4.5">ES5 specification for the method</a>.</p>
<p>As always, you can experiment with a version of Firefox with <code>Function.prototype.bind</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>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/09/07/now-in-spidermonkey-and-firefox-es5s-function-prototype-bind/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Incompatible ES5 change: literal getter and setter functions must now have exactly zero or one arguments</title>
		<link>http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/</link>
		<comments>http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 16:47:18 +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[firefox]]></category>
		<category><![CDATA[getter]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[programming language]]></category>
		<category><![CDATA[setter]]></category>
		<category><![CDATA[spidermonkey]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://whereswalden.com/?p=2141</guid>
		<description><![CDATA[ECMAScript accessor syntax in SpiderMonkey For quite some time SpiderMonkey and Mozilla-based browsers have supported user-defined getter and setter functions (collectively, accessors), both programmatically and syntactically. The syntaxes for accessors were once legion, but SpiderMonkey has pared them back almost to the syntax recently codified in ES5 (and added new syntax where required by ES5). [...]]]></description>
			<content:encoded><![CDATA[<h2>ECMAScript accessor syntax in SpiderMonkey</h2>
<p>For quite some time SpiderMonkey and Mozilla-based browsers have supported user-defined getter and setter functions (collectively, <dfn>accessors</dfn>), both programmatically and syntactically.  The syntaxes for accessors were once <a href="http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/">legion</a>, but SpiderMonkey has pared them back almost to the syntax recently codified in <abbr title="ECMAScript 5th edition">ES5</abbr> (and added new syntax where required by ES5).</p>
<pre data-language="javascript" class="code">
// All valid in ES5
var a = { get x() { } };
var b = { get "y"() { } };
var c = { get 2() { } };

var e = { set x(v) { } };
var f = { set "y"(v) { } };
var g = { set 2(v) { } };
</pre>
<p>SpiderMonkey has historically parsed literal accessors using a slightly-tweaked version of its function parsing code.  Therefore, <a href="http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/">as previously explained</a> SpiderMonkey would accept essentially anything which could follow <code>function</code> in a function expression as valid accessor syntax in object literals.</p>
<h2>ES5 requires accessors have exact numbers of arguments</h2>
<p>A consequence of parsing accessors using generalized function parsing is that SpiderMonkey accepted some nonsensicalities, such as no-argument setters or multiple-argument getters or setters:</p>
<pre data-language="javascript" class="code">
var o1 = { get p(a, b, c, d, e, f, g) { /* why have any arguments? */ } };
var o2 = { set p() { /* to what value? */ } };
var o3 = { set p(a, b, c) { /* why more than one? */ } };
</pre>
<p>ES5 accessor syntax sensibly deems such constructs errors: a conforming ES5 implementation would reject all of the above statements.</p>
<h2>SpiderMonkey is changing to follow ES5: getters require no arguments, setters require one argument</h2>
<p>SpiderMonkey has now been changed to follow ES5.  There seemed little to no gain in continuing to support bizarre numbers of arguments when the spec counseled otherwise, and any code which does end up broken is easily fixed.</p>
<p>As always, you can experiment with a version of Firefox with these changes to accessor syntax 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>
]]></content:encoded>
			<wfw:commentRss>http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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>&#8216;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>&#8216;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>&#8216;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>&#8216;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>13</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 don&#8217;t [...]]]></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!  new"
</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(v) { print("sotten!  " + v); });
var v2 = o2.property; // prints "gotten!", v2 === "get"
o2.property = "new"; // prints "sotten!  new"
</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>&#8216;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&#8242;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&#8242;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>&#8216;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&#8242;s strict mode support required adjustments to the area of parsing object literals (for ES5&#8242;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>17</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 [...]]]></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>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 three-minute [...]]]></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() { [...]]]></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&#8242;s behavior what you&#8217;d expect?  No, it isn&#8217;t.  In fact, ES3&#8242;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&#8242;s behavior.  ES3&#8242;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&#8242;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>
	</channel>
</rss>

