<?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>dherman at mozilla</title>
	<atom:link href="http://blog.mozilla.com/dherman/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mozilla.com/dherman</link>
	<description>language engineering on the web</description>
	<lastBuildDate>Tue, 06 Dec 2011 05:26:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>I&#8217;ve moved!</title>
		<link>http://blog.mozilla.com/dherman/2011/12/05/ive-moved/</link>
		<comments>http://blog.mozilla.com/dherman/2011/12/05/ive-moved/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 05:26:16 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=131</guid>
		<description><![CDATA[My blog has a new home: http://calculist.org Hope to see you there!]]></description>
			<content:encoded><![CDATA[<p>My blog has a new home:</p>
<p><strong><a href="http://calculist.org">http://calculist.org</a></strong></p>
<p>Hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/12/05/ive-moved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Now that&#8217;s a nice &#8216;stache</title>
		<link>http://blog.mozilla.com/dherman/2011/12/01/now-thats-a-nice-stache/</link>
		<comments>http://blog.mozilla.com/dherman/2011/12/01/now-thats-a-nice-stache/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 17:59:44 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[ECMAScript]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=126</guid>
		<description><![CDATA[There was something that bugged me about Allen Wirfs-Brock&#8216;s proposed &#8220;monocle-mustache&#8221; syntax for JS: it updates the properties of the object on the left-hand side, but it doesn&#8217;t look like it&#8217;s updating properties; it looks like some strange combination of &#8230; <a href="http://blog.mozilla.com/dherman/2011/12/01/now-thats-a-nice-stache/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img style="float: right;" src="http://people.mozilla.org/%7Edherman/images/natbo.gif" alt="dot-stache" />There was something that bugged me about <a href="http://www.wirfs-brock.com/allen/">Allen Wirfs-Brock</a>&#8216;s <a href="http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_extension_literal">proposed &#8220;monocle-mustache&#8221; syntax</a> for JS: it updates the properties of the object on the left-hand side, but it doesn&#8217;t look like it&#8217;s updating properties; it looks like some strange combination of creating a new object and accessing properties of the left-hand side:</p>
<pre>this.{ foo: 17, bar: "hello", baz: true };</pre>
<p>Then this week a couple things happened that got me thinking: <a href="http://reginald.braythwayt.com/">Reg Braithwaite</a> (aka <a href="https://twitter.com/#!/raganwald">@raganwald</a>) posted his <a href="https://github.com/raganwald/homoiconic/blob/master/2011/11/sans-titre.md#readme">proposal for a CoffeeScript syntax</a> to support the &#8220;<a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent style</a>&#8221; of programming, inspired by <a href="http://en.wikipedia.org/wiki/Smalltalk#Expressions">Smalltalk&#8217;s message cascades</a>:</p>
<pre>array
    .pop()
    .pop()
    .pop()

path
    .moveTo(10, 10)
    .stroke("red")
    .fill("blue")
    .ellipse(50, 50)</pre>
<p>Next I saw <a href="https://plus.google.com/100798142896685420545/posts">Bob Nystrom</a>&#8216;s <a href="https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/611c04100ac17142">post</a> about a <a href="https://docs.google.com/document/d/1U0PeHtVQHMQ8usy7xI5Luo01W5LuWR1acN5odgu_Mtw/edit?pli=1">cascade syntax proposal for Dart</a>, which is a small variation on Allen&#8217;s monocle-mustache:</p>
<pre>document.query('#myTable').{
    queryAll('.firstColumn').{
        style.{
            background = 'red',
            border = '2px solid black'
        },
        text = 'first column'
    },
    queryAll('.lastColumn').{
        style.background = 'blue',
        text = 'last column'
    }
};</pre>
<p>This is really just a tiny tweak to Allen&#8217;s original syntax, but it makes a world of difference: it uses the <code>=</code> sign for assignment. Much clearer!</p>
<p>Now, assignments with commas don&#8217;t really look like JS. But since the point of cascades is to do imperative sequencing — i.e., to ignore the result of intermediate message sends and do each message send on the original object from the left-hand side — it makes perfect sense to use a statement-like syntax:</p>
<pre>array.{
    pop();
    pop();
    pop();
};

path.{
    moveTo(10, 10);
    stroke("red");
    fill("blue");
    ellipse(50, 50);
};

this.{
    foo = 17;
    bar = "hello";
    baz = true;
};</pre>
<p>Even sweeter, JavaScript&#8217;s automatic semicolon insertion kicks in and lets you do this very concisely:</p>
<pre>array.{
    pop()
    pop()
    pop()
};

path.{
    moveTo(10, 10)
    stroke("red")
    fill("blue")
    ellipse(50, 50)
};

this.{
    foo = 17
    bar = "hello"
    baz = true
};</pre>
<p>What&#8217;s so great about a cascade syntax is that when you want to do imperative programming on the same object, you don&#8217;t have to rely on the API creator to return <code>this</code> from every method. As the client of the API you don&#8217;t have to care <em>what</em> the method returns, you throw away the result anyway.</p>
<p>In fact, this is one of the things I <em>don&#8217;t</em> like about method chaining in JS today: you can&#8217;t actually tell whether the method is mutating the current object and returning itself or producing some entirely new object. And when you mix the two styles, it gets even blurrier. Look at Bob&#8217;s example from jQuery:</p>
<pre>$('#myTable')
    .find('.firstColumn')
        .css({'background': 'red',
              'border': '2px solid black'})
        .text('first column')
    .end()
    .find('.lastColumn')
        .css('background','blue')
        .text('last column');</pre>
<p>You just have to know that <code>.find()</code> produces a new object, and <code>.css()</code> and <code>.text()</code> modify it and produce the same object, but nothing about the syntax tells you this. (And personally, I always feld that <code>.end()</code> was where jQuery&#8217;s API jumps the shark.)</p>
<p>With a cascade syntax in addition to normal method calls, you can so much more easily distinguish when you&#8217;re doing something to the same object, and when you&#8217;re selecting new objects:</p>
<pre>$('#myTable').{
    find('.firstColumn').{
        style.{
            background = 'red'
            border = '2px solid black'
        }
        text = 'first column'
    }
    find('.lastColumn').{
        style.background = 'blue'
        text = 'last column'
    }
};</pre>
<p><a href="https://gist.github.com/1414956">Gist here</a>. These are just my initial thoughts; I&#8217;ll have to work on fleshing out a full proposal, including the full grammar spec.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/12/01/now-thats-a-nice-stache/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Liar</title>
		<link>http://blog.mozilla.com/dherman/2011/11/22/liar/</link>
		<comments>http://blog.mozilla.com/dherman/2011/11/22/liar/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 16:19:48 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=117</guid>
		<description><![CDATA[I&#8217;ve claimed in a couple talks recently that the ES6 expression new Foo(...args) is impossible to implement in ES3 and only became possible in ES5 with Function.prototype.bind: Function.prototype.applyNew = function applyNew(a) { return new (this.bind.apply(this, [,].concat(a)))(); }; Foo.applyNew(args) This works &#8230; <a href="http://blog.mozilla.com/dherman/2011/11/22/liar/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve claimed in a couple talks recently that the ES6 expression</p>
<pre>new Foo(...args)</pre>
<p>is impossible to implement in ES3 and only became possible in ES5 with <code>Function.prototype.bind</code>:</p>
<pre>Function.prototype.applyNew = function applyNew(a) {
    return new (this.bind.apply(this, [,].concat(a)))();
};
Foo.applyNew(args)</pre>
<p>This works by closing the function over arguments array <code>a</code> with an undefined receiver (the <code>[,]</code> expression creates an array of length 1 but a hole at element 0). Since a function created by <code>Function.prototype.bind</code> ignores the bound receiver when called with <code>new</code>, this has the same behavior as the ES6 expression.</p>
<p>But I should not have counted ES3 out so easily — with the magic of <code>eval</code>, many impossible things are possible.</p>
<pre>Function.prototype.applyNew = function applyNew(a) {
    return eval("new this(" +
                a.map(function(x, i) {
                          return "a[" + i + "]";
                      }).join(",") +
                ")");
};
Foo.applyNew(args)</pre>
<p>Thanks to <a href="blog.trevorjnorris.com/2011/09/apply-the-new-operator/">Trevor Norris</a> for awakening me from my dogmatic slumber. His approach won&#8217;t work for functions like <code>String</code> that have different call and construct behavior, but he reminded me that I&#8217;d seen this solved before with <code>eval</code>.</p>
<p><strong>Edit:</strong> Oops, the <code>applyNew</code> method doesn&#8217;t take a function argument, it uses <code>this</code> as the function. That&#8217;s what I get for posting without testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/11/22/liar/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>contracts.coffee</title>
		<link>http://blog.mozilla.com/dherman/2011/08/29/contracts-coffee/</link>
		<comments>http://blog.mozilla.com/dherman/2011/08/29/contracts-coffee/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 23:36:10 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[Research interns]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=114</guid>
		<description><![CDATA[Mozilla Research intern Tim Disney has has just released contracts.coffee, a dialect of CoffeeScript with a sweet syntax for wrapping JavaScript objects with contracts that are automatically checked at runtime (&#8220;assert on steroids&#8221;). Tim&#8217;s got a great tutorial with lots &#8230; <a href="http://blog.mozilla.com/dherman/2011/08/29/contracts-coffee/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Mozilla Research intern <a href="http://disnetdev.com/">Tim Disney</a> has has just released <a href="http://disnetdev.com/contracts.coffee/">contracts.coffee</a>, a dialect of <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a> with a sweet syntax for wrapping JavaScript objects with<em></em> contracts that are automatically checked at runtime (&#8220;<code>assert</code> on steroids&#8221;). Tim&#8217;s got a great tutorial with lots of examples, so I won&#8217;t try to reproduce it here. Just <a href="http://disnetdev.com/contracts.coffee/">check it out</a>!</p>
<p>Contracts.coffee is a great demonstrator of the power of JavaScript <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Proxy">proxies</a>, which are coming in the ES6 standard, have been shipping in SpiderMonkey since Firefox 4, and are in the works for V8. Contracts.coffee uses proxies to create wrapper objects that do all the proper checking to enforce your contracts.</p>
<p>It&#8217;s also a nice experiment in language design (using ideas from <a href="http://c2.com/cgi/wiki?DesignByContract">Eiffel</a> and <a href="http://users.eecs.northwestern.edu/~robby/pubs/papers/ho-contracts-techreport.pdf">Scheme</a>), which we&#8217;ll be paying attention to along the road to ES7 and beyond.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/08/29/contracts-coffee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JS static analysis projects</title>
		<link>http://blog.mozilla.com/dherman/2011/06/30/js-static-analysis-projects/</link>
		<comments>http://blog.mozilla.com/dherman/2011/06/30/js-static-analysis-projects/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 04:38:17 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Doctor JS]]></category>
		<category><![CDATA[Static analysis]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=109</guid>
		<description><![CDATA[Taras was reflecting on his attempts to build community around static analysis tools for open source software. Taras has built some impressive tools for analyzing our massive C++ codebase at Mozilla, and has brought static analysis into the Mozilla lexicon. &#8230; <a href="http://blog.mozilla.com/dherman/2011/06/30/js-static-analysis-projects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Taras was reflecting on his attempts to <a href="http://blog.mozilla.com/tglek/2011/06/30/dehyratreehydra-static-analysis-thoughts/">build community around static analysis</a> tools for open source software. Taras has built some <a href="https://developer.mozilla.org/en/Dehydra">impressive</a> <a href="https://developer.mozilla.org/en/Treehydra">tools</a> for analyzing our massive C++ codebase at Mozilla, and has brought static analysis into the Mozilla lexicon. Lately, our research group has been carrying the torch by creating new tools for analyzing JavaScript.</p>
<p>Last summer, our research intern <a href="http://www.ccs.neu.edu/home/dimvar/info.html">Dimitris Vardoulakis</a> built <a href="http://doctorjs.org">Doctor JS</a>, a static analysis for JavaScript based on Dimitris&#8217;s <a href="http://people.csail.mit.edu/asolar/PLDISRC/PLDI_2011_Src.htm">award-winning</a> <a href="http://arxiv.org/pdf/1102.3676">CFA2</a> algorithm. Our first uses of Doctor JS were a <a href="http://doctorjs.org/try.html">type inference service</a> and <a href="https://github.com/mozilla/doctorjs/tree/master/lib/jsctags">js-ctags</a>, which generates output that IDE&#8217;s can use for auto-completion.</p>
<p>This summer, we&#8217;re starting more projects with Doctor JS. With the help of Dimitris and our intern <a href="http://paul.rutgers.edu/~rkarim/">Rezwana Karim</a>, we&#8217;re investigating event listener registration patterns in Firefox addons to test for compatibility issues with <a href="https://wiki.mozilla.org/Electrolysis">Electrolysis</a>. Another intern, <a href="http://www.cs.ucsb.edu/~vineeth/">Vineeth Kashyap</a>, is modifying Doctor JS to do static taint tracking as a way of doing security analyses for leaking chrome-privilege data into content-privilege code.</p>
<p>I&#8217;d like Doctor JS to get to a point where it&#8217;s more scriptable—a &#8220;semantic grep&#8221; tool like <a href="https://developer.mozilla.org/en/Dehydra">Dehydra</a>. I&#8217;m sure we&#8217;ll crib some notes from Taras&#8217;s work. But for a first step we&#8217;re just going to adapt the tool as needed to the specific applications we&#8217;re using it for. Hopefully this will give us a better feel for how to generalize it down the road to be more user-extensible.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/06/30/js-static-analysis-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The JS parser API has landed</title>
		<link>http://blog.mozilla.com/dherman/2011/06/28/the-js-parser-api-has-landed/</link>
		<comments>http://blog.mozilla.com/dherman/2011/06/28/the-js-parser-api-has-landed/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 22:23:37 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[SpiderMonkey]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=103</guid>
		<description><![CDATA[Today I pushed a patch to our development repository to expose the JavaScript Parser API to code with chrome privileges. I&#8217;ve fixed all known major bugs, and it&#8217;s been holding up under sustained attack from Jesse Ruderman&#8216;s fuzz tester without &#8230; <a href="http://blog.mozilla.com/dherman/2011/06/28/the-js-parser-api-has-landed/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today I <a href="http://hg.mozilla.org/tracemonkey/rev/2ce7546583ff">pushed a patch</a> to our development repository to expose the <a href="https://developer.mozilla.org/en/SpiderMonkey/Parser_API">JavaScript Parser API</a> to code with <a href="https://developer.mozilla.org/en/Chrome">chrome privileges</a>. I&#8217;ve fixed all known major bugs, and it&#8217;s been holding up under sustained attack from <a href="http://www.squarefree.com/">Jesse Ruderman</a>&#8216;s <a href="http://www.squarefree.com/2007/08/02/introducing-jsfunfuzz/">fuzz tester</a> without crashing for the better part of a year. So hopefully this means the landing will stick. If so, then this should land in <a href="http://nightly.mozilla.org/">Nightly</a> within the week and <a href="http://www.mozilla.com/en-US/firefox/channel/">Aurora</a> by next week. If all goes well, the library will ship with Firefox 7.</p>
<p>Once it ships, addons will be able to write:</p>
<blockquote><pre>&gt; Components.utils.import("resource://gre/modules/reflect.jsm");
&gt; var expr = Reflect.parse("obj.foo + 42").body[0].expression
&gt; expr.left.property.name
"foo"
&gt; expr.right.value
42</pre>
</blockquote>
<p>I hope people find this useful for introspecting on code, building development tools, or writing static analyses. I also hope people come up with cool new applications I haven&#8217;t even thought of.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/06/28/the-js-parser-api-has-landed/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>I&#8217;m worried about structured clone</title>
		<link>http://blog.mozilla.com/dherman/2011/05/25/im-worried-about-structured-clone/</link>
		<comments>http://blog.mozilla.com/dherman/2011/05/25/im-worried-about-structured-clone/#comments</comments>
		<pubDate>Thu, 26 May 2011 06:57:01 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=100</guid>
		<description><![CDATA[When I first heard about web workers using structured clone, I was nervous. The more I look into it, the more I think the whole idea of structured clone — regardless of what it&#8217;s used for — is problematic in &#8230; <a href="http://blog.mozilla.com/dherman/2011/05/25/im-worried-about-structured-clone/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I first heard about web workers using <a href="http://www.w3.org/TR/html5/common-dom-interfaces.html#safe-passing-of-structured-data">structured clone</a>, I was nervous. The more I look into it, the more I think the whole idea of structured clone — regardless of what it&#8217;s used for — is problematic in and of itself.</p>
<p><strong>Implicit copying is rarely what you want</strong></p>
<p>When data is mutable, it needs to be managed by the programmer who created it, because they know what they&#8217;re doing with it. When the language or API implicitly copies the data, the programmer has no control over it. Granted, structured clone is only used in a few published places in HTML5, but it would be preferable to have explicit ways to construct immutable data, and only be able to send immutable data between workers. (Or ways to safely transfer ownership of mutable data, but that&#8217;s irrelevant to the question of structured clone.)</p>
<p>This raises the question of how to express immutable data in JavaScript. That&#8217;s something that <a href="http://brendaneich.com/2011/01/harmony-of-my-dreams/">Brendan has recently blogged about</a>, and it&#8217;s worth adding to the language. But structured clone strikes me as a hack around the problem that we don&#8217;t currently have convenient ways of creating structured, immutable data.</p>
<p><strong>Structured clone ignores huge swathes of JavaScript data</strong></p>
<p>Structured clone is only defined on a handful of JavaScript built-in JavaScript and DOM object types. JavaScript objects are part of a deeply intertwined, deeply mutable object graph, and structured clone simply ignores most of that graph. An operation that uses structured clone will let you use <em>any</em> Object instance, regardless of what sorts of invariants it&#8217;s set up to expect based on its prototype chain, its getters or setters, its connectedness to the object graph&#8230; but structured clone will simply blithely disregard much of that structure.</p>
<p>Again, if we simply had some simple, immutable data structures like <a href="http://wiki.ecmascript.org/doku.php?id=strawman:tuples">tuples</a> and <a href="http://wiki.ecmascript.org/doku.php?id=strawman:records">records</a>, these would be totally reasonable things to share between workers.</p>
<p><strong>Automatically traversing mutable data structures is a code smell</strong></p>
<p>There&#8217;s a <a href="http://home.pipeline.com/~hbaker1/ObjectIdentity.html">famous paper by Henry Baker</a> that specifically argues that cloning mutable data structures rarely has a &#8220;one size fits all&#8221; solution, and that mutable data can&#8217;t be usefully traversed automatically by general purpose libraries. I have a sense that whenever some API is automatically, deeply traversing mutable data structures, it&#8217;s probably unlikely to be doing the right thing.</p>
<p><strong>Structured clone is not future-proof</strong></p>
<p>Structured clone is simply defined on a grab-bag of built-in datatypes, and the rest are treated as plain old objects. This means it&#8217;s going to behave very strangely on new data types that get introduced in future versions of ECMAScript, such as <a href="http://wiki.ecmascript.org/doku.php?id=strawman:simple_maps_and_sets">maps and sets</a>, or in user libraries.</p>
<p><strong>Alternatives?</strong></p>
<p>A more adaptable approach might be for ECMAScript to specify &#8220;transmittable&#8221; data structures. As we add immutable data structures, they could be defined to be transmittable, and we could even specify custom internal properties of certain classes of mutable objects with transmission-safe semantics such as ownership transfer.</p>
<p>Doing these kinds of things well, in a way that&#8217;s simple, clear and predictable, deserves built-in language support.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/05/25/im-worried-about-structured-clone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A semantics for JSON</title>
		<link>http://blog.mozilla.com/dherman/2011/05/25/a-semantics-for-json/</link>
		<comments>http://blog.mozilla.com/dherman/2011/05/25/a-semantics-for-json/#comments</comments>
		<pubDate>Wed, 25 May 2011 21:24:22 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=97</guid>
		<description><![CDATA[[Warning: here there be PL geekery.] In a discussion about Lloyd Hilaiel&#8216;s cool new JSONSelect library, Rob Sayre pointed out that the library shouldn&#8217;t permit ordered access to JSON objects. This got me thinking about the fact that the JSON &#8230; <a href="http://blog.mozilla.com/dherman/2011/05/25/a-semantics-for-json/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>[<strong>Warning:</strong> here there be PL geekery.]</p>
<p>In a discussion about <a href="https://twitter.com/#!/lloydhilaiel">Lloyd Hilaiel</a>&#8216;s cool new <a href="http://jsonselect.org">JSONSelect</a> library, <a href="https://twitter.com/#!/sayrer/status/71763046560829440">Rob Sayre pointed out</a> that the library <a href="https://github.com/lloyd/JSONSelect/issues/1">shouldn&#8217;t permit ordered access to JSON objects</a>. This got me thinking about the fact that the JSON RFC defines a syntax without a semantics. And yet the <a href="http://www.ietf.org/rfc/rfc4627.txt?number=4627">introduction</a> makes a semantic claim:</p>
<blockquote><p>An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.</p></blockquote>
<p>This claim from the introduction isn&#8217;t specified anywhere in the RFC. But it&#8217;s not too hard to provide a semantics that does. To keep things simple, let me just assume an existing semantics for Unicode strings and IEEE754 double-precision floating-point numbers, where <em>UnicodeValue</em>(<em>s</em>) produces the Unicode string for a JSON <em>string</em> literal <em>s</em>, and <em>DoubleValue</em>(<em>n</em>) produces the IEEE754 double-precision floating-point number value for the JSON <em>number</em> literal <em>n</em>.</p>
<p>Here goes!</p>
<p><strong>Values</strong></p>
<p>A JSON value is a member of one of the following disjoint sets:</p>
<ul>
<li>Unicode strings</li>
<li>IEEE754 numbers</li>
<li>the constants <strong>{ true</strong>, <strong>false</strong>, <strong>null }</strong></li>
<li>JSON arrays</li>
<li>JSON objects</li>
</ul>
<p>A JSON array is a finite sequence of JSON values. (I&#8217;ll write <strong>[]</strong> for the empty array, <strong>[</strong> <em>v</em> <strong>]</strong> for the singleton array containing the JSON value <em>v</em>, and <em>a1</em> ⋅ <em>a2</em> for array concatenation.)</p>
<p>A JSON object is a finite set of <strong>(</strong>Unicode string<strong>,</strong> JSON value<strong>)</strong> pairs.</p>
<p><strong>Operations</strong></p>
<p>Selecting the keys of a JSON object:</p>
<p><em>keys</em>(o) = <strong>{</strong> <em>s</em> | <strong>(</strong><em>s</em><strong>,</strong> <em>v</em><strong>)</strong> ∈ <em>o</em> <strong>}</strong></p>
<p>Extending a JSON object:</p>
<p><em>extend</em>(<em>o</em>, <em>s</em>, <em>v</em>) = (<em>o</em> &#8211; <strong>{ (</strong><em>s</em><strong>,</strong> <em>v</em>&#8216;<strong>) }</strong>) ∪ <strong>{ (</strong><em>s</em><strong>,</strong> <em>v</em><strong>) }</strong><br />
if <strong>(</strong><em>s</em><strong>,</strong> <em>v</em>&#8216;<strong>)</strong> ∈ <em>o</em><br />
<em>extend</em>(<em>o</em>, <em>s</em>, <em>v</em>) = <em>o</em> ∪ <strong>{ (</strong><em>s</em><strong>,</strong> <em>v</em><strong>) }</strong><br />
if <em>s</em> ∉ <em>keys</em>(<em>o</em>)</p>
<p>Looking up properties on a JSON object:</p>
<p><em>lookup</em>(<em>o</em>, <em>s</em>) = <em>v</em><br />
if <strong>(</strong><em>s</em><strong>,</strong> <em>v</em><strong>)</strong> ∈ <em>o</em></p>
<p>Note that <em>lookup</em> is partial: <em>lookup</em>(<em>o</em>, <em>s</em>) is unspecified when <em>s</em> ∉ <em>keys</em>(<em>o</em>).</p>
<p><strong>Interpretation</strong></p>
<p>Now that we have the data model, we can define the interpretation of the syntax:</p>
<p><em>Value</em>(<em>string</em>) = <em>UnicodeValue</em>(<em>string</em>)<br />
<em>Value</em>(<em>number</em>) = <em>DoubleValue</em>(<em>number</em>)<br />
<em>Value</em>({}) = <strong>∅</strong><br />
<em>Value</em>({ <em>members</em> }) = <em>ObjectValue</em>(<em>members</em>)<br />
<em>Value</em>([]) = <strong>[]</strong><br />
<em>Value</em>([ <em>elements</em> ]) = <em>ArrayValue</em>(<em>elements</em>)</p>
<p><em>ObjectValue</em>(<em>s</em> : <em>v</em>) = <strong>{ (</strong><em>UnicodeValue</em>(<em>s</em>)<strong>,</strong> <em>Value</em>(<em>v</em>)<strong>) }</strong><br />
<em>ObjectValue</em>(<em>members</em>, <em>s</em> : <em>v</em>) = <em>extend</em>(<em>ObjectValue</em>(<em>members</em>), <em>UnicodeValue</em>(<em>s</em>), <em>Value</em>(<em>v</em>))</p>
<p><em>ArrayValue</em>(<em>v</em>) = <strong>[</strong> <em>Value</em>(<em>v</em>) <strong>]</strong><br />
<em>ArrayValue</em>(<em>v</em>, <em>elements</em>) = <em>Value</em>(<em>v</em>) ⋅ <em>ArrayValue</em>(<em>elements</em>)</p>
<p>That&#8217;s it!</p>
<p>With this data model, you can now show that the order of object properties doesn&#8217;t matter, except insofar as when there are duplicate keys, later ones replace earlier ones.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/05/25/a-semantics-for-json/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A failure of imagination</title>
		<link>http://blog.mozilla.com/dherman/2011/05/02/a-failure-of-imagination/</link>
		<comments>http://blog.mozilla.com/dherman/2011/05/02/a-failure-of-imagination/#comments</comments>
		<pubDate>Mon, 02 May 2011 21:36:44 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=85</guid>
		<description><![CDATA[First, let me say that I&#8217;m enjoying my first JSConf very much. It&#8217;s obvious the organizers and speakers have all done a ton of work to make it a great event. I hope this post doesn&#8217;t come across as ungrateful &#8230; <a href="http://blog.mozilla.com/dherman/2011/05/02/a-failure-of-imagination/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>First, let me say that I&#8217;m enjoying my first JSConf very much. It&#8217;s obvious the organizers and speakers have all done a ton of work to make it a great event. I hope this post doesn&#8217;t come across as ungrateful or dyspeptic. But I feel like I should say something, and I don&#8217;t think I&#8217;m alone in my reaction:</p>
<p><a href="http://twitter.com/#!/JennLukas/status/65141321551196160"><img src="http://blog.mozilla.com/dherman/files/2011/05/tweet.jpg" alt="@JennLukas tweet" title="@JennLukas tweet" width="480" height="146" class="aligncenter size-full wp-image-86" /></a></p>
<p>There was a little lunchtime performance that just went places it didn&#8217;t need to go. There were jokes about &#8220;transvestite hipsters in downtown Portland&#8221; and about women in technology. They called out women in the audience, and said they were going to bring one up on stage by picking a woman&#8217;s name at random. First they brought up a man whose name looks like &#8220;Jan&#8221; and then asked him questions as if he was a woman (really, this was the level of humor). They asked him how it feels to be a woman in technology, surrounded by &#8220;smelly, sweaty men.&#8221; They joked about how they only discovered that this guy was a man &#8220;late last night&#8221; (get it?). Then they did call up a woman; I don&#8217;t know if she&#8217;d agreed in advance.</p>
<p>The questions they asked her were pretty tame. All in all, it wasn&#8217;t particularly out of control. But the whole time I was sitting there just praying it wasn&#8217;t going to get worse. And I imagine there were women in the audience feeling nervous that they were going to be called up and embarrassed or humiliated. </p>
<p>I&#8217;m sure the people putting on the show were nervous and just trying to give the audience a good show. And I&#8217;m sure they felt they didn&#8217;t cross any lines (even though the queer jokes pretty much did). But this is the part that I find really sad. It&#8217;s a failure of imagination, especially as a performer, not to be able to empathize with the audience: how were we supposed to know in advance how far it was going to go? Why would you make some of your audience feel intimidated or anxious just in the name of cheap laughs?</p>
<p>And let&#8217;s face it, this humor is cheap. These are the kinds of jokes you use when you&#8217;ve got nothing else. Comedy is really, really hard. If you aren&#8217;t a professional comic, maybe you just shouldn&#8217;t try. But at least stay away from jokes that isolate and intimidate your own audience. Some places are set up for raunchy or deliberately offensive humor, and that&#8217;s fine. But this is a technology conference.</p>
<p><b>Update:</b> I hope this post won&#8217;t lead people to generalize about the JS community or JSConf. I stand by what I said; Chris and Laura work to make JSConf fun and inclusive for everyone, and I don&#8217;t think the guys who did this bit yesterday lived up to that. But as I said in my post, JSConf really has been incredible.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/05/02/a-failure-of-imagination/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Who says JavaScript I/O has to be ugly?</title>
		<link>http://blog.mozilla.com/dherman/2011/03/11/who-says-javascript-io-has-to-be-ugly/</link>
		<comments>http://blog.mozilla.com/dherman/2011/03/11/who-says-javascript-io-has-to-be-ugly/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 23:34:44 +0000</pubDate>
		<dc:creator>dherman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mozilla.com/dherman/?p=81</guid>
		<description><![CDATA[I&#8217;m excited to announce the latest version of jsTask, which now supports first-class, synchronizable events. A Sync object represents an event that may or may not have fired yet, and which a task can block on. jsTask already makes it &#8230; <a href="http://blog.mozilla.com/dherman/2011/03/11/who-says-javascript-io-has-to-be-ugly/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m excited to announce the latest version of <a href="http://dherman.github.com/jstask">jsTask</a>, which now supports first-class, synchronizable events. A <a href="http://dherman.github.com/jstask/doc/api.html">Sync</a> object represents an event that may or may not have fired yet, and which a task can block on.</p>
<p>jsTask already makes it easy to write tasks that block on I/O:</p>
<blockquote><pre>var foo = yield read("foo.json");
var bar = yield read("bar.json");</pre>
</blockquote>
<p>But the power lurking behind that code is the fact that <code>read</code> actually returns a <code>Sync</code> value, which can be used to build more interesting synchronizable events. For example, we can do a <a href="http://blog.mozilla.com/dherman/2011/02/26/automatically-scheduled-js-tasks%E2%80%94with-joins/">join</a> on several concurrent operations, so that one doesn&#8217;t have to wait for the other before initiating:</p>
<blockquote><pre>var [ foo, bar ] = yield join(read("foo.json"), read("bar.json"));</pre>
</blockquote>
<p>Or we can choose from several different concurrent operations, letting whichever completes first produce the result (and cancelling the others):</p>
<blockquote><pre>var file = yield choose(read("a.json"), read("b.json"), read("c.json"));</pre>
</blockquote>
<p>With combinators like these, you can start to build interesting idioms, such as the <code>timeout</code> method, which I went ahead and built in to the library:</p>
<blockquote><pre>try {
    var file = yield read("foo.json").timeout(1000);
} catch (e) {
    // I/O error or timed out
}</pre>
</blockquote>
<p>This is just the beginning: I&#8217;ll be implementing <code>Sync</code> wrappers for all the major DOM I/O events, and I&#8217;ll keep experimenting with API&#8217;s for common use cases and helpful idioms.</p>
<p>I believe jsTask would also be useful for server-side JS frameworks like <a href="http://nodejs.org/">node.js</a>, which use non-blocking I/O heavily. But first we have to get <a href="http://wiki.ecmascript.org/doku.php?id=strawman:generators">generators</a> into V8 and ECMAScript!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mozilla.com/dherman/2011/03/11/who-says-javascript-io-has-to-be-ugly/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

