<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Mon, 20 Feb 2012 06:37:40 GMT--><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><title>Mascara JavaScript Compiler</title><subtitle>News</subtitle><id>http://blog.mascaraengine.com/news/</id><link rel="alternate" type="application/xhtml+xml" href="http://blog.mascaraengine.com/news/"/><link rel="self" type="application/atom+xml" href="http://blog.mascaraengine.com/news/atom.xml"/><updated>2012-01-08T12:19:15Z</updated><generator uri="http://www.squarespace.com/" version="Squarespace Site Server v5.11.81 (http://www.squarespace.com/)">Squarespace</generator><entry><title>Named arguments</title><id>http://blog.mascaraengine.com/news/2011/5/6/named-arguments.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2011/5/6/named-arguments.html"/><author><name>Olav</name></author><published>2011-05-06T20:22:55Z</published><updated>2011-05-06T20:22:55Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>In <a href="http://www.mascaraengine.com/download">Mascara 1.8.1</a> a limitation have been fixed. Previously, named argument could only be used with optional parameters. Now named arguments can be used with both required and optional parameters.</p>

<p>Named arguments can help making function calls more understandable in cases where the meaning of arguments is not immediately obvious. For example this:</p>

<pre class=prettyprint>
document.importNode(elem, true)
</pre>

<p>Is probably not as clear as this:</p>

<pre class=prettyprint>
document.importNode(elem, deep=true)
</pre>

<p>When using named arguments, the order of the arguments doesn't matter, as long as a value is provided for all required parameters. Both named an unnamed arguments can be used in the same call (as shown above), but named arguments must follow after all unnamed arguments.</p>
]]></content></entry><entry><title>Yield!</title><id>http://blog.mascaraengine.com/news/2011/4/19/yield.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2011/4/19/yield.html"/><author><name>Olav</name></author><published>2011-04-19T16:32:56Z</published><updated>2011-04-19T16:32:56Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>The <a href="http://www.mascaraengine.com/doc/yield">yield </a> statement is a new  feature in version 1.8.</p>

<p>A yield statement turns a function into a <em>generator</em>, which lazily generates a list of values. Each <code>yield</code> in the function returns a single value. The state of the generator function is preserved, so when the next value is requested, the execution of the generator function continues immediately after the yield.</p>

<p>An example (taken from <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Iterators_and_Generators#Generators.3a_a_better_way_to_build_Iterators">MDC</a>):</p>

<pre class=prettyprint>
function simpleGenerator(){
  yield "first";
  yield "second";
  yield "third";
  for (var i = 0; i < 3; i++)
    yield i;
}

var g = simpleGenerator();
print(g.next()); // prints "first"
print(g.next()); // prints "second"
print(g.next()); // prints "third"
print(g.next()); // prints 0
print(g.next()); // prints 1
print(g.next()); // prints 2
print(g.next()); // StopIteration is thrown
</pre>

<p>The generator can also be used in <code>for each</code>-loops as if it was an ordinary array:</p>

<pre class=prettyprint>
function numGenerator(){
  for (var i = 0; i < 3; i++)
    yield i;
}

for each (var x in numGenerator()) {
   console.log(x); // prints 0,1,2
}
</pre>

<p>(The for-loop catches the StopIteration internally in this case, as it just signals the end of the list.)</p>

<p>Generators are also known in languages like Python and C#. Mozilla have native support for generators, but no other browser I know of have.</p>

<p><strong>Caveat</strong></p>

<p>Generators are pretty cool, but there is one drawback. The compiler output is pretty inscrutable, since it requires somewhat convoluted code to support generator semantics in downlevel JavaScript. This is not a problem when the code runs, but may make it unpleasant to debug generator function produced by Mascara. </p>

<p>Generally Mascara aims to produce output code which is as clear and pretty as possible, but in the case of yield this is not really very clear.</p>

<p>Anyway, I would love feedback from Mascara users if you find 'yield' useful, and whether the generated code is acceptable.</p>
]]></content></entry><entry><title>Anonymous packages</title><id>http://blog.mascaraengine.com/news/2011/3/25/anonymous-packages-1.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2011/3/25/anonymous-packages-1.html"/><author><name>Olav</name></author><published>2011-03-25T20:36:36Z</published><updated>2011-03-25T20:36:36Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Version 1.8 adds (among other things) support for anonymous packages. Variables declared inside an anonymous package is only visible inside the same package block.</p>

<pre class="prettyprint">
    var x = "hello";
    package {
        var x = 17;
        console.log(x); // writes 17
    }
    console.log(x); // writes "hello"
</pre>

<p>Anonymous packages are useful for ensuring that a block of code doesn't pollute the global namespace. If you have several anonymous packages, they are also hidden for each other. </p>

<p>Behind the scenes, anonymous packages are transformed into this pattern:</p>

<pre class="prettyprint">
   (function(){
       ...
   })();
</pre>
]]></content></entry><entry><title>F3 - Open Definition</title><id>http://blog.mascaraengine.com/news/2011/1/22/f3-open-definition.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2011/1/22/f3-open-definition.html"/><author><name>Olav</name></author><published>2011-01-22T20:41:19Z</published><updated>2011-01-22T20:41:19Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>The <a href="http://blog.mascaraengine.com/news/2010/7/19/mascara-eclipse-plugin-alpha-preview.html">Eclipse plug-in for Mascara</a> development is progressing steadily.</p>
<p>Newest feature is support for "<strong>Open Definition</strong>". Place the caret on an identifier (variable name, property, function name, whatever) and hit F3 (or select "Open Definition" on the right-click context menu). The editor will jump to the definition of the identifier, even if it is a different file.</p>
<p>It also works with classic JavaScript code, by the way [1].</p>
<p>This feature is a major milestone because it shows deep integration between the editor and the compiler AST. Additional code-aware features like "Show References", "Show Call-graph" etc. can be built upon this infrastructure.</p>
<p>It also makes development much more pleasant and fun.</p>
<p>[1] As long as you don't get too fancy.</p>]]></content></entry><entry><title>Object Literals</title><id>http://blog.mascaraengine.com/news/2010/12/14/object-literals.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/12/14/object-literals.html"/><author><name>Olav</name></author><published>2010-12-14T10:36:17Z</published><updated>2010-12-14T10:36:17Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p><a href="http://www.mascaraengine.com/download">Release 1.7.3</a> adds full support for object literals.</p>

<p>Sometimes you just need a single, unique object. The easiest way to achieve this is an object literal. Example:</p>

<pre class="prettyprint">
    var x = { 
              _v:1, 
              get_v : function(){ return this._v; }
            };
    var y = x.get_v();
</pre>

<p>This common pattern is now fully supported including type verification and code completion.</p>

<p>Furthermore you can now use getters and setters in object literals, which is some of the new syntax syntax in ECMAScript 5:</p>

<pre class="prettyprint">
    var x = { 
             _v:0, 
             set v(x){ this._v = x; }, 
             get v(){ return this._v; }
             };
    x.v = 100;
    var y = x.v;
</pre>

<p>(Note that the getter/setter syntax is slightly different in object literals  - you don’t need the <code>function</code> keyword here.)</p>

<p>Object literals are most convenient when you only need a single instance of an object. For example a global object literal is the equivalent to the singleton pattern as in Java - but the syntax is much simpler.</p>

<p>The limitations are that an object literal does not support inheritance. If you need that, a class is better.</p>
]]></content></entry><entry><title>Definite assignment</title><id>http://blog.mascaraengine.com/news/2010/11/29/definite-assignment.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/11/29/definite-assignment.html"/><author><name>Olav</name></author><published>2010-11-29T11:14:26Z</published><updated>2010-11-29T11:14:26Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Another new feature to alleviate headaches: Definite assignment analysis.</p>

<p>Definite assignment analysis ensures that no variable is used before it has been assigned a value. This can prevent some subtle bugs, and will in general make code more clear. It is inspired by languages like Java and C#.</p>

<p>Consider this code:</p>

<pre class="prettyprint">
var x : String;
if (a>10) x = "Hello";
else if (a<10) x = "Goodbye";
alert(x);
</pre>

<p>There <em>may</em> be subtle bug in this code. What if <code>a</code> has a value which is exactly 10? In that case, <code>x</code> will never be assigned a value, and the <code>undefined</code> value will be sent to the alert function. </p>

<p>Now, that may or may not be a bug depending on the intentions of the developer. However it is easy to miss that the variable in some case will never be assigned a value. </p>

<p>In the newest Mascara, you will for this reason get a compiler warning on the last line, because <code>x</code> may not be assigned a value at that point.</p>

<p>The right way to fix the code depends on the intention. If the <code>if</code> branches were supposed to cover all eventualities, then add an <code>else</code> or change the <code>else if</code> into an <code>else</code>. </p>

<p>If the variable should be <code>null</code> if no of the conditions are met, then just initialize it to <code>null</code> in the first line. (The <code>null</code> value is somewhat equivalent to the <code>undefined</code> value. However, there is the important difference that <code>null</code> indicates that a legitimate variable has been explicitly assigned the <code>null</code> value, while ‘undefined’ usually indicates an error or missing variable.)</p>

<p>To be clear, this improvement is not a part of the ECMAScript spec, it is a Mascara-specific extra verification. It will probably never be part of the ECMAScript because of backwards-compatibility issues. As always, I appreciate feedback on this feature.</p>
]]></content></entry><entry><title>Another improvement related to 'var'.</title><id>http://blog.mascaraengine.com/news/2010/11/27/another-improvement-related-to-var.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/11/27/another-improvement-related-to-var.html"/><author><name>Olav</name></author><published>2010-11-27T16:48:50Z</published><updated>2010-11-27T16:48:50Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Wrote yesterday about the new warning if a local variable is used before it is declared with 'var'. A related issue is <code>var</code>-declarations inside nested blocks. Say:</p>

<pre class=prettyprint>    
    if (2+2=5) {
      switch (true) {
        case false:
            var location;
      }
    }
    location = "something"
</pre>

<p>Here the <code>var</code>-statement does occur before the first reference to the variable, but the var-statement is nested inside <code>if</code>- and <code>switch</code>-blocks, which makes it easy to miss, and it is counter-intuitive that a deeply nested statement will change the meaning of a variable in an outer block. Furthermore, the statement cannot possibly ever be executed as the code looks now - but the <code>var</code> still have effect.</p>

<p>All this is highly counter-intuitive, and makes it unnecessary tricky to read code. Therefore Mascara provides a simple fix.</p>

<p>The new rule is: A local variable can only be accessed in the same block, or in nested blocks inside the block where the variable is declared with <code>var</code>.</p>

<p>E.g. this is legal:</p>

<pre class=prettyprint>
    var location = "hello";
    location = "goodbuy";
</pre>

<p>And this is legal:</p>

<pre class=prettyprint>
    var location = "hello";
    if (something) {
        location = "goodbuy";
    }
</pre>

<p>And this is legal:</p>

<pre class=prettyprint>
    if (something) {
        var location = "hello";
        location = "goodbuy";
    }
</pre>

<p>But this is <em>not</em> legal, because the variable is used outside the block with the <code>var</code>-statement:</p>

<pre class=prettyprint>
    if (something) {
        var location = "hello";    
    }
    location = "goodbuy";  //WARNING!
</pre> 

<p>The last example will generate a ´Name not found: 'location' in function scope´-warning.</p>

<p>This is a small fix, but it is the accumulation of small things like these
which makes the difference between long nights of bug-tracing, enjoyable development of new features.</p>

<p><em>Clarification</em>: this does not change the run-time semantics of the <code>var</code> statement, it is just a compile-time verification which avoids some confusing and potentially buggy patterns. It is therefore not equivalent to the proposed 'let'-scopes which does change the semantics, e.g. by allowing you to 'shadow' variables declared in an outer scope.</p>
]]></content></entry><entry><title>Mascara 1.7 released</title><id>http://blog.mascaraengine.com/news/2010/11/25/mascara-17-released.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/11/25/mascara-17-released.html"/><author><name>Olav</name></author><published>2010-11-25T12:23:16Z</published><updated>2010-11-25T12:23:16Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p><a href="http://www.mascaraengine.com/download">Download here</a>!</p>
<p>I sometimes forget to post on the blog when a new version is released - sorry about that! We are now up to version 1.7. Everyone should upgrade, since each new release contains bug-fixes and improvements. (<a href="http://blog.mascaraengine.com/news/2010/7/19/mascara-eclipse-plugin-alpha-preview.html">The Eclipse plug-in</a> is also regularly updated, but should still be considered alpha stage. You will get the updates automatically through Eclipse, if you have it installed.)</p>
<p>Mascara version 1.7 in particular have some improvements which eliminates groups of subtle bugs which traditionally plagues JavaScript development.</p>
<p>The first one I will mention is the handling of the "<strong>var</strong>" statements.</p>
<p>In JavaScript, the var statement declares a local variable. If you refer to a variable which have not been declared with "var" in the current function, the runtime looks in the outer scopes and the global scope.</p>
<p>Now the subtle issue: A variable is resolved to a local var even if the "var"-statement occurs <em>after</em> the reference to the var.</p>
<p>Say:</p>
<pre class="prettyprint">function f() {
    location = "somewhere"; // does this assign to a local or global variable?

    {..lots of code...}

    var location; // this statement means that first line refers to a local.
}</pre>
<p>The '<strong>var</strong>' may even be defined deep inside nested statement block like ifs and loops - it still changes the meaning of the "location" in the first line of the function.</p>
<p>This is obviously bad. It breaks the basic assumption that you shouldn't have to look <em>forward </em>inside a function to determine if a variable is a local or global. Furthermore, no other language works like JavaScript in this case. Most languages will simply not allow you to use a local variable before it is declared. (Python does have semantics closer to JavaScript, but in a subtly different way which avoids the kind of bug JavaScript is prone to.)</p>
<p>The solution for this is simple: <strong>Mascara yields a warning if you read or assign to a local variable before it is declared with 'var'</strong>.</p>
<p>You may wonder why I made it just a <em>warning </em>which can be ignored, rather than a downright compiler <em>error</em>?</p>
<p>The reason is that some code (legacy code, third party libraries) may use this pattern for whatever reason, but still be thoroughly tested and work correctly. So you might not want to fix it, or you may want to fix it at your own lesiure, while still being able to use the code in the meantime.</p>
<p>Since it is a prime directive for Mascara to work well with libraries written in classic JavaScript, and allow a painless upgrade path from classic JavaScript, this is an important consideration.</p>
<p>Therefore the general principle for warning/error distinction in Mascara is that if something could just theoretically work if the developer knows what they're doing, it is just yields a warning. Errors is reserved for things which could never possibly work, like syntax errors or inconsistent types.</p>
<p>Anyway, hope you enjoy the improvements, and have fun coding.</p>]]></content></entry><entry><title>Mascara Eclipse Plugin - alpha preview</title><id>http://blog.mascaraengine.com/news/2010/7/19/mascara-eclipse-plugin-alpha-preview.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/7/19/mascara-eclipse-plugin-alpha-preview.html"/><author><name>Olav</name></author><published>2010-07-19T11:33:21Z</published><updated>2010-07-19T11:33:21Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Experienced developers know that tool support are very important for the overall productivity when working with a language. Mascara has a solid core compiler, but not much language specific editor support.</p>
<p>Therefore I have developed an Eclipse plugin with Mascara language support as the first example of integrated editor support. (Support for other editors/IDE's may follow in the future depending on user interest, although I'm not promising anything at this point.)</p>
<p>The Eclipse plugin is still in the alpha stage which means it has bugs and missing features, and you basically should't expect to be able use it for serious work yet. It is released mainly as a proof of concept, and because many users like to see how its going.</p>
<h3>How to get to "Hello world" using the Mascara Eclipse Plugin</h3>
<p><strong>1) Install <a href="http://www.python.org/download/releases/2.6.5/">python 2.6</a></strong> if you dont already have it.<br /><br /><strong>2) Install the plugin</strong><br />Open eclipse 3.5 (earlier versions may work but I havent tested it) and install the editor support via:</p>
<p><strong>Help </strong>-&gt; <strong>Install new software</strong> -&gt; <strong>Add</strong><br />In the "Add site" dialog:</p>
<p>Name: Mascara editor<br />Location: http://mascaradownload.appspot.com/releases/mascaraeclipse</p>
<p style="padding-left: 30px;"><span class="thumbnail-image-block ssNonEditable"><span><a href="javascript:showFullImage('/display/ShowImage?imageUrl=%2Fstorage%2Frepository.png%3F__SQUARESPACE_CACHEVERSION%3D1279536876745',669,793);"><img src="../../storage/thumbnails/4492088-7772212-thumbnail.jpg?__SQUARESPACE_CACHEVERSION=1279536876748" alt="" /></a></span></span></p>
<p>Check off "Mascara editor" in the list, and click <strong>Next</strong>. The feature shound now install automatically.<br />&nbsp;&nbsp; &nbsp;<br />You will get a warning that the content is unsigned. Press <strong>OK</strong>.<br />&nbsp;&nbsp; &nbsp;<br />When prompted to restart Eclipse, choose <strong>OK</strong><br />&nbsp;&nbsp; &nbsp;<br /><em>Note: On Unix, the plugin assumes python is located in /usr/bin. If it is located somewhere else,<br />you have to configure it on the preference page for Mascara (Window-&gt;Preferences).</em></p>
<p><strong>3) Create a new project in Eclipse:</strong></p>
<p style="padding-left: 30px;"><strong>File </strong>-&gt; <strong>New </strong>-&gt; <strong>New project</strong><br />(Just select a standard project: <strong>General </strong>-&gt; <strong>Project</strong>. There is no specific Mascara project type yet.)</p>
<p><strong>4) Create a new file:</strong></p>
<p style="padding-left: 30px;"><strong>File </strong>-&gt; <strong>New </strong>-&gt; <strong>Other</strong><br />Select <strong>Mascara </strong>/ <strong>New Esx File</strong>.</p>
<p style="padding-left: 30px;">The file shoult have the ".esx", extension, so call it something like "main.esx".</p>
<p><br /><strong>5) Write something like this in the file:</strong></p>
<pre class="prettyprint">import browserapi;<br />window.onload&nbsp; = function() window.alert("hello world");</pre>
<p>(Here you should hopefully notice syntax coloring and language aware code completion! At least halfway working.)</p>
<p><strong>6) Click "Run"</strong> (the green arrow)</p>
<p style="padding-left: 30px;"><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/run.png?__SQUARESPACE_CACHEVERSION=1279537066147" alt="" /></span></span></p>
<p>The first time run is clicked you get a wizard to create a <em>run configuration</em>:</p>
<p style="padding-left: 30px;"><span class="thumbnail-image-block ssNonEditable"><span><a href="javascript:showFullImage('/display/ShowImage?imageUrl=%2Fstorage%2Frunconfig.png%3F__SQUARESPACE_CACHEVERSION%3D1279539650769',604,525);"><img src="http://blog.mascaraengine.com/storage/thumbnails/4492088-7772224-thumbnail.jpg?__SQUARESPACE_CACHEVERSION=1279539650772" alt="" /></a></span></span></p>
<p>In <em>Select Esx source</em>, the new file is selected by default</p>
<p>Select <strong>Create new HTML file</strong>. This will generate a new HTML file with a script reference to the (compiled) esx file. Click <strong>Next</strong>.</p>
<p style="padding-left: 30px;"><span class="thumbnail-image-block ssNonEditable"><span><a href="javascript:showFullImage('/display/ShowImage?imageUrl=%2Fstorage%2Fsave.png%3F__SQUARESPACE_CACHEVERSION%3D1279539993945',604,525);"><img src="http://blog.mascaraengine.com/storage/thumbnails/4492088-7772237-thumbnail.jpg?__SQUARESPACE_CACHEVERSION=1279539993948" alt="" /></a></span></span></p>
<p>The dialog suggest where to save the HTML file. Click <strong>Finish</strong>.</p>
<p style="padding-left: 30px;">&nbsp;</p>
<p>7) The default browser should now start with the HTML page and the included script, displaying the <em>hello world</em> alert.</p>
<p style="padding-left: 30px;"><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/hello.png?__SQUARESPACE_CACHEVERSION=1279537193827" alt="" /></span></span></p>
<p>Have fun! As always, questions or suggestions welcome.</p>
<h3>Bonus: Hello world using JQuery</h3>
<p>If you like JQuery you may prefer to write something like this  instead:</p>
<pre class="prettyprint">import browserapi;<br />import jqueryapi;<br /><br />$(function(){<br />&nbsp;&nbsp; &nbsp;$("Body").append(<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$("&lt;div&gt;Hello world&lt;/div&gt;")<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;.fadeIn(1000));<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;});<br /><br /></pre>
<p>You will have to add a &lt;script&gt;-element to the HTML referencing the JQuery library before running. You can dowload JQuery, or just hotlink to the hosted version:</p>
<pre class="prettyprint">&lt;script src ="<span class="free external">http://code.jquery.com/jquery-1.4.2.min.js</span>"&gt;&lt;/script&gt;</pre>
<p>Happy coding!</p>]]></content></entry><entry><title>Mascara 1.5 released - faster compilation</title><id>http://blog.mascaraengine.com/news/2010/7/9/mascara-15-released-faster-compilation.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2010/7/9/mascara-15-released-faster-compilation.html"/><author><name>Olav</name></author><published>2010-07-09T15:11:47Z</published><updated>2010-07-09T15:11:47Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Mascara 1.5 has been released. <a href="http://www.mascaraengine.com/download">Download</a>.<br /><br />The most important new feature is probably support for incremental compilation.<br />Previously, the Mascara compiler could get quite slow when working on larger projects using the command-line compiler. The reason is that for every invocation of the compiler, all<br />files included or imported were recompiled from scratch - even if only a single file were edited. Also, there is some overhead for initializing the compiler itself which happen every time the compiler is invoked.<br /><br />The solution (inspired by the Scala language) is a new command line option with creates the compiler as a background process. <br />This background compiler caches partially-compiled files, and only recompiles the files that have changed since last compilation, and since the compiler itself<br />is pre-loaded in memory, the result is that the output is much faster.<br /><br />It is used like this:<br /><br /></p>
<pre>translate.py somefiletocompile.esx --server 9000<br /></pre>
<p><br />The number after the <strong>--server</strong> argument is a port number (can be any number not in use) where the compiler process will listen. The first time, the compiler is initialized (which may take some time)<br />but afterwards the script will detect that the compiler is already running at the port, and dispatch the compilation job, which should be much faster.</p>
<p>Have fun!</p>
<p>&nbsp;</p>]]></content></entry></feed>
