<?xml version="1.0" encoding="UTF-8"?> 
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        >
<channel>
  <title>asgaard</title>
  <description></description>
  <link>https://blog.asgaard.co.uk/2013/5</link>
  <lastBuildDate>Wed, 15 Apr 26 23:29:14 +0000</lastBuildDate>
  <language>en</language>
  <count>2</count>
  <offset>0</offset>
      <item>
    <title>My favourite JavaScript bug</title>
    <link>https://blog.asgaard.co.uk/2013/05/27/my-favourite-javascript-bug</link>
    <pubDate>Mon, 27 May 13 18:41:00 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2013/05/27/my-favourite-javascript-bug</guid>
    <description><![CDATA[
<p>
To be clear, this isn&#039;t a bug *in* JavaScript, it&#039;s a bug in my own code. But one which various properties of JavaScript made it both easy to write and very hard to detect later.
<p>
I am currently writing a LOLCODE interpreter in JavaScript. It&#039;s not overly complex - it just recursively evaluates an AST directly. 
<p>
LOLCODE allows for user defined functions:
<p>
<pre>HOW DUZ I ADD YR NUM1 AN NUM2
  SUM OF NUM1 AN NUM2
OIC

VISIBLE ADD 1 AN 3 MKAY  BTW =&gt; prints 4</pre>
<p>
The AST for the function definition looks something like:
<p>
<pre>{
    &quot;_name&quot;: &quot;FunctionDefinition&quot;,
    &quot;name&quot;: &quot;ADD&quot;,
    &quot;args&quot;: [
        &quot;NUM1&quot;,
        &quot;NUM2&quot;
    ],
    &quot;body&quot;: {
        &quot;_name&quot;: &quot;Body&quot;,
        &quot;lines&quot;: [
            {
                &quot;_name&quot;: &quot;FunctionCall&quot;,
                &quot;name&quot;: &quot;SUM OF&quot;,
                &quot;args&quot;: {
                    &quot;_name&quot;: &quot;A</pre>[...]]]></description>
    <content:encoded><![CDATA[
<p>
To be clear, this isn&#039;t a bug *in* JavaScript, it&#039;s a bug in my own code. But one which various properties of JavaScript made it both easy to write and very hard to detect later.
<p>
I am currently writing a LOLCODE interpreter in JavaScript. It&#039;s not overly complex - it just recursively evaluates an AST directly. 
<p>
LOLCODE allows for user defined functions:
<p>
<pre>HOW DUZ I ADD YR NUM1 AN NUM2
  SUM OF NUM1 AN NUM2
OIC

VISIBLE ADD 1 AN 3 MKAY  BTW =&gt; prints 4</pre>
<p>
The AST for the function definition looks something like:
<p>
<pre>{
    &quot;_name&quot;: &quot;FunctionDefinition&quot;,
    &quot;name&quot;: &quot;ADD&quot;,
    &quot;args&quot;: [
        &quot;NUM1&quot;,
        &quot;NUM2&quot;
    ],
    &quot;body&quot;: {
        &quot;_name&quot;: &quot;Body&quot;,
        &quot;lines&quot;: [
            {
                &quot;_name&quot;: &quot;FunctionCall&quot;,
                &quot;name&quot;: &quot;SUM OF&quot;,
                &quot;args&quot;: {
                    &quot;_name&quot;: &quot;ArgList&quot;,
                    &quot;values&quot;: [
                        {
                            &quot;_name&quot;: &quot;Identifier&quot;,
                            &quot;name&quot;: &quot;NUM1&quot;
                        },
                        {
                            &quot;_name&quot;: &quot;Identifier&quot;,
                            &quot;name&quot;: &quot;NUM2&quot;
                        }
                    ]
                }
            }
        ]
    }
}</pre>
<p>
The easiest way to evaluate this is not to do anything special to compile that function, but just to use the magic of JS to represent the evaluation of the FunctionDefinition node as an interpreter action in its own right, similarly to how we&#039;d evaluate an identifier or any other construct.
<p>
If we can create a function which represents the evaluation of &#039;ADD&#039;, then we have a nice consistency with evaluation of built-in functions, which are implemented natively, like SUM OF, e.g:
<p>
<pre>lol = function() {
    var self = this;

    this.symbols = {
      'SUM OF': function(a, b) { return a + b; }
    }


    var evalFuncDef = function(node) {
        self.symbols[node.name] = function() {
            return self.evaluate(node.body);
        }
    }

    this.evaluate(node) {
       // delegate to appropriate sub function
       if (node._name === 'FunctionDefinition') {
           return evalFuncDef(node);
       }
    }
}</pre>
<p>
I&#039;ve omitted setting up the argument list, figuring out the return value, etc, but the basic point is that both &#039;SUM OF&#039; (a native function) and &#039;ADD&#039; (a user supplied LOLCODE function) both exist in the symbol table in the form of an executable JavaScript function.
<p>
It&#039;s a fairly innocent looking piece of code.
<p>
Except for one thing.
<p>
The interpreter also has the ability to pause the program and evaluate watch-statements, like what you&#039;d find in Firebug or Chrome&#039;s debugger. For various uninteresting reasons<sup>1</sup>, the easiest way to do this is to clone the current symbol table and other scope into another interpreter and execute it there.
<p>
Something interesting happens here.
<p>
In the above code, I&#039;ve used the this/self idiom to get a reference to the current object into a nested function.
<p>
When we clone the symbol table into a different object, the self reference comes across unchanged. What that means is that the second interpreter happily executes any expression you give it correctly, until you supply it with one that tries to invoke a user written function. At this point, the <em>first</em> kicks into action, and continues executing. Imagine how difficult to debug this was - you can trace it all you want, you will see you&#039;re always in the right functions. The key is realising you&#039;ve suddenly switched to the wrong <em>object</em>, which in this case, always has very similar (if not identical) state!
<p>
The solution to this is obvious - don&#039;t rely on self inside the function we create, instead require it to be called with <code>symbols[node.name].call(this, ...)</code>. 
<p>
But the bug itself is admirable in its subtlety.
<br>
____
<br>
1. Mostly to do with keeping track of an awkward asynchronous callback. Call it an &#039;implementation issue&#039;.
<p>
]]></content:encoded>
  </item>
      <item>
    <title>RIP Tumblr</title>
    <link>https://blog.asgaard.co.uk/2013/05/20/rip-tumblr</link>
    <pubDate>Mon, 20 May 13 18:26:04 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2013/05/20/rip-tumblr</guid>
    <description><![CDATA[
<p>
Dear Tumblrians!
<p>
Take some advice from those of us who used Yahoo products for a while: move to a good platform. Wordress.com is good.
<p>
If Yahoo lives up to its past, here is what will happen to Tumblr:<ol><li>Yahoo will gradually try to their other products into it, or it into their other products somehow. They&#039;ll decide that the users want something different to what they chose to use and begin a slow plod towards something nobody wants. It will be buggy and unstable. If you complain loud enough, they&#039;ll tell you that Yahoo knows better than its users what its users want.</li><li>In nine months they&#039;ll realise that have completely failed to gain users or do anything interesting, and will begin to get bored with it. The half implemented changes will stay half implemented.</li><li>In 12 to 18 months they&#039;ll announce that it wasn&#039;t what they wanted to do after all. They&#039;ll announce no further development will occur. Bugs will suspiciously start to appear. Reliability will go down the pan. You&#039;ll ha</li></ol>[...]]]></description>
    <content:encoded><![CDATA[
<p>
Dear Tumblrians!
<p>
Take some advice from those of us who used Yahoo products for a while: move to a good platform. Wordress.com is good.
<p>
If Yahoo lives up to its past, here is what will happen to Tumblr:<ol><li>Yahoo will gradually try to their other products into it, or it into their other products somehow. They&#039;ll decide that the users want something different to what they chose to use and begin a slow plod towards something nobody wants. It will be buggy and unstable. If you complain loud enough, they&#039;ll tell you that Yahoo knows better than its users what its users want.</li><li>In nine months they&#039;ll realise that have completely failed to gain users or do anything interesting, and will begin to get bored with it. The half implemented changes will stay half implemented.</li><li>In 12 to 18 months they&#039;ll announce that it wasn&#039;t what they wanted to do after all. They&#039;ll announce no further development will occur. Bugs will suspiciously start to appear. Reliability will go down the pan. You&#039;ll have to refresh your page four times to get to the right content.</li><li>In about three years they&#039;ll eventually pull the plug. Probably at short term notice, giving nobody adequate chance to export all their content. If you&#039;re really unlucky they&#039;ll rub salt in the wound by launching a half-hearted alternative that&#039;s too rubbish to be useful to anyone.</li></ol>]]></content:encoded>
  </item>
  </channel>
</rss>