Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

burst.MOP Class Reference

List of all members.

Detailed Description

Scoping class to hold various pieces of a MetaObject Protocol for ECMAScript.

The term "MetaObject Protocol" (MOP) originates with Gregor Kiczales's work in implementing CLOS, the object system in Common Lisp. See for example http://www.alu.org/mop/index.html

Briefly, a MOP provides programmatic access to a programming language's implementation of such things as classes, methods, initialization, and so on, both at declaration and execution time.

Such access enables an application programmer to customize or add semantics in cases where the base programming language either does not have the ability, or it doesn't fit the needs of the programmer.

Using a MOP, it is possible to implement functionality such as:

For more information, see his book, "The Art of the Metaobject Protocol". Currently Kiczales is occupied with dumbing down the concept of the MOP into "Aspect Oriented Programming" (AOP) and "Separation of Concerns" (SOC) so that it is accessible to unwashed Java programmers.

This is by no means a full MOP for ECMAScript, for reasons of time, need, and inadequacies in the base language that would make it prohibitively expensive at run time.

A key requirement for a programming language to have a useful MOP is reflection, which has two important parts: introspection, the ability to query about objects and metaobjects, and intercession, the ability to "hook" or alter their runtime behavior. ECMAScript has serious limitations in both of these.

For introspection, some challenges with ECMAScript:

For intercession, there is no:

Furthermore, there are inflexibilities in the language:

Some of these inadequacies may be compensated for easily and efficiently, others cannot.

See Also

See also:


Static Public Member Functions

void inherits (Function subclass, Function superclass)
 Perform the prototype idiom of connecting a sublcass Function and a superclass Function.

void add_mixin (Function cls, Function mixin)
void assertImplements (Function objclass, Function iface)
 Assert that a given Function constructor implements the functions in the given interface Function.

Object singleton (String funcname, Function ctor)
 Calls the provided constructor and returns the result, if it has never been called before.

Function memoize (Object obj, String funcname, Function func)
 Returns a replacement function that proxies for the passed-in one, and ensures that the underlying function is only called once for any give set of arguments.

Function lazyChooseMethod (Object obj, String memname, Function getfunc)
 Return a function which will set the actual function implementation the first time it is called (and then call the real implementation).

Object memoizeNew1 (Object cache, Function ctor, Object arg1)
 Convenience to memoize a constructor with a single argument.

Object memoizeNew1 (Object cache, Function ctor, Object arg1, Object arg2)
 Same as memoizeNew1 but for 2 arguments.

Object onceonly (Object cache, String key, Function func)
 Simple low-level utility for once-only semantics.

Function combineMethods (Function f, Function g)
 A method combinator which returns a method which calls f then g: f.apply(this, arguments); g.apply(this, arguments) If f throws an exception, then g is not run.

void afterMethod (Object obj, String methname, Function aftermeth, Boolean must_exist)
 Arrange for method aftermeth to be called whenever the method named methname of object obj is called.

void addMethodAdvice (Object obj, String methname, Function advice, String advice_kind, String precedence, Boolean meth_must_exist)
 Add some advice at a method call pointcut.

Boolean removeMethodAdvice (Object obj, String methname, Function advice, String advice_kind, Boolean missing_ok)
 Remove some advice.

Boolean removeAllMethodAdvice (Object obj, String methname, Boolean missing_ok)
 Remove all advice from a method, restoring it to its initial state.

Object initNamed (Object obj, Object values, Object names, Boolean nullDefault)
 Initialize the object obj's instance variables from values.


Member Function Documentation

void burst.MOP.addMethodAdvice Object  obj,
String  methname,
Function  advice,
String  advice_kind,
String  precedence,
Boolean  meth_must_exist
[static]
 

Add some advice at a method call pointcut.

If advice_kind is 'before' or 'after', then the supplied function advice() must expect the same arguments as the wrapped method call (and with 'this' bound to the wrapped aobject obj).

If advice_kind is 'around', then the supplied function advice() is called with a single argument which is an instance of burst.MethodInvocation. That instance mi has instance variables mi.object and mi.args which can be modified, and a method mi.proceed() which can (and generally should) be called to find out what any other wrapped advice will return. Unlike 'before' and 'after', an 'around' advice can change the overall result (it can return something other than the result it gets from calling proceed()).

Note that we are far from a complete AOP implementation, lacking for example:

  • pointcuts designators (pointcut name; set of name-based join points; property-based pointcut designator)
  • other pointcut types besides method call (whole classes, etc.)
  • "after" advice for exceptions
  • precedence control besides first or last
  • a "thisJoinPoint" context available to advice
  • introductions

Some discussion of advice for other languages:

Parameters:
obj An instance or prototype.
methname The name of the method within obj.
advice The function with the advice
advice_kind Either 'before', 'after', or 'around'
precedence Either 'first' or 'last'. Defaults to last.
meth_must_exist Whether the object must already have such a method (otherwise a do-nothing function is created for it)

void burst.MOP.afterMethod Object  obj,
String  methname,
Function  aftermeth,
Boolean  must_exist
[static]
 

Arrange for method aftermeth to be called whenever the method named methname of object obj is called.

If instead of passing in a single instance foo, you pass in foo.prototype, then this hooking will apply to all instances.

Naturally, aftermeth must expect the same arguments as methname.

This function uses combineMethods. If the first method throws an exception, aftermeth is not called. Furthermore, there is no way to undo this operation. However, this implementation is efficient at run time. See also addMethodAdvice

Parameters:
obj An instance or prototype.
methname The name of the method within obj.
aftermeth The function to apply after calling methname.
must_exist Whether the object must already have such a method (that is, aftermeth would become the sole handler)

void burst.MOP.assertImplements Function  objclass,
Function  iface
[static]
 

Assert that a given Function constructor implements the functions in the given interface Function.

It does this by comparing the prototype objects of the two of them. throws if it finds any missing functions.

void burst.MOP.inherits Function  subclass,
Function  superclass
[static]
 

Perform the prototype idiom of connecting a sublcass Function and a superclass Function.

See also many other idioms, for example at http://bclary.com/log/2002/12/08/1.html

Object burst.MOP.initNamed Object  obj,
Object  values,
Object  names,
Boolean  nullDefault
[static]
 

Initialize the object obj's instance variables from values.

If names, the function throws if there is a property in values but not in names. Also names values are booleans, true if the value must be provided, and false if not.

If obj already has a value for any given key, it is overwritten.

Parameters:
obj The object to set.
values The object whose members to copy.
names optional. An object whose member keys indicate the ones of interest in values. If not specified, all are copied.
nullDefault optional. If true, any member key in names that is not in values is put into obj with a value of null.
Returns:
obj

Function burst.MOP.lazyChooseMethod Object  obj,
String  memname,
Function  getfunc
[static]
 

Return a function which will set the actual function implementation the first time it is called (and then call the real implementation).

Parameters:
obj The Object which has the function as a member (could be a prototype).
memname The name of the member function.
getfunc A function to call to get the actual function implementation.

Function burst.MOP.memoize Object  obj,
String  funcname,
Function  func
[static]
 

Returns a replacement function that proxies for the passed-in one, and ensures that the underlying function is only called once for any give set of arguments.

example with a static function:

MyMath.cosine = burst.MOP.memoize(MyMath, 'MyMath.cosine', MyMath.cosine);

example with an instance method, with caching just for that instance:

var abook = new AddressBook(); abook.lookup = burst.MOP.memoize(abook, 'AddressBook.lookup', abook.prototype.lookup);

Parameters:
obj Object which should be used when apply-ing the wrapped function
funcname globally unique name identifying the function
func The wrapped function.

Object burst.MOP.memoizeNew1 Object  cache,
Function  ctor,
Object  arg1
[static]
 

Convenience to memoize a constructor with a single argument.

If the constructor has been called before with the same argument, that previous instance is returned.

Parameters:
cache Any Object to be used as a cache (could be the ctor itself).
ctor The constructor
arg1 The single constructor argument.

Object burst.MOP.onceonly Object  cache,
String  key,
Function  func
[static]
 

Simple low-level utility for once-only semantics.

Works only with functions of no arguments.

Boolean burst.MOP.removeMethodAdvice Object  obj,
String  methname,
Function  advice,
String  advice_kind,
Boolean  missing_ok
[static]
 

Remove some advice.

If it is found, it is removed and we return true. If it is not found, we return false if missing_ok, otherwise throw.


The documentation for this class was generated from the following file:
Generated on Tue Nov 30 04:03:08 2004 for BurstProject by doxygen 1.3.4