Posted on Wednesday 23 November 2005
One of the inconsistencies between ActionScript and all flavors of ECMAScript is the lack of a proper eval() statement for ActionScript. ActionScript's eval() statement cannot evaluate a simple string such as "a = 1 + Math.sin(0);", which makes it a pain to use for plotting applications, JSON, and every other place where dynamic script writing would be in order.
The reason is simple: ActionScript is compiled to bytecode, and the Flash player only understands this bytecode, not ActionScript, and does not include any kind of inline compiler.
With the introduction of AS3, dynamic actionscript evaluation may finally be possible. Now, mind you, this is not a built-in feature of the language, but simply something which MAY be accomplished thanks to a dedicated programmer. Three new features in AS3 makes this feasible:
- A faster string library which makes lexing and parsing in a reasonable time feasible
- A binary-safe ByteArray class to store generated ByteCode
- The new Loader.loadBytes method which allows a SWF generated client-side to be loaded into an empty movieclip without going through the server
So what's the point you ask? Well, first of all, think of a plot generator. A plot generator with dynamic formula entry would be much faster for evaluation than the current available solutions which basically make up their own stack, and emulate what the Flash Player does already, but a lot more slowly.
Another area where this would be interesting is for various XHTML related works. We know that Claus Walhers has done a terrific job with DENG for declarative XML dialects, but what about ECMAScript? This could have a wide range of applications, including in DENG itself, but also in various systems which store content server-side in text blobs in database engines, like the recent slew of Flash-powered blogs.
Perhaps most interesting would be however the creation of non-ECMAscript scripting engines in Flash. I may get shot for what I'm about to say, but here goes: the Flash community may just need another programming language for special purposes, one that is highly dynamic, easy for beginners to learn, loosely-typed, event or timeline-oriented (exactly the opposite direction that Nicolas Canasses has leaned toward with HaXe).
What wrong with ECMAScript/Actionscript? Well, it is not especially well suited for certain tasks, the worst of which is time-based/timeline-oriented animation and declarative drawing. I mean, it seems pretty ironic that ActionScript is derived from ECMAScript which was primarily oriented at scripting in a text-based environment, HTML and the DOM. Simply put, ActionScript has leaned more and more towards heavy lifting text operations (like RegEx and the new XML implementation) and less towards building programmatically what Flash was originally meant for, animation.
For one thing, ActionScript does not have a sleep function. It doesn't have a draw or frame loop, or any sort of structure which seems to be specifically geared towards an animation environment, except for the dreaded enterFrame which poses major problems because it's so damn awkward to put a partial for loop in a enterFrame'd function. What I'm thinking is perhaps a dialect of "scripting language x" which allows you to do something like this:
everyFrame{
write framesPassed + " have passed so far", 100, 100, defaultFont
}
always{
draw pieslice 0, 100, secondsPassed*10, 50, 50, blue
wait(100);
}
This is all very sketchy of course but I think you get the point. Another possible type of scripting language within the language would be a declarative interface for drawing stuff on screen, the way REBOL for Flash has implemented so far.
What purpose would this serve? Well, for one thing, we could embed this new scripting language easily in Flash itself using the new XML quote-less syntactic sugar, like so:
var myScript = <script type="text/javascript">
<![CDATA[
//Stuff goes here
]]></script>;
ScriptEngine.exec(myScript, this);
One of the things that comes to mind immediately to me is Processing. In fact, I would suggest that a good starting point for a new scripting language would be Processing, which we could further expand to fit Flash's specifics. Flash has a few metaphors and features which would allow it to be significantly cooler and more useful than Java Processing, in particular blend modes, filters, vectors, video, and the ability to integrate with other types of Flash content seamlessly. I think this would properly qualify as a killer app.
I'd definitely be interested in seeing people run with this idea. I think our best bet for undertaking a lexer for ActionScript would be starting with the Rhino and Processing lexers which may be simple enough to convert to AS3 given the similarity of AS3 to Java. From then on, a bytecode compiler does not seem overly difficult given the reported easiness with which Nicolas wrote MTASC (although honestly I think Nicolas is a genius and so allowing a couple of weeks instead of 3 days would be a safe bet ;)


