Expose your tool (ExposureUtil)

2008 August 31

With help from wvxvw from the flashkit forums, I finally got the syntax to fix the namespace issues. For the curious, it's like this:

   var ns:Namespace = new Namespace(describeType(foo).method.@uri[0]);
   var fn:String = describeType(foo).method.@name[0];
   trace(foo.ns::[fn]);
   

I've also added some convenience functions and info. The names of all exposed functions are put into an array, which is available through a function "getFunctionNames". getFunctionNames itself is exposed to javascript. There is also a "describeFunctions" function which returns a JSON string describing all exposed functions and arrays of their parameter types. These can be used for introspection on the javascript side. The example page automatically puts up buttons and input/output fields for each exposed function.

To do the JSON export, this now depends on as3corelib.


Usage: First, if you're using Flex or FlashDevelop with the mxmlc compiler, you can use the nifty [Exposable] tags. In FlashDevelop, open up your project->properties->compiler options dialog, and put this in the "additional compiler options" box:
   -keep-as3-metadata+=Exposable
   

Make sure that's "+=", rather than "=", otherwise other tags won't be preserved.

Second, import me.cosmodro.utils.ExposureUtil;

You can tag methods to expose via [Exposable], like so:

           [Exposable]
           public function anotherPublicFunction(i:int):void
           {
               tf.appendText("anotherPublicFunctionn");
               return;
           }
   

Or, if you want to override the function name, you can do it like this:

           [Exposable(name="yapf")]
           public function yetAnotherPublicFunction():void
           {
               tf.appendText("yetAnotherPublicFunctionn");
               return;
           }
   

If you're not using the mxmlc compiler, you can still expose all public functions (either inherited or only declared in the class) via:

               ExposureUtil.expose(this, "");
   

The second argument there is an optional prefix to be added to all the js identifiers for this class. It would be useful if you're exposing more than one class for some reason.

There are two more parameters to expose, both booleans. The first is "tagOnly", which determines whether only tagged or all public functions are exposed. The second is "inheritedToo", which determines if inherited functions are also exposed.

There's also a passthrough function for ExternalInterface.addCallback, but it takes a third argument which is an Array of Strings for the parameter types to expose through describeFunctions.

Try the demo

And get the source

privacy policy
Sponsored links