August 10, 2007

JSFL updated to JavaScript 1.6, gains E4X

For Flash CS3, we updated our JSFL engine to version 1.6 of SpiderMonkey. Firefox started using this version in Firefox 1.5. This is just the JavaScript interpreter, not the Document Object Model, which differs greatly between Firefox and Flash. (For those not familiar with JSFL, it's the JavaScript engine inside the Flash authoring tool; it's not in the Flash Player.)

SpiderMonkey 1.6 introduced JavaScript 1.6, which is JavaScript 1.5 plus several new features:

E4X

ECMAScript for XML (E4X) is a programming language extension that adds native XML support to JavaScript. It does this by providing access to the XML document in a form that feels natural for ECMAScript programmers. The goal is to provide an alternative, simpler syntax for accessing XML documents than via DOM interfaces.

E4X is standardized by Ecma International in ECMA-357 standard (currently in its second edition, December 2005).

E4X is implemented (at least partially) in SpiderMonkey (Gecko's JavaScript engine) and in Rhino (JavaScript engine written in Java).

Array Extras

There are seven new Array methods that can be separated into two categories, item location methods and iterative methods. The item location methods are:

  • indexOf() - returns the index of the given item's first occurrence.
  • lastIndexOf() - returns the index of the given item's last occurrence.

The iterative methods are:

  • every() - runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
  • filter() - runs a function on every item in the array and returns an array of all items for which the function returns true.
  • forEach() - runs a function on every item in the array.
  • map() - runs a function on every item in the array and returns the results in an array.
  • some() - runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit.

For more information, see Nicholas C. Zakas' article, Mozilla's New Array Methods.

Array and String Generics

Sometimes you would like to apply array methods to strings. By doing this, you treat a string as an array of characters. For example, in order to check that every character in the variable str is a letter, you would write:


function isLetter(character) {
return (character >= "a" && character <= "z");
}

if (Array.prototype.every.call(str, isLetter))
alert("The string '" + str + "' contains only letters!");


This notation is rather wasteful and JavaScript 1.6 introduces a generic shorthand:


if (Array.every(str, isLetter))
alert("The string '" + str + "' contains only letters!");

Similarly you can easily apply String methods to any object:


var num = 15;
alert(String.replace(num, /5/, '2'));

[from http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6]

Firefox 2 introduced JavaScript 1.7, which has some quite interesting features like generators and iterators. In the future, SpiderMonkey and Firefox will integrate the Tamarin virtual machine, which today runs ActionScript 3.

So JSFL developers gain some tools in Flash CS3 just from the SpiderMonkey update. As well, we added a number of new JSAPIs, which I'll talk about in a later post.

1 comment:

ekameleon said...

Hello :)

i hope an update of the JS engine in FMS3 too with the JS1.7 for example :)

EKA+ :)