<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Fri, 01 Jun 2012 03:42:44 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-04-17T07:07:32Z</updated><generator uri="http://www.squarespace.com/" version="Squarespace Site Server v5.11.81 (http://www.squarespace.com/)">Squarespace</generator><entry><title>Sourcemap support in Chrome greatly improves debugging</title><id>http://blog.mascaraengine.com/news/2012/4/16/sourcemap-support-in-chrome-greatly-improves-debugging.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2012/4/16/sourcemap-support-in-chrome-greatly-improves-debugging.html"/><author><name>Olav</name></author><published>2012-04-16T08:02:27Z</published><updated>2012-04-16T08:02:27Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>The latest version of the Chrome browser has support for 'sourcemaps' which promises to make degugging compiled JavaScript much more pleasant.</p>

<p>Until now, a major drawback to compiled JavaScript (like what is produced by Mascara) is that debugging becomes more tricky. Moderne browsers have quite nice debugging tools, but they only operate on the generated code, not the source we initially wrote.</p>

<p>Browser vendors have recognized this issue and agreed on "source map", a standard for matching compiled javascript with source code. Chrome is the first browser to support this feature.</p>

<p>As an example, here is some code which will generate an error at runtime:</p>

<pre class=prettyprint>
// some filler code just to be sure lines are different
class A {}
class B {} extands A {}

var a : Date = null;
g.getDate(); // should throw runtime error
</pre>

<p>This is how the runtime error will be displayed normally, if you open the Developer Tools window in Chrome: (Wrench icon -> Tools -> Developer Tools or press F12)</p>

<p><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/DebuggingWithout.png?__SQUARESPACE_CACHEVERSION=1334566593980" alt=""/></span></span></p>

<p>You see the error in the (more or less opaque) generated code. You have to manually match with the source code to find the source of the error.</p>

<p>But with sourcemap support, the browser can show the error directly in the original source:</p>

<p><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/DebuggingWith.png?__SQUARESPACE_CACHEVERSION=1334566784799" alt=""/></span></span> </p>

<p>It is even possible to set breakpoints and debug through the original source:</p>

<p><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/DebuggingWith2.png?__SQUARESPACE_CACHEVERSION=1334566816418" alt=""/></span></span></p>

<p>I havent yet investigated how "deep" the support is for sourcmap in Chrome, but it looks impressive. I would love to hear feedback from Mascara-users about how this feature works for them.</p>

<p><em>How it works</em></p>

<p>Under the hood Mascara generating a seperate "sourcemap"-file which associates code positions in the generated code with the source. (The sourcemap file has the same name as the generated file but with an additional ".map" extension.) Debugging requires requires that the map and source files is deployed along with the generated files. You might choose to only allow debugging on the development server and not deploy the map and sources to the production server.</p>

<p><em>How to enable</em></p>

<p>When using the command-line compiler, source maps are enabled with the cli argument "--generate-source-mappings-file", eg. </p>

<pre>
python translate.py somesourcefile.esx --generate-source-mappings-file
</pre>

<p>Sourcemaps are generated by default when using the Eclipse-plugin though.</p>

<p>It also reqires enabling in Crome developer tools. Click the bolt in the lower right corner of the window to get the settings window, and check "enable source map":</p>

<p><span class="full-image-block ssNonEditable"><span><img src="http://blog.mascaraengine.com/storage/Enable1.png?__SQUARESPACE_CACHEVERSION=1334567202668" alt=""/></span></span></p>

<p>Chrome is the first browser to support source map, but support is also underway in Mozilla/Firefox.</p>

<p>Here is a<a href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/"> more detailed article</a> about source maps.</p>
]]></content></entry><entry><title>Deprecation warning: Namespace attributes in declarations</title><id>http://blog.mascaraengine.com/news/2012/3/12/deprecation-warning-namespace-attributes-in-declarations.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2012/3/12/deprecation-warning-namespace-attributes-in-declarations.html"/><author><name>Olav</name></author><published>2012-03-12T17:36:56Z</published><updated>2012-03-12T17:36:56Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>The next release of Mascara is going to have a deprecation warning for namespace attributes in declarations:</p>

<pre class=prettyprint>
namespace otherworld;

otherworld var x = "hello";
</pre>

<p>The recommended alternative is to use packages, like for example:</p>

<pre class=prettyprint>
package otherworld  {
    var x = "hello";
}
</pre>

<p>Namespace attributes will still work though, if you ignore the warning, but the functionality will probably be completely removed some time in the future. </p>

<p>The use of namespace attributes is kind of an obscure feature hailing from the drafts of ECMAScript 4, but which have since been abandoned. I suspect it is quite rarely used, but if any user likes or is dependent on this feature, please let us know.</p>
]]></content></entry><entry><title>Support for "arrow" shorthand for function expressions</title><id>http://blog.mascaraengine.com/news/2012/3/8/support-for-arrow-shorthand-for-function-expressions.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2012/3/8/support-for-arrow-shorthand-for-function-expressions.html"/><author><name>Olav</name></author><published>2012-03-08T12:29:04Z</published><updated>2012-03-08T12:29:04Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>The "arrow" syntax is new (experimental) syntax for writing more compact function expressions:</p>

<pre class="prettyprint">
x=>x+1
</pre>

<p>Is equivalent to:</p>

<pre class="prettyprint">
function(x)x+1
</pre>

<p>or</p>

<pre class="prettyprint">
function(x){return x+1}
</pre>

<p>The new syntax is inspired by C#, and is under consideration because the <code>function</code> syntax is somewhat verbose. For example this:</p>

<pre class="prettyprint">
var x = [1,2,3].map(x=>x*2).filter(x=>x>2).map(x=>x.toString());
</pre>

<p>Is a lot easier on the eyes than:</p>

<pre class="prettyprint">
var x = [1,2,3].map(function(x){return x*2;})
    .filter(function(x){return x>2})
    .map(function(x){return x.toString();});
</pre>

<p>The arrow syntax allows multiple parameters if they are enclosed in parenthesis:</p>

<pre class="prettyprint">
var x = [1,2,3].map((x, ix)=>x*ix)
</pre>

<p>And type annotations, just like ordinary function sytax:</p>

<pre class="prettyprint">
var x = [1,2,3].map((x:int)=>x*2)
</pre>

<p>It also allows multiple statements in the body if it is enclosed in curly brackets:</p>

<pre class="prettyprint">
var x = [1,2,3].map(x=>{ var twice = x*2; return twice;})
</pre>

<p>Be aware however, that the syntax is just a proposal and not fully agreed on by the standard committee. For example the semantics of <code>this</code> and <code>break</code> and <code>continue</code> inside the function is not decided. For this reason, it is just treated the same way as in ordinary functions in Mascara.</p>
]]></content></entry><entry><title>Improved type inference for function expressions</title><id>http://blog.mascaraengine.com/news/2012/3/7/improved-type-inference-for-function-expressions.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2012/3/7/improved-type-inference-for-function-expressions.html"/><author><name>Olav</name></author><published>2012-03-07T12:56:51Z</published><updated>2012-03-07T12:56:51Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>This is a subtle - but in my opionion very attractive - improvement in the type inference. What is the type of this expression:</p>

<pre class=prettyprint>
var a = [1,2,3,4].map(function(x)x+1);
</pre>

<p>(<code>map</code> is one of the convenient new Array-methods in ECMAScript 5. It takes a function as argument, and return a new array containing the result of applying the function to each item in the original array.)</p>

<p>In previous version, Mascara would not know the type of the parameter x in the function expression. You had to add a type annotation like <code>function(x:int)x+1</code> to get the code to be type safe and get code completion and so on.</p>

<p>Now Mascara 1.9 can infer from the context that <code>x</code> will always be an <code>int</code>, so you get type safety automatically.</p>

<p>Mascara also infers that the result is an array of integers. If you wrote:</p>

<pre class=prettyprint>
var a = [1,2,3,4].map(function(x)x>2);
</pre>

<p>It would infer that the result is an array of boolans. All this makes it much more convenient to work with function expressions as arguments (also known as higher-order functions).</p>
]]></content></entry><entry><title>Mascara 1.9 released</title><id>http://blog.mascaraengine.com/news/2012/3/7/mascara-19-released.html</id><link rel="alternate" type="text/html" href="http://blog.mascaraengine.com/news/2012/3/7/mascara-19-released.html"/><author><name>Olav</name></author><published>2012-03-07T12:42:47Z</published><updated>2012-03-07T12:42:47Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Mascara 1.9.0 <a href="http://www.mascaraengine.com/download">has been released</a>, with a bunch of improvements which will be detailed in upcoming blog posts.</p>

<p>A notable change is that Mascara has been upgraded to use <a href="http://www.python.org/download/releases/2.7.2/">Python 2.7</a>, so you will have to download this if you dont have it already.</p>
]]></content></entry><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></feed>
