Javascript and AOP

Title Text

2 options

  • runtime
  • compile (transpile)

Runtime

  • aspectjs.com
  • https://github.com/cujojs/meld
  • https://github.com/antivanov/jsAspect
  • problems: people compile/transpile a lot (ES6)

example in js-aspect

var beforeAdvice = 
    new jsAspect.Advice.Before(function() {
      console.log("joinPoint", "before");
    }).withRegex("set.*");

var aspect = 
    new jsAspect.Aspect(beforeAdvice);

Text

var obj = {
   method1: function() {
       return "doing Something";
   }
};

aspect.applyTo(obj);

obj.method1();

example in js-aspect

Compilation

  • solve some problems
  • not that complicated, but still is.

Compilation

the problem is, that nobody did it (as far as I know)

Babel

transpile ES6 into ES5

https://babeljs.io/

Babylon.js parser into AST

plugins

 

How it's done

1. create AST from source files (Babel)

2. parse aspects from aspects.js file

3. traverse AST and apply aspects

var aspects = [
  {
    joinpoint: Joinpoints.BEFORE,
    pointcut: 'Test.myMethod',
    advice: function advice() {
      console.log('before advice');
    }
  }
];
class Test {
    myMethod() {
        console.log('my method');
    }
}
class Test {
    myMethod() {
        console.log('before advice');
        console.log('my method');
    }
}

Example usage

 

var aspects = [
  {
    rule: 'after returning MyClass.get*',
    advice: function advice(value) {
      console.log('after advice');
      console.log('context', this);
    }
  }
];

Goal

join points - class method call

pointcuts - signatures with *

advices: Before, After, After returning, After throwing, Around

 

https://github.com/PatrikGallik/babel-plugin-transform-aop

Thank you

Javascript and AOP

By patrikgallik

Javascript and AOP

in progress

  • 670