This blog is for news, announcements and questions regarding the
Mascara JavaScript compiler. Try it online | Download latest release
Friday
May062011

Named arguments

In Mascara 1.8.1 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.

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

document.importNode(elem, true)

Is probably not as clear as this:

document.importNode(elem, deep=true)

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.

Tuesday
Apr192011

Yield!

The yield statement is a new feature in version 1.8.

A yield statement turns a function into a generator, which lazily generates a list of values. Each yield 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.

An example (taken from MDC):

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

The generator can also be used in for each-loops as if it was an ordinary array:

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

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

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

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

Caveat

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.

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.

Anyway, I would love feedback from Mascara users if you find 'yield' useful, and whether the generated code is acceptable.

Friday
Mar252011

Anonymous packages

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.

    var x = "hello";
    package {
        var x = 17;
        console.log(x); // writes 17
    }
    console.log(x); // writes "hello"

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.

Behind the scenes, anonymous packages are transformed into this pattern:

   (function(){
       ...
   })();
Saturday
Jan222011

F3 - Open Definition

The Eclipse plug-in for Mascara development is progressing steadily.

Newest feature is support for "Open Definition". 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.

It also works with classic JavaScript code, by the way [1].

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.

It also makes development much more pleasant and fun.

[1] As long as you don't get too fancy.

Tuesday
Dec142010

Object Literals

Release 1.7.3 adds full support for object literals.

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

    var x = { 
              _v:1, 
              get_v : function(){ return this._v; }
            };
    var y = x.get_v();

This common pattern is now fully supported including type verification and code completion.

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

    var x = { 
             _v:0, 
             set v(x){ this._v = x; }, 
             get v(){ return this._v; }
             };
    x.v = 100;
    var y = x.v;

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

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.

The limitations are that an object literal does not support inheritance. If you need that, a class is better.