<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Olov Lassus</title>
  <link href="http://blog.lassus.se/"/>
  <link type="application/atom+xml" rel="self" href="http://blog.lassus.se/atom.xml"/>
  <updated>2015-06-04T13:55:34+02:00</updated>
  <id>http://blog.lassus.se/</id>
  <author>
    <name>Olov Lassus</name>
  </author>

  
  <entry>
    <id>http://blog.lassus.se/2015/06/sane-trim-on-osx</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2015/06/sane-trim-on-osx.html"/>
    <title>Sane TRIM on Mac OS X</title>
    <updated>2015-06-04T13:35:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;Using a third party SSD on your Mac while trying to keep TRIM enabled kind of drives you crazy, and OS X Yosemite didn’t help. Here’s my procedure for keeping my SSD reasonably trimmed while keeping my mind reasonably sane. It may work for you too.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Find an external disk, preferably a 2.5” one. If you’re like me then you have a bunch of spare 2.5” SATA disks on a shelf somewhere - pick a lucky winner and buy a $10 USB enclosure for it.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Install OS X 10.8 (10.9 may also work well) on the external disk. Boot it up (hold &lt;code&gt;alt&lt;/code&gt; upon boot to select it). Disable automatic OS X updates. Keep it offline because it is (or will become) obsolete in terms of security updates.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Enable TRIM via the hack you know of already. It doesn’t really matter how you do it - the “TRIM Enabler” app will work fine. Reboot and verify that System Report says “TRIM Support: Yes”.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once every 1 to 3 months, you’ll boot your Mac with the USB disk. Open Disk Utility, select your SSD partition(s) - &lt;em&gt;do not select the entire disk&lt;/em&gt; - click Repair Disk and wait a minute or two. The log should say “Trimming unused blocks.” You can reboot back into your main system immediately. The whole procedure should just take a couple of minutes.&lt;/p&gt;

&lt;p&gt;I found this to strike a nice balance between sanity and perf. Oh and yes, any TRIM-aware SSD will benefit from this treatment - however great its GC may be. Good luck!&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2013/10/little-dunder-proto</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2013/10/little-dunder-proto.html"/>
    <title>Little dunder proto</title>
    <updated>2013-10-15T13:50:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;Hi, this is your son’s school. We’re having some computer trouble.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Oh dear - did he break something?&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In a way. Did you really name your son &lt;code&gt;__proto__&lt;/code&gt;?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Oh yes. Little dunder proto, we call him.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Well, our administrative web application is completely broken now. I hope you’re happy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;And I hope you’ve learned to use &lt;a href=&quot;https://github.com/olov/stringmap&quot;&gt;stringmap.js&lt;/a&gt; because objects aren’t stringmaps.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Also &lt;a href=&quot;https://github.com/olov/stringset&quot;&gt;stringset.js&lt;/a&gt;. They resemble ES6 &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt; API-wise, add minimal overhead and work everywhere.&lt;/p&gt;

&lt;p&gt;In tribute to &lt;a href=&quot;http://xkcd.com/327/&quot;&gt;xkcd 327 - Exploits of a Mom&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=6552823&quot;&gt;Show comments (Hacker News)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2013/05/defsjs</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2013/05/defsjs.html"/>
    <title>ES3 <3 block scoped const and let => defs.js</title>
    <updated>2013-05-04T17:29:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I released &lt;a href=&quot;https://github.com/olov/defs&quot; title=&quot;Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars&quot;&gt;defs.js&lt;/a&gt; at the Front-Trends conference last week. Defs transpiles ES6 block scoped &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; declarations to beautiful ES3 code using &lt;code&gt;var&lt;/code&gt;s. It’s also a pretty decent linter when it comes to variable and scope analysis.&lt;/p&gt;

&lt;h3 id=&quot;es6_block_scoped_let_and_const&quot;&gt;ES6 block scoped let and const&lt;/h3&gt;

&lt;p&gt;Let me give you some background on why I created Defs. One of my favorite features in the upcoming ES6 language is block scoped &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variable declarations (&lt;a href=&quot;http://wiki.ecmascript.org/doku.php?id=harmony:block_scoped_bindings&quot;&gt;harmony wiki page&lt;/a&gt;). A while back I decided to start using them in my back-end code. This is possible thanks to &lt;code&gt;node --harmony&lt;/code&gt;. Run it like so, make sure your code is in strict mode and off you go! With that a large JS annoyance (function scope) was gone with the wind, and I got to use &lt;code&gt;const&lt;/code&gt; as a bonus - which turned out to work remarkably well with my JS style of programming. Nowadays mostly all of my node code is written in ES5 + cherry picked ES6 &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; features. Let’s call this &lt;code&gt;constlet&lt;/code&gt; style JS for now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;constlet&lt;/code&gt; style JS is great, and I highly encourage you to try it out. It makes me much happier as a programmer and it makes my code much prettier and easier to reason about. Are you still reading? Up with a console and try it out in node! Then come back.&lt;/p&gt;

&lt;h3 id=&quot;const_is_the_norm&quot;&gt;const is the norm&lt;/h3&gt;

&lt;p&gt;That block scoped variables is an awesome thing is no news for those of you that have a &lt;code&gt;{}&lt;/code&gt; background. &lt;code&gt;let&lt;/code&gt; is a better &lt;code&gt;var&lt;/code&gt; in every respect so I won’t pitch it further. &lt;code&gt;let&lt;/code&gt; is awesome. &lt;code&gt;const&lt;/code&gt;, on the other hand, may seem unimpressive at a glance. If you’re not used to it then it may seem like something to use for the rare occasions when we define a numerical constant such as &lt;code&gt;const godspeed = Infinity&lt;/code&gt;. But it’s so much more than that.&lt;/p&gt;

&lt;p&gt;A striking thing that happens when you start using &lt;code&gt;const&lt;/code&gt; is that you realize that the vast majority of your old &lt;code&gt;var&lt;/code&gt;s were initialized but never modified (don’t confuse this with deep immutability which is a different story). Non-mutating variables are the norm, so they should have the default variable declaration keyword - which is &lt;code&gt;const&lt;/code&gt; in ES6. For those variables that you modify, be it accumulators, loop indices or whatever - you make those stand out by using &lt;code&gt;let&lt;/code&gt;. Once you get hooked on this style it’s impossible to go back. Ask any Scala-slinging friend of yours about &lt;code&gt;val&lt;/code&gt; (~&lt;code&gt;const&lt;/code&gt;) and &lt;code&gt;var&lt;/code&gt; (~&lt;code&gt;let&lt;/code&gt;). There are people swearing by this in Java-land too, finalista’s I’ve heard some of them call themselves because of the &lt;code&gt;final&lt;/code&gt; keyword they have to add everywhere. To give you an idea of how common &lt;code&gt;const&lt;/code&gt; is: The source code for &lt;a href=&quot;https://github.com/olov/defs&quot; title=&quot;Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars&quot;&gt;defs.js&lt;/a&gt; has a const coverage of over 80%, which means that I used 4 &lt;code&gt;const&lt;/code&gt;s for every &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; is not the new &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; is.&lt;/p&gt;

&lt;h3 id=&quot;defs_transpiles_to_beautiful_es3&quot;&gt;Defs transpiles to beautiful ES3&lt;/h3&gt;

&lt;p&gt;So. Use &lt;code&gt;constlet&lt;/code&gt; style JS and be happy, right? Except for the browser. We need to be able to run our client-side code in current browsers (so ES3 or ES5). Until now there was no decent way of transpiling &lt;code&gt;constlet&lt;/code&gt; style JS back to ES3. Traceur and others use a style where a myriad of &lt;code&gt;try&lt;/code&gt;-&lt;code&gt;catch&lt;/code&gt; statements are generated which is as bad on your eyes as it is on your VM. This is no criticism on Traceur which is a very ambitious project with a lot of features to cover. Traceur will surely improve also when it comes to block scope.&lt;/p&gt;

&lt;p&gt;So I created &lt;a href=&quot;https://github.com/olov/defs&quot; title=&quot;Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars&quot;&gt;defs.js&lt;/a&gt;, a transpiler that takes &lt;code&gt;constlet&lt;/code&gt; style JS and spits out plain-old ES3 code, as beautiful as its input and maximally non-intrusive. It works by doing static analysis, changing all &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; declarations to &lt;code&gt;var&lt;/code&gt;, and an occasional variable rename here and there. The output looks more or less identical to the code you would have written with &lt;code&gt;var&lt;/code&gt;s in the first place. Same line-numbers, easy debugging and no lock-in shall you decide to go back to the world of &lt;code&gt;var&lt;/code&gt;s. Because Defs needs to do a bunch of scope analysis anyways, it ended up also being a pretty decent linter when it comes to variable and scope analysis.&lt;/p&gt;

&lt;h3 id=&quot;try_defs&quot;&gt;Try Defs&lt;/h3&gt;

&lt;p&gt;Install Defs by &lt;code&gt;npm install -g defs&lt;/code&gt;, then run &lt;code&gt;defs mycode.js&lt;/code&gt; and there’s your transpiled code on stdout or your errors on stderr. There’s more info about configuration such as how to tell Defs about your globals (if you want it to analyse those) in the &lt;a href=&quot;https://github.com/olov/defs/blob/master/README.md&quot;&gt;defs.js README&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want to toy around directly? Bring up your browser’s JS console and copy and paste this: &lt;code&gt;defs(&amp;quot;const x = 1&amp;quot;).src&lt;/code&gt;. It should give you &lt;code&gt;&amp;quot;var x = 1&amp;quot;&lt;/code&gt; in return. Try &lt;code&gt;defs(&amp;quot;const x = 1; { let x = 2; }&amp;quot;).src&lt;/code&gt;, which should return &lt;code&gt;&amp;quot;var x = 1; { var x$0 = 2; }&amp;quot;&lt;/code&gt;, a rename example. Try more advanced examples. Try modifying a &lt;code&gt;const&lt;/code&gt;, try creating two &lt;code&gt;const&lt;/code&gt;s (or &lt;code&gt;let&lt;/code&gt;s) with the same name in the same block scope or try accessing a &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt; before it has been initialized.&lt;/p&gt;

&lt;p&gt;I know of only one use case that cannot be handled with renaming, namely loop closures - i.e. a closure that references a variable that is declared outside of its own scope but inside the loop. An example would be:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;let&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;10&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;++&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;kd&#39;&gt;let&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;y&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;c1&#39;&gt;// we can skip this when v8 issue 2560 is closed&lt;/span&gt;
    &lt;span class=&#39;nx&#39;&gt;arr&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;push&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;return&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;y&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;});&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this ES6 code &lt;code&gt;let y&lt;/code&gt; is a new binding per iteration while a transformed &lt;code&gt;var&lt;/code&gt; won’t be. If you run this through &lt;code&gt;defs&lt;/code&gt; it will refuse to transpile it and give you this error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;line 3: can&amp;#39;t transform closure. y is defined outside closure, inside loop&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The solution is to do as we already to in our current ES3 or ES5 code, create the binding manually:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;let&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;10&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;++&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;y&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
        &lt;span class=&#39;nx&#39;&gt;arr&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;push&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;return&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;y&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;});&lt;/span&gt;
    &lt;span class=&#39;p&#39;&gt;})(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Defs will happily transpile that.&lt;/p&gt;

&lt;h3 id=&quot;toolchain_and_convenience&quot;&gt;Toolchain and convenience&lt;/h3&gt;

&lt;p&gt;I can already hear people saying that they’d like to try this kind of thing but that the save-and-refresh development cycle is precious. Fear not. I guess that most aren’t running their code locally using &lt;code&gt;file://&lt;/code&gt; but use a local HTTP server, in which case you may have the opportunity of hooking a toolchain (including Defs) into it. If you serve using node this should be very simple.&lt;/p&gt;

&lt;p&gt;It’s even simpler if you use Chrome. Go to &lt;code&gt;chrome://flags&lt;/code&gt;, check Enable experimental JavaScript and you’ve got the same support for &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; as you have with &lt;code&gt;node --harmony&lt;/code&gt;. Just remember that you must &lt;code&gt;&amp;quot;use strict&amp;quot;&lt;/code&gt; for it to work.&lt;/p&gt;

&lt;p&gt;Defs works great with James Halliday’s excellent &lt;a href=&quot;http://browserify.org/&quot;&gt;Browserify&lt;/a&gt;. Here’s how I create the defs_bundle.js file that’s included in this page (view source):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;browserify -r &amp;quot;./defs-main&amp;quot; &amp;gt; defs_bundle_constlet.js
# defs_bundle_constlet.js contains defs.js and all its dependencies

# defs-config.json:
# we allow duplicated vars because of defs dependencies
{
    &amp;quot;environments&amp;quot;: [&amp;quot;browser&amp;quot;],
    &amp;quot;globals&amp;quot;: {
        &amp;quot;define&amp;quot;: false,
        &amp;quot;require&amp;quot;: true
    },
    &amp;quot;disallowDuplicated&amp;quot;: false
}

defs defs_bundle_constlet.js &amp;gt; defs_bundle.js&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Things will be even easier once we have a Browserify plugin for Defs.&lt;/p&gt;

&lt;h3 id=&quot;underpinnings&quot;&gt;Underpinnings&lt;/h3&gt;

&lt;p&gt;Defs uses Ariya Hidayat’s &lt;a href=&quot;http://esprima.org&quot;&gt;Esprima&lt;/a&gt; parser to create an AST from source code. Defs performs static analysis on that AST and spits out errors or transformed source code. Static analysis is the hard part of Defs’ work, the actual source transformation is pretty trivial after that and it can be done in one fast textual shot using &lt;a href=&quot;https://github.com/olov/alter&quot;&gt;alter.js&lt;/a&gt;. If you wonder why I didn’t use &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; to create Defs then it’s for exactly that reason. Defs need a rock-solid and well-maintained parser. Esprima is that, my forked version of Narcissus I used for JSShaper is not. Thanks for Esprima Ariya, I have a couple of &lt;code&gt;constlet&lt;/code&gt; parser bugs coming your way soon. :)&lt;/p&gt;

&lt;h3 id=&quot;scope_of_the_defs_project&quot;&gt;Scope of the Defs project&lt;/h3&gt;

&lt;p&gt;The scope [sic] of the Defs project is small on purpose. It transpiles &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; back to ES3 in a maximally non-intrusive manner, it performs static code analysis related to scope, and nothing else. Defs is not likely to take on other ES6 features.&lt;/p&gt;

&lt;h3 id=&quot;presentation&quot;&gt;Presentation&lt;/h3&gt;

&lt;p&gt;Feel free to check out my Front Trend presentation &lt;a href=&quot;http://blog.lassus.se/files/lets_const_together_ft2013.pdf&quot;&gt;LET’s CONST together, right now (with ES3)&lt;/a&gt; for more info about Defs.&lt;/p&gt;

&lt;h3 id=&quot;license&quot;&gt;License&lt;/h3&gt;

&lt;p&gt;MIT because others are generous so I want to be generous too.&lt;/p&gt;

&lt;h3 id=&quot;try_it_break_it_help_fix_it_dont_hate_it&quot;&gt;Try it, break it, help fix it don’t hate it&lt;/h3&gt;

&lt;p&gt;I hope that I triggered some interest in checking out what &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; could do for your code base. Use it with &lt;code&gt;node --harmony&lt;/code&gt; and then consider using Defs to bring the same features to your client code. Check out the &lt;a href=&quot;https://github.com/olov/defs&quot; title=&quot;Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars&quot;&gt;github project&lt;/a&gt; and &lt;a href=&quot;https://npmjs.org/package/defs&quot;&gt;npm package&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let me know what works and what doesn’t. File issues or even better send pull requests. JS needs &lt;code&gt;&amp;lt;3&lt;/code&gt; and so does Defs. Peace and happy hacking! &lt;code&gt;\o/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/1doja5/es3_3_block_scoped_const_and_let_defsjs/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;
&lt;script src=&#39;/scripts/defs_bundle.js&#39;&gt; &lt;/script&gt;&lt;script&gt;var defs = require(&quot;./defs-main&quot;)&lt;/script&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/06/taking-the-leap</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/06/taking-the-leap.html"/>
    <title>Taking the leap</title>
    <updated>2012-06-06T23:40:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I’m taking the leap. I’m starting my own software company.&lt;/p&gt;

&lt;p&gt;I’ve been working with IP-TV set top boxes for the last 6 years; first with Kreatel Communications, then aquired by Motorola, then split into Motorola Mobility and now recently acquired by Google. I came to Kreatel as a software engineer and since two years back I’ve had the luxury of leading our Technology Group, meaning our research and prototyping work. It’s been great, and it still is.&lt;/p&gt;

&lt;p&gt;In my previous life before all this I did computer and console games, starting with the 3d shooter &lt;a href=&quot;http://www.mobygames.com/game/substation&quot;&gt;SubStation&lt;/a&gt; for &lt;a href=&quot;http://en.wikipedia.org/wiki/Atari_STE&quot;&gt;Atari STE&lt;/a&gt; two decades ago and followed by PC, PSX and XBOX development full time a few years around the millenium.&lt;/p&gt;

&lt;p&gt;I then attended the Linköping University (one decade ago, in 2002), and got my M.Sc. four years after. I learned tons there. It brought me to Lisp, gave me a deeper understanding of hardware all the way down and allowed me to experiment with CPU &amp;amp; computer design/implementation in FPGA’s. And I finally really learned the maths behind the 3d-games I used to write.&lt;/p&gt;

&lt;p&gt;My finishing stretch of the University also came to define my spare time programming to come. I got into JavaScript by accident while researching how to create richer programming environments that turns the live-knob to eleven (where Smalltalk, Lisp Machines and modern edit-and-continue are well below ten), see &lt;a href=&quot;http://blog.lassus.se/2011/10/describing-live-programming-again.html&quot;&gt;Describing Live programming (again)&lt;/a&gt;. I’d love to revisit that topic some day.&lt;/p&gt;

&lt;p&gt;My more recent spare-time programming led me to creating &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;Restrict mode for JavaScript&lt;/a&gt; and the &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; source transformation framework, a year and a half ago. The big difference here compared to my previous spare time projects and ideas (that mostly remained ideas) was that this time, I decided to just do something, release the code as permissive open source, and see what happens. Testing the &lt;em&gt;do good and good will come to you&lt;/em&gt; thesis, if you will. Somehow that must have worked because I got invited to speak at &lt;a href=&quot;http://www.responsive.se/devlin/&quot;&gt;DevLin&lt;/a&gt;, &lt;a href=&quot;http://2012.front-trends.com/&quot;&gt;Front-Trends&lt;/a&gt; and &lt;a href=&quot;http://webshaped.fi/&quot;&gt;Webshaped&lt;/a&gt; all in the last three months. I met a lot of interesting people there and I got to speak about things I’m deeply passionate about. In my book that’s as &lt;em&gt;good&lt;/em&gt; as it gets.&lt;/p&gt;

&lt;p&gt;I should mention Dart too. It resonates with me a lot, obviously when it comes to certain aspects of language semantics and &lt;em&gt;easier to reason about&lt;/em&gt; programming, but more importantly in the direction I believe that they intend to push their development environment. There is a lot of &lt;a href=&quot;http://blog.lassus.se/2011/10/describing-live-programming-again.html&quot;&gt;Live Programming&lt;/a&gt; like thinking there. Gilad Bracha’s recent example of creating a test that fails with a noSuchMethod, then gradually filling out the implementation without ever stopping the program - while the context is right in front of the programmer, is similar to one I gave in my &lt;a href=&quot;http://blog.lassus.se/files/liveprogramming.pdf&quot; title=&quot;Live programming thesis PDF&quot;&gt;thesis&lt;/a&gt;. So yeah - it resonates with me, a lot.&lt;/p&gt;

&lt;p&gt;With the same idea of just &lt;em&gt;doing something&lt;/em&gt; to see what happens, I got involved with Dart early on. I thought of creating a JSShaper for Dart but got distracted partly by an increased community interest in my JS projects. So far my most useful contribution to Dart is a &lt;a href=&quot;http://blog.lassus.se/2012/04/call-for-testing-pygments-dart-highlighter.html&quot;&gt;syntax highlighter&lt;/a&gt;. Speaking about Dart. I’ve had the opportunity to meet with a few members of the team, including Peter Ahé, Mads Ager, Kasper Lund and Lars Bak. It’s well known that they are VM experts but I quickly realized, especially so during discussions with Peter, that their ideas about what programming should be expands well beyond VM’s and language semantics. I hope to be able to play with a Live Programming like Dart environment one day. Three more things about Dart. (1) Any community should have a Bob Nystrom. He’s the money. My wife quotes him now and then (“I feel all giggly inside” - regarding the syntax highlighter running live at Github), which is kind of weird. (2) The haters got at least one thing right: Dart isn’t a silver bullet for &lt;em&gt;everything&lt;/em&gt; web, because it doesn’t have a great JS interop story yet and perhaps never will. (3) If you think that Dart is mostly trying to solve a performance issue, think again.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;By the way - I can talk/rant for hours about programming environments and what programming today is vs what it used to be decades ago already, and where we could go should we just prioritize it. Invite me and I’ll come talk about it. Or programming from an “easier to reason” about perspective. Or restrict mode and JSShaper. Or making superfast sprite routines for the Atari STE by generating op codes from pixel data. I like conferences!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And now, I’m starting my own software company. I’m lucky enough to have Motorola wanting me to stay part time, allowing me to keep doing the things I love there while at the same time taking a leap to grab a specific opportunity with my own company.&lt;/p&gt;

&lt;p&gt;What I’ll do? A software product, not related to anything above. It’s for companies within a niche market, where it should simplify things and increase productivity a lot. So in that way similar to restrict mode and Live Programming, but not programming. It’ll be a web app and written in JavaScript using &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrict mode&lt;/a&gt;. I don’t know where it’ll take me but I’m putting my bets on &lt;em&gt;do good and good will come to you&lt;/em&gt; once again.&lt;/p&gt;

&lt;p&gt;I’m taking the leap, and I can’t wait to get started.&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/05/jsshaper-grows-up-but-stays-fit-and-fun</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/05/jsshaper-grows-up-but-stays-fit-and-fun.html"/>
    <title>JSShaper grows up but stays fit and fun</title>
    <updated>2012-05-22T00:42:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I created &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; a year and a half ago because I needed a way to do source-preserving JS-to-JS transformations so that I could implement &lt;code&gt;restricter&lt;/code&gt;, the &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;Restrict mode for JavaScript&lt;/a&gt; checker. There was nothing else out there so I ended up creating Shaper. Some of the defining (and often unique) features of Shaper at the time was its simplicity of AST traversals (one function, not dozens of visitors), its precise weaved token and node representation that facilitates repeated tranformations on the node level at O(1) complexity, its declarative matching based on templates and wildcards that lets you describe the template as JavaScript code but still matches on the node level, its corresponding declarative replace function that lets you rewrite fragments of a tree without knowing the details of its inner property-structure, the smart and mostly closure compiler compatible binding of comments to nodes, the notion of pluggable Shape(r) pipelines, and more. Those are still defining features, and still often unique. While I created Shaper mostly for &lt;code&gt;restricter&lt;/code&gt;, I decided to write a bunch of other plugins as well to show as examples if nothing else. One of the simplest of these ended up being one of the most useful, namely &lt;code&gt;asserter&lt;/code&gt;. Check out &lt;a href=&quot;http://blog.lassus.se/2011/03/c-style-assertions-in-javascript-via.html&quot;&gt;C-style assertions in JavaScript via JSShaper&lt;/a&gt; for more information about it.&lt;/p&gt;

&lt;p&gt;I wanted to share with you some recent good stuff happening in Shaper land.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/cscott&quot;&gt;C. Scott Ananian&lt;/a&gt; has created the most advanced Shaper plugin yet, &lt;a href=&quot;https://github.com/cscott/jsshaper/blob/master/src/plugins/yielder/yielder.js&quot;&gt;yielder&lt;/a&gt;, which transforms &lt;code&gt;yield&lt;/code&gt; generators (JavaScript 1.8) into plain ES5. Impressive work. I hope to be able to merge it into Shaper master soon, but for now use cscott’s repo.&lt;/p&gt;

&lt;p&gt;cscott also contributed a number of fixes to Shaper, imported a newer version of Narcissus and rebased all my modifications on top of it. If you find any valid JS that Shaper fails parsing or round-tripping (&lt;code&gt;node run-shaper.js yourfile.js --source&lt;/code&gt;) then please open an issue and we’ll fix it.&lt;/p&gt;

&lt;p&gt;Finally, cscott performed &lt;a href=&quot;http://requirejs.org/docs/whyamd.html&quot; title=&quot;Why AMD&quot;&gt;AMD&lt;/a&gt;-fication surgery on the Shaper code base. I initially supported running Shaper through any shell you could think of, using the technique described in &lt;a href=&quot;http://blog.lassus.se/2011/04/javascript-modules-in-shells-browsers.html&quot;&gt;JavaScript modules in shells, browsers, everywhere&lt;/a&gt;. While cute, it was cumbersome and we decided to drop support for anything but node.js and the browser. &lt;a href=&quot;http://requirejs.org/docs/whyamd.html&quot; title=&quot;Why AMD&quot;&gt;AMD&lt;/a&gt; was a nice match; it works great in node and great in the browser, and the r.js tool is more convenient then concatenating files together. I also want to mention that &lt;a href=&quot;https://github.com/int3&quot;&gt;Jez Ng&lt;/a&gt; participated in pushing node-compatibility forward, although that patch never landed. Another one of his patches did though.&lt;/p&gt;

&lt;p&gt;Noah Sloan independently created &lt;a href=&quot;https://github.com/iamnoah/jmvc-3.2-upgrade-assistant&quot;&gt;jmvc-3.2-upgrade-assistant&lt;/a&gt;, a Shaper based script for helping upgrade from JavaScriptMVC 3.1 to JavaScriptMVC 3.2. Looks awesome!&lt;/p&gt;

&lt;p&gt;I was at &lt;a href=&quot;http://webshaped.fi&quot;&gt;webshaped.fi&lt;/a&gt; last week presenting &lt;a href=&quot;http://blog.lassus.se/files/javascript_the_subsets_we_use_webshaped.pdf&quot;&gt;JavaScript: the subsets we use&lt;/a&gt; (including &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;Restrict mode for JavaScript&lt;/a&gt; and a little bit of Shaper) and met a guy who who told me that his company uses Shaper similar to Noah, for automatically migrating code between different framework versions. Whoever you are I’m terrible at names so please ping me on Twitter and let’s keep in touch!&lt;/p&gt;

&lt;p&gt;I gave the same talk at &lt;a href=&quot;http://2012.front-trends.com/&quot;&gt;Front-Trends&lt;/a&gt; the month before, which was my international conference speaking debut. Both talks were recorded so I’ll post the links once available. If you want me to come talk somewhere about coding with a focus on “easier to reason about”, restrict mode, Shaper or something related just contact me! I can easily do an hour presentation about Shaper and the concepts behind it, for example.&lt;/p&gt;

&lt;p&gt;Creating Shaper plugins is very fun, and not that hard. Try it! To get started I recommend checking out the sources for &lt;code&gt;asserter&lt;/code&gt; and &lt;code&gt;stringconverter&lt;/code&gt; (simple transformational plugins) or &lt;code&gt;bitwiser&lt;/code&gt; (simple static analysis plugin). You’ll find these in the &lt;code&gt;plugins/&lt;/code&gt; directory. Then check out everything in the &lt;code&gt;examples/&lt;/code&gt; directory as well.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thank you&lt;/em&gt; for trying out, contributing to, writing about or filing issues on &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Peace!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/ty9ss/jsshaper_grows_up_but_stays_fit_and_fun/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/04/call-for-testing-pygments-dart-highlighter</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/04/call-for-testing-pygments-dart-highlighter.html"/>
    <title>Call for testing: Pygments Dart highlighter</title>
    <updated>2012-04-22T13:30:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;As mentioned before, the Dart highlighter lives in the upstream Pygments repository now and is included in releases since 1.5 (released Mar 10, 2012). I will try to keep it correct and updated.&lt;/p&gt;

&lt;p&gt;Another milestone is that Github recently started using it! I found a few bugs just by clicking around in people’s repositories. The most obvious was keyword-detection on substrings, i.e. the &lt;code&gt;String&lt;/code&gt; substring of &lt;code&gt;StringBuffer&lt;/code&gt; got marked as a keyword. Another was failing to recognize escape characters inside string literals. Both are now fixed in my repo, so the example below renders fine.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;n&#39;&gt;main&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;StringBuffer&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;sb&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;c1&#39;&gt;// String isn&amp;#39;t a keyword&lt;/span&gt;
  &lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;integer&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;c1&#39;&gt;// nor is int&lt;/span&gt;
  &lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;str&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;Escapes &lt;/span&gt;&lt;span class=&#39;se&#39;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;just works&lt;/span&gt;&lt;span class=&#39;se&#39;&gt;\&amp;quot;&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt; now&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I need help with testing the highlighter to find bugs or missing features. I’m maintaining the Dart highlighter in my &lt;a href=&quot;https://bitbucket.org/olov/pygments-main&quot;&gt;bitbucket repo&lt;/a&gt;. You can help by downloading it, trying it out on your own code and letting me know what works or not:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;text&#39;&gt;hg clone https://bitbucket.org/olov/pygments-main
cd pygments-main
./pygmentize -f html -o yourfile.html yourfile.dart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don’t have Mercurial (&lt;code&gt;hg&lt;/code&gt;) installed then you can also click “get source” on &lt;a href=&quot;https://bitbucket.org/olov/pygments-main&quot;&gt;bitbucket&lt;/a&gt; to download the source as a zip-file. Running &lt;code&gt;pygmentize&lt;/code&gt; will create markup from your source code, which you then need to combine with a style sheet to get some color going. Feel free to copy my color scheme (&lt;a href=&quot;http://blog.lassus.se/stylesheets/syntax.css&quot;&gt;syntax.css&lt;/a&gt;) if you like it. You can also run &lt;code&gt;pygmentize yourfile.dart&lt;/code&gt; without &lt;code&gt;-f&lt;/code&gt; and &lt;code&gt;-o&lt;/code&gt; to get highlighted text directly on your console.&lt;/p&gt;

&lt;p&gt;To install the development version of pygments system-wide just run &lt;code&gt;sudo ./setup.py install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;File issues on &lt;a href=&quot;https://bitbucket.org/olov/pygments-main&quot;&gt;bitbucket&lt;/a&gt; or fire a mail to the &lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a65d1625f9b98fb/&quot;&gt;Dart misc thread&lt;/a&gt; or me directly. Even better, submit a pull request! :)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a65d1625f9b98fb/&quot;&gt;Show comments (Dart misc)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/04/meteor-meets-nogpl</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/04/meteor-meets-nogpl.html"/>
    <title>Meteor meets NoGPL</title>
    <updated>2012-04-13T09:30:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;Update 2012-04-21: Meteor has &lt;a href=&quot;http://meteor.com/faq/how-is-meteor-licensed&quot;&gt;switched&lt;/a&gt; to the permissive MIT license.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.meteor.com&quot;&gt;Meteor&lt;/a&gt; made a big splash this week. Check out their screencast if you haven’t already. For “real-time” web applications it really seems to simplify things a whole lot. The team behind it seems plenty experienced and they are building a business on Meteor by going with the GPL/commercial dual-license approach made popular by MySQL and Trolltech/Qt. The basic idea is that the source code is publicly available for people to look at and modify and that it costs nothing to use as long as you’re building a GPL application with it and give out your source code too. Alternatively, it can be licensed commercially like any other proprietary product. Dual-licensing is not the only way to monetize open source development - a popular alternative is to offer support, training and consulting, like 10gen does for mongoDB. Anyways, I think Meteor is plenty cool and I wish them the best of luck!&lt;/p&gt;

&lt;p&gt;The web development community hasn’t really taken GPL libraries/frameworks to heart, neither client- nor server side. Let me elaborate on that.&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href=&quot;https://github.com/languages/JavaScript/most_watched&quot;&gt;most watched JavaScript&lt;/a&gt; repositories on Github. Every single one on the first page is available under a &lt;a href=&quot;http://en.wikipedia.org/wiki/Permissive_free_software_licence&quot;&gt;permissive&lt;/a&gt; license. In a nutshell permissive licenses often require attribution but never require source code redistribution. Popular permissive licenses are the 2/3-clause BSD, MIT and Apache 2.0. Contrast this with the GPL whose &lt;a href=&quot;http://en.wikipedia.org/wiki/Copyleft&quot;&gt;copyleft&lt;/a&gt; nature spreads to derived works. Back to those popular JavaScript repos. The list includes Node.js, Bootstrap, jQuery, Backbone.js, Modernizr, Three.js, Express, Socket.IO and plenty more. Node.js is built upon V8, OpenSSL, libuv, libev, libeio and other libraries. Permissive. So is the vast majority of the NPM ecosystem.&lt;/p&gt;

&lt;p&gt;Let’s check out some server-side frameworks in other languages. Rails, Django, Tornado, Play, the list goes on. Permissive.&lt;/p&gt;

&lt;p&gt;What about the data stores? Server-side code typically connects with those through client/driver libraries of some sort. PostgreSQL, CouchDB, Redis, Riak, Cassandra, Memcached, SQLite – all permissive licenses. MongoDB is AGPL but the client is permissive (Apache) so there’s no copyleft affecting your program that talks to the DB. MySQL is more complex. After all it was the product that defined dual-licensing so that’s not entirely unexpected. The DB itself is GPL, but the client library is LGPL in really old versions, GPL with a FLOSS exception in versions prior to 5.1.55 and plain GPL in recent versions released by Oracle. Oracle also provides a PHP client library under the PHP license. You can also acquire a commercial license of MySQL, with a non-copyleft client library. So whether you can write a proprietary program that talks to MySQL, depends. That’s a whole lot of babbling about data stores licenses, pardon.&lt;/p&gt;

&lt;p&gt;Not everything is permissive of course. A good amount of projects use limited-copyleft licenses such as the MPL, EPL and LGPL. The intent is almost always to allow for proprietary usage of the library/framework/program, but to mandate that modifications and additions to the project itself get contributed back, to various extents. EPL is particularly common in Java-camps (so is the permissive Apache 2.0 mentioned earlier). Mozilla use MPL for most (all?) of their stuff, and it also happens to be the most permissive of the three.&lt;/p&gt;

&lt;p&gt;And then we have the regular copyleft licenses, most notably the GPL, which isn’t that popular when it comes to web development libraries and frameworks. Sencha’s ExtJS framework is GPL/commercial dual-licensed. Many avoid it for that reason, and for those there are similar projects available under permissive licenses. Wordpress is a another GPL project that is very popular.&lt;/p&gt;

&lt;p&gt;If we start looking under the hood, there’s a good amount of permissive licensing going on in other parts of the web stack. PHP, Ruby, Python, Apache HTTPD and nginx are all permissive. There’s also a good amount of GPL and LGPL, most notably the Linux kernel and the GNU userland.&lt;/p&gt;

&lt;p&gt;Let’s focus again on client- and server side web development libraries/frameworks. By far and large, the web developer community seems to prefer permissive licenses for that. Permissive licenses are proprietary-friendly, and many web developers write proprietary programs for a living. Many also don’t have a problem with giving out their code for others to use however they wish, save for some attribution. Most importantly, many consider the idea of proprietary programs and services to be fine, sound, ethical and all that. It’s also fairly obvious that permissive OSS projects can attract huge contributions even without any help from a license that mandates it. Imagine Node.js or Rails being GPL. Would they have received more or less contributions, you think?&lt;/p&gt;

&lt;p&gt;I hope you won’t blame me for calling web developers “rewrite-happy”. Heck, the entire programmer population is, to some extent. Someone takes the ideas of a library or framework and rewrites it in another language. Someone else rewrites it from scratch just because he/she (hen!) was unhappy with a particular design decision. Competing frameworks overlap. Just look at the throne of JS conference. Now this is both good and bad. Good in the sense that it’s very evolutionary and that it constantly pushes innovation forward. Bad when yet another author loses yet another potential contributor to yet another similar-looking 0-day friendly competitor. But it’s mostly good.&lt;/p&gt;

&lt;p&gt;The publicly available Meteor code base is licensed as GPL (v2). Any application you write using it - definitively the client part and quite likely the server part as well (due to Meteor’s tightly intertwined client and server architecture) - any such application must be released under GPL compatible terms. I.e. you must hand out the source code, and no you can’t obfuscate it. If you want to write a proprietary program then you need to have a discussion with the Meteor team about how to acquire a commercial license. Combining rewrite-happy with a preference for permissive licenses, I think it’s reasonable to expect existing project to adapt a few of Meteor’s core features and design ideas, and new ones popping up as friendly competitors. Hardly shocking.&lt;/p&gt;

&lt;p&gt;The thing about Meteor is that they really want to establish an ecosystem of community-contributed smart packages, harnessing the goodness of existing Node.js and JavaScript server- and client side libraries. Those smart packages must be GPL or GPL compatible (most permissive licenses are). For Meteor to be able to include a contributed smart package with their commercial offering, the contributed smart package must either use a permissive license or come with a separate contributor agreement that allows Meteor to ship it commercially. Otherwise the GPL backfires in the sense that your contributed GPL smart package cannot be combined with a proprietary version of Meteor.&lt;/p&gt;

&lt;p&gt;The copyleft (viral, contaminating, whatever you want to call it) aspect of GPL is tricky. Take MongoDB as an example. Meteor uses it by importing the node-mongodb-native package (&lt;code&gt;require(&amp;#39;mongodb&amp;#39;)&lt;/code&gt;). That one is Apache 2.0 licensed, which is a permissive license that happens not to be compatible with Meteor’s GPL (v2) license, at least not according to the FSF. Tricky. Dependency chains, bindings between JS ↔ C and RPC makes it trickier even. I wouldn’t be surprised to see Meteor change to a GPL + a-bunch-of-OSS-exceptions license similar to what Qt and MySQL used to have, to avoid issues like this.&lt;/p&gt;

&lt;p&gt;I’m curious to find out to what extent the Meteor ecosystem will attract outside contributors, and to what extent people will start using the GPL version of Meteor in their projects (accepting the copyleft). Copyleft code grows from one project to another because it must or else the license screams VIOLATION. Permissive code grows from one project to another by culture and an honor system. It’s like, I used all this code and all that was required by me was a tiny bit of attribution (if that even) - I wasn’t limited in terms of how to combine it with other code. When I choose to pitch in something in return, it’s only fair of me to be permissive too. That’s how I perceive the mindset of the web development community at large.&lt;/p&gt;

&lt;p&gt;And yeah, I used a sensational title. Tricky.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Update 2012-04-21: Meteor has &lt;a href=&quot;http://meteor.com/faq/how-is-meteor-licensed&quot;&gt;switched&lt;/a&gt; to the permissive MIT license.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://news.ycombinator.com/item?id=3836212&quot;&gt;Show comments (Hacker News)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/02/speaking-at-devlin-and-front-trends</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/02/speaking-at-devlin-and-front-trends.html"/>
    <title>Speaking at DevLin and Front-Trends</title>
    <updated>2012-02-14T22:00:00+01:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I’m excited to be invited to speak at &lt;a href=&quot;http://events.responsive.se/joomla/index.php?option=com_civicrm&amp;task=civicrm/event/info&amp;reset=1&amp;id=19&amp;Itemid=78&quot;&gt;DevLin2012&lt;/a&gt; (Sweden) and &lt;a href=&quot;http://2012.front-trends.com/&quot;&gt;Front-Trends&lt;/a&gt; (Poland). Lots of interesting topics there that I want to attend. Summaries of my own talks follows below:&lt;/p&gt;

&lt;h3 id=&quot;evolution_vs_revolution_with_javascript_and_dart_devlin2012&quot;&gt;Evolution vs. revolution with JavaScript and Dart (DevLin2012)&lt;/h3&gt;

&lt;p&gt;There are clear benefits to developing modern web apps but what are the drawbacks? How are we limited by the programming language JavaScript which in practice means ECMAScript 3 from 1999?&lt;/p&gt;

&lt;p&gt;We’ll cover different opportunities and challenges such as performance, startup time, scalability with a growing code base and developer team, productive development environments, strict enough but at the same time expressive enough language semantics, tools for code analysis and more.&lt;/p&gt;

&lt;p&gt;The upcoming ECMAScript 6 represents an evolution of today’s JavaScript. Dart starts from scratch with a newly designed language for the web. How do they relate to the challenges above? When can you start using them and what do they require from the end-user environments?&lt;/p&gt;

&lt;p&gt;How can we make the best of JavaScript as we know it today?&lt;/p&gt;

&lt;h3 id=&quot;javascript_the_subsets_we_use_fronttrends&quot;&gt;JavaScript: the subsets we use (Front-Trends)&lt;/h3&gt;

&lt;p&gt;Is it possible to make our programs more robust, easier to reason about and more enjoyable to write by carefully restricting our usage of the JavaScript language? Let’s talk about that.&lt;/p&gt;

&lt;p&gt;Olov Lassus is a Swedish passionate programmer who started his career in the 16-bit game development era of the early 90′s. He got into JavaScript by accident when writing his master’s thesis and it has pursued him ever since. He likes to fiddle with language design, programmer productivity and tooling.&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2012/02/pygments-contribution-now-upstream</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2012/02/pygments-contribution-now-upstream.html"/>
    <title>Pygments contribution now upstream</title>
    <updated>2012-02-05T12:30:00+01:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;My &lt;a href=&quot;http://pygments.org&quot;&gt;Pygments&lt;/a&gt; Dart highlighter that I’ve &lt;a href=&quot;http://blog.lassus.se/2011/11/dart-syntax-highlighting-update.html&quot;&gt;written about&lt;/a&gt; previously is &lt;a href=&quot;https://bitbucket.org/birkenfeld/pygments-main/changeset/972091dacea4&quot;&gt;now upstream&lt;/a&gt;! This means that there’s now out of the box support for Dart if you get the development version from source control and that the next formal release is likely to have it too. Github enabling it shouldn’t be far away now.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a65d1625f9b98fb/c1723a3709964991&quot;&gt;Show comments (Dart misc)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/11/dart-syntax-highlighting-update</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/11/dart-syntax-highlighting-update.html"/>
    <title>Dart syntax highlighting update</title>
    <updated>2011-11-26T15:00:00+01:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;Here’s a short update on the &lt;a href=&quot;http://pygments.org&quot;&gt;Pygments&lt;/a&gt; Dart support I created &lt;a href=&quot;http://blog.lassus.se/2011/10/dart-syntax-highlighting.html&quot;&gt;a month back&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I consider it good to go now and will submit it to the Pygments upstream and github shortly. The most notable addition is support for string interpolation. I’ve also created a custom color scheme (based on tango) that works well with at least Dart, JS and Diff.&lt;/p&gt;

&lt;p&gt;It looks like this:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;cp&#39;&gt;#!/bin/dart&lt;/span&gt;

&lt;span class=&#39;n&#39;&gt;main&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;kt&#39;&gt;String&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;name&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;Zoidberg&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
  &lt;span class=&#39;kd&#39;&gt;const&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;age&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;42&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
  &lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;liar&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Math&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;random&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;0.5&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;

  &lt;span class=&#39;c1&#39;&gt;// Present ourselves&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;Hi! My name is &lt;/span&gt;&lt;span class=&#39;si&#39;&gt;$&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;name&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt; and I&amp;#39;m &lt;/span&gt;&lt;span class=&#39;si&#39;&gt;${&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;liar&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;?&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;age&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;/&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;2&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;:&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;age&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;}&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt; years old&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Source code is available at &lt;a href=&quot;https://gist.github.com/1320163&quot;&gt;https://gist.github.com/1320163&lt;/a&gt;. Install it by appending the gist to &lt;a href=&quot;http://pygments.org&quot;&gt;Pygments&lt;/a&gt;’s &lt;code&gt;agile.py&lt;/code&gt; and add the following line to &lt;code&gt;_mapping.py&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;python&#39;&gt;    &lt;span class=&#39;s&#39;&gt;&amp;#39;DartLexer&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;:&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;pygments.lexers.agile&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;s&#39;&gt;&amp;#39;Dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;),&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;*.dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,),&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;text/x-dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then run &lt;code&gt;pygmentize -f html -o yourfile.html yourfile.dart&lt;/code&gt; to generate markup from your source code. You’ll need to combine it with a style sheet. Feel free to copy my color scheme (&lt;a href=&quot;http://blog.lassus.se/stylesheets/syntax.css&quot;&gt;syntax.css&lt;/a&gt;) if you like it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a65d1625f9b98fb/c1723a3709964991&quot;&gt;Show comments (Dart misc)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/11/lets-create-us-some-dart-ranges</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/11/lets-create-us-some-dart-ranges.html"/>
    <title>Let's create us some Dart ranges</title>
    <updated>2011-11-21T11:27:00+01:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;We recognize the good ol’ &lt;code&gt;for&lt;/code&gt; loop from the C-family of programming languages. It is available with the same familiar syntax and semantics in Dart, too:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;10&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;++&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In addition to that, Dart has a &lt;code&gt;for-in&lt;/code&gt; loop, similar to Java in semantics (&lt;code&gt;iterator()&lt;/code&gt;, &lt;code&gt;hasNext()&lt;/code&gt; and &lt;code&gt;next()&lt;/code&gt;). &lt;code&gt;for-in&lt;/code&gt; is used to conveniently loop over any &lt;code&gt;Iterable&lt;/code&gt; sequence, such as a &lt;code&gt;List&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;in&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;[&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;1&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;3&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;4&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;5&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;6&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;7&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;8&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;9&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;])&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you’re into Python then you know that over there you &lt;code&gt;for&lt;/code&gt; loop over sequences exclusively, and use &lt;code&gt;xrange&lt;/code&gt; extensively.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;python&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;ow&#39;&gt;in&lt;/span&gt; &lt;span class=&#39;nb&#39;&gt;xrange&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;10&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;):&lt;/span&gt;
    &lt;span class=&#39;k&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;xrange&lt;/code&gt; is quite convenient and prevents a lot of variable name stuttering. It takes up to three parameters: &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt; and &lt;code&gt;step&lt;/code&gt;. Python &lt;code&gt;xrange&lt;/code&gt;s exclude the &lt;code&gt;stop&lt;/code&gt; endpoint and &lt;code&gt;step&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt; by default. &lt;code&gt;start&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; by default so you’ll often see &lt;code&gt;xrange(len(seq))&lt;/code&gt; used in Python code to iterate over sequence indices.&lt;/p&gt;

&lt;p&gt;Back to Dart. While good ol’ &lt;code&gt;for&lt;/code&gt; loops have been working fine for the last few decades or so, perhaps something &lt;code&gt;xrange&lt;/code&gt;-like could be fun here too. We’ve already got &lt;code&gt;for-in&lt;/code&gt; and iterators after all. Let’s create a &lt;code&gt;Range&lt;/code&gt; class that implements the &lt;code&gt;Iterable&lt;/code&gt; interface with a corresponding &lt;code&gt;RangeIterator&lt;/code&gt; and we’ll be able to do &lt;code&gt;for-in range&lt;/code&gt;, with top-level function &lt;code&gt;range&lt;/code&gt; being a wrapper around the &lt;code&gt;Range&lt;/code&gt; constructor. We’ll include both endpoints so &lt;code&gt;range(0, 9)&lt;/code&gt; means &lt;code&gt;[0, 1, .. 9]&lt;/code&gt;. Again &lt;code&gt;step&lt;/code&gt; defaults to &lt;code&gt;1&lt;/code&gt; so &lt;code&gt;range(0, 9)&lt;/code&gt; is the same as &lt;code&gt;range(0, 9, 1)&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;in&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;9&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;))&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That looks swell. No surprise it has already been discussed on dart-misc. We can also pass a range to anything that expects an &lt;code&gt;Iterable&lt;/code&gt;. Here’s how to generate the &lt;code&gt;List&lt;/code&gt; &lt;code&gt;[0, 2, .. 8]&lt;/code&gt; using the &lt;code&gt;List.from&lt;/code&gt; constructor:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;list&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;new&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;List&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;from&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;8&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Iterating over indices is done like so:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;str&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;hellojed&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;in&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;str&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;length&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;-&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;1&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;))&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;$&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;: &lt;/span&gt;&lt;span class=&#39;si&#39;&gt;${&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;str&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;[&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;]&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;}&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’ll add a top-level function &lt;code&gt;indices&lt;/code&gt; to sweeten it a bit:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;in&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;indices&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;str&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;))&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;$&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;: &lt;/span&gt;&lt;span class=&#39;si&#39;&gt;${&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;str&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;[&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;]&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;}&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can make &lt;code&gt;range&lt;/code&gt; more useful still. The &lt;code&gt;Collection&lt;/code&gt; interface (implemented by &lt;code&gt;List&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt; among others) contains &lt;code&gt;length&lt;/code&gt;, &lt;code&gt;every&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;isEmpty&lt;/code&gt; and &lt;code&gt;some&lt;/code&gt;. Let’s implement those too. Now we can pass around &lt;code&gt;range&lt;/code&gt;s anywhere a &lt;code&gt;Collection&lt;/code&gt; is expected, for example to &lt;code&gt;addAll&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;queue&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;new&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Queue&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;();&lt;/span&gt;
&lt;span class=&#39;c1&#39;&gt;// ...&lt;/span&gt;
&lt;span class=&#39;n&#39;&gt;queue&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;addAll&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;9&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How about a &lt;code&gt;List&lt;/code&gt; with the numbers &lt;code&gt;[0, 1, .. 50]&lt;/code&gt; that are perfect squares:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;kt&#39;&gt;bool&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;isSquare&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;n&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Math&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;pow&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;Math&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;sqrt&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;n&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;).&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;round&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(),&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;==&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;n&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;squares&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;50&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;).&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;filter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;isSquare&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or the other way around? &lt;code&gt;map&lt;/code&gt; isn’t part of &lt;code&gt;Collection&lt;/code&gt; but let’s add it anyways:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;squares&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;7&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;).&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;map&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;((&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;e&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;e&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;*&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;e&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The original example can also be rewritten like this, using &lt;code&gt;forEach&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;9&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;).&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;forEach&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Not too shabby, all in all. Good ol’ &lt;code&gt;for&lt;/code&gt; loops have less overhead than their &lt;code&gt;for-in range&lt;/code&gt; equivalent but the difference is nothing to write home about, especially if your loop body does something useful. Your personal style is probably more important - as for myself I consider &lt;code&gt;range&lt;/code&gt; a nice complement.&lt;/p&gt;

&lt;p&gt;Check out the &lt;a href=&quot;http://github.com/olov/dart-range/blob/master/range_tests.dart&quot;&gt;tests&lt;/a&gt; for more examples. The &lt;a href=&quot;http://github.com/olov/dart-range/blob/master/range.dart&quot;&gt;implementation&lt;/a&gt; should be easy to follow if you want to learn how to create a &lt;code&gt;Collection&lt;/code&gt; of your own. If you want a little Dart exercise then change &lt;code&gt;Range&lt;/code&gt; so that it only implements &lt;code&gt;Iterable&amp;lt;int&amp;gt;&lt;/code&gt; and remove everything &lt;code&gt;Collection&lt;/code&gt;-specific. Then add it back again guided by the unit tests, this time with your own code. Or start from scratch altogether.&lt;/p&gt;

&lt;p&gt;Oh and the Dart &lt;code&gt;for-in&lt;/code&gt; loop is just (very convenient) syntactic sugar of something similar to this:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;k&#39;&gt;for&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;Iterator&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;it&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;range&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;9&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;).&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;iterator&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;();&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;it&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;hasNext&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;();&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;i&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;it&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;next&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;();&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;i&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;http://github.com/olov/dart-range&quot;&gt;dart-range source&lt;/a&gt; available on github.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/33d763479ceacda9#&quot;&gt;Show comments (Dart misc)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/10/dart-syntax-highlighting</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/10/dart-syntax-highlighting.html"/>
    <title>Dart syntax highlighting support for Pygments</title>
    <updated>2011-10-27T19:35:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;Source code is available at &lt;a href=&quot;https://gist.github.com/1320163&quot;&gt;https://gist.github.com/1320163&lt;/a&gt;. Install it by appending the gist to &lt;a href=&quot;http://pygments.org&quot;&gt;Pygments&lt;/a&gt;’s &lt;code&gt;agile.py&lt;/code&gt; and add the following line to &lt;code&gt;_mapping.py&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;python&#39;&gt;    &lt;span class=&#39;s&#39;&gt;&amp;#39;DartLexer&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;:&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;pygments.lexers.agile&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;s&#39;&gt;&amp;#39;Dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;),&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;*.dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,),&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s&#39;&gt;&amp;#39;text/x-dart&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then run &lt;code&gt;pygmentize -f html -o yourfile.html yourfile.dart&lt;/code&gt; to generate markup from your source code. You’ll need to combine it with a style sheet. Feel free to copy my color scheme (&lt;a href=&quot;http://blog.lassus.se/stylesheets/syntax.css&quot;&gt;syntax.css&lt;/a&gt;) if you like it.&lt;/p&gt;

&lt;p&gt;Dart syntax highlighting example:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;dart&#39;&gt;&lt;span class=&#39;c1&#39;&gt;// Greeter example from&lt;/span&gt;
&lt;span class=&#39;c1&#39;&gt;// &amp;lt;http://www.dartlang.org/docs/getting-started/interface.html&amp;gt;&lt;/span&gt;
&lt;span class=&#39;kd&#39;&gt;class&lt;/span&gt; &lt;span class=&#39;nc&#39;&gt;Greeter&lt;/span&gt; &lt;span class=&#39;kd&#39;&gt;implements&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Comparable&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;kt&#39;&gt;String&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;prefix&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;s1&#39;&gt;&amp;#39;Hello,&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{}&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;withPrefix&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;k&#39;&gt;this&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;prefix&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;greet&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kt&#39;&gt;String&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;name&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;print&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&#39;si&#39;&gt;$&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;prefix&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt; &lt;/span&gt;&lt;span class=&#39;si&#39;&gt;$&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;name&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;

  &lt;span class=&#39;kt&#39;&gt;int&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;compareTo&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;other&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;prefix&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;compareTo&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;other&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;prefix&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;

&lt;span class=&#39;kt&#39;&gt;void&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;main&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;greeter&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;new&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;();&lt;/span&gt;
  &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;greeter2&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;new&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;Greeter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;withPrefix&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt;&amp;#39;Hi,&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;

  &lt;span class=&#39;kt&#39;&gt;num&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;result&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;n&#39;&gt;greeter2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;compareTo&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;greeter&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
  &lt;span class=&#39;k&#39;&gt;if&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;result&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;==&lt;/span&gt; &lt;span class=&#39;m&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;n&#39;&gt;greeter2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;greet&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt;&amp;#39;you are the same.&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
  &lt;span class=&#39;p&#39;&gt;}&lt;/span&gt; &lt;span class=&#39;k&#39;&gt;else&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;n&#39;&gt;greeter2&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;n&#39;&gt;greet&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s1&#39;&gt;&amp;#39;you are different.&amp;#39;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
  &lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a65d1625f9b98fb/c1723a3709964991&quot;&gt;Show comments (Dart misc)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/10/describing-live-programming-again</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/10/describing-live-programming-again.html"/>
    <title>Describing Live programming (again)</title>
    <updated>2011-10-09T17:00:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;It’s funny how the brain does context switching sometimes. You work really hard on something just to almost forget about it immediately after the release. You then let it be for a few years until you dare to open up that project or document again to figure out why you worked that hard on it in the first place.&lt;/p&gt;

&lt;p&gt;Anyways. I wrote my master’s thesis in 2006 and I was really passionate about it at the time. I wanted to describe how to create a better programming environment than what we currently had without falling into the “everything has already been invented in good ol’ Smalltalk and the LISP machines” trap. I specifically focused on enabling the programmer to write and modify code at run-time, thus the name &lt;em&gt;Live programming&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I was satisfied with what I accomplished. I had a prototype running that I used to test that the source code, AST and callstack transformations I described in the thesis were valid. I knew what the next step would be (post thesis) but I also knew that I didn’t have the time to invest into making a full blown implementation, so I let it be.&lt;/p&gt;

&lt;p&gt;Almost six years has passed since then and the programming environments people use today haven’t changed much. I’m still passionate about Live programming. I figured it wouldn’t hurt to expose the concept to a new reader or two, thus this blog article.&lt;/p&gt;

&lt;p&gt;When I wrote the thesis I was primarily interested in finding out how far I could take the concept. I picked JavaScript as the prototype language because it seemed to have momentum at the time and I guess that choice turned out well. I picked Narcissus because it was the simplest interpreter I could find. The prototype environment uses a heavily modified Narcissus (I turned it stackless/callstack-explicit) and it seems to have bitrot since then but I’ll see if I can beat the code into shape and release it out again if there’s any interest.&lt;/p&gt;

&lt;p&gt;I still think that JavaScript is an excellent language for implementing a Live programming environment. I’d love to see one integrated directly in the browser, and also tied together with a node.js environment. It needs to be directly integrated with the JS impl, i.e. the V8, SpiderMonkey or what have you. It’s not a small feat.&lt;/p&gt;

&lt;p&gt;So here we go: &lt;a href=&quot;http://blog.lassus.se/files/liveprogramming.pdf&quot; title=&quot;Live programming thesis PDF&quot;&gt;Describing Live programming using program transformations and a callstack explicit interpreter&lt;/a&gt;. Read chapter 1 and 2 for more info about Live programming. Read the other chapters only if you’re interested in how it works under the hood (the transformations).&lt;/p&gt;

&lt;p&gt;Oh and I did an s/Johansson/Lassus thing in 2007, in case you wonder.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/l6247/describing_live_programming_again/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/06/seems-like-restrict-mode-found-us-a-jquery-bug</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/06/seems-like-restrict-mode-found-us-a-jquery-bug.html"/>
    <title>Seems like restrict mode found us a jQuery bug</title>
    <updated>2011-06-16T19:30:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;Feel free to skip the first paragraph if you’ve read my previous restrict mode articles.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I’ve created this thing called &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrict mode for JavaScript&lt;/a&gt;. It’s a definition of a proper subset of the language which I believe is great because I think it increases robustness and makes your program more easy to reason about. It comes with a tool that will check if your program really complies to restrict mode, when you say it does. The tool is called &lt;code&gt;restricter&lt;/code&gt; and is a plugin for &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt;, an extensible framework for JavaScript syntax tree shaping. Restrict mode makes JS programming more enjoyable since it helps you focus on real programming challenges instead of annoyances/defects/inconsistencies of the JS language.&lt;/p&gt;

&lt;p&gt;I’ve written about how I made &lt;a href=&quot;http://blog.lassus.se/2011/04/making-v8bench-restrict-mode-clean.html&quot;&gt;v8bench&lt;/a&gt; and &lt;a href=&quot;http://blog.lassus.se/2011/04/i-made-jslint-restrict-mode-clean-heres.html&quot;&gt;JSLint&lt;/a&gt; restrict mode clean and I promised to do the same for jQuery.&lt;/p&gt;

&lt;p&gt;I’ve tweaked the restrict mode semantics somewhat since then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;I removed the restrict mode custom versions of &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt;. This shouldn’t come as a big surprise since I hinted that I wasn’t happy with clause 1 in &lt;a href=&quot;http://blog.lassus.se/2011/03/case-for-restrict-mode.html&quot;&gt;the case for restrict mode&lt;/a&gt;. Now all of &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, &lt;code&gt;===&lt;/code&gt; and &lt;code&gt;!==&lt;/code&gt; behave identically in normal and restrict mode and you should strongly consider not using &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt; irrespective of whether you use restrict mode or not.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;I relaxed the restrict mode &lt;code&gt;+&lt;/code&gt; operator slightly. It was previously only allowed for &lt;code&gt;str + str&lt;/code&gt; (concatenation) or &lt;code&gt;num + num&lt;/code&gt; (addition), but never a mix. It’s now also allowed for &lt;code&gt;num + str&lt;/code&gt; or &lt;code&gt;str + num&lt;/code&gt;, which happens to be the most common coercing usage of it out there in the wild. I hope that this will lower the barrier of restrict mode adoption.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Restrict mode is still a proper subset of the JavaScript language and will remain so. It will freeze one day but we’re not there yet.&lt;/p&gt;

&lt;p&gt;Back to jQuery. I cloned the &lt;a href=&quot;https://github.com/jquery/jquery&quot;&gt;jQuery repo&lt;/a&gt;, ran it through &lt;code&gt;restricter&lt;/code&gt; (with restrict mode forced no matter &lt;code&gt;&amp;quot;use restrict&amp;quot;;&lt;/code&gt; directives) and fixed the restrict mode run-time errors until all tests passed again. &lt;code&gt;restricter&lt;/code&gt; ran on everything under &lt;code&gt;src/&lt;/code&gt; excluding the Sizzle dependency, to be precise. It should be easy to try it on Sizzle as well, or on something else for that matter.&lt;/p&gt;

&lt;p&gt;The required changes to get jQuery restrict mode clean were few and small. They can be partitioned into three classes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Most were about fixing the &lt;code&gt;str + nostr_nonum&lt;/code&gt; antipattern&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;One fixing &lt;code&gt;str &amp;lt; num&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;One fixing &lt;code&gt;str + undefined&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Neither class 1 or 2 are bugs in jQuery. They represent the current mismatch between the restrict mode subset of JavaScript and the subset that the jQuery developers use. The mismatch seems to be very small, further strengthening &lt;a href=&quot;http://blog.lassus.se/2011/03/case-for-restrict-mode.html&quot;&gt;the case for restrict mode&lt;/a&gt;. I only had to change a total of 9 lines of code, all by explicitly converting a primitive or object into a string using &lt;code&gt;String(expr)&lt;/code&gt;. The patched version may be a bit easier to reason about and slightly more robust to future changes, especially so for newcomers to the jQuery codebase. Being explicit makes it slightly more verbose.&lt;/p&gt;

&lt;p&gt;The class 3 change is more interesting. &lt;code&gt;str + undefined&lt;/code&gt; is rarely done on purpose. &lt;code&gt;jQuery.fn.hasClass&lt;/code&gt; should guard against missing className properties but doesn’t, and as a consequence &lt;code&gt;element.hasClass(&amp;quot;undefined&amp;quot;)&lt;/code&gt; may return &lt;code&gt;true&lt;/code&gt; when it shouldn’t. An easy way to demonstrate this is to change &lt;code&gt;test/unit/attributes.js, line 860&lt;/code&gt;. Change &lt;code&gt;ok(!j.hasClass(&amp;quot;asdf&amp;quot;),
...)&lt;/code&gt; to &lt;code&gt;ok(!j.hasClass(&amp;quot;undefined&amp;quot;), ...)&lt;/code&gt; and watch the assert blow. Talking about assert feel free to check out &lt;a href=&quot;http://blog.lassus.se/2011/03/c-style-assertions-in-javascript-via.html&quot;&gt;C-style assertions in JavaScript via JSShaper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Seems like restrict mode found us a jQuery bug.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.lassus.se/patches/jquery_restrictmode.patch&quot;&gt;Here’s the patch&lt;/a&gt; aggregating the changes above including the bugfix. It applies cleanly against d59b0f3e27827d189b8b2595142ec6bbc3941dd9. And here’s the &lt;a href=&quot;http://forum.jquery.com/topic/i-made-jquery-restrict-mode-clean-and-i-think-i-found-a-tiny-bug-doing-so&quot;&gt;jQuery forum thread&lt;/a&gt;, &lt;a href=&quot;http://bugs.jquery.com/ticket/9630&quot;&gt;ticket&lt;/a&gt; and &lt;a href=&quot;https://github.com/jquery/jquery/pull/418&quot;&gt;pull request&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want to try restrict mode on your own project? Go to &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrictmode.org&lt;/a&gt; and feel free to stop by the &lt;a href=&quot;http://groups.google.com/group/jsshaper?pli=1&quot; title=&quot;JSShaper mailing list&quot;&gt;JSShaper mailing list&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/i1mfa/seems_like_restrict_mode_found_us_a_jquery_bug/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/04/i-made-jslint-restrict-mode-clean-heres</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/04/i-made-jslint-restrict-mode-clean-heres.html"/>
    <title>I made JSLint restrict mode clean. Here's what I found</title>
    <updated>2011-04-21T20:50:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I’ve created this thing called &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrict mode for JavaScript&lt;/a&gt;. It’s a definition of a proper subset of the language which I believe is great because I think it increases robustness and makes your program more easy to reason about. It comes with a tool that will check if your program really complies to restrict mode, when you say it does. The tool is called &lt;code&gt;restricter&lt;/code&gt; and is a plugin for &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt;, an extensible framework for JavaScript syntax tree shaping. JSShaper is cool for many other reasons as well but this post is about restrict mode. For me restrict mode makes JS programming more enjoyable since it helps me focus on real programming challenges instead of annoyances/defects/inconsistencies of the JS language. It eases my mind.&lt;/p&gt;

&lt;p&gt;I want to know how well the restrict mode subset of JavaScript matches existing programs. I’ve already written about &lt;a href=&quot;http://blog.lassus.se/2011/04/making-v8bench-restrict-mode-clean.html&quot;&gt;how I made v8bench restrict mode clean&lt;/a&gt;. I’ve also done the same to jQuery and Kraken, though I haven’t written about it yet (I’m worse at writing than coding but it’s on my todo-list). When I saw a JSLint discussion on reddit I figured that it would be a great test case, so I decided to make it restrict mode clean and write about what I found. I’m glad I did because it exposed a bug in JSShaper (now fixed). I also think that it serves as a good example of what you can expect from restrict mode.&lt;/p&gt;

&lt;p&gt;Here are my findings and how I changed JSLint to make it restrict mode clean. In my checkout of JSLint, &lt;a href=&quot;https://github.com/douglascrockford/JSLint/blob/99091cd30d354e77cf9580b643bce1ad93bc1277/jslint.js&quot;&gt;commit 99091cd30d354e77cf95&lt;/a&gt; was the latest and greatest so the changes apply cleanly against that revision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First of all&lt;/strong&gt;, we need to specify which parts of the program should follow restrict mode. We want it to cover all of JSLint so that the JSShaper restricter plugin can help us catch any issues. JSLint is already enclosed in a function which starts with a &lt;code&gt;&amp;quot;use strict&amp;quot;&lt;/code&gt; string. Let’s add &lt;code&gt;&amp;quot;use restrict&amp;quot;&lt;/code&gt; as well:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;var JSLINT = (function () {
&lt;span class=&#39;gd&#39;&gt;-    &amp;quot;use strict&amp;quot;;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+    &amp;quot;use strict&amp;quot;; &amp;quot;use restrict&amp;quot;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To show what I changed I’ve included the diff’s (ceremony removed). Red &lt;code&gt;-&lt;/code&gt; lines are what I removed, green &lt;code&gt;+&lt;/code&gt; lines are what I added. Scroll down for full patch.&lt;/p&gt;

&lt;p&gt;With that change in place, I ran jslint.js through JSShaper (restricter): &lt;code&gt;js run-restricter.js -- JSLint/jslint.js &amp;gt;
JSLint/jslint_restrict.js&lt;/code&gt;. I tweaked the jslint.html file to use the restrict mode checked version and to dump the errors to console instead of throwing exceptions. To make JSLint restrict mode clean I just had to inspect the errors (when running JSLint) and fix the issues. I iterated on this, and here’s what I found and changed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First change&lt;/strong&gt; is to fix a coercion-happy &lt;code&gt;String.prototype.isAlpha&lt;/code&gt;. When &lt;code&gt;&amp;quot;hello&amp;quot;.isAlpha()&lt;/code&gt; or &lt;code&gt;str.isAlpha()&lt;/code&gt; is evaluated the expression left of the dot is first boxed into a temporary &lt;code&gt;String&lt;/code&gt; object, effectively the same as &lt;code&gt;(new
String(&amp;quot;hello&amp;quot;)).isAlpha()&lt;/code&gt; or &lt;code&gt;(new String(str)).isAlpha()&lt;/code&gt;. In the &lt;code&gt;String.prototype.isAlpha&lt;/code&gt; function, the temporary object is bound to &lt;code&gt;this&lt;/code&gt;. &lt;em&gt;[This behavior was then changed in ES5 strict mode to further add to the confusion since it’s not backwards compatible, which is another reason for First change]&lt;/em&gt;. If &lt;code&gt;this&lt;/code&gt; is used as an operand like below it will trigger coercion, sometimes with unexpected results. You can get the primitive value from a &lt;code&gt;String object&lt;/code&gt; by applying the &lt;code&gt;valueOf&lt;/code&gt; method or &lt;code&gt;String&lt;/code&gt; function. Let’s do that, so that the &lt;code&gt;&amp;lt;=&lt;/code&gt; and &lt;code&gt;&amp;gt;=&lt;/code&gt; operators won’t need to coerce their operands any longer:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;String.prototype.isAlpha = function() {
&lt;span class=&#39;gd&#39;&gt;-            return (this &amp;gt;= &amp;#39;a&amp;#39; &amp;amp;&amp;amp; this &amp;lt;= &amp;#39;z\uffff&amp;#39;) ||&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                (this &amp;gt;= &amp;#39;A&amp;#39; &amp;amp;&amp;amp; this &amp;lt;= &amp;#39;Z\uffff&amp;#39;);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            var v = this.valueOf();&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            return (v &amp;gt;= &amp;#39;a&amp;#39; &amp;amp;&amp;amp; v &amp;lt;= &amp;#39;z\uffff&amp;#39;) ||&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                (v &amp;gt;= &amp;#39;A&amp;#39; &amp;amp;&amp;amp; v &amp;lt;= &amp;#39;Z\uffff&amp;#39;);&lt;/span&gt;
         };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Second change&lt;/strong&gt; is to do the same for &lt;code&gt;String.prototype.isDigit&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;String.prototype.isDigit = function () {
&lt;span class=&#39;gd&#39;&gt;-            return (this &amp;gt;= &amp;#39;0&amp;#39; &amp;amp;&amp;amp; this &amp;lt;= &amp;#39;9&amp;#39;);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            var v = this.valueOf();&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            return (v &amp;gt;= &amp;#39;0&amp;#39; &amp;amp;&amp;amp; v &amp;lt;= &amp;#39;9&amp;#39;);&lt;/span&gt;
         };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Third change&lt;/strong&gt; is to fix &lt;code&gt;String.prototype.name&lt;/code&gt;. It used to &lt;code&gt;return
this&lt;/code&gt; sometimes, which is not the right thing to do. I described above how primitives get boxed into temporary objects. Returning these objects is almost always bad for two reasons: It spreads the coercion disease, and it’s not what the user of the function expects (because now sometimes the function returns &lt;code&gt;string&lt;/code&gt;s, sometimes it returns &lt;code&gt;String object&lt;/code&gt;s). Let’s fix that by returning &lt;code&gt;this.valueOf()&lt;/code&gt;. Let’s also use the primitive value with the &lt;code&gt;+&lt;/code&gt; operator, and the coercion is gone. Here we go:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;String.prototype.name = function () {
&lt;span class=&#39;gd&#39;&gt;-            if (ix.test(this)) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                return this;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            var v = this.valueOf();&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            if (ix.test(v)) {&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                return v;&lt;/span&gt;
             }
&lt;span class=&#39;gd&#39;&gt;-            if (nx.test(this)) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                return &amp;#39;&amp;quot;&amp;#39; + this.replace(nxg, function (a) {&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            if (nx.test(v)) {&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                return &amp;#39;&amp;quot;&amp;#39; + v.replace(nxg, function (a) { // this.replace works too&lt;/span&gt;
                     if (escapes[a]) {
                         return escapes[a];
                     }
                     return &amp;#39;\\u&amp;#39; + (&amp;#39;0000&amp;#39; + a.charCodeAt().toString(16)).slice(-4);
                 }) + &amp;#39;&amp;quot;&amp;#39;;
             }
&lt;span class=&#39;gd&#39;&gt;-            return &amp;#39;&amp;quot;&amp;#39; + this + &amp;#39;&amp;quot;&amp;#39;;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+            return &amp;#39;&amp;quot;&amp;#39; + v + &amp;#39;&amp;quot;&amp;#39;;&lt;/span&gt;
         };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Fourth and final&lt;/strong&gt; change is to fix the &lt;code&gt;report&lt;/code&gt; function which performs a lot of string formatting relying on &lt;code&gt;number&lt;/code&gt;s being coerced into &lt;code&gt;string&lt;/code&gt;s &lt;code&gt;(str + nostr)&lt;/code&gt;. It’s a fine example of code that would greatly benefit from using &lt;a href=&quot;https://github.com/olov/jsshaper/blob/master/src/fmt.js&quot; title=&quot;JavaScript string formatting library&quot;&gt;Fmt&lt;/a&gt; or something similar. There’s also the option to bail out of restrict mode for the entire report function by annotating it with &lt;code&gt;/*@loose*/&lt;/code&gt;. For this example I fixed it in the least obtrusive manner by applying the &lt;code&gt;String&lt;/code&gt; function to the &lt;code&gt;number&lt;/code&gt;s. Here we go:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;&lt;span class=&#39;gu&#39;&gt;@@ -6523,7 +6526,7 @@ loop:   for (;;) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                            warning.line + &amp;#39; character &amp;#39; + warning.character : &amp;#39;&amp;#39;) +&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                            String(warning.line) + &amp;#39; character &amp;#39; + String(warning.character) : &lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -6536,7 +6539,7 @@ loop:   for (;;) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                        data.implieds[i].line + &amp;#39;&amp;lt;/i&amp;gt;&amp;#39;;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                        String(data.implieds[i].line) + &amp;#39;&amp;lt;/i&amp;gt;&amp;#39;;&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -6545,7 +6548,7 @@ loop:   for (;;) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                        data.unused[i].line + &amp;#39; &amp;lt;/i&amp;gt; &amp;lt;small&amp;gt;&amp;#39; +&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                        String(data.unused[i].line) + &amp;#39; &amp;lt;/i&amp;gt; &amp;lt;small&amp;gt;&amp;#39; +&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -6583,7 +6586,7 @@ loop:   for (;;) {&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-                output.push(&amp;#39;&amp;lt;br&amp;gt;&amp;lt;div class=function&amp;gt;&amp;lt;i&amp;gt;&amp;#39; + the_function.line + &amp;#39;&amp;lt;/i&amp;gt; &amp;#39; +&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+                output.push(&amp;#39;&amp;lt;br&amp;gt;&amp;lt;div class=function&amp;gt;&amp;lt;i&amp;gt;&amp;#39; + String(the_function.line) + &amp;#39;&amp;lt;/i&amp;gt; &amp;#39; +&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;And that’s it.&lt;/strong&gt; JSLint is now restrict mode clean, as far as I can tell. I didn’t find any tests but I’ve tested it manually with a few programs, including JSLint itself.&lt;/p&gt;

&lt;p&gt;What have we learned?&lt;/p&gt;

&lt;p&gt;For a start, it’s another step in the right direction in verifying that the restrict mode subset of JavaScript is sane and well enough matches the JS subset people already use in practice. It took little effort to get JSLint restrict mode clean, and it required no prior experience with that code base. We had to change a few lines of code as expected, and the signal vs noise was very high.&lt;/p&gt;

&lt;p&gt;Did we find any real defects? We caught &lt;code&gt;String.prototype.name&lt;/code&gt; sometimes returning temporary boxed objects which I classify as a defect, even though it doesn’t cause any harm at the moment. Fixing that is a keeper.&lt;/p&gt;

&lt;p&gt;Even if we don’t buy into the idea restrict mode, is there something else that we’d like to keep? I’d suggest the fixes for coercion happiness examplified in change two and three. Especially so if you follow the recommendation of preferring &lt;code&gt;===&lt;/code&gt; over &lt;code&gt;==&lt;/code&gt; (I do). Think about it: It would be a bit weird to see &lt;code&gt;this &amp;gt;= &amp;#39;0&amp;#39; &amp;amp;&amp;amp; this &amp;lt;= &amp;#39;0&amp;#39;&lt;/code&gt; yield &lt;code&gt;true&lt;/code&gt; when &lt;code&gt;v&lt;/code&gt; is a temporary boxed &lt;code&gt;String object&lt;/code&gt; (it will coerce), but have &lt;code&gt;this === &amp;#39;0&amp;#39;&lt;/code&gt; yield &lt;code&gt;false&lt;/code&gt; (it won’t coerce). Leaving it as it is will increase the risk of future bugs. If you don’t prefer &lt;code&gt;===&lt;/code&gt; over &lt;code&gt;==&lt;/code&gt; then you’re likely not to care about this change.&lt;/p&gt;

&lt;p&gt;And some of you would perhaps consider keeping the changes for our fourth finding as well &lt;code&gt;(str + nostr)&lt;/code&gt; even if you didn’t yet buy in on restrict mode, because quite frankly the string formatting code in that function is not the most beautiful or easiest code to follow. Using &lt;a href=&quot;https://github.com/olov/jsshaper/blob/master/src/fmt.js&quot; title=&quot;JavaScript string formatting library&quot;&gt;Fmt&lt;/a&gt; or something similar could greatly improve it. Many will consider the code fine as-is and won’t change it unless they go restrict.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To summarize:&lt;/strong&gt; Of our four findings/changes, three seem noteworthy enough that I think many would consider applying the changes even if they don’t care about restrict mode. Not bad for a few hours of work of which the majority went into writing this post.&lt;/p&gt;

&lt;p&gt;If you choose to keep all changes as well as the &lt;code&gt;&amp;quot;use restrict&amp;quot;&lt;/code&gt; directive, then you can easily run the program through JSShaper (restricter) again in the future and perhaps find more stuff. It could help you during development, and it could help your program become more robust. You can choose to run the checked version only once in a while, every time your unit tests run or all the time. It’s totally up to you.&lt;/p&gt;

&lt;p&gt;A program complying to the subset of JavaScript defined by restrict mode (i.e. a restrict mode clean program) is also much easier to reason about compared to a program that (may) use the entire spectra of loose JS semantics. That may help you when debugging tomorrow or when coming back to your old code one year from now. If you read your friend’s code and happen to find the &lt;code&gt;&amp;quot;use restrict&lt;/code&gt;” directive in there, you may find yourself relieved knowing that not only will you faster and more accurately understand what it does and how, chances are that your friend accurately understands his/her own program too.&lt;/p&gt;

&lt;p&gt;The JSLint restrict mode experiment &lt;strong&gt;exceeded my expectations&lt;/strong&gt;. Why don’t you try applying restrict mode to one of your own programs? What’s to lose?&lt;/p&gt;

&lt;p&gt;There’s more info available on &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrictmode.org&lt;/a&gt;. If you have any questions about restrict mode or &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; then post a message to the &lt;a href=&quot;http://groups.google.com/group/jsshaper?pli=1&quot; title=&quot;JSShaper mailing list&quot;&gt;JSShaper mailing list&lt;/a&gt; and I’ll help you out. You can also &lt;a href=&quot;http://lassus.se&quot; title=&quot;Olov Lassus homepage&quot;&gt;contact me&lt;/a&gt; directly.&lt;/p&gt;

&lt;p&gt;Here’s &lt;a href=&quot;http://blog.lassus.se/patches/jslint_restrictmode.patch&quot;&gt;the patch&lt;/a&gt; for everything described above (&lt;code&gt;jslint.html&lt;/code&gt; and &lt;code&gt;jslint.js&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/gvmvi/i_made_jslint_restrict_mode_clean_heres_what_i/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/04/javascript-modules-in-shells-browsers</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/04/javascript-modules-in-shells-browsers.html"/>
    <title>JavaScript modules in shells, browsers, everywhere!</title>
    <updated>2011-04-04T22:52:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;This is the technique I’m using to get JavaScript module support in the console and in the browser, with the same code base. It’s tested with the following JS shells: &lt;code&gt;js&lt;/code&gt; (SpiderMonkey), &lt;code&gt;jsc&lt;/code&gt; (JavaScriptCore), &lt;code&gt;v8&lt;/code&gt;/&lt;code&gt;d8&lt;/code&gt; and &lt;code&gt;node&lt;/code&gt;. It’s useful since it lets you stay in the console as much as you want to and it enables you write once, run everywhere (that’s what they said).&lt;/p&gt;

&lt;p&gt;I’m using it in &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt;. Do you find it useful? Do you know of a better way? Let me know!&lt;/p&gt;

&lt;p&gt;In your program top-level file, define &lt;code&gt;require&lt;/code&gt; as follows. It’s now either defined as the built-in &lt;code&gt;require&lt;/code&gt; or a version of the built-in &lt;code&gt;load&lt;/code&gt; that returns nothing.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;require&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;require&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;||&lt;/span&gt; &lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;f&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;load&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;f&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In all other files, when you need to import a module such as &lt;a href=&quot;https://github.com/olov/jsshaper/blob/master/src/fmt.js&quot; title=&quot;JavaScript string formatting library&quot;&gt;Fmt&lt;/a&gt;, do it like this. The first &lt;code&gt;||&lt;/code&gt; clause is truthy (and thus assigned) when &lt;code&gt;Fmt&lt;/code&gt; is already defined in the global namespace, the second when &lt;code&gt;require&lt;/code&gt; is CommonJS-style and the third when &lt;code&gt;require&lt;/code&gt; wraps &lt;code&gt;load&lt;/code&gt; (thus &lt;code&gt;Fmt&lt;/code&gt; is &lt;em&gt;now&lt;/em&gt; in the global namespace).&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Fmt&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Fmt&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;||&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;require&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;./fmt.js&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;||&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Fmt&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, your modules should have the following structure, for example &lt;code&gt;Fmt&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;var&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Fmt&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;c1&#39;&gt;// ...&lt;/span&gt;
    &lt;span class=&#39;k&#39;&gt;return&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;...;&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;})();&lt;/span&gt;
&lt;span class=&#39;k&#39;&gt;if&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;k&#39;&gt;typeof&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;exports&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;!==&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;undefined&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;nx&#39;&gt;module&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;exports&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;=&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Fmt&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Voila, your module-separated program now works in the JS shells and the browsers! For the browsers, just include (or concatenate) the scripts in dependency-order. This isn’t a dynamic JS loader - if you want one there’s gazillions of them.&lt;/p&gt;

&lt;p&gt;Here are the underpinnings for the curious:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;node&lt;/code&gt;, &lt;code&gt;require&lt;/code&gt; is a built-in function used to load CommonJS-style modules. &lt;code&gt;require&lt;/code&gt; reads code from disk and evaluates it. It returns the object exported by the module. The module lives in its own namespace. There’s no &lt;code&gt;load&lt;/code&gt; function.&lt;/li&gt;

&lt;li&gt;In &lt;code&gt;v8&lt;/code&gt;, &lt;code&gt;load&lt;/code&gt; reads code from disk and evaluates it in the global namespace. It returns nothing (&lt;code&gt;undefined&lt;/code&gt;). There’s no &lt;code&gt;require&lt;/code&gt; function.&lt;/li&gt;

&lt;li&gt;In &lt;code&gt;js&lt;/code&gt;, &lt;code&gt;load&lt;/code&gt; reads code from disk and evaluates it in the global namespace. It returns the &lt;code&gt;load&lt;/code&gt; function itself (don’t ask me why). There’s no &lt;code&gt;require&lt;/code&gt; function.&lt;/li&gt;

&lt;li&gt;In &lt;code&gt;jsc&lt;/code&gt;, &lt;code&gt;load&lt;/code&gt; reads code from disk and evaluates it in the global namespace. It returns the value of the last statement in the file (or something like it). There’s no &lt;code&gt;require&lt;/code&gt; function.&lt;/li&gt;

&lt;li&gt;In browsers, there are no &lt;code&gt;load&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt; functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/javascript/comments/gilvs/javascript_modules_in_shells_browsers_everywhere/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/04/making-v8bench-restrict-mode-clean</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/04/making-v8bench-restrict-mode-clean.html"/>
    <title>Making v8bench restrict mode clean</title>
    <updated>2011-04-01T00:37:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I’d like to know how well the &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrict mode&lt;/a&gt; subset of JavaScript matches existing programs. I decided to test it on Google’s &lt;a href=&quot;http://v8.googlecode.com/svn/data/benchmarks/v6/revisions.html&quot;&gt;v8bench&lt;/a&gt;. I checked out the latest revision from SVN, and slapped &lt;code&gt;&amp;quot;use
restrict&amp;quot;;&lt;/code&gt; on top of every file. I ran that through &lt;code&gt;restricter&lt;/code&gt; (part of &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt;) before executing v8bench.&lt;/p&gt;

&lt;p&gt;The benchmark then threw an exception whenever restrict mode detected that things were going on outside of its rules. I tweaked the v8bench source, and iterated.&lt;/p&gt;

&lt;p&gt;I had to change only 8 lines of code to get v8bench restrict mode clean, i.e. have it complete without throwing any restrict mode exceptions. That’s 8 lines out of 11208, or 0.07%! 99.93% was already valid restrict mode. It’s a step in the right direction in verifying that the restrict mode subset of JavaScript is sane and well enough matches the JS subset people already use in practice.&lt;/p&gt;

&lt;p&gt;Oh, and if the same procedure is applied to r2537 instead of HEAD, one defect in the splay benchmark (&lt;a href=&quot;http://code.google.com/p/v8/issues/detail?id=690&quot;&gt;converting the same numeric key to a string over and over again&lt;/a&gt;) stands out and is easily corrected. That defect was later fixed in version 6 of the benchmark. Can restrict mode help you catch similar or other defects in your program?&lt;/p&gt;

&lt;p&gt;To learn more about restrict mode read &lt;a href=&quot;http://blog.lassus.se/2011/03/case-for-restrict-mode.html&quot;&gt;the case for restrict mode&lt;/a&gt; and then visit &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrictmode.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the full diff:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;diff&#39;&gt;&lt;span class=&#39;gh&#39;&gt;Index: run.js&lt;/span&gt;
&lt;span class=&#39;gh&#39;&gt;===================================================================&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;--- run.js (revision 7463)&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+++ run.js (working copy)&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -38,7 +38,7 @@&lt;/span&gt;
 var success = true;
  
 function PrintResult(name, result) {
&lt;span class=&#39;gd&#39;&gt;-  print(name + &amp;#39;: &amp;#39; + result);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+  print(name + &amp;#39;: &amp;#39; + String(result));&lt;/span&gt;
 }
  
  
&lt;span class=&#39;gh&#39;&gt;Index: raytrace.js&lt;/span&gt;
&lt;span class=&#39;gh&#39;&gt;===================================================================&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;--- raytrace.js (revision 7463)&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+++ raytrace.js (working copy)&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -617,8 +617,8 @@&lt;/span&gt;
                 rayDepth: 2
             }, options || {});
  
&lt;span class=&#39;gd&#39;&gt;-        this.options.canvasHeight /= this.options.pixelHeight;&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-        this.options.canvasWidth /= this.options.pixelWidth;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+        this.options.canvasHeight /= Number(this.options.pixelHeight);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+        this.options.canvasWidth /= Number(this.options.pixelWidth);&lt;/span&gt;
  
         /* TODO: dynamically include other scripts */
     },
&lt;span class=&#39;gh&#39;&gt;Index: base.js&lt;/span&gt;
&lt;span class=&#39;gh&#39;&gt;===================================================================&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;--- base.js (revision 7463)&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+++ base.js (working copy)&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -201,10 +201,10 @@&lt;/span&gt;
 BenchmarkSuite.prototype.RunSingleBenchmark = function(benchmark, data) {
   function Measure(data) {
     var elapsed = 0;
&lt;span class=&#39;gd&#39;&gt;-    var start = new Date();&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+    var start = Number(new Date());&lt;/span&gt;
     for (var n = 0; elapsed &amp;lt; 1000; n++) {
       benchmark.run();
&lt;span class=&#39;gd&#39;&gt;-      elapsed = new Date() - start;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+      elapsed = Number(new Date()) - start;&lt;/span&gt;
     }
     if (data != null) {
       data.runs += n;
&lt;span class=&#39;gh&#39;&gt;Index: deltablue.js&lt;/span&gt;
&lt;span class=&#39;gh&#39;&gt;===================================================================&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;--- deltablue.js (revision 7463)&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+++ deltablue.js (working copy)&lt;/span&gt;
&lt;span class=&#39;gu&#39;&gt;@@ -800,7 +800,7 @@&lt;/span&gt;
  
   // Build chain of n equality constraints
   for (var i = 0; i &amp;lt;= n; i++) {
&lt;span class=&#39;gd&#39;&gt;-    var name = &amp;quot;v&amp;quot; + i;&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+    var name = &amp;quot;v&amp;quot; + String(i);&lt;/span&gt;
     var v = new Variable(name);
     if (prev != null)
       new EqualityConstraint(prev, v, Strength.REQUIRED);
&lt;span class=&#39;gu&#39;&gt;@@ -836,8 +836,8 @@&lt;/span&gt;
  
   var dests = new OrderedCollection();
   for (var i = 0; i &amp;lt; n; i++) {
&lt;span class=&#39;gd&#39;&gt;-    src = new Variable(&amp;quot;src&amp;quot; + i, i);&lt;/span&gt;
&lt;span class=&#39;gd&#39;&gt;-    dst = new Variable(&amp;quot;dst&amp;quot; + i, i);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+    src = new Variable(&amp;quot;src&amp;quot; + String(i), i);&lt;/span&gt;
&lt;span class=&#39;gi&#39;&gt;+    dst = new Variable(&amp;quot;dst&amp;quot; + String(i), i);&lt;/span&gt;
     dests.add(dst);
     new StayConstraint(src, Strength.NORMAL);
     new ScaleConstraint(src, scale, offset, dst, Strength.REQUIRED);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/ggdmv/9993_of_the_javascript_source_code_was_already/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/03/c-style-assertions-in-javascript-via</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/03/c-style-assertions-in-javascript-via.html"/>
    <title>C-style assertions in JavaScript via JSShaper</title>
    <updated>2011-03-30T00:35:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;Earlier today I created &lt;code&gt;asserter&lt;/code&gt;, a new plugin for the &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; framework that provides you C-style assertions in JavaScript.&lt;/p&gt;

&lt;p&gt;Here’s a common style of doing JS assertions:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;myfn&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;nx&#39;&gt;assertTrue&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;argument x must be positive or zero&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When failing (let’s say &lt;code&gt;x&lt;/code&gt; is &lt;code&gt;-1&lt;/code&gt;) it will print something like this, perhaps with some kind of cleaned up stack trace:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;nx&#39;&gt;assertTrue&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;()&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;failed&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;:&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;argument&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;must&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;be&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;positive&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;or&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;zero&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is what you would write instead when using &lt;code&gt;asserter&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;myfn&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;nx&#39;&gt;Assert&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When failing, it will print this:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;nx&#39;&gt;Assertion&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;failed&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;:&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;myfn&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;file&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;prog&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;.&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;js&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;line&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I know which version I prefer. Thanks to the JSShaper framework I was able to create the &lt;code&gt;asserter&lt;/code&gt; plugin in no time, using only &lt;del&gt;34&lt;/del&gt; 29 &lt;em&gt;(edit: shorter and easier with declarative matching)&lt;/em&gt; lines of code.&lt;/p&gt;

&lt;p&gt;Here’s how it works. &lt;code&gt;asserter&lt;/code&gt; searches the syntax tree for calls to the &lt;code&gt;Assert&lt;/code&gt; function. It adds an extra string argument containing the assert condition expression (in text form), the enclosing function name (or &lt;code&gt;&amp;lt;anonymous&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;), the filename and lineno. Here’s what it would be replaced with:&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;nx&#39;&gt;Assert&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;x&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&#39;mi&#39;&gt;0&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;s2&#39;&gt;&amp;quot;x &amp;gt;= 0, function myfn, file prog.js, line 2&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;Assert&lt;/code&gt; function could look like this. Note that it will execute just fine even if you didn’t run your program through &lt;code&gt;asserter&lt;/code&gt;, although in that case it will just print a falsy value.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;kd&#39;&gt;function&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;Assert&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;condition&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;,&lt;/span&gt; &lt;span class=&#39;nx&#39;&gt;str&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
    &lt;span class=&#39;k&#39;&gt;if&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;!&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;condition&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;{&lt;/span&gt;
        &lt;span class=&#39;nx&#39;&gt;log&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;Assertion failed: &amp;quot;&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;+&lt;/span&gt; &lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;str&lt;/span&gt; &lt;span class=&#39;o&#39;&gt;||&lt;/span&gt; &lt;span class=&#39;nb&#39;&gt;String&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;nx&#39;&gt;condition&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;)));&lt;/span&gt;
        &lt;span class=&#39;nx&#39;&gt;quit&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;(&lt;/span&gt;&lt;span class=&#39;o&#39;&gt;-&lt;/span&gt;&lt;span class=&#39;mi&#39;&gt;1&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;);&lt;/span&gt;
    &lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;span class=&#39;p&#39;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Asserting preconditions in JS has never been simpler, no?&lt;/p&gt;

&lt;p&gt;In C you would disable assertions in your release-build (allowed since assertions mustn’t contain side effects). In JS we could do the same by simply creating another JSShaper pass that removes the &lt;code&gt;Assert&lt;/code&gt; calls.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/javascript/comments/h1b9n/chaining_assert_for_javascript/c1s47nx&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/03/case-for-restrict-mode</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/03/case-for-restrict-mode.html"/>
    <title>The case for restrict mode</title>
    <updated>2011-03-28T21:01:00+02:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;I sent this mail to the &lt;a href=&quot;http://groups.google.com/group/jsmentors/browse_thread/thread/d552623443f70df9?hl=en&quot;&gt;JSMentors&lt;/a&gt; list yesterday &lt;a href=&quot;http://groups.google.com/group/jsmentors/browse_thread/thread/d552623443f70df9?hl=en&quot;&gt;[full thread]&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;I’m announcing restrict mode for JavaScript, a proper subset of the language designed for improved program robustness. It’s opt-in similar to ES5 strict mode and enabled with a string literal (&lt;code&gt;&amp;quot;use
restrict&amp;quot;;&lt;/code&gt;). Continue reading for an explanation of how it works and why I think it’s useful.&lt;/p&gt;

&lt;p&gt;Operator semantics in regular (I call it loose) JavaScript are complex. In many cases the increased capabilities of this complexity doesn’t really outweigh the drawbacks, which include less robust programs and a language that’s both harder to learn for the beginners and harder to reason about for the experts.&lt;/p&gt;

&lt;p&gt;Of course, this is nothing new. The idea of writing programs that for example perform advanced number arithmetics on operands of type strings seems silly. Still, the language lets us do exactly that. I’m arguing that much of the operator semantics complexity in the language, with corresponding type coercion, is not wished for. If we could make some of it go away, fewer bugs would hide and our programs would become more robust. It may also become even more fun to write and debug them.&lt;/p&gt;

&lt;p&gt;This was discovered years ago when people started recommending the use of the (strict) &lt;code&gt;===&lt;/code&gt; operator instead of the (loose) &lt;code&gt;==&lt;/code&gt; operator, and the same for &lt;code&gt;!==&lt;/code&gt; vs &lt;code&gt;!=&lt;/code&gt;. It’s entirely possible to write programs without using the &lt;code&gt;==&lt;/code&gt; operator at all, and many programmers have chosen to do so. The result? More robust programs that are easier to reason about. For a beginner, it’s also a programming style that’s easier to learn and master.&lt;/p&gt;

&lt;p&gt;The problem is that there is no strict version of the (loose) &lt;code&gt;&amp;lt;&lt;/code&gt; operator. Or &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt; , &lt;code&gt;&amp;gt;=&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt; or &lt;code&gt;-&lt;/code&gt; and so on and so forth. Because these are loose and loose-only, we’ve kind of accepted them as-is and moved on. Imagine for a second what would happen if you suddenly had strict versions of these operators available, with the possibility of making the strict versions the default for parts of your program. How would you then define the strict version of &lt;code&gt;&amp;lt;&lt;/code&gt;? I bet you wouldn’t want it to accept an operand of primitive type number and another of primitive type string, since that would most likely be a bug in your program. Would you want it to accept &lt;code&gt;undefined &amp;lt; 3&lt;/code&gt;? I bet not. Perhaps you would like to get notified as early as possible with an exception if your strict rules are broken?&lt;/p&gt;

&lt;p&gt;Here’s the thing. We already limit ourselves to a subset of the semantics for these operators. At least we try to. We don’t mix strings, numbers and booleans randomly. We don’t perform arithmetics to &lt;code&gt;undefined&lt;/code&gt;, on purpose. We try to be in control of when type conversion happens, and so on. It’s just that we don’t necessarily use precisely the same subset, because we haven’t really though about what we should be restricting our programs to. And we don’t have any good means of getting notified as soon as we break that subset. We would like to, because no matter how good we are at this language, we still have to fix defects in our code. Defects some of which are related to breaking the subset.&lt;/p&gt;

&lt;p&gt;As you might have guessed by now I’ve defined such a subset and created a tool that modifies a JavaScript program so that it throws an exception whenever the subset is broken. I call the subset restrict mode for JavaScript and would like to get your feedback on it. It’s opt-in similar to ES5 strict mode, and you can easily opt-out for any part of your program. The tool is called &lt;code&gt;restricter&lt;/code&gt;. It’s a plugin for JSShaper, an extensible framework for JavaScript syntax tree shaping.&lt;/p&gt;

&lt;p&gt;The restricter-modified program should be used when running your test suite. Since restrict mode is a proper subset of regular JavaScript, you can still deploy the original program.&lt;/p&gt;

&lt;p&gt;Here are the restrict mode rules:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;0. === and !==
   are already strict by default, with identical semantics in
   restrict mode
1. == and !=
   are restricted to undefined == null (true) and
   undefined != null (false). They are otherwise only allowed
   when the === or !=== operator yields an identical result
2. + &amp;lt; &amp;lt;= &amp;gt;= &amp;gt;
   are restricted to primitive strings or numbers
   (but never a mix)
3. - * / % &amp;amp; | ^ ~v &amp;lt;&amp;lt; &amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; -v ++v v++ --v v--
   are restricted to primitive numbers
4. That&amp;#39;s it!
   +v !v &amp;amp;&amp;amp; || ?: o[k] and all other operators
   have identical semantics in restrict mode&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Many programs already conform to most of these rules, with the notable exception of the restrict &lt;code&gt;+&lt;/code&gt; operator in clause 2. In restrict mode you can no longer use &lt;code&gt;str + nostr&lt;/code&gt; to perform string conversion and concatenation at the same time. You can easily replace this with a call to a string-formatting function (I use &lt;code&gt;Fmt&lt;/code&gt;) or an explicit conversion via &lt;code&gt;String(nostr)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Clause 1 is a compromise and may need to change. I’d rather see &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;!=&lt;/code&gt; disallowed but there seem to be many programs relying on the &lt;code&gt;undefined == null&lt;/code&gt; behavior. For now I’ve added a pedantic setting where it’s disallowed.&lt;/p&gt;

&lt;p&gt;I’d like to hear your thoughts on all this. Does restrict mode resonate with you and if so, do the rules define the right subset?&lt;/p&gt;

&lt;p&gt;I have a preliminary web site up at &lt;a href=&quot;http://restrictmode.org/&quot;&gt;http://restrictmode.org/&lt;/a&gt; with more information. You can try restrict mode live in your browser by following the “Try restrict mode” link. Source code for JSShaper is available at &lt;a href=&quot;https://github.com/olov/jsshaper&quot;&gt;https://github.com/olov/jsshaper&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;/Olov&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/gddgf/ok_proggit_heres_the_longer_announcement_the_case/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <id>http://blog.lassus.se/2011/03/announcing-restrict-mode-for-javascript</id>
    <link type="text/html" rel="alternate" href="http://blog.lassus.se/2011/03/announcing-restrict-mode-for-javascript.html"/>
    <title>Announcing restrict mode for JavaScript</title>
    <updated>2011-03-27T00:34:00+01:00</updated>
    <author>
      <name>Olov Lassus</name>
      <uri>http://blog.lassus.se/</uri>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;Welcome redditors! I’m answering your questions in the &lt;a href=&quot;http://www.reddit.com/r/programming/comments/gc9i1/hi_proggit_im_announcing_restrict_mode_for/&quot;&gt;comment thread&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;div class=&#39;highlight&#39;&gt;&lt;pre&gt;&lt;code class=&#39;js&#39;&gt;&lt;span class=&#39;s2&#39;&gt;&amp;quot;use restrict&amp;quot;&lt;/span&gt;&lt;span class=&#39;p&#39;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Add that to the top of your JavaScript program. Run it through the &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; &lt;code&gt;restricter&lt;/code&gt; plugin, then combine it with the &lt;code&gt;restrictmode.js&lt;/code&gt; prelude and run it. Then hook it up with your units tests.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://restrictmode.org/try/&quot; title=&quot;Try restrict mode for JavaScript&quot;&gt;Try restrict mode&lt;/a&gt; directly in your browser (Firefox, Chrome or Safari)!&lt;/p&gt;

&lt;p&gt;For semantics and rationale visit &lt;a href=&quot;http://restrictmode.org&quot; title=&quot;a proper subset of the JavaScript language&quot;&gt;restrictmode.org&lt;/a&gt;. It’s not set in stone so &lt;a href=&quot;http://lassus.se&quot; title=&quot;Olov Lassus homepage&quot;&gt;let me know&lt;/a&gt; what you think.&lt;/p&gt;

&lt;p&gt;I’ll write more about &lt;a href=&quot;http://jsshaper.org/&quot; title=&quot;an extensible framework for JavaScript syntax tree shaping&quot;&gt;JSShaper&lt;/a&gt; in time. It’s an extensible framework for JavaScript syntax tree shaping and we’re going to be able to do lots of nifty things with it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.reddit.com/r/programming/comments/gc9i1/hi_proggit_im_announcing_restrict_mode_for/&quot;&gt;Show comments (reddit)&lt;/a&gt;&lt;/p&gt;</content>
  </entry>
  

</feed>
