Pre-Processing ActionScript files / classes

One thing I often do in my code is create custom trace / output functions that recursively go through and print out properties. However, this requires that I add some custom code to my classes which I don’t really want in the final version.

So, I started exploring pre-processors that I could use to make it easier to add and remove debugging code. I first looked at m4. While it seems to be very powerful, it was overkill for what I needed, and looked to be very difficult to use. Christian Cantrell then suggested that I look at cpp, a C pre-processor. I checked it out, and it does exactly what I need, in an easy to learn and use syntax.

For example, here is a simple class, in a file call HelloWorld.p

[code]class HelloWorld{function HelloWorld(){#ifdef DEBUGtrace(“Hello World Loaded”);#endif}}[/code]

#ifdef is a cpp directive that is basically an if statement. If the DEBUG variable is defined, then the block will be included in the generated file, if not, then it will be omitted.

I then run this file through cpp like so:

cpp -D DEBUG -P HelloWorld.p HelloWorld.as

The -D DEBUG defines the DEBUG variable. -P tells cpp to not insert line-markers in the generated file. You have to use this for ActionScript or else Flash won’t compile your file. (You can find a completely list of command-line args here).

Running this results in the following HellowWorld.as file:

[code]class HelloWorld{function HelloWorld(){trace(“Hello World Loaded”);}}[/code]

Running it without defining DEBUG:

cpp -P HelloWorld.p HelloWorld.as

results in the following file:

[code]class HelloWorld{function HelloWorld(){}}[/code]

With the trace statement removed.

Note that by default, cpp will remove any comments from the generated AS file. If you want it to keep the comments in the file, just add -C to the argumenst that you pass to cpp

Anyways, this is just a simple example. There are a ton of directives you can use, and you can also define your own.

The main thing that you have to remember is that you must run your class file through cpp before it is compiled by Flash. Flash is compiling the file generated by cpp, and not the file you are editing.

You can find more info on cpp here, and you can download it from here (it is included with the GCC compiler).

Note, cpp is included on Mac OS X, but for some reason would not work for me. I had to download the source, and compile it before I could get it to work. If anyone can get it to work on Mac, let me know in the comments.

Post any additional tips / uses of cpp with ActionScript, or suggestions in the comments.

12 Responses to Pre-Processing ActionScript files / classes

  1. Keith Peters says:

    Funny, just in the last few days I was thinking of doing something with preprocessing in JSFL. I think #ifdef would be pretty easy to do. It could create a new .as file and then go right ahead and compile your swf.

  2. ericd says:

    we do something quite similar for our debug builds… if its a debug version we put in our own custom assert handling, debug buttons, all kinds of stuff. and if its not for debug, the code never makes the SWF.

  3. Campbell says:

    Maybe this could be something for an extended trace class in the next version?http://www.markme.com/flashteam/archives/007357.cfmjust a thought ;o)

  4. bokel says:

    Wouldn’t it be great, if we could insert a tool like cpp into the flash tool chain? I’m not sure if it is possible at all, but a hook into the process of compiling which calls a configurable external programm would offer endless possibilities. Unix tools do this for ages via stdio.

  5. Simon Wacker says:

    The As2lib Logging API (only in CVS) does something quite similar. It is not actually pre-processing the as-file and removing “trace” completely, but you can put the actual logging into an if-statement like:if (logger.isDebugEnabled()) {logger.debug(“Debug Message [” + someObject + “]!”);}The pre-processing is of course a lot faster (because it removes any logging calls completely). A little more pre-processing automization would be great. Maybe in conjunction with mtasc, ant and eclipse? 😉

  6. George Medve says:

    Just a thought, but In Publish Settings there is a check box for Omit Trace actions, wouldn’t it just be easier to toggle that when you need? (I am assuming that removes the trace() line from the code too).

  7. mike chambers says:

    >Just a thought, but In Publish Settings there is a check box for Omit Trace actions, wouldn’t it just be easier to toggle that when you need? (I am assuming that removes the trace() line from the code too).Yes. If all you want to do is omit trace actions, then a preprocessor is overkill.But, as I stated in my post, I write my own debug / trace functions which I want to remove. Omit trace actions doesnt do that.Plus, if you read through the cpp docs you will find a ton of other useful / cool stuff that you can do.mike chambersmesh@macromedia.com

  8. Julien says:

    I use SE|PY actionScript viewer ans this software can comment or uncomment all the trace in a class, very usefull and very simple.

  9. Kenny Bunch says:

    I’d like to comment that if you understand the notion of a preprocessor on files, it allows you to do a lot more than remove debug statements like Mike said. For instance, to an extent the Flash 7 compiler is a preprocessor. It takes your AS2 syntax and processes it down to AS1 syntax. Knowing this, you could actually have a preprocessor that could create files from more advanced files. For example, you could have abstract keywords in your files, that define an abstract method, but your preprocessor pulls that abstract key word off and creates a function with a throw statement. In this case, you are writing what appears to b syntax that allows an abstract class, but the preprocessor aids you into creating syntax for the flash compiler. This would actually allow you to write any language with equivalent constructs, etc. preprocess it and have it convert to Actionscript.

  10. Sweet tip, Mike.I agree with Bokel – it’d be really nice if we could get pre-processor functionality inside the IDE.

  11. mike chambers says:

    Btw, if you dont want to use CPP, you can try another pre-processor such as the Java based JAPPO:http://jappo.opensourcefinland.org/index.htmlThis is pretty cool because it uses a lot of the CPP directives, but also has built in macros (like timestamp, date, etc…).mike chambersmesh@macromedia.com

  12. I stumbled upon filepp.http://www.cabaret.demon.co.uk/filepp/“filepp is a generic file preprocessor. It is designed to allow the functionality provided by the C preprocessor to be used with any file type. filepp supports the following keywords, all of which have their usual C preprocessor meanings and usage…Also, as filepp is written in Perl, it is 8-bit clean and so works on any character set, not just ASCII characters. filepp is also customisable and hopefully more user friendly than cpp.”