Created: 2012/11/08 20:27:15+0000 Revised: 2013/04/03 20:18:37+0000 Revisions:  2 1

Not long after learning about Tcl's trace command I first heard about aspect-oriented programming. I saw some similarities between the two but the friend who told me about aspect-oriented programming wasn't so keen.

I implemented some code that took advantage of trace to measure the execution time of a procedure. Trace added procedures to execute when entering or leaving the timed procedure. These were used to modify a stack that contained the start times of the procedures being watched. I intended this as a rough proof of concept.

This provides much more restrictive functionality then could be obtained with a dedicated implementation for aspects. Aside from it only providing performance information: it matches on the exact name of the procedure, it does not provide pattern matching; it allows code to be added at the execution of a procedure, it does not allow code to be added to the call of the procedure and it allows code to be added before and after a procedure, it does not allow code to replace a procedure. To address some of these limitations with trace I decided to implement a Tcl package that allows the static weaving of code into procedures.

I defined new procedures to create, pointcuts, advice, aspects, and procedures. The procedures for pointcuts and advice both allow an optional name and return a handle. This allows them to be reused or to be used only once. Each aspect is created from the handles to a pointcut and a piece of advice. The new procedure to create procedures uses the same signature as proc but weaves the body with any matching aspects.

One of the limitations of this is the reduced number of joinpoints available in Tcl compared to Java. Types, visibility and annotations have all been removed leaving names and the number of arguments.

Work on this is goes very quickly, when I can find the time. Sadly because I don't have any use for this and I'm just doing it because I was wrong it is a low priority for me. It has been months since I spent anytime on it. I have made it available on BitBucket at Please take a look but ignore the version number it is still very much a work in progress.

Currently it supports weaving proc calls with execution level advice. It supports both before, after and around advice. The around advice allows the complete replacement of the existing code. It uses regular expressions to match to full qualified proc name, the start and end line delimiters are implicit. I made them implicit as I felt it would be easier to detect bugs with a proc expected to match not matching than a proc you were not expecting to match matching.