<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>The Brown PLT Blog</title>
 <link href="http://brownplt.github.com/feed/" rel="self"/>
 <link href="http://brownplt.github.com/"/>
 <updated>2012-04-01T00:20:50-07:00</updated>
 <id>http://brownplt.github.com/</id>
 <author>
   <name>Brown PLT</name>
   <email>joe@cs.brown.edu</email>
 </author>

 
 <entry>
   <title>ECMA Announces Official λJS Adoption</title>
   <link href="http://brownplt.github.com/2012/04/01/ecma-lambdajs-announcement.html"/>
   <updated>2012-04-01T00:00:00-07:00</updated>
   <id>http://brownplt.github.com/2012/04/01/ecma-lambdajs-announcement</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2012/04/01/ecma-lambdajs-announcement.html&quot;&gt;ECMA Announces Official λJS Adoption&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  01 April 2012.&lt;/em&gt;

&lt;div style=&quot;font-size: 10pt; font-family: arial&quot;&gt;

&lt;p&gt;&lt;em&gt;GENEVA&lt;/em&gt; -
ECMA's Technical Committee 39, which oversees the standardization of
ECMAScript, has completed the adoption of Brown PLT's λ&lt;sub&gt;JS&lt;/sub&gt; as the new
basis for the language. &quot;We were being hampered by the endless
debates about the semantics of ECMAScript 5&quot;, said J. Neumann, the
Chairman of the Committee. &quot;By adopting λ&lt;sub&gt;JS&lt;/sub&gt;, we can return to
focusing on the important parts of the programming language instead,
such as its interaction with parts of the W3C DOM Specification.&quot;
&lt;/p&gt;

&lt;p style=&quot;width: 8em; float:right; font-size:12pt; padding: 1em;
margin: 1em; background-color: #eee; border: 1px solid black;
text-align: left;&quot;&gt;&quot;The replacement of scope objects
with substitution is a clear design flaw.&quot;&lt;br/&gt;-Arjun Guha&lt;/br&gt;&lt;/p&gt;

&lt;p&gt; &lt;b&gt;Improvements to λ&lt;sub&gt;JS&lt;/sub&gt;&lt;/b&gt; - Neumann added that the
standardization process uncovered a significant weakness in
λ&lt;sub&gt;JS&lt;/sub&gt;: the absence of the &lt;code&gt;with&lt;/code&gt; construct. The
Technical Committee therefore mandated its introduction.  Lead designer
Arjun Guha agreed, stating, &quot;The replacement of scope objects with
substitution is a clear design flaw.  It was pointed out to me by
numerous academic researchers who have obtained considerable mileage
from them, but it took me a while to appreciate their value.&quot; The
Committee also recommended a &quot;strict mode&quot;, so Guha removed first-class
functions, which are widely believed to induce laxity by deferring
decision-making.  &lt;/p&gt;

&lt;p&gt; &lt;b&gt;Opposition to the Change&lt;/b&gt; - The adoption of λ&lt;sub&gt;JS&lt;/sub&gt; has
not, however, met with unanimous approval.  When asked for comment,
Douglas Crockford of Yahoo!  complained that the small parts are not
good while the good parts are not small.  Another detractor,
Northeastern University researcher Sam Tobin-Hochstadt, had pushed for
the adoption of Racket as the core language instead of λ&lt;sub&gt;JS&lt;/sub&gt;,
but he admitted that Racket was untenable as it suffered from having a
working module system. The team from Apple declined response, but it is
widely rumored that Jonathan Ive is at work on a new core calculus that
will have only one operation, which will automatically take the step
that the user did not know they should have performed.  &lt;/p&gt;

&lt;p style=&quot;width: 8em; float:left; font-size:12pt; padding: 1em;
margin: 1em; background-color: #eee; border: 1px solid black;
text-align: left;&quot;&gt;&quot;We see this as a fight for the future
of the Internet.&quot;&lt;br/&gt;-David Herman&lt;/br&gt;&lt;/p&gt;

&lt;p&gt; &lt;b&gt;Influential Support&lt;/b&gt; - Nevertheless, the adoption has support
from various influential circles. The Internet Explorer group at
Microsoft has already agreed to implement λ&lt;sub&gt;JS&lt;/sub&gt; in the core
engine of their upcoming release, IE12; lead designer Dean Hachamovitch
said it is second in innovation only to the introduction of tabs. Strict
mode will be supported in IE13.  Google's Mark Miller pointed out, &quot;With
the aid of membranes, any primordial vat can be instantiated with
desirable liveness properties.&quot; When asked to comment about
λ&lt;sub&gt;JS&lt;/sub&gt; instead of the Miller-Urey experiment, Miller repeated
the comment. Finally, noted Mozilla researcher Dave Herman commented,
&quot;For Mozilla, we see this as a fight for the future of the Internet.&quot; On
questioning, he admitted that he diverts all interviews into
conversations about Boot2Gecko.  &lt;/p&gt;


&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Objects in Scripting Languages</title>
   <link href="http://brownplt.github.com/2012/02/28/scripting-objects.html"/>
   <updated>2012-02-28T00:00:00-08:00</updated>
   <id>http://brownplt.github.com/2012/02/28/scripting-objects</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2012/02/28/scripting-objects.html&quot;&gt;Objects in Scripting Languages&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 28 February 2012.&lt;/em&gt;

&lt;p&gt; We've been studying scripting languages in some detail, and have
collected a number features of their object systems that we find
unusually expressive.  This expressiveness can be quite powerful, but
also challenges attempts to reason about and understand programs that
use these features.  This post outlines some of these exceptionally
expressive features for those who may not be intimately familiar with
them.  &lt;/p&gt;

&lt;h3&gt;Dictionaries with Inheritance&lt;/h3&gt;

&lt;p&gt;
Untyped scripting languages implement objects as dictionaries mapping
member names (strings) to values.  Inheritance affects member lookup,
but does not affect updates and deletion.  This won't suprise any
experienced JavaScript programmer:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;z&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Using __proto__ sets up inheritance directly in most browsers&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__proto__&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// evaluates to 1&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// evaluates to 9&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// creates new field on obj&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;// evaluates to 50, z on parent is &amp;quot;overridden&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// evaluates to 9; parent.z was unaffected by obj.z = 50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; In other scripting languages, setting up this inheritance can't be
done quite so directly.  Still, its effect can be accomplished, and the
similar object structure observed.  For example, in Python:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;                 &lt;span class=&quot;c&quot;&gt;# class member&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;          &lt;span class=&quot;c&quot;&gt;# instance member&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;                 &lt;span class=&quot;c&quot;&gt;# evaluates to 1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;                 &lt;span class=&quot;c&quot;&gt;# evaluates to 9&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;            &lt;span class=&quot;c&quot;&gt;# creates new field on obj&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;                 &lt;span class=&quot;c&quot;&gt;# evaluates to 50, z on parent is &amp;quot;overridden&amp;quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;              &lt;span class=&quot;c&quot;&gt;# evaluates to 9, just like JavaScript &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; We can delete the field in both languages, which returns
&lt;code&gt;obj&lt;/code&gt; to its original state, before it was extended with a
&lt;code&gt;z&lt;/code&gt; member.  In JavaScript: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;k&quot;&gt;delete&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;z&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;// evaluates to 9 again&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This also works in Python:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;nb&quot;&gt;delattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;z&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;               &lt;span class=&quot;c&quot;&gt;# evaluates to 9 again&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
In both languages, we could have performed the assignments and lookups
with computed strings as well:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// JavaScript&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;x &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;yz&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// creates a new field, &amp;quot;x yz&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;x y&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;z&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;// evaluates to 99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Python&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;setattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;x &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;yz&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# creates a new field, &amp;quot;x yz&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;x y&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;z&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;c&quot;&gt;# evaluates to 99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
We can go through this entire progression in Ruby, as well:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# returns 1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# returns 9&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# return 50&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# no simple way to invoke shadowed z method&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remove_method&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# returns 9&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;define_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;xyz&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xyz&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# returns 99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;h3&gt;Classes Do Not Shape Objects&lt;/h3&gt;

&lt;p&gt; The upshot is that a class definition in a scripting language says
little about the structure of its instances.  This is in contrast to a
language like Java, in which objects' structure is completely determined
by their class, to the point where memory layouts can be predetermined
for runtime objects.  In scripting languages, this isn't the case.  An
object is an instance of a 'class' in JavaScript, Python, or Ruby merely
by virtue of several references to other runtime objects.  Some of these
be changed at runtime, others cannot, but in all cases, members can be
added to and removed from the inheriting objects.  This flexibility can
lead to some unusual situations.  &lt;/p&gt;

&lt;p&gt; &lt;b&gt;Brittle inheritance:&lt;/b&gt; Fluid classes make inheritance brittle.
If we start with this Ruby class: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@privateFld&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;myMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@privateFld&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@privateFld&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; Then we might assume that implementation of &lt;code&gt;myMethod&lt;/code&gt; assumes
a numeric type for &lt;code&gt;@privateFld&lt;/code&gt;.  This assumption can be
broken by subclasses, however: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@privateFld&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;string (not num)&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Since both &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; use the same name, and it
is simply a dictionary key, &lt;code&gt;B&lt;/code&gt; instances violate the
assumptions of &lt;code&gt;A&lt;/code&gt;'s methods:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myMethod&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# error: cannot multiply strings&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Ruby's authors are well aware of this; &lt;a target=&quot;_blank&quot;
href=&quot;http://books.google.com/books?hl=en&amp;lr=&amp;id=jcUbTcr5XWwC&amp;oi=fnd&amp;pg=PR5&amp;dq=the+ruby+programming+language&amp;ots=fIDgwbarbD&amp;sig=kPP2ZlLrH96wOTjyL7KdrUjsAbg#v=onepage&amp;q=the%20ruby%20programming%20language&amp;f=false&quot;&gt;
the Ruby manual&lt;/a&gt; states &quot;it is only
safe to extend Ruby classes when you are familiar with (and in control
of) the implementation of the superclass&quot; (page 240).
&lt;/p&gt;

&lt;p&gt; &lt;b&gt;Mutable Inheritance:&lt;/b&gt; JavaScript and Python expose the
inheritance chain through mutable object members.  In JavaScript, we
already saw that the member &lt;code&gt;&quot;__proto__&quot;&lt;/code&gt; could be used to
implement inheritance directly.  The &lt;code&gt;&quot;__proto__&quot;&lt;/code&gt; member is
mutable, so class hierarchies can be changed at runtime.  We found it a
bit more surprising when we realized the same was possible in Python:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;from class A&amp;quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;from class B&amp;quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;       &lt;span class=&quot;c&quot;&gt;# evaluates to &amp;quot;from class A&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# evaluates to True&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__class__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# the __class__ member determines inheritance&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;       &lt;span class=&quot;c&quot;&gt;# evaluates to &amp;quot;from class B&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# evaluates to True: obj&amp;#39;s &amp;#39;class&amp;#39; has changed!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;h3&gt;Methods?&lt;/h3&gt;

&lt;p&gt; These scripting languages also have flexible, and different,
definitions of &quot;methods&quot;.
&lt;/p&gt;

&lt;p&gt;
JavaScript simply does not have methods.  The syntax
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Binds &lt;code&gt;this&lt;/code&gt; to the value of &lt;code&gt;obj&lt;/code&gt; in the body of
&lt;code&gt;method&lt;/code&gt;.  However, the &lt;code&gt;method&lt;/code&gt; member is just a
function and can be easily extracted and applied:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Since &lt;code&gt;f()&lt;/code&gt; does not use the method call syntax above, it is
treated as a function call.  In this case, it is a well known JavaScript
wart that &lt;code&gt;this&lt;/code&gt; is bound to a default &quot;global object&quot; rather
than &lt;code&gt;obj&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt; Python and Ruby make a greater effort to retain a binding for the
&lt;code&gt;this&lt;/code&gt; parameter.  Python doesn't care about the name of the
parameter (though &lt;code&gt;self&lt;/code&gt; is canonically used), and simply has
special semantics for the first argument of a method.  If a method is
extracted via member access, it returns a function that binds the object
from the member access to the first parameter:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_in_init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_in_init&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myField&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;900&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self_in_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self_in_method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myField&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# the access binds self_in_method to obj&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;             &lt;span class=&quot;c&quot;&gt;# evaluates to 900, using the above binding&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
If the same method is accessed as a field multiple times, it isn't the same
function both times―a new function is created for each access:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# first extraction&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# second extraction&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;         &lt;span class=&quot;c&quot;&gt;# evaluates to False, no reference equality&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Python lets programmers access the underlying function without the first
parameter bound through the member &lt;code&gt;im_func&lt;/code&gt;.  This is
actually the same reference across all extracted methods, regardless of
even the original object of extraction:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# first extraction&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# second extraction&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;otherobj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# extraction from another object&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# evaluates to True, same function referenced from extractions on the&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# same object&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_func&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_func&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# evaluates to True, same function referenced from extractions on&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# different objects&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_func&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_func&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; Ruby has a similar treatment of methods, their extraction, and their
reapplication to new arguments.&lt;/p&gt;

&lt;h3&gt;But Why?&lt;/h3&gt;

&lt;p&gt; These features aren't just curiosities―we've found examples where
they are used in practice.  For example, Django's ORM builds classes
dynamically, modifying them based on strings that come from modules
describing database tables and relationships (&lt;a target=&quot;_blank&quot;
href=&quot;https://github.com/django/django/blob/de8cbd29beec8a3b1ef4b8b3cf5aa530883fa350/django/db/models/base.py#L157&quot;&gt;
base.py&lt;/a&gt;):
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;attr_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;_ptr&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_meta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;module_name&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OneToOneField&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;auto_created&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parent_link&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;new_class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_to_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attr_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Ruby on Rails' ActiveRecord uses dynamic field names as well, iterating
over fields and invoking methods only when their names match certain
patterns (&lt;a target=&quot;_blank&quot;
href=&quot;https://github.com/rails/rails/blob/acf7e86024fa8f7768f02a84688ae6e20c808c9c/activerecord/lib/active_record/attribute_assignment.rb#L78&quot;&gt;
base.rb&lt;/a&gt;):
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;multi_parameter_attributes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;elsif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_a?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;nested_parameter_attributes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;UnkownAttributeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;unknown attribute: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; These applications use objects as dictionaries (with inheritance) to
build up APIs that they couldn't otherwise.

&lt;p&gt;
These expressive features aren't
without their perils.  Django has &lt;a target=&quot;_blank&quot;
href=&quot;https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name&quot;&gt;
explicit warnings&lt;/a&gt; that things can go awry if relationships between
tables expressed in ORM classes overlap. And the fact that
&lt;code&gt;__proto__&lt;/code&gt; is in the same namespace as the other members bit
Google Docs, whose &lt;a target=&quot;_blank&quot;
href=&quot;http://groups.google.com/a/googleproductforums.com/forum/#!category-topic/docs/documents/0hQWeOvCcHU&quot;&gt;
editor would crash&lt;/a&gt; if the string &lt;code&gt;&quot;__proto__&quot;&lt;/code&gt; was
entered.  The implementation was using an object as a hashtable keyed by
strings from the document, which led to an assignment to
&lt;code&gt;__proto__&lt;/code&gt; that changed the behavior of the map.
&lt;/p&gt;

&lt;h3&gt;So?&lt;/h3&gt;

&lt;p&gt; The languages presented here are widely adopted and used, and run
critical systems.  Yet, they contain features that defy conventional
formal reasoning, at the very least in their object systems.  Perhaps
these features' expressiveness outweighs the cognitive load of using
them.  If it doesn't, and using these features is too difficult or
error-prone, we should build tools to help us use them, or find better
ways to implement the same functionality.  And if not, we should take
notice and recall that we have these powerful techniques at our
disposal in the next object system we design.  &lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>S5: Wat?</title>
   <link href="http://brownplt.github.com/2012/01/31/s5-wat.html"/>
   <updated>2012-01-31T00:00:00-08:00</updated>
   <id>http://brownplt.github.com/2012/01/31/s5-wat</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2012/01/31/s5-wat.html&quot;&gt;S5: Wat?&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 31 January 2012.&lt;/em&gt;

&lt;p&gt;&lt;a target=&quot;_blank&quot;
href=&quot;https://www.destroyallsoftware.com/talks/wat&quot;&gt;Gary Bernhardt's Wat
talk&lt;/a&gt; has been making a well-deserved round of the blogodome in the
past few weeks.  If you haven't seen it, go give it a watch (you can
count it as work time, since you saw it on the Brown PLT Blog, and we're
Serious Researchers).  The upshot of the second half of the talk is that
JavaScript has some less than expected behaviors.  We happen to have a
JavaScript implementation floating around &lt;a
href=&quot;http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html&quot;&gt;in the form of S5&lt;/a&gt;,
and like to claim that it handles the hairy corners of the language.  We
decided to throw Gary's examples at it.  &lt;/p&gt;

&lt;h3&gt;The Innocuous +&lt;/h3&gt;

&lt;p&gt;
Gary's first JavaScript example went like this:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;failbowl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:~&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jsc&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
 

&lt;p&gt; S5 lacks a true REPL―it simply takes JavaScript strings and produces
output and answers―so we started by approximating a little bit.  We
first tried a series of &lt;code&gt;print&lt;/code&gt; statements to see if we got
the same effect: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;cat&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;unit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arrays&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;js&lt;/span&gt; 
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{});&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;unit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;wat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arrays&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;js&lt;/span&gt; 

&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;WAT.&lt;/p&gt;

&lt;p&gt; Well, that doesn't seem good at all.  Only half of the answers are
right, and there's an &lt;code&gt;undefined&lt;/code&gt; at the end. What went
wrong?  It turns out the semantics of REPLs are to blame.  If we take
the four programs and run them on their own, we get something that looks
quite a bit better: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;[] + []&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;[] + {}&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;[object Object]&amp;quot;&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;{} + []&amp;quot;&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;{} + {}&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;nan&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
There are two issues here:

&lt;ol&gt;
&lt;li&gt;Why do &lt;code&gt;0.&lt;/code&gt; and &lt;code&gt;nan&lt;/code&gt;
print like that?&lt;/li&gt;
&lt;li&gt;Why did this work, when the previous
attempt didn't?&lt;/li&gt;
&lt;/ol&gt;

&lt;/p&gt;

&lt;p&gt; The answer to the first question is pretty straightforward:  under
the covers, S5 is using Ocaml floats and printing Ocaml values at the
end of its computation, and Ocaml makes slightly different decisions
than JavaScript in printing numbers.  We could change S5 to print
answers in JavaScript-printing mode, but the values themselves are the
right ones.  &lt;/p&gt;

&lt;p&gt; The second question is more interesting.  Why do we get such
different answers depending on whether we evaluate individual strings
versus printing the expressions?  The answer is in the semantics of
JavaScript REPLs.  When parsing a piece of JavaScript, the REPL needs to
make a choice.  Sensible decisions would be to treat each new JavaScript
string as a &lt;a target=&quot;_blank&quot;
href=&quot;http://es5.github.com/#A.4&quot;&gt;Statement&lt;/a&gt;, or as an entire
JavaScript &lt;a target=&quot;_blank&quot;
href=&quot;http://es5.github.com/#x14&quot;&gt;Program&lt;/a&gt;.  Most REPLs choose the
Program production.  &lt;/p&gt;

&lt;p&gt; The upshot is that the parsing of &lt;code&gt;{} + {}&lt;/code&gt; is quite
different from &lt;code&gt;[] + []&lt;/code&gt;.  With S5, it's trivial to print the
desugared representation and understand the difference.  When we parse
and desugar, we get very different results for &lt;code&gt;{} + {}&lt;/code&gt; and
&lt;code&gt;[] + []&lt;/code&gt;: &lt;/p&gt;

&lt;pre&gt;
$ ./s5-print &quot;{} + {}&quot;
{undefined;
 &lt;a target=&quot;_blank&quot;
href=&quot;https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/envs/es5.env#L4613&quot;&gt;%UnaryPlus&lt;/a&gt;({[#proto: %ObjectProto,
              #class: &quot;Object&quot;,
              #extensible: true,]
             })}

$ ./s5-print &quot;[] + []&quot;
&lt;a target=&quot;_blank&quot;
href='https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/envs/es5.env#L4057'&gt;%PrimAdd&lt;/a&gt;({
    [#proto: %ArrayProto,
     #class: &quot;Array&quot;,
     #extensible: true,]
    'length' : {#value 0., #writable true, #configurable false}
  },
  {
    [#proto: %ArrayProto,
     #class: &quot;Array&quot;,
     #extensible: true,]
    'length' : {#value 0., #writable true, #configurable false}
  }
)
&lt;/pre&gt;

&lt;p&gt; It is clear that &lt;code&gt;{} + {}&lt;/code&gt; parses as two statements (an
&lt;code&gt;undefined&lt;/code&gt; followed by a &lt;code&gt;UnaryPlus&lt;/code&gt;), and
&lt;code&gt;[] + []&lt;/code&gt; as a single statement containing a binary addition
expression.  What's happening is that in the Program production, for the
string &lt;code&gt;{} + {}&lt;/code&gt;, the first &lt;code&gt;{}&lt;/code&gt; is matched with
the &lt;a target=&quot;_blank&quot; href=&quot;http://es5.github.com/#x12.1&quot;&gt;Block&lt;/a&gt;
syntactic form, with no internal statements.  The rest of the expression
is parsed as a &lt;a target=&quot;_blank&quot;
href=&quot;http://es5.github.com/#x11.4&quot;&gt;UnaryExpression&lt;/a&gt;.  This is in
contrast to &lt;code&gt;[] + []&lt;/code&gt;, which only correctly parses as an
ExpressionStatement containing an &lt;a target=&quot;_blank&quot;
href=&quot;http://es5.github.com/#x11.6&quot;&gt;AdditiveExpression&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt; In the example where we used successive &lt;code&gt;print&lt;/code&gt;
statements, &lt;em&gt;every&lt;/em&gt; expression in the argument position to print
was parsed in the second way, hence the different answers.  The lesson?
When you're at a REPL, be it Firebug, Chrome, or the command line, make
sure the expression you're typing is what you think it is: not being
aware of this difference can make it even more difficult to know what to
expect!  &lt;/p&gt;

&lt;h3&gt;If You Can't Beat 'Em...&lt;/h3&gt;

&lt;p&gt; Our first example led us on an interesting excursion into parsing,
from which S5 emerged triumphant, correctly modelling the richness
and/or weirdness of the addition examples.  Next up, Gary showed some
straightforward uses of &lt;code&gt;Array.join()&lt;/code&gt;: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;failbowl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:~&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;master&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;jsc&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,,,,,,,,,,,,,,,,&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;wat&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;watwatwatwatwatwatwatwatwatwatwatwatwatwatwatwat&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;wat&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;wat&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot; Batman&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Batman&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; Our results look oh-so-promising, right up until the last line
(note: we call &lt;code&gt;String&lt;/code&gt; on the first case, because S5 doesn't
automatically &lt;code&gt;toString&lt;/code&gt; answers, which the REPL does).
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;String(Array(16))&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;,,,,,,,,,,,,,,,,&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Array(16).join(&amp;#39;wat&amp;#39;)&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;watwatwatwatwatwatwatwatwatwatwatwatwatwatwatwat&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Array(16).join(&amp;#39;wat&amp;#39; + 1)&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1wat1&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Array(16).join(&amp;#39;wat&amp;#39; - 1) + &amp;#39; Batman&amp;#39;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull Batman&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
WAT.
&lt;/p&gt;

&lt;p&gt;
Are we really that awful that we somehow yield &lt;code&gt;null&lt;/code&gt; rather
than &lt;code&gt;NaN&lt;/code&gt;?  A quick glance at the desugared code shows us
that we actually have the &lt;em&gt;constant value&lt;/em&gt; &lt;code&gt;null&lt;/code&gt; as
the argument to &lt;code&gt;join()&lt;/code&gt;.  How did that happen?
Interestingly, the following version of the program works:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;s5&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;var wat = &amp;#39;wat&amp;#39;; Array(16).join(wat - 1) + &amp;#39; Batman&amp;#39;;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batman&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; This leads us to our answer.  We use SpiderMonkey's very handy &lt;a
target=&quot;_blank&quot;
href=&quot;https://developer.mozilla.org/en/SpiderMonkey/Parser_API&quot;&gt;Parser
API&lt;/a&gt; as part of our toolchain. &lt;code&gt;Reflect.parse()&lt;/code&gt; takes
strings and converts them to JSON structures with rich AST information,
which we stringify and pass off to the innards of S5 to do desugaring
and evaluation.  &lt;code&gt;Reflect.parse()&lt;/code&gt; is part of a JavaScript
implementation that strives for performance, and to that end it performs
constant folding.  That is, as an optimization, when it sees the
expression &lt;code&gt;&quot;wat&quot; - 1&lt;/code&gt;, it automatically converts it to
&lt;code&gt;NaN&lt;/code&gt;.  All good so far.  &lt;/p&gt;

&lt;p&gt; The issue is that the &lt;code&gt;NaN&lt;/code&gt; yielded by constant folding
is not quite the same &lt;code&gt;NaN&lt;/code&gt; we might expect in JavaScript
programs.  In JavaScript, the &lt;em&gt;identifier&lt;/em&gt; &lt;code&gt;NaN&lt;/code&gt; is a
property of the global object &lt;a target=&quot;_blank&quot;
href=&quot;http://es5.github.com/#x15.1.1.1&quot;&gt;with the value
&lt;code&gt;NaN&lt;/code&gt;&lt;/a&gt;.  The Parser API can't safely fold to the
identifier &lt;code&gt;NaN&lt;/code&gt; (as was &lt;a target=&quot;blank&quot;
href=&quot;https://bugzilla.mozilla.org/show_bug.cgi?id=722087#c1&quot;&gt;pointed
out to us&lt;/a&gt; when we reported this bug), because it might be shadowed
in a different context.  Presumably to avoid this pitfall, the folding
yields a JSON structure that looks like: &lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Literal&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; But we can't sensibly use &lt;code&gt;JSON.stringify()&lt;/code&gt; on this
structure, because &lt;code&gt;NaN&lt;/code&gt; isn't valid JSON!  Any guesses on
what SpiderMonkey's JSON implementation turns &lt;code&gt;NaN&lt;/code&gt; into?  If
you guessed &lt;code&gt;null&lt;/code&gt;, we owe you a cookie.  &lt;/p&gt;

&lt;p&gt; We have designed a hack based on suggestions from the bug report to
get around this (passing a function to &lt;code&gt;stringify&lt;/code&gt; to look
for &lt;code&gt;NaN&lt;/code&gt;s and return a stylized object literal instead).
There's a bug open to make constant folding optional in
&lt;code&gt;Reflect.parse()&lt;/code&gt;, so this will be fixed in Mozilla's parser.
&lt;/p&gt;

&lt;p&gt;
Producing a working JavaScript implementation leads to a whole host of
exciting moments and surprising discoveries.  Building this semantics
and its desugaring gives us much more confidence that our tools say
something meaningful about real JavaScript programs.  These examples
show that getting perfect correspondence is difficult, but we strive to
be as close as possible.
&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Belay Lessons: Smarter Web Programming</title>
   <link href="http://brownplt.github.com/2011/12/18/resume-belay.html"/>
   <updated>2011-12-18T00:00:00-08:00</updated>
   <id>http://brownplt.github.com/2011/12/18/resume-belay</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2011/12/18/resume-belay.html&quot;&gt;Belay Lessons: Smarter Web Programming&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 18 December 2011.&lt;/em&gt;

&lt;p&gt;
&lt;em&gt;

This post comes from the keyboard of Matt Carroll, who has worked with us for
the past two years.  He's the main implementer of desugaring for &lt;a
href=&quot;http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html&quot;&gt;S5&lt;/a&gt;, and spent this
semester rebuilding and improving in-house Brown PLT web applications.  He
writes about his experience here.

&lt;/em&gt;
&lt;/p&gt;

&lt;p&gt;The Brown computer science department uses a home-grown web application
called Resume to conduct its
faculty recruitment process.  This semester, Joe and I re-wrote Resume with &lt;a
href='https://sites.google.com/site/belayresearchproject/'&gt;Belay&lt;/a&gt;.  Belay is
the product of Joe and Arjun's summer research at Google: it's an ongoing
inquiry into web development best practices, specifically concerning identity,
account management, and security.  From my perspective (that of a novice web
programmer), getting to grips with the Belay philosophy was a thought-provoking
experience, and a great education in the pitfalls that a web developer must
(unfortunately) bear in mind.&lt;/p&gt;

&lt;h3&gt;I Am Not My Cookies&lt;/h3&gt;

&lt;p&gt;Standard web applications make use of cookies for authentication.
When you visit a site and enter your credentials, the site's response sets a
session cookie in your browser. Subsequent requests to the site use the
information in the cookie to determine 'who you are' and whether 'you' are
allowed to do what 'your' request is trying to do.  I use quotations in the
prior sentence to highlight the fact that HTTP cookies are a poor method of
establishing user identity.  If another, malicious, web site you visit manages
to trick you into sending a request to the original site, that request will
contain your cookie, and the good site may treat that request as legitimate and
execute it.  This is the infamous &lt;a
href='http://en.wikipedia.org/wiki/Cross-site_request_forgery'&gt;cross-site
request forgery (CSRF)&lt;/a&gt; attack.&lt;/p&gt;

&lt;p&gt;Belay applications eschew the use of cookies, especially for
authentication, and thus they are secure by design against this type of
vulnerability.  This begs the question: without cookies, how do Belay
applications decide whether a request is authenticated?  The answer may shock
you (as it did me):  all requests that reach request handler code are treated
as legitimate.  At this point, we must examine the server-side of Belay apps in
greater detail.&lt;/p&gt;


&lt;h3&gt;Web Capabilities&lt;/h3&gt;

&lt;p&gt;Your everyday possibly-CSRF-vulnerable site probably has a URL scheme with
well-known endpoints that lead directly to application functionality.  For
example, to post to your blog, you (typically via your browser) send a POST
request to &lt;code&gt;www.blog.com/post&lt;/code&gt; with your cookies and the
blog body's text.  The server-side handler finds your account in the database using
your cookie, checks that your account can post to that blog, and adds a new
post.  If the whole surface of the site's URL space is well-known, a CSRF-ing
attacker can excercise the entirety of a user's view of the site with one
compromised cookie.&lt;/p&gt;

&lt;p&gt;In contrast, Belay applications have few well-known URLs, corresponding to
the public entry points to the site (the login and contact pages, for
instance).  Instead, Belay's libraries allow server-side code to dynamically
generate random unique URLs and map them to request handler functions.  Each of
these handlers services a particular type of request for a particular set of
data.  The randomly generated &quot;capability&quot; urls are embedded in the JavaScript
or markup returned to the browser.  In a well-designed Belay application, each
page has the minimum necessary set of capabilities to carry out its mission,
and the capabilities are scoped to the minimum set of data with which they need
concern themselves.   After you successfully log in to a Belay site, the
response will contain the set of capabilities needed by the page, and scoped to
only that data which is needed by the page's functionality &lt;em&gt;and&lt;/em&gt;
associated with your user account.  No cookies are necessary to identify you as
a user or to authenticate your requests.&lt;/p&gt;

&lt;p&gt;A Belay app uses its limited URL scheme as its primary security mechanism,
ignoring requests unless they come along trusted capability URLs created by a
prior, explicit grant. As long as we can rely on our platform's ability to
generate unguessable large random numbers, attackers are out of luck.  And,
even if a capability URL is leaked from its page, it is scoped to only a small
set of data on the server, so the vulnerability is limited.  This is a
much-improved situation compared to a site using cookie-based
authentication---leaking a cookie leaks access to the user's entire view of the
site.&lt;/p&gt;

&lt;h3&gt;Grants and Cap Handlers&lt;/h3&gt;

&lt;p&gt;Here's a Belay request handler, taken from Resume:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GetLetterHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bcap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CapHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_letter_filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;letter&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
 

&lt;p&gt;This handler simply looks up the filename associated with a reference and
returns it (using a few helper functions).  Accessing a letter written by an
applicant's reference is quite a sensitive operation---letting the wrong
person access a letter would be a serious security bug.  Yet,
&lt;code&gt;GetLetterHandler&lt;/code&gt; is a two-liner with no apparent security checks
or guards.  How can this be safe?
&lt;/p&gt;

&lt;p&gt;To answer this, we need to consider how a client can cause
&lt;code&gt;GetLetterHandler&lt;/code&gt; to be invoked.  The Belay server library will
only invoke this handler via capability URLs created with a &lt;code&gt;grant&lt;/code&gt;
to &lt;code&gt;GetLetterHandler&lt;/code&gt;.  So, we can search the codebase for code
that granted such access.  A quick search shows one spot:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GetApplicantsHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bcap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CapHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reviewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;applicants_json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applicant&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reviewer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_applicants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; 
      &lt;span class=&quot;c&quot;&gt;# ... some processing&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;refs_json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;applicants&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_references&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;refs_json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&amp;#39;refName&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
          &lt;span class=&quot;s&quot;&gt;&amp;#39;getLetter&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bcap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetLetterHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ... add some things to applicants_json&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bcap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bcapResponse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applicants_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; When &lt;code&gt;GetApplicantsHandler&lt;/code&gt; is invoked, it will return a
structure that, for each applicant, shows something like:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Theodore Roosevelt&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;getLetter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;&amp;#39;https://resume.cs.brown.edu/cap/f7327056-4b91-ad57-e5e4f6c514b6&amp;#39;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; On the server, the string
&lt;code&gt;f7327056-4b91-ad57-e5e4f6c514b6&lt;/code&gt; was created and mapped to
the pair of &lt;code&gt;GetLetterHandler&lt;/code&gt; and the &lt;code&gt;Reference&lt;/code&gt;
database item for Theodore Roosevelt.  A GET request to the URL above will
return the reference letter.  Note a nice feature of this setup: the server
doesn't use any information from the client, other than the capability URL, to
decide &lt;em&gt;which&lt;/em&gt; reference's letter to return.  Thus, a client cannot try
providing different id's or other parameters to explore which letters they
have access to.  Only those explicitly granted are accessible.  &lt;/p&gt;

&lt;p&gt; Poking around in the codebase more, we can see that
&lt;code&gt;GetApplicantsHandler&lt;/code&gt; is only granted to reviewers, who can only
create accounts via an email from the administrator.  This reasoning is how we
convince ourselves, as developers, that we haven't screwed up and given away
the ability to see a letter to the wrong user.  We do all of this without
worrying about a check on accessing the letter, instead relying on the
unguessability of the URLs generated by &lt;code&gt;grant&lt;/code&gt; to enforce our
access restrictions.  &lt;/p&gt;

&lt;p&gt;This may seem like a new-concept overload, and indeed, I had that exact
reaction at first.  Over time I gained familiarity with the Belay style, and I
became more and more convinced by the benefits it offers.  Porting Resume
became a fairly straightforward process of identifying each server-side request
handler, converting it to a Belay handler, and ensuring that whatever pages
needed that functionality received grants to call the handler.  There were
wrinkles, many due to the fact that Resume also uses &lt;a
href='http://www.flapjax-lang.org/'&gt;Flapjax&lt;/a&gt; (a language/library for
reactive programming in the browser).  Flapjax is another Brown PLT product and
it is certainly worthy of its own blog post.  We had to account for the
interaction between Belay's client-side library and Flapjax.&lt;/p&gt;

&lt;p&gt;  Note that Belay isn't the first place these ideas have surfaced.  Belay
builds on foundational research:  &lt;a
href='http://waterken.sourceforge.net/'&gt;Waterken&lt;/a&gt; and &lt;a
href='http://cs.brown.edu/~sk/Publications/Papers/Published/khmgpf-impl-use-plt-web-server-journal/'&gt;PLT
Web Server&lt;/a&gt; both support cookie-less, capability-based web interactions.
The Belay project addresses broader goals in identity management and sharing
on the web, but we've leveraged its libraries to build a more robust system
for ourselves.

&lt;p&gt;At the end, the benefits of the redesigned Resume are numerous.  Cookies are
no longer involved.  JavaScript code doesn't know or care about unique IDs for
picking items out of the database.  Random HTTP request probes result in a 404
response and a line in the server's log, instead of a possible data corruption.
You can open as many tabs as you like, with each one logged into its own Resume
account, and experience no unwanted interference.  We were able to realize
these improvements while re-using a significant portion of the original Resume
code, unchanged.&lt;/p&gt;

&lt;p&gt;After my experience with the Resume port, I'm certainly a Belay fan. The
project has more to say about topics such as cross-site authorization, sharing,
and multi-site identity management, so check out their site and stay tuned for
future updates:&lt;/p&gt;

&lt;a href='https://sites.google.com/site/belayresearchproject/'&gt;Belay
Research&lt;/a&gt;

</content>
 </entry>
 
 <entry>
   <title>S5: Semantics for Accessors</title>
   <link href="http://brownplt.github.com/2011/12/11/getters-and-setters.html"/>
   <updated>2011-12-11T00:00:00-08:00</updated>
   <id>http://brownplt.github.com/2011/12/11/getters-and-setters</id>
   <content type="html">&lt;h2&gt;&lt;a href='http://brownplt.github.com/2011/12/11/getters-and-setters.html'&gt;S5: Semantics for Accessors&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 11 December 2011.&lt;/em&gt;

&lt;style&gt;
.semantic {
  font-family: DejaVu Sans;
  font-size: small;
}
&lt;/style&gt;

&lt;p&gt; Getters and setters (known as accessors) are a new feature in ECMAScript 5
that extend the behavior of assignment and lookup expressions on JavaScript
objects.  If a field has a &lt;em&gt;getter&lt;/em&gt; defined on it, rather than simply
returning the value in field lookup, a getter function is invoked, and its
return value is the result of the lookup:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;timesGotten&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;timesGotten&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// calls the function above, evaluates to 22&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;timesGotten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// is now 1, due to the increment in the getter&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// calls the function above, still evaluates to 22&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;timesGotten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// is now 2, due to another increment in the getter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Similarly, if a field has a &lt;em&gt;setter&lt;/em&gt; defined on it, the setter
function is called on field update.  The setter function gets the assigned
value as its only argument, and its return value is ignored:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// calls the function above (with v=37)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// evaluates to 37&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;// evaluates to undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; Getters and setters have a number of proposed uses―they can be used to wrap
DOM objects that have interesting effects on assignment, like
&lt;code&gt;onmessage&lt;/code&gt; and &lt;code&gt;onbeforeunload&lt;/code&gt;, for example.  We leave
discovering good uses to more creative JavaScript programmers, and focus on
their semantic properties here.  &lt;/p&gt;

&lt;p&gt; The examples above are straightforward, and it seems like a simple model
might work out quite easily.  First, we need some definitions, so we'll start
with what's in λ&lt;sub&gt;JS&lt;/sub&gt;.  Here's a fragment of the values that
λ&lt;sub&gt;JS&lt;/sub&gt; works with, and the most basic of the operations on objects:

&lt;pre style='font-family:DejaVu Sans;'&gt;
v := str  | { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;:v&lt;sub&gt;n&lt;/sub&gt; } | func(x ⋯) . e | ⋯
e := e[e] | e[e=e] | e(e, ⋯) | ⋯

(E-Lookup)
  { ⋯, str:v, ⋯ }[str&lt;sub&gt;x&lt;/sub&gt;] → v
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-Update)
  { ⋯, str:v, ⋯}[str&lt;sub&gt;x&lt;/sub&gt;=v'] → { ⋯, str:v', ⋯}
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-UpdateAdd)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯}[str=v] → { str:v, str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯}
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯
&lt;/pre&gt;

&lt;p&gt; We update and set fields when they are found, and add fields if there is an
update on a not-found field.  Clearly, this isn't enough to model the semantics
of getters and setters.  On lookup, if the value of a field is a getter, we
need to have our semantics step to an invocation of the function.  We need to
make the notion of a field richer, so the semantics can have behavior that
depends on the kind of field.  We distinguish two kinds of fields &lt;span
class='semantic'&gt;p&lt;/span&gt;, one for simple values and one for
accessors: &lt;/p&gt;

&lt;pre class='semantic'&gt;
&lt;b&gt;p := [get: v&lt;sub&gt;g&lt;/sub&gt;, set: v&lt;sub&gt;s&lt;/sub&gt;] | [value: v]&lt;/b&gt;
v := str  | { str&lt;sub&gt;1&lt;/sub&gt;:&lt;b&gt;p&lt;sub&gt;1&lt;/sub&gt;&lt;/b&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;:&lt;b&gt;p&lt;sub&gt;n&lt;/sub&gt;&lt;/b&gt; } | func(x ⋯) . e | ⋯
e := e[e] | e[e=e] | e(e, ⋯) | ⋯
&lt;/pre&gt;

&lt;p&gt;
The updated rules for simple values are trivial to write down (differences in bold):
&lt;/p&gt;

&lt;pre class='semantic'&gt;
(E-Lookup)
  { ⋯, str:&lt;b&gt;[value:v]&lt;/b&gt;, ⋯ }[str&lt;sub&gt;x&lt;/sub&gt;] → v
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-Update)
  { ⋯, str:&lt;b&gt;[value:v]&lt;/b&gt;, ⋯}[str&lt;sub&gt;x&lt;/sub&gt;=v'] → { ⋯, str:&lt;b&gt;[value:v']&lt;/b&gt;, ⋯}
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-UpdateAdd)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯}[str=v] → { str:&lt;b&gt;[value:v]&lt;/b&gt;, str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯}
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯
&lt;/pre&gt;

&lt;p&gt;
But now we can also handle the cases where we have a getter or setter.  If a
lookup expression &lt;span class='semantic'&gt;e[e]&lt;/span&gt; finds a getter, it applies
the function, and the same goes for setters, which get the value as an argument:
&lt;/p&gt;

&lt;pre class='semantic'&gt;
(E-LookupGetter)
  { ⋯, str:&lt;b&gt;[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;]&lt;/b&gt;, ⋯ }[str&lt;sub&gt;x&lt;/sub&gt;] → &lt;b&gt;v&lt;sub&gt;g&lt;/sub&gt;()&lt;/b&gt;
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-UpdateSetter)
  { ⋯, str:&lt;b&gt;[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;]&lt;/b&gt;, ⋯}[str&lt;sub&gt;x&lt;/sub&gt;=v'] → &lt;b&gt;v&lt;sub&gt;s&lt;/sub&gt;(v')&lt;/b&gt;
  when str&lt;sub&gt;x&lt;/sub&gt; = str
&lt;/pre&gt;

&lt;p&gt;
Great!  This can handle the two examples from the
beginning of the post.  But those two examples weren't the whole story for
getters and setters, and our first fragment wasn't the whole story for
λ&lt;sub&gt;JS&lt;/sub&gt; objects.
&lt;/p&gt;

&lt;p&gt;
Consider this program:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// calls the set function above (with v=5)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// evaluates to 10, because of assignment in the setter&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// evaluates to 11, because of addition in the getter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
Here, we see that the functions also have access to the target object of the
assignment or lookup, via the &lt;code&gt;this&lt;/code&gt; parameter.  We could try to
encode this into our rules, but let's not get too far ahead of ourselves.
JavaScript objects have more subtleties up their sleeves.  We can't forget
about prototype inheritance.  Let's start with the same object &lt;code&gt;o&lt;/code&gt;,
this time called &lt;code&gt;parent&lt;/code&gt;, and use it as the prototype of another
object:
&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Sets... what exactly to 10?&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// ??? &lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// ??? &lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// ??? &lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;// ??? &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt; Take a minute to guess what you think each of the values should be.  &lt;a
id='showanswers' href=&quot;javascript://Answers&quot;&gt;Click here&lt;/a&gt; to see the answers
(which hopefully are what you expected).  &lt;/p&gt;

&lt;script&gt;
document.getElementById('showanswers').addEventListener('click', function() {
  document.getElementById('answers').style.display = 'block';
});  
&lt;/script&gt;

&lt;div style='height:7em;padding:0;margin:0;'&gt;
&lt;div id='answers' style='display:none;'&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;  &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// undefined&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 10&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// undefined&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt; So, JavaScript is passing the object &lt;em&gt;in the lookup expression&lt;/em&gt; into
the function, for both field access and field update.  Something else subtle is
going on, as well.  Recall that before, when an update occurred on a field that
wasn't present, JavaScript simply added it to the object.  Now, on field
update, we see that the assignment traverses the prototype chain to check for
setters.  This is fundamentally different from JavaScript before
accessors―assignment never considered prototypes.  So, our semantics needs to
do two things: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass the correct &lt;code&gt;this&lt;/code&gt; argument to getters and setters;&lt;/li&gt;
&lt;li&gt;Traverse the prototype chain for assignments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Let's think about a simple way to pass the &lt;code&gt;this&lt;/code&gt; argument to
getters:
&lt;/p&gt;

&lt;pre class='semantic'&gt;
(E-LookupGetter)
  { ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯ }[str&lt;sub&gt;x&lt;/sub&gt;] → v&lt;sub&gt;g&lt;/sub&gt;(&lt;b&gt;{ ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯ }&lt;/b&gt;)
  when str&lt;sub&gt;x&lt;/sub&gt; = str
&lt;/pre&gt;

&lt;p&gt; Here, we simply copy the object over into the first argument to the
function &lt;span class='semantic'&gt;v&lt;sub&gt;g&lt;/sub&gt;&lt;/span&gt;.  We can (and do) desugar
functions to have an implicit first &lt;code&gt;this&lt;/code&gt; argument to line up with
this invocation.  But we need to think carefully about this rule's interaction
with prototype inheritance.  &lt;/p&gt;

&lt;p&gt;
Here is &lt;span class='semantic'&gt;E-Lookup-Proto&lt;/span&gt; from the original
λ&lt;sub&gt;JS&lt;/sub&gt;:

&lt;/p&gt;
&lt;pre class='semantic'&gt;
(E-Lookup-Proto)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯, &quot;__proto__&quot;: v&lt;sub&gt;p&lt;/sub&gt;, str&lt;sub&gt;n&lt;/sub&gt;:v&lt;sub&gt;n&lt;/sub&gt;, ⋯}[str] → v&lt;sub&gt;p&lt;/sub&gt;[str]
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;, ⋯
&lt;/pre&gt;

&lt;p&gt; Let's take a moment to look at this rule in conjunction with &lt;span
class='semantic'&gt;E-LookupGetter&lt;/span&gt;.  If the field isn't found, and
__proto__ is present, it looks up the __proto__ field and performs the same
lookup on that object (we are eliding the case where proto is not present or
not an object for this presentation).  But note something crucial: the
expression on the right hand side drops &lt;em&gt;everything&lt;/em&gt; about the original
object except its prototype.  If we applied this rule to &lt;code&gt;child&lt;/code&gt;
above, the getter rule would pass &lt;code&gt;parent&lt;/code&gt; to the getter instead of
&lt;code&gt;child&lt;/code&gt;!  &lt;/p&gt;

&lt;p&gt;  The solution is to keep track of the original object as we traverse the
prototype chain.  If we don't, the reduction relation simply won't have the
information it needs to pass in to the getter or setter when it reaches the
right point in the chain.  This is a deep change―we need to modify our
expressions to get it right:&lt;/p&gt;

&lt;style&gt;
.new-outlined {
  font-weight: bold;
  border: 1px solid red;
}
&lt;/style&gt;

&lt;pre class='semantic'&gt;
p := [get: v&lt;sub&gt;g&lt;/sub&gt;, set: v&lt;sub&gt;s&lt;/sub&gt;] | [value: v]
v := str  | { str&lt;sub&gt;1&lt;/sub&gt;:p&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;:p&lt;sub&gt;n&lt;/sub&gt; } | func(x ⋯) . e | ⋯
e := e[e] | e[e=e] | e&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[e] | e&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[e=e] | e(e, ⋯) | ⋯
&lt;/pre&gt;

&lt;p&gt; And now, when we do a prototype lookup, we can keep track of the same
&lt;code&gt;this&lt;/code&gt; argument (written as &lt;span class='semantic'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/span&gt;)
the whole way up the chain, and the rules for getters and setters can use this
new piece of the expression: &lt;/p&gt;

&lt;pre class='semantic'&gt;
(E-Lookup-Proto)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯, &quot;__proto__&quot;: v&lt;sub&gt;p&lt;/sub&gt;, str&lt;sub&gt;n&lt;/sub&gt;:v&lt;sub&gt;n&lt;/sub&gt;, ⋯}&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str] → v&lt;sub&gt;p&lt;/sub&gt;&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str]
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;, ⋯

(E-LookupGetter)
  { ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯ }&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str&lt;sub&gt;x&lt;/sub&gt;] → v&lt;sub&gt;g&lt;/sub&gt;(&lt;span class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/span&gt;)
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-UpdateSetter)
  { ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯}&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str&lt;sub&gt;x&lt;/sub&gt;=v'] → v&lt;sub&gt;s&lt;/sub&gt;(&lt;span class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/span&gt;,v')
  when str&lt;sub&gt;x&lt;/sub&gt; = str
&lt;/pre&gt;

&lt;p&gt; This idea was inspired by Di Gianantonio, Honsell, and Liquori's 1998 &lt;a
href='http://dl.acm.org/citation.cfm?id=286955'&gt;paper&lt;/a&gt;, &lt;em&gt;A lambda
calculus of objects with self-inflicted extension&lt;/em&gt;.  They use a similar
encoding to model method dispatches in a small prototype-based object calculus.
The original expressions, &lt;span class='semantic'&gt;e[e]&lt;/span&gt; and &lt;span
class='semantic'&gt;e[e=e]&lt;/span&gt;, simply copy values into the new positions once
the subexpressions have reduced to values: &lt;/p&gt;

&lt;pre class='semantic'&gt;
(E-Lookup)
  v[str] → v&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[str]

(E-Update)
  v[str=v'] → v&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[str=v']
&lt;/pre&gt;

&lt;p&gt;
The final set of evaluation rules and expressions is a little larger:
&lt;/p&gt;

&lt;pre class='semantic'&gt;
p := [get: v&lt;sub&gt;g&lt;/sub&gt;, set: v&lt;sub&gt;s&lt;/sub&gt;] | [value: v]
v := str  | { str&lt;sub&gt;1&lt;/sub&gt;:p&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;:p&lt;sub&gt;n&lt;/sub&gt; } | func(x ⋯) . e | ⋯
e := e[e] | e[e=e] | e&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[e] | e&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[e=e] | e(e, ⋯) | ⋯

(E-Lookup)
  v[str] → v&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[str]

(E-Update)
  v[str=v'] → v&lt;sup class='new-outlined'&gt;v&lt;/sup&gt;[str=v']

(E-LookupGetter)
  { ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯ }&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str&lt;sub&gt;x&lt;/sub&gt;] → v&lt;sub&gt;g&lt;/sub&gt;(&lt;span class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/span&gt;)
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-Lookup-Proto)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯, &quot;__proto__&quot;: v&lt;sub&gt;p&lt;/sub&gt;, str&lt;sub&gt;n&lt;/sub&gt;:v&lt;sub&gt;n&lt;/sub&gt;, ⋯}&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str] → v&lt;sub&gt;p&lt;/sub&gt;&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str]
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;, ⋯

(E-UpdateSetter)
  { ⋯, str:[get:v&lt;sub&gt;g&lt;/sub&gt;, set:v&lt;sub&gt;s&lt;/sub&gt;], ⋯}&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str&lt;sub&gt;x&lt;/sub&gt;=v'] → v&lt;sub&gt;s&lt;/sub&gt;(&lt;span class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/span&gt;,v')
  when str&lt;sub&gt;x&lt;/sub&gt; = str

(E-Update-Proto)
  { str&lt;sub&gt;1&lt;/sub&gt;:v&lt;sub&gt;1&lt;/sub&gt;, ⋯, &quot;__proto__&quot;: v&lt;sub&gt;p&lt;/sub&gt;, str&lt;sub&gt;n&lt;/sub&gt;:v&lt;sub&gt;n&lt;/sub&gt;, ⋯}&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str=v'] → v&lt;sub&gt;p&lt;/sub&gt;&lt;sup class='new-outlined'&gt;v&lt;sub&gt;t&lt;/sub&gt;&lt;/sup&gt;[str=v']
  when str ≠ str&lt;sub&gt;1&lt;/sub&gt;, ⋯, str&lt;sub&gt;n&lt;/sub&gt;, ⋯
&lt;/pre&gt;

&lt;p&gt; This is most of the rules―we've elided some details to only present the
key insight behind the new ones.  Our full semantics (discussed in our &lt;a
href='http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html'&gt;last post&lt;/a&gt;), handles the
details of the &lt;code&gt;arguments&lt;/code&gt; object that is implicitly available
within getters and setters, and using built-ins, like
&lt;code&gt;defineProperty&lt;/code&gt;, to add already-defined functions to existing
objects as getters and setters.  &lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>S5: A Semantics for Today's JavaScript</title>
   <link href="http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html"/>
   <updated>2011-11-11T00:00:00-08:00</updated>
   <id>http://brownplt.github.com/2011/11/11/s5-javascript-semantics</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html&quot;&gt;S5: A Semantics for Today's JavaScript&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 11 November 2011.&lt;/em&gt;

&lt;p&gt; The JavaScript language isn't static―the ECMAScript committee is working
hard to improve the language, and browsers are implementing features both in
and outside the spec, making it difficult to understand just what &quot;JavaScript&quot;
means at any point in time.  Existing implementations aren't much help―their
goal is to serve pages well and fast.  We need a JavaScript architecture that
can help us make sense of the upcoming (and existing!) features of the
language.  &lt;/p&gt;

&lt;p&gt; To this end, we've developed &lt;a
href=&quot;http://www.github.com/brownplt/LambdaS5&quot;&gt;S5&lt;/a&gt;, an ECMAScript 5
runtime, built on λ&lt;sub&gt;JS&lt;/sub&gt;, with the explicit goal of helping people
understand and tinker with the language.  We built it to understand the
features in the new standard, building on our &lt;a
href=&quot;http://brownplt.github.com/2011/09/29/js-essence.html&quot;&gt;previous
efforts&lt;/a&gt; for the older standard.  We've now begun building analyses for this
semantics, and are learning more about it as we do so.  We're making it
available with the hope that you can join us in playing with ES5,
extending it with new features, and building tools for it.&lt;/p&gt;

&lt;p&gt; S5 implements the core features of ES5 strict mode.  How do we know
this?  We've tested S5 against &lt;a
href=&quot;http://test262.ecmascript.org/&quot;&gt;Test262&lt;/a&gt; to measure our progress.  We
are, of course, not feature complete, but we're happy with our progress, which
you can check out &lt;a
href=&quot;http://www.cs.brown.edu/~joe/public/s5-tests/summary.html&quot;&gt;here&lt;/a&gt;.


&lt;h3&gt; A Malleable Implementation &lt;/h3&gt;

&lt;p&gt; The semantics of S5 is designed to be two things: a language for writing
down the algorithms of the specification, and a translation target for
JavaScript programs.  We've implemented an &lt;a
href='https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/src/ljs/ljs_eval.ml'&gt;interpreter&lt;/a&gt;
for S5, and a &lt;a
href=&quot;https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/src/exprjs/exprjs_to_ljs.ml&quot;&gt;&lt;em&gt;desugaring function&lt;/em&gt;&lt;/a&gt;
that translates JavaScript source into S5 programs.&lt;/p&gt;

&lt;p&gt; We have a number of choices to make in defining desugaring.  The ECMAScript
standard defines a whole host of auxiliary functions and library routines that
we must model.  Putting these implementations directly in the desugaring
function would work, but would make desugaring unnecessary brittle, and require
recompilation on every minor change.  Instead, we implement the bulk of this
functionality as an S5 program.  The majority of our work happens in &lt;a
href=&quot;https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/envs/es5.env&quot;&gt;an
environment file&lt;/a&gt; that defines the spec in S5 itself.  The desugaring
defines a translation from the syntactic forms of JavaScript to the (smaller)
language of S5, filled with calls into the functions defined in this
environment.&lt;/p&gt;

&lt;p&gt; This separation of concerns is what makes our implementation so amenable to
exploration.  If you want to try something out, you can edit the environment
file and rerun whatever tests you care to learn about.  Want to try a different
implementation of the &lt;code&gt;==&lt;/code&gt; operator?  Just change the definition, as
it was pulled from the spec, at &lt;a
href=&quot;https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/envs/es5.env#L300&quot;&gt;line
300&lt;/a&gt;.  Want a more expressive &lt;code&gt;Object.toString()&lt;/code&gt; that doesn't
just print &lt;code&gt;&quot;[object Object]&quot;&lt;/code&gt;?  That's &lt;a
href=&quot;https://github.com/brownplt/LambdaS5/blob/a20d9885ca8277af81029982e5a5c0d903600b31/envs/es5.env#L990&quot;&gt;right
here&lt;/a&gt;.  No changing an interpreter, no recompiling a desugaring function
necessary.  &lt;/p&gt;

&lt;p&gt; The environment we've written reflects the standard's algorithms as we
understand them in terms of S5.  The desugaring from JavaScript to S5 code with
calls into this library is informed by the specification's definitions of
expression and statement evaluation.  We have confidence in the combination of
desugaring and library implementation, given our increasing test coverage.
Further, we know how to continue―implement more of the spec and pass more test
cases!  &lt;/p&gt;


&lt;h3&gt; More than λ&lt;sub&gt;JS&lt;/sub&gt; &lt;/h3&gt;

&lt;p&gt; S5 is built on λ&lt;sub&gt;JS&lt;/sub&gt;, but extends it in three significant ways:

&lt;ul&gt;
&lt;li&gt; Explicit getters and setters; &lt;/li&gt;
&lt;li&gt; Object fields with attributes, like &lt;code&gt;writable&lt;/code&gt; and &lt;code&gt;configurable&lt;/code&gt;, built-in; &lt;/li&gt;
&lt;li&gt; Support for &lt;code&gt;eval().&lt;/code&gt;
&lt;/ul&gt;

For those that haven't fiddled with getters and setters, they are a new feature
introduced in ECMAScript 5 that allow programmer-defined behavior on property
access and assignment.  Getters and setters fundamentally change how property
access and assignment work.  They make property assignment interact with the
prototype chain, which used to not be the case, and cause syntactically similar
expressions to behave quite differently at runtime.  In a separate post we'll
discuss the interesting problems they introduce for desugaring and how we
implement them in the semantics. (&lt;b&gt;Update&lt;/b&gt;: This post has been written,
&lt;a href='http://brownplt.github.com/2011/12/11/getters-and-setters.html'&gt;check it out&lt;/a&gt;!)

&lt;/p&gt;

&lt;p&gt; Attributes on objects weren't treated directly in the original
λ&lt;sub&gt;JS&lt;/sub&gt;.
In 5th Edition, they are crucial to several security-relevant operations on
objects. For example, the standard specifies &lt;code&gt;Object.freeze()&lt;/code&gt;,
which makes an object's properties forever unwritable.  S5 directly models the
&lt;code&gt;writable&lt;/code&gt; and &lt;code&gt;configurable&lt;/code&gt; attributes that make this
operation possible, and make its implementation in S5 easy to understand.
&lt;/p&gt;

&lt;p&gt;

λ&lt;sub&gt;JS&lt;/sub&gt; explicitly elided &lt;code&gt;eval()&lt;/code&gt; from its semantics.  S5
implements &lt;code&gt;eval()&lt;/code&gt; by performing desugaring &lt;em&gt;within the
interpreter&lt;/em&gt; and then interpreting the desugared code.  We implement only
the strict mode version of &lt;code&gt;eval&lt;/code&gt;, which restricts the environment
that the &lt;code&gt;eval&lt;/code&gt;'d code can affect.  With these restrictions, we can
implement &lt;code&gt;eval&lt;/code&gt; in a straightforward way within our interpreter.
We'll cover the details of how we do this, and why it works, in another post.

&lt;/p&gt;


&lt;h3&gt;Building on S5&lt;/h3&gt;

There's a ton we can do with S5.  More, in fact, than we can do by ourselves:

&lt;ul&gt;


&lt;li&gt;

&lt;b&gt;Experiment with Harmony features&lt;/b&gt;:  ECMAScript 6, or Harmony, as it is
often called, is being designed &lt;em&gt;right now&lt;/em&gt;.  Proxies, string
interpolation, syntactic sugar for classes, and modules are just a few of the
upcoming features.  Modeling them in S5 would help us understand these
features better as they get integrated into the language.

&lt;/li&gt;
&lt;li&gt;

&lt;b&gt;Build Verification Tools&lt;/b&gt;:  Verification based on objects' attributes is an
interesting research problem―what can we prove about interacting programs if
we know about unwritable fields and inextensible objects?  Building this
knowledge into a type-checker or a program analysis could give interesting new
guarantees.

&lt;/li&gt;
&lt;li&gt;

&lt;b&gt;Abstract Our Machine&lt;/b&gt;:  Matt Might and David van Horn wrote about
abstracting λ&lt;sub&gt;JS&lt;/sub&gt; for program analysis.  We've added new constructs
to the language since then.  Do they make abstraction any harder?

&lt;/li&gt;

&lt;li&gt;

&lt;b&gt;Complete the Implementation&lt;/b&gt;: We've made a lot of progress, but there's
still more ground to cover.  We need support for more language features,
like JSON and regular expressions, that would move our implementation along
immensely.  We'll work on this more, but anyone who wants to get involved is
welcome to help.

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;
If any of this sounds interesting, or if you're just curious, go ahead and
check out S5!  It's open source and lives in a &lt;a
href=&quot;https://github.com/brownplt/LambdaS5&quot;&gt;Github repository&lt;/a&gt;. Let us
know what you do with it!
&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>The Essence of JavaScript</title>
   <link href="http://brownplt.github.com/2011/09/29/js-essence.html"/>
   <updated>2011-09-29T00:00:00-07:00</updated>
   <id>http://brownplt.github.com/2011/09/29/js-essence</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2011/09/29/js-essence.html&quot;&gt;The Essence of JavaScript&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 29 September 2011.&lt;/em&gt;

&lt;p&gt; Back in 2008, the group decided to really understand JavaScript.  Arjun had
built a &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/gkj-stat-anal-ajax-id/&quot;&gt;static
analysis for JavaScript&lt;/a&gt; from scratch.  Being the honest chap that he is, he
was forced to put the following caveat into the paper:&lt;/p&gt;

&lt;blockquote&gt;
&quot;We would like to formally prove that our analysis is sound.  A
sound analysis would guarantee that our tool will never raise a false alarm,
an imporant usability concern.  However, a proof of soundness would require a
formal semantics for JavaScript and the DOM in browsers, and this does not
exist.&quot;
&lt;/blockquote&gt;

&lt;p&gt;A &quot;formal semantics for JavaScript [...] does not exist&quot;?  Didn't he know
about the  official documents on such matters, the ECMAScript standard?
ECMAScript 3rd edition, the standard at the time, was around 180 pages long,
written in prose and pseudocode.  Reading it didn't help much.  It includes
gems such as this description of the &lt;code&gt;switch&lt;/code&gt; statement: &lt;/p&gt;

&lt;blockquote&gt;
&lt;pre style=&quot;font-size: 8pt&quot;&gt;
1.  Let A be the list of CaseClause items in the first
    CaseClauses, in source text order.
2.  For the next CaseClause in A, evaluate CaseClause. If there is
    no such CaseClause, go to step 7.
3.  If input is not equal to Result(2), as defined by the !== 
    operator, go to step 2.
4.  Evaluate the StatementList of this CaseClause.
5.  If Result(4) is an abrupt completion then return Result(4).
6.  Go to step 13.
7.  Let B be the list of CaseClause items in the second
    CaseClauses, in source text order.
8.  For the next CaseClause in B, evaluate CaseClause. If there is
    no such CaseClause, go to step 15.
9.  If input is not equal to Result(8), as defined by the !== 
    operator, go to step 8.
10. Evaluate the StatementList of this CaseClause.
11. If Result(10) is an abrupt completion then return Result(10).
12. Go to step 18.
...
&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt; And this is just one of 180 pages of lesser or greater eloquence.  With
&lt;em&gt;this&lt;/em&gt; as his formal reference, it's no wonder Arjun had a hard time
making soundness claims.&lt;/p&gt;

&lt;p&gt; Around the same time, Ankur Taly, Sergio Maffeis, and John Mitchell noticed
the same problem. They presented a formal semantics for JavaScript in their &lt;a
href=&quot;http://www-cs-students.stanford.edu/~ataly/Papers/aplas08.pdf&quot;&gt;APLAS 2008
paper&lt;/a&gt;.  You can find their semantics &lt;a href=&quot;http://jssec.net/&quot;&gt;here&lt;/a&gt;,
and it is a truly staggering effort, running for 40+ pages (that's at least four times easier to understand!).  However, we
weren't quite satisfied. Their semantics formalizes the ECMAScript
specification as written, and therefore inherits some of its weirdness, such as
heap-allocated &quot;scope objects&quot;, implicit coercions, etc.  We still couldn't
build tools over it, and were unwilling to do 40-page case analyses for proofs.
&lt;a href=&quot;http://www.eecs.berkeley.edu/~lmeyerov/&quot;&gt;Leo Meyerovich&lt;/a&gt;, peon
extraordinaire and friend of the blog, felt &lt;a
href=&quot;http://research.microsoft.com/apps/pubs/?id=115390&quot;&gt;the same&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&quot;Challenging current attempts to analyze JavaScript, there is no
formal semantics realistic enough to include many of the attack vectors we have
discussed yet structured and tractable enough that anyone who is not the
inventor has been able to use; formal proofs are therefore beyond the scope of
this work.&quot;&lt;/blockquote&gt;

&lt;h3&gt;How To Tackle JavaScript: The PLT Way&lt;/h3&gt;

&lt;p&gt;We decided to start smaller.  In the fall of 2009, Arjun wrote down
a semantics for the &quot;core&quot; of JavaScript that fits on just three pages (that's
60 times easier to understand!).  This is great programming languages
research&amp;mdash;we defined away the hairy parts of the problem and focused
on a small core that was amenable to proof.
  For these proofs, we could assume the
existence of a trivial desugaring that maps real JavaScript programs
into programs in the core semantics, which Arjun dubbed λ&lt;sub&gt;JS&lt;/sub&gt;.&lt;/p&gt;

&lt;p&gt; Things were looking great until one night Arjun had a few too many glasses
of wine and decided to &lt;em&gt;implement&lt;/em&gt; desugaring.  Along with 
Claudiu Saftoiu, he wrote &lt;a
href=&quot;https://github.com/brownplt/LambdaJS/blob/LambdaJS-1.0/LambdaJS/src/BrownPLT/JavaScript/Semantics/Desugar.hs&quot;&gt;a
thousand lines of Haskell&lt;/a&gt; that turns JavaScript programs into
λ&lt;sub&gt;JS&lt;/sub&gt; programs.  Even worse, they implemented an &lt;a
href=&quot;https://github.com/brownplt/LambdaJS/blob/LambdaJS-1.0/Redex/interp.ss&quot;&gt;interpreter&lt;/a&gt;
for λ&lt;sub&gt;JS&lt;/sub&gt;, so the resulting programs actually &lt;em&gt;run&lt;/em&gt;. They had therefore produced a JavaScript runtime.&lt;/p&gt;

&lt;p&gt;Believe it or not, there are other groups in the business of creating
JavaScript runtimes, namely Google, Mozilla, Microsoft, and a few more.  And
since they care about the correctness of their implementations, they have
actual test suites.  Which Arjun's system could run, and give answers for, that
may or may not be the right ones:&lt;/p&gt;

&lt;figure&gt;
&lt;img width=&quot;100%&quot; src=&quot;http://brownplt.github.com/img/desugar.png&quot;/&gt;
&lt;/figure&gt;

&lt;p&gt; As it turns out, Arjun and Claudiu did a pretty good job.   λ&lt;sub&gt;JS&lt;/sub&gt;
agrees with Mozilla SpiderMonkey on a few thousand lines of tests.  We say
&quot;agreed&quot; and not &quot;passed&quot; because SpiderMonkey fails some of its own tests.
Without any other standard of correctness, λ&lt;sub&gt;JS&lt;/sub&gt; strives for
bug-compatibility with SpiderMonkey on those tests.&lt;/p&gt;

&lt;h3&gt;Building on  λ&lt;sub&gt;JS&lt;/sub&gt;&lt;/h3&gt;

&lt;p&gt; λ&lt;sub&gt;JS&lt;/sub&gt; is discussed in our &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/gsk-essence-javascript/&quot;&gt;ECOOP
paper&lt;/a&gt;, but it's the work built on λ&lt;sub&gt;JS&lt;/sub&gt; that's most
interesting. We've built the following systems ourselves:
&lt;ul&gt;

&lt;li&gt;A type-checker for JavaScript that employs a novel mix of
type-checking and flow analysis (&quot;flow typing&quot;), discussed in our &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/gsk-flow-typing-theory/&quot;&gt;ESOP
2011 paper&lt;/a&gt;, and&lt;/li&gt;

&lt;li&gt;An extension to the above type-checker to &lt;a
href=&quot;http://brownplt.github.com/2011/09/13/adsafety.html&quot;&gt;verify ADsafe&lt;/a&gt;, as discussed in our &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/pegk-type-verif-js-sandbox/&quot;&gt;USENIX Security 2011&lt;/a&gt; paper.&lt;/li&gt;

&lt;/ul&gt;
Others have built on λ&lt;sub&gt;JS&lt;/sub&gt; too:
&lt;ul&gt;

&lt;li&gt;David van Horn and Matt Might use λ&lt;sub&gt;JS&lt;/sub&gt; to build an &lt;a
href=&quot;http://www.ccs.neu.edu/home/dvanhorn/pubs/vanhorn-might-preprint11.pdf&quot;&gt;analytic
framework for JavaScript&lt;/a&gt;,&lt;/li&gt;

&lt;li&gt;Rodolfo Toledo and Éric Tanter use λ&lt;sub&gt;JS&lt;/sub&gt; to specify &lt;a
href=&quot;http://swp.dcc.uchile.cl/TR/2011/TR_DCC-20110516-007.pdf&quot;&gt;aspects for
JavaScript&lt;/a&gt;,&lt;/li&gt;

&lt;li&gt;&lt;a href=&quot;http://research.microsoft.com/apps/pubs/?id=146018&quot;&gt;IBEX&lt;/a&gt;, from
Microsoft Research, uses λ&lt;sub&gt;JS&lt;/sub&gt; for its JavaScript backend to produce
verified Web browser extensions, and&lt;/li&gt;

&lt;li&gt;Others have a secret reimplementation of λ&lt;sub&gt;JS&lt;/sub&gt; in Java.
We are now enterprise-ready.&lt;/li&gt;

&lt;/ul&gt;
&lt;/p&gt;

&lt;p&gt;Want to use λ&lt;sub&gt;JS&lt;/sub&gt; to write JavaScript tools?  &lt;a
href=&quot;https://github.com/brownplt/LambdaJS&quot;&gt;Check&lt;/a&gt; &lt;a
href=&quot;https://github.com/brownplt/LambdaS5&quot;&gt;out&lt;/a&gt; &lt;a
href=&quot;https://github.com/arjunguha/ML-LambdaJS&quot;&gt;our&lt;/a&gt; &lt;a
href=&quot;https://github.com/brownplt/strobe&quot;&gt;software&lt;/a&gt; and let us know what you
think!&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Coming up next: &lt;/b&gt;The latest version of JavaScript, ECMAScript 5th ed.,
is vastly improved. We've nearly finished updating our JavaScript semantics to
match ECMAScript 5th ed. Our new semantics uses the official ECMAScript test
suite and tackles problems, such as &lt;code&gt;eval&lt;/code&gt;, that the original
λ&lt;sub&gt;JS&lt;/sub&gt; elided. We'll talk about it next time. &lt;b&gt;Update:&lt;/b&gt;
We've written about our update, &lt;a
href='http://brownplt.github.com/2011/11/11/s5-javascript-semantics.html'&gt;dubbed S5&lt;/a&gt;, its &lt;a
href=&quot;http://brownplt.github.com/2011/12/11/getters-and-setters.html&quot;&gt;semantics for accessors&lt;/a&gt;,
and a &lt;a href='http://brownplt.github.com/2012/01/31/s5-wat.html'&gt;particularly interesting
example&lt;/a&gt;.

&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ADsafety</title>
   <link href="http://brownplt.github.com/2011/09/13/adsafety.html"/>
   <updated>2011-09-13T00:00:00-07:00</updated>
   <id>http://brownplt.github.com/2011/09/13/adsafety</id>
   <content type="html">&lt;h2&gt;&lt;a href=&quot;http://brownplt.github.com/2011/09/13/adsafety.html&quot;&gt;ADsafety&lt;/a&gt;&lt;/h2&gt;

&lt;em style=&quot;color: gray; font-size: smaller&quot;&gt;
  Posted on 13 September 2011.&lt;/em&gt;

&lt;figure style=&quot;float: right;&quot;&gt;&lt;img src=&quot;http://brownplt.github.com/img/adsafety.png&quot; /&gt;&lt;/figure&gt;

&lt;p&gt; A &lt;i&gt;mashup&lt;/i&gt; is a webpage that mixes and mashes content from various
sources. Facebook apps, Google gadgets, and various websites with embedded maps
are obvious examples of mashups. However, there is an even more pervasive use
case of mashups on the Web. Any webpage that displays third-party ads is a
mashup. It's well known that third-party content can include third-party
cookies; your browser can even block these if you're concerned about &quot;tracking
cookies&quot;. However, third party content can also include third-party JavaScript
that can do all sorts of wonderful and malicious things (&lt;a
href=&quot;http://www.bbc.co.uk/news/technology-12891182&quot;&gt;just&lt;/a&gt; &lt;a
href=&quot;http://news.cnet.com/8301-27080_3-20000898-245.html&quot;&gt;some&lt;/a&gt; &lt;a
href=&quot;http://www.networkworld.com/news/2011/030111-malware-ads-hit-london-stock.html&quot;&gt;examples&lt;/a&gt;).
&lt;/p&gt;

&lt;p&gt;Is it possible to safely embed untrusted JavaScript on a page? Google Caja,
Microsoft Web Sandbox, and ADsafe are &lt;i&gt;language-based Web sandboxes&lt;/i&gt; that
try to do so. Language-based sandboxing is a programming language technique
that restricts untrusted code using static and runtime checks and rewriting
potential dangerous calls to safe, trusted functions.&lt;/p&gt;

&lt;p&gt;Sandboxing JavaScript, with all its corner cases, is particularly hard. A
single bug can easily break the entire sandboxing system. JavaScript sandboxes
do not clearly state their intended guarantees, nor do they clearly argue why
they are safe.  &lt;figure style=&quot;float: left;&quot;&gt; &lt;img
src=&quot;http://brownplt.github.com/img/adsafe-settimeout.png&quot; /&gt; &lt;figcaption&gt;This is how ADsafe
works.&lt;/figcaption&gt; &lt;/figure&gt; &lt;/p&gt;

&lt;h3&gt;Verifying Web Sandboxes&lt;/h3&gt;

&lt;p&gt; A year ago, we embarked on a project to verify &lt;a
href=&quot;http://www.adsafe.org/&quot;&gt;ADsafe&lt;/a&gt;, &lt;a
href=&quot;http://www.crockford.com/&quot;&gt;Douglas Crockford&lt;/a&gt;'s Web sandbox. ADsafe is
admittedly the simplest of the aforementioned sandboxes. But, we were also
after the shrimp bounty that Doug offers for sandbox-breaking bugs:

&lt;blockquote cite=&quot;http://tech.groups.yahoo.com/group/caplet/message/44&quot;&gt;
Write a program [...] that calls the alert function when run on any browser. If the program produces no errors when linted with the ADsafe option, then I will buy you a plate of shrimp. (&lt;a href=&quot;http://tech.groups.yahoo.com/group/caplet/message/44&quot;&gt;link&lt;/a&gt;)
&lt;/blockquote&gt;

A year later, we've produced a &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/pegk-type-verif-js-sandbox/&quot;&gt;USENIX
Security paper&lt;/a&gt; on our work, which we &lt;a
href=&quot;http://www.usenix.org/events/sec11/stream/politz/index.html&quot;&gt;presented&lt;/a&gt;
in San Francisco in August. The paper discusses the many common techniques
employed by Web sandboxes and discusses the intricacies of their
implementations. (TLDR: JavaScript and the DOM are really hard.) Focusing on
ADsafe, it precisely states what ADsafety actually means. The meat of the paper
is our approach to verifying ADsafe using types. Our verification leverages our
earlier work on &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/gsk-essence-javascript/&quot;&gt;semantics&lt;/a&gt;
and &lt;a
href=&quot;http://www.cs.brown.edu/~sk/Publications/Papers/Published/gsk-flow-typing-theory/&quot;&gt;types&lt;/a&gt;
for JavaScript, and also introduces some new techniques:&lt;/p&gt;

&lt;ul&gt;

&lt;li&gt;Check out the &lt;span style=&quot;font-size: large;&quot;&gt;★&lt;/span&gt;s and &lt;span
style=&quot;font-size: large;&quot;&gt;☠&lt;/span&gt;s in our object types; we use them to
type-check &quot;scripty&quot; features of JavaScript. &lt;span style=&quot;font-size:
large;&quot;&gt;☠&lt;/span&gt; marks a field as &quot;banned&quot; and &lt;span style=&quot;font-size:
large;&quot;&gt;★&lt;/span&gt; specifies the type of all other fields.&lt;/li&gt;

&lt;li&gt;We also characterize JSLint as a type-checker. The Widget type presented in
the paper specifies, in 20 lines, the syntactic restrictions of JSLint's
ADsafety checks.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Unlike conventional type systems, ours does not prevent runtime errors.
After all, stuck programs are safe because they trivially don't execute any
code. If you think type systems only catch &quot;method not found&quot; errors, you
should have a look at ours.&lt;/p&gt;

&lt;p&gt;We found bugs in both ADsafe and JSLint that manifested as type errors. We reported all of them and they were promptly fixed by Doug Crockford. A big thank you to Doug for his encouragement, for answering our many questions, and for buying us every type of shrimp dish in the house.
&lt;div class=&quot;center&quot;&gt;
&lt;figure&gt; &lt;img class=&quot;center&quot; src=&quot;http://brownplt.github.com/img/shrimp-bounty.jpg&quot; width=&quot;320&quot; /&gt; &lt;figcaption&gt;Doug Crockford, Joe, Arjun, and seven shrimp dishes&lt;/figcaption&gt; &lt;/figure&gt; &lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;
Learn more about ADsafety!  Check out:
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
  The &lt;a href=&quot;http://www.cs.brown.edu/research/plt/dl/adsafety/v1&quot;&gt;paper,
  code, and proofs&lt;/a&gt;;
&lt;/li&gt;
&lt;li&gt;
  &lt;a href=&quot;http://www.usenix.org/multimedia/sec11politz&quot;&gt;Video&lt;/a&gt; of Arjun presenting at USENIX Security;
&lt;/li&gt;
&lt;li&gt;
  &lt;a href=&quot;http://www.adsafe.org&quot;&gt;ADsafe&lt;/a&gt; and &lt;a href=&quot;http://www.jslint.org&quot;&gt;JSLint&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 
</feed>

