views:

227

answers:

1

I'm looking for a full featured AOP Library for Actionscript 3.

The following projects I noticed so far, but they all seem to have their problems:

- http://farmcode.org/page/Sodality.aspx (looks most promising so far, however it requires you to create a whole new class for every AOP "call" I believe, and it forces you to follow quite a lot of restrictions, anyone has experience with it?
- http://code.google.com/p/loom-as3/ (this one is discontinued)
- http://code.google.com/p/floxy/ (dynamic proxy generation? this isn't really AOP as I know it, right?)
- http://code.google.com/p/flemit/ (dynamic byte code generation? this is something AOP needs I think, but not the full featured AOP framework I am looking for)

Does anyone know of a better solution? Or does anyone have any experiences with AOP in Actionscript 3?

Best regards,

Tom

+3  A: 

AOP in ActionScript 3 is really hard. All aproaches have their problems:

  • proxying: actually works fine and transparent to the programmer, but not to the runtime. If any function signature expects an object of type A, then a proxy to an object of type A will fail to pass the typecheck
  • bytecode generation: well, sort of works, but not too good. loading bytecode in AVM2 is asynchronous, so you can't do it just in time. Also, once bytecode is loaded, there's no way of discarding it, so you can not modify a class a second time.
  • anything else is verbous

What you can do is to use haXe. Not only does haXe have other advantages over AS3, but it also allows some AOP. There are two approaches:

Dynamic Methods

In haXe, you can declare methods to be dynamic. These can be replaced at runtime to provide advices. Let me explain, what happens under the hood. The following haXe code:

public function foo(param1:Type1):Type2 { /*body*/ }
public dynamic function bar(param1:Type1):Type2 { /*body*/ }

Is the equivalent of the following AS3 code:

public function foo(param1:Type1):Type2 { /*body*/ }
public var bar:Function = function (param1:Type1):Type2 { /*body*/ }

Using the latter always performs worse than the former, since there's a lot of runtime type checking involved. In haXe however, you do not lose the compile time benefits of strict typing, unlike in AS3.

Likewise, using haXe's accessors (that are indeed very different from AS3's), you can also use AOP for properties:

public var foo(get_foo, set_foo):Type;
dynamic function get_foo() {//haXe will automatically infer types: http://haxe.org/ref/type_infer
    return this.foo;
}
dynamic function set_foo(param) {
    return this.foo = param;
}

Any methods, that are dynamic can be replaced at runtime to do whatever you wish. AS2 allowed this, and libraries like as2lib provided AOP. unfortunately, it is no longer available. But I guess you can figure out the rest on you own.

Proxying

haXe has the notion of anonymous types. At runtime, these are merely * (thus a perfomance loss is to be expected), but at compile time, they are type safe. What you can do, is create a proxy to an object of whatever type you need and use it as AOP container. In your whole code, you never use its type explicetely, but rather an equivalent anonymous type. The problem with proxying I described will be gone. The only thing you need to do is to make an unsafe cast of your proxy to that anonymous type.

hope that helps

greetz
back2dos

back2dos
Thanks, though unfortunately I have 4 problems with using haXe: 1) I'd have to learn a new language 2) Because my projects are usually very big, I do everything alone, and I am self-taught, I need a community like SO to succeed at the moment, unfortunately there is much less support for haXe 3) the future of haXe is more uncertain than of commonly used languages as Actionscript 3, 4) there are a lot more public code snippets, frameworks and libraries available for Actionscript 3 than for there are for haXe. What do you think of Sodality, by the way?
Tom
back2dos
@Tom, About Sodality: It sounds weirdly verbous and overly complicated. AOP is supposed to be simple and elegant. And not to require lines and lines of modification in the source file to work (not touching it is the actual point). But, from what I can tell, it'll probably work just fine. OTOH, you could spend the time you'd lose writing all the extra code, you can learn haXe. :)
back2dos
Fact is, haXe has only been tagged 48 times while actionscript-3 has been tagged 5104 times. That's just an example of course. I take it there's not much hope for AOP in AS3 though, which is a shame. Thanks a lot for the help. If noone comes up with a magical solution I'll mark this answer as accepted.
Tom
back2dos
I'll look into it then.. can't say I am happy about it though, would rather stick to what I have learned and start coding after 1 year of preparation.
Tom