fredag den 7. august 2009

Mascara 1.1 and ECMAScript 5

Mascara version 1.1 has been released. New in this release is support for a number of features from ECMAScript 5.

The ECMAScript 5 spec (PDF link) is the latest spec released by the ECMA group. It is in "candidate draft" stage, which means that it is more or less final, but it awaits experience from implementations.

It may seem curious that ECMA releases an ECMAScript 5 version when there haven't been released a final ECMAScript 4 version. (The previous release was ECMAScript 3 from 1999!) The reason is this: The ECMAScript 4 version was a large, monolithic specification where a great number of new features were introduced all at once. Some parts of the working group felt that this was far to ambitious, and wanted lesser, incremental improvements to the language. It was therefore decided to evolve the language in smaller steps over the course of several spec releases.

Perhaps to avoid confusion, the working group have decided to simply skip the version number 4, and jump straight to ECMAScript 5 - although ECMAScript 5 is a small subset of what as planned for ECMAScript 4. The next version after that, ECMAScript 6 (code named "Harmony") is under active development, and it is still not settled what it will encompass. There may very well be a ECMAScript 7 or 8 beyond that.

Since Mascara already implements a large part of what was proposed for ECMAScript 4, it is far more advanced than ECMAScript 5, and perhaps even more advanced than ECMAScript 6 will turn out. We will see.

Here is a overview of ECMAScript 5 and how it relates to Mascara.

New features
There is a number of new features, like getters/setter and the higher-order array methods like map, reduce, forEach etc. These features are available in Mascara 1.1 (and will be detailed in a later post).
They are also the foundation for features like array comprehensions and for-each loops which may be introduced in ES6 (and which already are available in Mascara).

Attribute-control and object-lockdown
There is a number of core methods in ECMAScript 5 which allows the programmer to "seal" and "freeze" objects, and to to manipulate mutability and enumerability of object properties. John Resig have a written a good overview of this. This level of control cannot be implemented or emulated in Mascara, since they require direct support in the language engine. However Mascara solves many of the uses cases for this using const variables, non-dynamic objects and in general the type system ensures at compile time that you don't modify values that are supposed to be immutable.

ECMA is definitely doing the right thing by strengthening the foundation of the language with this new level of control. However, it will probably take many years before this is broadly supported, so until then static guarantees (like what Mascara can provide) is a lot better than nothing.

Strict mode
ECMAScript 5 introduces a strict mode, where some of the more reckless styles of programming JavaScript is disallowed. For example the notoriously dangerous with-statement is disallowed, it is illegal to assign to a variable which have not been declared and so on. This should help catch a bunch of bugs.

Compared to this Mascara is only halfway strict. Some parts of strict mode, like the requirement that variables must be declared, is also required in Mascara. On the other hand, with is still allowed, although the compiler will give you a warning. Generally Mascara takes a passive-aggressive approach to strictness, where the compiler will complain (using warnings) about a lot of potentially unsafe code (e.g. using with, type errors and so on), but will still allow it. A compilation will only fail if it is totally impossible to the compiler to generate meaningful output. The filp side to this is that users of Mascara should pay attention to warnings, since unsafe code which would be flagged as a compiler error in more strict languages (like Java or C#) only will lead to a warning in Mascara.

I am contemplating introducing a way to customize the pedantic level of the Mascara compiler. I think it would be useful with a "strict" mode where all type mismatches led to errors, not just warnings. Suggestions from readers are welcome!

In general ECMAScript 5 is a well thought-out and important spec, but it is also somewhat unexciting for most developers. Many of the features are foundational work, and they only become interesting when you start to build more advanced language features on top of them. However, the future looks bright!

Ingen kommentarer:

This blog has moved to http://blog.mascaraengine.com. See you!

This blog is (was) for news, announcements and questions regarding the
Mascara ECMAScript 6 -> JavaScript translator.