<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Tim Disney</title>
  <link href="http://disnetdev.com/blog/atom.xml" rel="self"/>
  <link href="http://disnetdev.com/"/>
  <updated>2016-11-01T15:50:06+00:00</updated>
  <id>http://disnetdev.com/</id>
  <author>
    <name>Tim Disney</name>
    <email>tim.disney@gmail.com</email>
  </author>
  
    <entry>
      <title>Hygiene in sweetjs</title>
      <link href="http://disnetdev.com/blog/blog/2013/09/27/hygiene-in-sweet.js"/>
      <updated>2013-09-27T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;The most important feature of &lt;a href=&quot;http://sweetjs.org&quot;&gt;sweet.js&lt;/a&gt; is hygiene. Hygiene prevents variables names inside of macros from clashing with variables in the surrounding code. It’s what gives macros the power to actually be syntactic abstractions by hiding implementation details and allowing you to use a hygienic macro &lt;em&gt;anywhere&lt;/em&gt; in your code.&lt;/p&gt;

&lt;p&gt;For hygiene to work sweet.js must rename variables. Recently several people have asked me why sweet.js renames &lt;strong&gt;all&lt;/strong&gt; the variables. Wouldn’t it be better and cleaner to only rename the variables that macros introduce?&lt;/p&gt;

&lt;p&gt;The tl;dr is “because hygiene” but let’s unpack that a little.&lt;/p&gt;

&lt;h2 id=&quot;hygiene-part-1-binding&quot;&gt;Hygiene Part 1 (Binding)&lt;/h2&gt;

&lt;p&gt;The part of hygiene most people intuitively grok is keeping track of the variable &lt;em&gt;bindings&lt;/em&gt; that a macro introduces. For example, the swap macro creates a &lt;code class=&quot;highlighter-rouge&quot;&gt;tmp&lt;/code&gt; variable that should only be bound inside of the macro:&lt;/p&gt;

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;macro&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;swap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;rule&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;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$b&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;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;$b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp&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;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&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;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;swap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&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;Hygiene keeps the two &lt;code class=&quot;highlighter-rouge&quot;&gt;tmp&lt;/code&gt; bindings distinct by renaming them both:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&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;b$2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&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;tmp$3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp$1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;tmp$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b$2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b$2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp$3&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 is the point where most people say “why bother renaming the variables outside of the macro”? Can’t you just rename the bindings created by the macro? Wouldn’t it be cleaner for the expansion to just be something like:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&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;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&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;tmp$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;tmp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tmp$1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;hygiene-part-2-reference&quot;&gt;Hygiene Part 2 (Reference)&lt;/h2&gt;

&lt;p&gt;The complication comes in with variable &lt;em&gt;references&lt;/em&gt;. The body of a macro can contain references to bindings declared outside of the macro and those references must be consistent no matter the context in which the macro is invoked.&lt;/p&gt;

&lt;p&gt;Some code to clarify. Let’s say you have a macro that uses a random number function:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;seed&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;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;macro&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;rule&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&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;p&quot;&gt;}&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 macro needs to refer to &lt;code class=&quot;highlighter-rouge&quot;&gt;random&lt;/code&gt; in any context that it gets invoked. But its context could have a different binding to &lt;code class=&quot;highlighter-rouge&quot;&gt;random&lt;/code&gt;!&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&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;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&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;Hygiene needs to keep the two &lt;code class=&quot;highlighter-rouge&quot;&gt;random&lt;/code&gt; bindings different. So sweet.js will expand this into something like:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random$1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;seed$4&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;cm&quot;&gt;/* ... */&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&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;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random$2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&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;n$3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;random$1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&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;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Note that there is no way for hygiene to do this if it only renamed identifiers inside of macros since both &lt;code class=&quot;highlighter-rouge&quot;&gt;random&lt;/code&gt; bindings were declared outside of the macro. Hygiene is necessarily a whole program transformation.&lt;/p&gt;

&lt;p&gt;(ps if this sort of feels like a closure you’re on to something: one of the early techniques that led to modern hygiene algorithms was called &lt;a href=&quot;ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-1049.pdf&quot;&gt;syntactic closures&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Strictly speaking the hygiene algorithm is still conservative. Variable bindings declared outside of a macro that are never referenced by a macro don’t really need to be renamed. However, modifying the hygiene algorithm to only rename &lt;em&gt;exactly&lt;/em&gt; what needs to be renamed seems pretty difficult (especially to do so efficiently). If anyone knows techniques for this definitely let me know (or even better submit a pull request).&lt;/p&gt;
</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>How to read macros</title>
      <link href="http://disnetdev.com/blog/blog/2012/12/20/how-to-read-macros"/>
      <updated>2012-12-20T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;In my last &lt;a href=&quot;http://disnetdev.com/blog/2012/10/14/hygienic-macros-for-javascript/&quot; title=&quot;Hygienic Macros for JavaScript&quot;&gt;post&lt;/a&gt; I gave a little overview of &lt;a href=&quot;http://sweetjs.org/&quot; title=&quot;Sweet.js&quot;&gt;sweet.js&lt;/a&gt; the hygienic macro system I built over the summer. Today I want to write a little bit about what makes sweet.js possible and why we haven’t really seen a macro system for JavaScript before now. I gave hints at some of this in my intern &lt;a href=&quot;https://air.mozilla.org/sweetjs/&quot; title=&quot;Sweet.js Talk&quot;&gt;talk&lt;/a&gt; but now we can finally do a deep dive!&lt;/p&gt;

&lt;h2 id=&quot;basics&quot;&gt;Basics&lt;/h2&gt;

&lt;p&gt;First, let’s take a look at compilers 101:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/parser_pipeline.png&quot; alt=&quot;Parser Pipeline&quot; width=&quot;600px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The traditional way you build a compiler front end is to first write a lexer and a parser. Code (a string) is fed to the lexer which produces an array of tokens which gets fed to the parser which produces an Abstract Syntax Tree (AST).&lt;/p&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&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;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bar&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;alert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;w00t!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&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;gets lexed into something like:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;if&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;(&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;{&quot;&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;The lexer is basically responsible for throwing away unnecessary whitespace and grouping identifiers, strings, and numbers into discrete chunks (ie tokens). The array of tokens is then parsed into an AST that might look something like this:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// output of esprima&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Program&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;body&quot;&lt;/span&gt;&lt;span class=&quot;err&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;IfStatement&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&quot;test&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;BinaryExpression&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&quot;operator&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&quot;left&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Identifier&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&quot;right&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Identifier&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&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;s2&quot;&gt;&quot;consequent&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;BlockStatement&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&quot;body&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ExpressionStatement&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;s2&quot;&gt;&quot;expression&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;CallExpression&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;s2&quot;&gt;&quot;callee&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Identifier&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;alert&quot;&lt;/span&gt;
                            &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                            &lt;span class=&quot;s2&quot;&gt;&quot;arguments&quot;&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;s2&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Literal&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                    &lt;span class=&quot;s2&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;w00t!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                    &lt;span class=&quot;s2&quot;&gt;&quot;raw&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;\&quot;w00t!\&quot;&quot;&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;p&quot;&gt;},&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&quot;alternate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&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;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The AST gives you the structure necessary to do code optimization/generation etc.&lt;/p&gt;

&lt;p&gt;So where can we fit macros into this picture? Which representation is best for macros to do their stuff?&lt;/p&gt;

&lt;p&gt;Well, by the time we get to an AST it’s too late since parsers only understand a fixed grammar (well, technically there is research on &lt;a href=&quot;http://en.wikipedia.org/wiki/Adaptive_grammar&quot; title=&quot;Adaptive Grammar&quot;&gt;adaptive/extensible grammars&lt;/a&gt; but that way leads to madness!). Obviously the raw code as a string is too unstructured for macros so how about the array of tokens produced by the lexer?&lt;/p&gt;

&lt;p&gt;Tokens are fine for cpp &lt;code class=&quot;highlighter-rouge&quot;&gt;#define&lt;/code&gt; style macros but we want moar power! And, as it turns out, just normal tokens aren’t going to cut it for us. Consider this simple macro that provides a concise way to define functions:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;macro&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$name&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$params&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$body&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;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$name&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$params&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$body&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;def&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&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;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&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;which should be expanded into:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&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;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&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;Critically, note that the macro needs to match &lt;code class=&quot;highlighter-rouge&quot;&gt;$params&lt;/code&gt; with &lt;code class=&quot;highlighter-rouge&quot;&gt;(a, b)&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;$body&lt;/code&gt; with &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;b;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;. However, we don’t have enough structure to do this with just the token array &lt;code class=&quot;highlighter-rouge&quot;&gt;[&quot;def&quot;, &quot;add&quot;, &quot;(&quot;, &quot;a&quot;, &quot;,&quot;, &quot;b&quot;, ...]&lt;/code&gt;: we need to match the delimiters (&lt;code class=&quot;highlighter-rouge&quot;&gt;()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;[]&lt;/code&gt;) first!&lt;/p&gt;

&lt;p&gt;If you remember your compilers class (or went to wikipedia), delimiter matching is what separates &lt;a href=&quot;http://en.wikipedia.org/wiki/Context-free_language&quot; title=&quot;Context-Free Language&quot;&gt;context-free&lt;/a&gt; languages (what parsers recognize) from &lt;a href=&quot;http://en.wikipedia.org/wiki/Regular_language&quot; title=&quot;Regular Language&quot;&gt;regular&lt;/a&gt; languages (what lexers recognize).&lt;/p&gt;

&lt;p&gt;This is one of the reasons why macros are big in the lisp family of languages (scheme, racket, clojure, etc.). S-expressions (with (all (those (parentheses)))) are already fully delimited so it becomes almost trivial to do delimiter matching. Some people say this is due to &lt;a href=&quot;http://en.wikipedia.org/wiki/Homoiconicity&quot; title=&quot;Homoiconicity&quot;&gt;homoiconicity&lt;/a&gt; but as Dave Herman &lt;a href=&quot;http://calculist.org/blog/2012/04/17/homoiconicity-isnt-the-point&quot; title=&quot;Homoiconicity not the point&quot;&gt;pointed out&lt;/a&gt;, homoiconicity isn’t really the point. It’s not that the lisp family is homoiconic but rather that the nature of s-expressions makes it easy to implement &lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt; which is necessary for macros.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt; is the crucial function that gives a little bit more structure to the array of tokens by matching up all those delimiters. Now instead of just a flat array of tokens we are going to get a &lt;em&gt;read tree:&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;def&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;add&quot;&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;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;()&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;innerTokens&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;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&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;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;innerTokens&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;s2&quot;&gt;&quot;return&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&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;Note this doesn’t have all the structure of a full AST, it just knows about tokens and delimiters not expressions and statements. So now our &lt;code class=&quot;highlighter-rouge&quot;&gt;def&lt;/code&gt; macro pattern variables will match up like so:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;$params&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;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;()&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;innerTokens&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;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&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;$body&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;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;innerTokens&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;s2&quot;&gt;&quot;return&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&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;Great! Now our pipeline looks something like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/pipeline_plus_read.png&quot; alt=&quot;Pipeline With Read&quot; width=&quot;600px&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-devil-in-the-details&quot;&gt;The Devil in the Details&lt;/h2&gt;

&lt;p&gt;Ok, so all well and good but then why haven’t we seen people implement &lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt; and build a macro system for JavaScript before now?&lt;/p&gt;

&lt;p&gt;It turns out that there’s this really annoying token (for potential macro implementers) in JavaScript: &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the problem, depending on context &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; can mean two different things: the divide operator or the beginning of a regular expression literal.&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;10&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;c1&quot;&gt;// 5&lt;/span&gt;
&lt;span class=&quot;sr&quot;&gt;/foo/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;(Well technically I guess &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; can also mean the start of a comment but this is always easy to figure out (since &lt;code class=&quot;highlighter-rouge&quot;&gt;//&lt;/code&gt; &lt;strong&gt;always&lt;/strong&gt; means line comment))&lt;/p&gt;

&lt;p&gt;So how do we disambiguate between divide and regex? It turns out that the way a normal parser (like &lt;a href=&quot;http://esprima.org/&quot; title=&quot;Esprima&quot;&gt;esprima&lt;/a&gt; for example) does it is by running the lexer and parser together and resolving the ambiguity via the current &lt;strong&gt;parsing&lt;/strong&gt; context. In other words, as the parser is working through each production, it calls out to the lexer with a flag saying what context it is in. Depending on that context the lexer will either lex &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; as a divide token or as a regular expression literal.&lt;/p&gt;

&lt;p&gt;But, we can’t use the parsing context in &lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt; because we don’t have any parsing context yet!&lt;/p&gt;

&lt;p&gt;So, we somehow need to separate the lexer/reader from the parser.&lt;/p&gt;

&lt;p&gt;Now you might think we could get away with just leaving &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; as an ambiguous token (say a &lt;code class=&quot;highlighter-rouge&quot;&gt;divOrRegex&lt;/code&gt; token for example) to be handled by the parser once all the macros have been expanded away but consider this code fragment we might want to &lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&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;sr&quot;&gt;/foo}bar/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// as a token array this would be&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// [... &quot;{&quot;, &quot;/&quot;, &quot;foo&quot;, &quot;}&quot;, &quot;bar&quot;, &quot;/&quot;, ...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Remember that the entire point of &lt;code class=&quot;highlighter-rouge&quot;&gt;read&lt;/code&gt; is to do delimiter matching, so should we match the &lt;code class=&quot;highlighter-rouge&quot;&gt;}&lt;/code&gt; with the opening &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;/code&gt; or as part of a regular expression literal (remember &lt;code class=&quot;highlighter-rouge&quot;&gt;/}/&lt;/code&gt; is a valid regex that matches a single &lt;code class=&quot;highlighter-rouge&quot;&gt;}&lt;/code&gt;)? It completely depends on our interpretation of &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Therefore, in our lexer/reader we &lt;strong&gt;must&lt;/strong&gt; disambiguate the meaning of &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; without the help of the parser. So how do we do that?&lt;/p&gt;

&lt;p&gt;This is the hard technical problem that &lt;a href=&quot;https://twitter.com/PaulStansifer&quot; title=&quot;Paul Stansifer&quot;&gt;Paul Stansifer&lt;/a&gt; (he also designed the &lt;a href=&quot;http://www.rust-lang.org/&quot; title=&quot;Rust&quot;&gt;Rust&lt;/a&gt; macro system) solved this summer, unlocking the power of JavaScript macros for us all!&lt;/p&gt;

&lt;p&gt;The basic idea is when you see a &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; as you are reading, just look back a couple of tokens and a small fixed set of tokens will determine unambiguously if &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; should be a divide or the start of a regex literal. To figure out exactly how far back and which tokens to look for requires working through all the different cases in the JavaScript grammar which is hard but done!&lt;/p&gt;

&lt;p&gt;A snippet of this algorithm goes something like:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if tok is /
    if tok-1 is )
        look back to matching (
        if identifier before ( in &quot;if&quot; &quot;while&quot;
                                  &quot;for&quot; &quot;with&quot;
            tok is start of regex literal
        else
            tok is divide
    ...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;For example, if we have:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&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;mi&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bar&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;baz&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;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&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 we see the &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; we note that the previous token was &lt;code class=&quot;highlighter-rouge&quot;&gt;)&lt;/code&gt; so we find its matching &lt;code class=&quot;highlighter-rouge&quot;&gt;(&lt;/code&gt; and note that the token before that was &lt;code class=&quot;highlighter-rouge&quot;&gt;if&lt;/code&gt; so &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; must be the beginning of a regular expression.&lt;/p&gt;

&lt;p&gt;What’s really cool here is that when we need to disambiguate &lt;code class=&quot;highlighter-rouge&quot;&gt;/&lt;/code&gt; we’ve already been reading up to that point so &lt;code class=&quot;highlighter-rouge&quot;&gt;(foo + 24 &amp;gt; bar)&lt;/code&gt; is a single token (the &lt;code class=&quot;highlighter-rouge&quot;&gt;()&lt;/code&gt; token with inner tokens &lt;code class=&quot;highlighter-rouge&quot;&gt;foo&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;+&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;24&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;bar&lt;/code&gt;) and checking the token before the parens is literally as simple as &lt;code class=&quot;highlighter-rouge&quot;&gt;tokens[idx-2] === &quot;if&quot;&lt;/code&gt;. By creating the read tree as we go along we don’t need to carry lookbehind state in a complicated way; in fact, in the worst case, we only have to look back 5 tokens.&lt;/p&gt;

&lt;p&gt;If you want to read more about how this works, I’ve got the entire algorithm pseudo-coded up &lt;a href=&quot;https://github.com/mozilla/sweet.js/wiki/design&quot; title=&quot;Read Design Doc&quot;&gt;here&lt;/a&gt; and the actual JavaScript implementation in these relatively short &lt;a href=&quot;https://github.com/mozilla/sweet.js/blob/master/src/parser.js#L3713&quot; title=&quot;Github&quot;&gt;two&lt;/a&gt; &lt;a href=&quot;https://github.com/mozilla/sweet.js/blob/master/src/parser.js#L3631&quot; title=&quot;Github&quot;&gt;functions&lt;/a&gt;.&lt;/p&gt;

</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Hygienic Macros for JavaScript</title>
      <link href="http://disnetdev.com/blog/blog/2012/10/14/hygienic-macros-for-javascript"/>
      <updated>2012-10-14T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;Been slow to embloggen this (start of the quarter etc.) but my summer intern project was released a little while ago at &lt;a href=&quot;http://sweetjs.org&quot;&gt;sweetjs.org&lt;/a&gt;. Sweet.js is a &lt;a href=&quot;http://en.wikipedia.org/wiki/Hygienic_macro&quot;&gt;hygienic macro&lt;/a&gt; compiler for JavaScript that takes JavaScript written with macros and produces normal JavaScript you can run in your browser or on node. It’s an experiment to see if macros can work well for JavaScript.&lt;/p&gt;

&lt;p&gt;Macros allow you to extend the syntax of JavaScript by writing macro definitions that perform syntax transformations, thus allowing you to do cool things like add sweet syntax for &lt;a href=&quot;https://gist.github.com/3881008&quot;&gt;var destructuring&lt;/a&gt; or even haskell-like &lt;a href=&quot;https://gist.github.com/3831514&quot;&gt;do notation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The idea is to provide a middle ground between no syntax extension and having to build an entire compile-to-js language like CoffeeScript, which can’t compose with other syntax extensions.&lt;/p&gt;

&lt;p&gt;I talk a bit about the motivation and design in this &lt;a href=&quot;https://air.mozilla.org/sweetjs/&quot;&gt;presentation&lt;/a&gt; I gave at the end of my internship.&lt;/p&gt;

&lt;p&gt;The language that most directly inspires sweet.js is, of course, scheme. I’m not really much of a schemer myself but have always admired the fancy work going on in that world and macros are pretty fancy. At the start of the summer I was still somewhat of a macro newbie but being at Mozilla was fantastic since I was able to draw on and learn from two well-versed scheme macrologists &lt;a href=&quot;https://twitter.com/littlecalculist&quot;&gt;Dave Herman&lt;/a&gt; (who’s PhD thesis was on macros) and &lt;a href=&quot;https://twitter.com/PaulStansifer&quot;&gt;Paul Stansifer&lt;/a&gt; (who has been developing the &lt;a href=&quot;http://www.rust-lang.org/&quot;&gt;Rust&lt;/a&gt; macro system).&lt;/p&gt;

&lt;p&gt;Sweet.js is still in very early stages, lots of bugs and missing features, but I think it shows some real promise. Let me know what you think!&lt;/p&gt;
</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Memory Checking in Low-Level JavaScript</title>
      <link href="http://disnetdev.com/blog/blog/2012/07/18/memory-checking-in-low-level-javascript"/>
      <updated>2012-07-18T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;So this past month I’ve been helping out on the
&lt;a href=&quot;http://lljs.org/&quot; title=&quot;Low-Level JavaScript&quot;&gt;Low-Level JavaScript&lt;/a&gt; (LLJS) project. Bit of a change for me since I usually
hide out at
as &lt;a href=&quot;http://www.quickmeme.com/meme/358lli/&quot; title=&quot;Abstract all the things!&quot;&gt;high a level as I can possibly get&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;LLJS is an experiment in adding low-level features like pointers and
manual memory management to a dialect of JavaScript.
These low-level features are pretty cool and can get you some nice
performance wins, but they also come at a cost since
you are always one
null pointer dereference or memory leak away from oblivion.&lt;/p&gt;

&lt;p&gt;One way C/C++ programmers have handled this danger is by using an
analysis tool like &lt;a href=&quot;http://valgrind.org/&quot; title=&quot;Valgrind&quot;&gt;Valgrind&lt;/a&gt; to detect memory errors 
that happen while the program is running.
Since memory checking has proven to be useful in the C world, we
figured it might be helpful for LLJS. So, I’ve hacked up a
valgrind-like memory analysis for LLJS. It can detect four kinds
of memory errors:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;reads/writes to unallocated memory (pointers we haven’t &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;‘ed)&lt;/li&gt;
  &lt;li&gt;reads from undefined memory (&lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;‘ed memory we haven’t
written to)&lt;/li&gt;
  &lt;li&gt;bad frees (calling &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; on memory that hasn’t been &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt;‘ed)&lt;/li&gt;
  &lt;li&gt;leaks (any unfreed memory at the end of the program run)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;how-to-use&quot;&gt;How to use&lt;/h2&gt;

&lt;p&gt;Go grab the latest version of LLJS from its github &lt;a href=&quot;https://github.com/mbebenita/LLJS&quot; title=&quot;LLJS on github&quot;&gt;repo&lt;/a&gt;.
Then add the following snippet to the end of your script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'memory'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;memcheck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This pulls in the memory module and prints out the result of the
memory checking (this snippet is for node.js, so use &lt;code class=&quot;highlighter-rouge&quot;&gt;load&lt;/code&gt; if you’re
using SpiderMonkey or grab &lt;code class=&quot;highlighter-rouge&quot;&gt;memory&lt;/code&gt; off the global object if you’re in
the browser).&lt;/p&gt;

&lt;p&gt;Then compile your LLJS code with the &lt;code class=&quot;highlighter-rouge&quot;&gt;-m&lt;/code&gt; flag:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;bin/ljc -m -o your_file.js your_file.ljs&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;and run it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;node --harmony-proxies your_file.js&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see the memory checker uses &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Proxy&quot; title=&quot;Proxy&quot;&gt;Proxies&lt;/a&gt;, which node/V8
hides behind a flag (no flag needed on SpiderMonkey).&lt;/p&gt;

&lt;p&gt;For example, the following script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;extern&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;test_unallocated&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;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;o&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;// not allocated yet!&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&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;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;test_unallocated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;test_leak&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;c1&quot;&gt;// leak!&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;leak&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uint&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;test_leak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;test_free&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;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uint&lt;/span&gt; &lt;span class=&quot;o&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;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;uint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;delete&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;k&quot;&gt;delete&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;test_free&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'memory'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;memcheck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;will print out the following report:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Unallocated:
0 at:
    test_unallocated:3:0

Undefined:
0 at:
    test_unallocated:3:0

Bad frees:
8184 at:
    test_free:16:0

Leaks:
8200 at:
    test_leak:10:0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;how-it-works&quot;&gt;How it works&lt;/h2&gt;

&lt;p&gt;Memory allocation in LLJS is implemented as a big &lt;a href=&quot;https://developer.mozilla.org/en/JavaScript_typed_arrays&quot; title=&quot;Typed Arrays&quot;&gt;typed array&lt;/a&gt;
where pointers are really just array indexes into the array.&lt;/p&gt;

&lt;p&gt;To do memory checking we create a second &lt;em&gt;shadow memory&lt;/em&gt; typed array
that stores the allocation status of each byte of real memory (if the byte
has been allocated, initialized, etc.). Calls to &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt;
change the allocation status of the bytes in shadow memory. The real memory
array is
wrapped in a Proxy that 
intercepts each get and set operation and
checks the shadow memory 
for a possible error.&lt;/p&gt;

&lt;p&gt;For example, say we we allocate an int and try to do some pointer
arithmetic and dereference unallocated memory:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&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;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;int&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;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;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When the dereference of &lt;code class=&quot;highlighter-rouge&quot;&gt;x+1&lt;/code&gt; happens, the Proxy wrapping real memory
will check in shadow memory to see if &lt;code class=&quot;highlighter-rouge&quot;&gt;x+1&lt;/code&gt; has been allocated. Since
it hasn’t, an unallocated memory access error is recorded.&lt;/p&gt;

&lt;p&gt;This approach is basically a simplified version of how 
Valgrind &lt;a href=&quot;http://valgrind.org/docs/shadow-memory2007.pdf&quot; title=&quot;Memcheck&quot;&gt;does memory checking&lt;/a&gt;.&lt;/p&gt;

</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Stable node.js now runs contracts.coffee!</title>
      <link href="http://disnetdev.com/blog/blog/2012/07/15/stable-nodejs-now-runs-contractscoffee"/>
      <updated>2012-07-15T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;Took almost a year but now the stable version of node.js (0.8.0+) has
a recent enough version of V8 to run contracts.coffee! The much
needed proxy support is still behind a flag so you’ll need to
supply &lt;code class=&quot;highlighter-rouge&quot;&gt;--harmony_proxies&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;--harmony-collections&lt;/code&gt; as usual.&lt;/p&gt;

&lt;p&gt;Also, the latest version of contracts.coffee (0.3.1) 
and is now up on npm and
collects a few bugs fixes.&lt;/p&gt;
</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Link Roundup</title>
      <link href="http://disnetdev.com/blog/blog/2012/05/13/link-roundup"/>
      <updated>2012-05-13T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;&lt;a href=&quot;http://mbebenita.github.com/Mvm/&quot; title=&quot;*JS&quot;&gt;*JS&lt;/a&gt; is a really cool (if ungoogleable) project that attempts
to be “the bastard child of JavaScript and C” by giving you the low
level tools to build fast programs in a high level language.&lt;/p&gt;

&lt;p&gt;After the recent excitement surrounding Bret Victor’s
&lt;a href=&quot;http://vimeo.com/36579366&quot; title=&quot;Inventing on Principle&quot;&gt;amazing talk&lt;/a&gt;, Jonathan Edwards has a &lt;a href=&quot;http://alarmingdevelopment.org/?p=680&quot; title=&quot;An IDE is not enough&quot;&gt;sobering essay&lt;/a&gt;
about the limits of IDE innovation. Makes the point that languages
must co-evolve to get real innovation.&lt;/p&gt;

&lt;p&gt;Wired has a &lt;a href=&quot;http://www.wired.com/threatlevel/2012/05/everyone-hacked/all/1&quot; title=&quot;Everyone Has Been Hacked&quot;&gt;nice writeup&lt;/a&gt; on the current state of computer security
(spoiler: it sucks) and
The New Yorker has a &lt;a href=&quot;http://www.newyorker.com/reporting/2012/05/07/120507fa_fact_kushner?currentPage=all&quot; title=&quot;Machine Politics&quot;&gt;great article&lt;/a&gt; on the recent geohot vs. Sony
craziness.&lt;/p&gt;

</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Link Roundup</title>
      <link href="http://disnetdev.com/blog/blog/2012/04/28/link-roundup"/>
      <updated>2012-04-28T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;&lt;a href=&quot;http://infrequently.org/2012/04/class-warfare/&quot; title=&quot;Class Warfare&quot;&gt;Lots&lt;/a&gt; of &lt;a href=&quot;http://www.mikealrogers.com/posts/tc-thirty-what.html&quot; title=&quot;TC-thirty-what&quot;&gt;discussion&lt;/a&gt; around classes and
other changes being considered for the future of JavaScript.
Interesting how difficult it has been for the standardization
committee (or the JS community in general) to come to a consensus on
what classes should look like (or if they should even exist).&lt;/p&gt;

&lt;p&gt;In what is almost becoming a weekly story, &lt;a href=&quot;http://sns.cs.princeton.edu/2012/04/javascript-in-javascript-js-js-sandboxing-third-party-scripts/&quot; title=&quot;js.js&quot;&gt;emscripten has been used to
impressive effect again&lt;/a&gt;. This time SpiderMonkey (the JS engine in
Firefox) has been compiled to JavaScript.&lt;/p&gt;

&lt;p&gt;Finally, an
&lt;a href=&quot;http://calculist.org/blog/2012/04/17/homoiconicity-isnt-the-point/&quot; title=&quot;Homoiconicity isn't the point&quot;&gt;explanation of why Lisp’s syntax is so important and useful&lt;/a&gt; 
that finally clicked for me. The key: “it’s possible to &lt;em&gt;read&lt;/em&gt; it
without &lt;em&gt;parsing&lt;/em&gt;.”&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://pksunkara.github.com/semicolon/&quot; title=&quot;Semicolon&quot;&gt;greatest language in the world&lt;/a&gt; has been made. PL is done,
we can all go home now.&lt;/p&gt;

</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>contracts.coffee now runs in Chrome!</title>
      <link href="http://disnetdev.com/blog/blog/2012/04/19/contractscoffee-works-on-chrome"/>
      <updated>2012-04-19T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;Chrome can now run &lt;a href=&quot;http://disnetdev.com/contracts.coffee/&quot;&gt;contracts.coffee&lt;/a&gt;!
I’ve been waiting a while for the proxy support in Chrome to get far enough along but
it’s finally here.&lt;/p&gt;

&lt;p&gt;Chrome hides experimental JavaScript features
like proxies behind flags so you’ll need to go to 
about:flags and enable the experimental JavaScript flag.&lt;/p&gt;

&lt;p&gt;Try it out and if you run into any issues let me know on 
&lt;a href=&quot;https://github.com/disnet/contracts.coffee/issues?direction=desc&amp;amp;sort=created&amp;amp;state=open&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;
</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Updates to contracts.coffee</title>
      <link href="http://disnetdev.com/blog/blog/2012/04/16/updates-to-contractscoffee"/>
      <updated>2012-04-16T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;A new version of contracts.coffee is up on npm. We’re now synced with
the latest release version of CoffeeScript (1.3.1) and have collected
a few bug fixes. We also have experimental require.js/AMD support.
Change log &lt;a href=&quot;http://disnetdev.com/contracts.coffee/#log&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Get it with &lt;code class=&quot;highlighter-rouge&quot;&gt;npm install contracts.coffee&lt;/code&gt;.&lt;/p&gt;
</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
    <entry>
      <title>Weekly Link Roundup</title>
      <link href="http://disnetdev.com/blog/blog/2012/04/08/link-roundup"/>
      <updated>2012-04-08T00:00:00+00:00</updated>
      <id>http://disnetdev.com/blog{ post.id }}</id>
      <content type="html">&lt;p&gt;&lt;a href=&quot;http://bits.blogs.nytimes.com/2012/04/04/google-begins-testing-its-augmented-reality-glasses/&quot; title=&quot;Google Glasses&quot;&gt;Details surface about Google’s project Glass&lt;/a&gt;. The future is now
and all that. &lt;a href=&quot;http://www.amazon.com/Rainbows-End-Vernor-Vinge/dp/0812536363/ref=sr_1_1?ie=UTF8&amp;amp;qid=1333925648&amp;amp;sr=8-1&quot; title=&quot;Rainbows End&quot;&gt;Rainbows End&lt;/a&gt; and &lt;a href=&quot;http://www.amazon.com/Daemon-Daniel-Suarez/dp/B003L1ZXCU/ref=sr_1_1?ie=UTF8&amp;amp;qid=1333925660&amp;amp;sr=8-1&quot; title=&quot;Daemon&quot;&gt;Daemon&lt;/a&gt;/&lt;a href=&quot;http://www.amazon.com/Freedom-TM-Daemon-ebook/dp/B002VUFKDY/ref=sr_1_3?ie=UTF8&amp;amp;qid=1333925660&amp;amp;sr=8-3&quot; title=&quot;Freedom&quot;&gt;Freedom™&lt;/a&gt;
are now required reading.&lt;/p&gt;

&lt;p&gt;I didn’t realize when I first saw it but xkcd’s April 1st comic was
actually quite &lt;a href=&quot;http://www.reddit.com/r/comics/comments/rnpiw/mindboggling_xkcd_april_fools_comic/&quot; title=&quot;April 1st xkcd&quot;&gt;brilliant&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some &lt;a href=&quot;http://cscs.umich.edu/~crshalizi/weblog/900.html&quot; title=&quot;Academic Talks&quot;&gt;good advice on giving academic talks&lt;/a&gt; under the twin
lenses of memory (your audience’s resource — build context and 
be wary of details) and fear (your resource — desensitize and
dissociate).&lt;/p&gt;

&lt;p&gt;An &lt;a href=&quot;https://priv.ly/&quot; title=&quot;Priv.ly&quot;&gt;interesting privacy hack&lt;/a&gt; for the web was announced and
&lt;a href=&quot;http://www.kickstarter.com/projects/229630898/protect-your-content-anywhere-on-the-web-privly&quot; title=&quot;Priv.ly kickstarter&quot;&gt;kickstarted&lt;/a&gt;. 
Amusingly enough they use Libya’s &lt;a href=&quot;http://en.wikipedia.org/wiki/CcTLD&quot; title=&quot;ccTLD&quot;&gt;ccTLD&lt;/a&gt; (&lt;a href=&quot;http://en.wikipedia.org/wiki/.ir&quot; title=&quot;Iran's ccTLD&quot;&gt;.ir&lt;/a&gt; or &lt;a href=&quot;http://en.wikipedia.org/wiki/.eg&quot; title=&quot;Egypt's ccTLD&quot;&gt;.eg&lt;/a&gt; would also have
been apropos). Hopefully they’ve thought about domain seizures.&lt;/p&gt;

&lt;p&gt;And on a related note, &lt;a href=&quot;http://m.vanityfair.com/culture/2012/05/internet-regulation-war-sopa-pipa-defcon-hacking.print&quot; title=&quot;World War 3.0&quot;&gt;here’s a good read on the internet wars&lt;/a&gt;.
In particular the forces of order (SOPA/PIPA, Iran, China, Egypt,
etc.) vs. the forces of chaos (Anonymous, LulzSec, etc.).&lt;/p&gt;

</content>
      <author>
        <name>Tim Disney</name>
        <uri>http://disnetdev.com/</uri>
      </author>
    </entry>
  
</feed>
