Expose yourself

2008 August 30

I've spent all afternoon working on what I thought would be a really simple utility class to automate some common-ish ExternalInterface tasks. It's turned out a lot more interesting and a lot more complex than I thought.

There are two methods in ExposureUtil now:

   public static function expose(obj:Object, prefix:String="", tagOnly:Boolean = false, inheritedToo:Boolean = false):void

public static function addCallback(fname:String, f:Function):void

Expose is the main thing. addCallback is just a passthrough for ExternalInterface.addCallback, but it does a little bit of bookkeeping on the side.

Expose takes 4 arguments. The object to expose, an optional prefix (usually ""), and two boolean arguments that define what get exposed. The first is "tagOnly", which if true, means that only functions with an [Exposable] tag will be exposed. The second is "inheritedToo", which determines whether inherited functions are exposed as well.

If you didn't know what I was talking about with [Exposable] up there, it's really neat. The flex compiler (which I use through FlashDevelop) lets you keep metadata attached to functions via custom tags. I simply told the compiler to keep the Expose metadata via this commandline argument: -keep-as3-metadata+=Exposable

And viola, that information is available through describe type. Not just that, but I can also use it to override the name of the exposed function like this:

   [Exposable(name="yapf")]
   public function yetAnotherPublicFunction():void
   

That function will be exposed via the callback name "yapf".

All this works fine, except for one really unusual case. If a function is defined in a custom namespace, it can be marked Exposable, but I haven't been able to find a way to actually get access to it to expose it. So that case results in a dummy function being bound instead.

There's a not-very-impressive demo page that I'm periodically updating with my experiments on this. Here, try it.

Related tags: javascript

privacy policy
Sponsored links