The Blog

Jan 21, 2009

AOP Advice for ActionScript 3 and Flex Coming Very, Very Soon 

by Maxim Porges @ 12:56 AM | Link | Feedback (6)

UPDATE: You can now follow the latest goings-on with Loom at the Loom Blog.

At the end of December, I made a passing reference to my bytecode weaving library (Loom) for ActionScript 3 and Flex apps. Today, I finished the first phase of this library, which was full parsing of the AVM2 spec in to AS3 domain objects.

Why the hell am I doing this? Well, ever since AS3 came out, I've been loving the compiler optimization but hating the inflexibility of the new traits/object prototype model. There's a serious need for AOP functionality in AS3 to support logging, security, mock objects, run/compile-time weaving, and all of the other exciting dynamic proxy stuff you can do with AOP, but there is neither language support nor a library out there for doing this as of yet.

After much digging in Google, I have found heroic (yet naive) efforts to get something equivalent to eval() in AS3 and a few libraries for introspecting and printing the bytecode, but it was pretty obvious that none of these attempts were going to cut it for what I wanted to do. Since Adobe published the AVM2 spec, I figured that something akin to a Java CGLIB port couldn't be too hard. After looking through the documentation about two months ago, I decided that a bytecode weaving library for AS3 looked entirely achievable.

I have a few more phases to roll through, but if I can continue at the current rate I think I'll be replacing static elements of bytecode by the end of the week, and possibly weaving functional mixin code in to classes in a week or two. The first mixin I'm planning on working on is dynamic subclassing of existing compiled classes.

Basically, what this will entail is introspecting the bytecode for the existing class, and creating a dynamic subclass that has an overridden version of every method from the superclass. Each overridden method will have a near-identical set of opcodes that instructs the AVM to invoke a method internal to the dynamic class - a method which will also be mixed in at runtime via bytecode enhancement. This mixed-in method will accept a reference to the original function, and will be able to invoke AOP advice before, after, and during exception scenarios for the original method (if such advice exists - otherwise the original method will simply be invoked). The advice itself will be stored as closures within a map inside the dynamic subclass; this will allow a programmer to load the dynamic proxy through an AOP factory, add advice to it by method name, and supply a closure that can either replace or run sidecar to the original method. Examples of this sidecar method could be a logger, global exception handler, or around advice that can optionally proceed with or cancel invocation.

To hook this functionality in to a Flex app or test suite, all that's really needed is a runtime module loader that can pull in a SWF and manipulate the bytecode block before handing the enhanced code off to the Flash Player. Once this is in place, we should have full AOP capabilities in AS3.

I'd also like to see the library end up with runtime weaving via compile-time annotations (possibly with an AspectJ-style Eclipse plugin down the line), but my main goal is to get the basic AOP stuff in there so we can have proper mocking support in AS3. We'll see where things go after that, but since I will be hosting the project open-source on Google Code, people will be able to do whatever they like with it.

Stay tuned to my blog and I'll keep you posted as things develop. You can expect to see the alpha release of the code in the SVN repo on my Google Code project within a week or so.