tutorial

Tutorial

In order to be able to dynamically weave aspects without modifying a particular JavaScript engine, AspectScript first performs a code transformation phase in which some expressions are rewritten. This instrumentation can be performed in two ways. The first is easier, but less efficient (useful for development). The second is a little bit more complicated, but more efficient (choose this one for production code).

The easy way --- on-line transformation

This option is ideal when you are learning AspectScript or you are developing code, because the code is transformed each time your Javascript script is loaded. For on-line tranformation, it is only necessary to include the JavaScript source in the following way:

<script language="javascript" src="jst.php?url=script.js"> </script>

Where the jst.php is the PHP file available in the standard distribution of AspectScript.

For instance, this is the original tetris application, and the transformed one using jst.php (it can take some time to load because the transformation is a heavy process, please click once).

The efficient way - off-line transformation

On-line transformation is a little bit inefficient because the scripts are transformed each time they are requested (on every page load). As an altenative, Javascript scripts can be transformed once and then the transformed code included in pages. The following commands performs this off-line transformation:

cat script.js | js -f 'launcher.js' > script-t.js

And then, in the web page you include the transformed script:

<script language="javascript" src="script-t.js"> </script>

The launcher.js script and the js executable are included in the standard AspectScript distribution.

The aspects of AspectScript are just a pointcut-advice pair. Pointcuts and advices are plain JavaScript functions. For instance, if you need to identify all executions of the foo function.

var pointcut = AspectScript.Pointcuts.exec(foo);
var advice   = function() {
 alert("Foo was executed");
};
AspectScript.after(pointcut, advice);

This page is still under construction. Please see the examples page if you want to learn more about AspectScript.

  • tutorial.txt
  • Last modified: 2009/10/26 14:55
  • by aspectscript