User Tools

Site Tools


at:tutorial:reflection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
at:tutorial:reflection [2008/11/06 15:25]
elisag
at:tutorial:reflection [2010/11/16 16:32] (current)
tvcutsem
Line 4: Line 4:
  
 The reflective model of AmbientTalk is based on [[http://bracha.org/mirrors.pdf|mirrors]], meta-level objects which allow one to reflect on an object's state and behaviour. How to create such mirrors and how they can be used is demonstrated in the first part of the tutorial. The second part of the tutorial showcases how to construct mirages, objects which override the default meta-level operations with custom behaviour. This tutorial concludes with a brief overview of the meta-level operations which are offered by AmbientTalk mirrors. The reflective model of AmbientTalk is based on [[http://bracha.org/mirrors.pdf|mirrors]], meta-level objects which allow one to reflect on an object's state and behaviour. How to create such mirrors and how they can be used is demonstrated in the first part of the tutorial. The second part of the tutorial showcases how to construct mirages, objects which override the default meta-level operations with custom behaviour. This tutorial concludes with a brief overview of the meta-level operations which are offered by AmbientTalk mirrors.
 +
  
 ===== Mirrors ===== ===== Mirrors =====
Line 45: Line 46:
 > def x :=mirrorOnP.grabSlot(`x) > def x :=mirrorOnP.grabSlot(`x)
 >> <accessor method for:x> >> <accessor method for:x>
-> x() 
->> 2 
 // and we can also invoke methods reflectively: // and we can also invoke methods reflectively:
 > mirrorOnP.invoke(p, lobby.at.lang.values.createInvocation(`distanceToOrigin,[],[])); > mirrorOnP.invoke(p, lobby.at.lang.values.createInvocation(`distanceToOrigin,[],[]));
Line 85: Line 84:
 ===== Mirages ===== ===== Mirages =====
  
-Extending the AmbientTalk core language involves adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer can redefine the programming language's default semantics of the MOP, by means of mirages.+Extending the AmbientTalk core language requires adding objects which have a different implementation for some of the default meta-operations. In this part of the tutorial, we describe how a programmer can redefine the programming language's default semantics of the MOP, by means of mirages.
  
 As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the ''defaultMirror'', which encapsulates the default semantics. By selectively overriding this mirror's methods, we can make small adjustments to the language, e.g. as follows: As a simple example, we show how to trace all method calls made on an object. The first step is to define a //mirror// object that encapsulates this logging behaviour. A mirror object must implement the complete AmbientTalk MOP. To make it convenient to make small changes to the MOP, AmbientTalk provides the ''defaultMirror'', which encapsulates the default semantics. By selectively overriding this mirror's methods, we can make small adjustments to the language, e.g. as follows:
Line 91: Line 90:
 <code> <code>
 def createTracingMirror(baseObject) { def createTracingMirror(baseObject) {
-  extend: defaultMirror with: {+  extend: defaultMirror.new(baseObject) with: {
     def invoke(slf, invocation) {     def invoke(slf, invocation) {
       system.println("invoked "+invocation.selector+" on "+baseObject);       system.println("invoked "+invocation.selector+" on "+baseObject);
Line 100: Line 99:
 </code> </code>
  
-The primitive ''mirror:'' is syntactic sugar for extending from the ''defaultMirror'', allowing the above example to be rewritten as:+The primitive ''mirror:'' is syntactic sugar for creating a new prototype that extends from the ''defaultMirror''. Thuswe could have also defined the tracing mirror as:
  
 <code> <code>
-def createTracingMirror(baseObject) { +def TracingMirror := mirror: { 
-  mirror: { +  def invoke(slf, invocation) { 
-    def invoke(slf, invocation) { +    system.println("invoked "+invocation.selector+" on "+self.base); 
-      system.println("invoked "+invocation.selector+" on "+baseObject); +    super^invoke(slf, invocation);
-      super^invoke(slf, invocation); +
-    }+
   }   }
 } }
Line 123: Line 120:
 In the code above, the closure passed to ''mirroredBy:'' is a //mirror construction closure//. This closure is applied to a new, empty mirage object and it is expected that it returns a new mirror that reflects upon this mirage. When the mirror is constructed, the object initialization closure is executed. In the code above, the closure passed to ''mirroredBy:'' is a //mirror construction closure//. This closure is applied to a new, empty mirage object and it is expected that it returns a new mirror that reflects upon this mirage. When the mirror is constructed, the object initialization closure is executed.
  
-When invoking the method ''m'' on the mirage, ''invoke'' will be invoked on the tracing mirrorcausing the following behaviour:+Another alternative is to just pass a mirror prototype to ''object:mirroredBy:'', like so:
  
 <code> <code>
-> mirage.m(); +def mirage := object: { 
-invoked on <obj:6276614{foo,...}>+  def foo() { 42 }; 
 +} mirroredBy: TracingMirror; 
 +</code> 
 + 
 +The AmbientTalk VM will then call ''new'' on the ''TracingMirror'' prototype to create a new mirror for the given ''mirage'' object. 
 + 
 +When invoking the method ''foo'' on the mirage, ''invoke'' will be invoked on the tracing mirror, causing the following behaviour: 
 + 
 +<code> 
 +> mirage.foo(); 
 +invoked foo on <obj:6276614{foo,...}>
 >> 42 >> 42
 </code> </code>
 +
 +The picture below gives an overview of the different objects involved in the actor.
 +
 +{{:at:tutorial:meta-2.jpg|:at:tutorial:meta-2.jpg}}
  
 Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk's main domain of application). An example language construct which is conceived as a reflective extension to the language is that of future-type message passing. Futures are discussed in the next chapter of this tutorial. Whereas the example provided above may seem a little contrived, the reflective capabilities of AmbientTalk allow it to be extended with many abstraction relating to distributed computing for mobile ad hoc networks (AmbientTalk's main domain of application). An example language construct which is conceived as a reflective extension to the language is that of future-type message passing. Futures are discussed in the next chapter of this tutorial.
Line 135: Line 146:
 ===== The Metaobject Protocol ===== ===== The Metaobject Protocol =====
  
-The Meta-Object Protocol of AmbientTalk can be divided into a series of independent protocols. Whereas the full semantics and signature of the meta-methods can be found in the [[http://prog.vub.ac.be/amop/at2langref/edu/vub/at/objects/MirrorRoot.html|language reference]], this section provides an overview of the various protocols.+The Meta-Object Protocol of AmbientTalk can be divided into a series of independent protocols. Whereas the full semantics and signature of the meta-methods can be found in the [[http://soft.vub.ac.be/amop/at2langref/edu/vub/at/objects/MirrorRoot.html|language reference]], this section provides an overview of the various protocols
 + 
 +The **Message Invocation Protocol** consists of methods to deal with both synchronous and asynchronous method invocation. It provides necessary hooks to intercept both the reception of asynchronous messages and the invocation of synchronous messages. Moreover, it provides a hook to intercept asynchronous messages being sent by the object, allowing the object to add additional metadata to the message. The ''invoke'' meta-method illustrated above is an example of a method belonging to this protocol. The picture below gives an overview of that protocol.
  
-The **Message Passing Protocol** consists of methods to deal with both synchronous and asynchronous message sendingIt provides necessary hooks to intercept both the reception of asynchronous messages and the invocation of synchronous messages. Moreover, it provides a hook to intercept asynchronous messages being sent by the object, allowing the object to add additional metadata to the message. The ''invoke'' meta-method illustrated above is an example of a method belonging to this protocol.+{{:at:tutorial:msgReceptionProtocol.jpg?500|:at:tutorial:msgReceptionProtocol.jpg}}
  
-The **Object Passing Protocol** consists of two methods ''pass'' and ''resolve'', which allow an object to prescribe how it should be passed to other objects and how the object should subsequently be resolved upon arrival. The default semantics allow objects to be passed by copy if they are tagged with the ''Isolate'' type tag. Otherwise, objects are passed by handing out a far object reference.+The **Object Marshalling Protocol** consists of two methods ''pass'' and ''resolve'', which allow an object to prescribe how it should be passed to other objects and how the object should subsequently be resolved upon arrival. The default semantics allow objects to be passed by copy if they are tagged with the ''Isolate'' type tag. Otherwise, objects are passed by handing out a far object reference.
  
 The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic access and modification to slots. For instance, ''o.x'' can be intercepted using the ''invokeField'' meta-method, while ''o.x := 5'' is trapped using ''invoke'' where the selector will equal ''x:=''. The **Slot Access and Modification Protocol** consists of operations which allow trapping both dynamic access and modification to slots. For instance, ''o.x'' can be intercepted using the ''invokeField'' meta-method, while ''o.x := 5'' is trapped using ''invoke'' where the selector will equal ''x:=''.
  
-The **Structural Access Protocol** consists of operations used list all available slots, get access to a first-class slot representation and to add new slots to an existing object. The ''listSlots'' meta-method used in previous example is a member of this protocol.+The **Structural Access Protocol** reifies the structure of an AmbientTalk objects as a collection of slot objects. It consists of operations used list all available slots, get access to a first-class slot representation and to add new slots to an existing object. The ''listSlots'' meta-method used in previous example is a member of this protocol. 
  
-The **Instantiation Protocol** consists of the ''clone'' and ''newInstance'' methods, which are implictly called when using base-level code of the form ''clone: object'' and ''object.new(@args)'' respectively. In the default implementation, ''newInstance'' calls ''clone()'' to create a clone of the current object, and subsequently invokes the base-level ''init'' method with the supplied arguments on the cloned object.+The **Object Instantiation Protocol** consists of the ''clone'' and ''newInstance'' methods, which are implictly called when using base-level code of the form ''clone: object'' and ''object.new(@args)'' respectively. In the default implementation, ''newInstance'' calls ''clone()'' to create a clone of the current object, and subsequently invokes the base-level ''init'' method with the supplied arguments on the cloned object.
  
 The **Relational Testing Protocol** consists of the methods ''isCloneOf'' and ''isRelatedTo'' which allow testing whether 2 objects are related through cloning or a combination of cloning and object extension. Note that these operators are not able to discern that objects were generated by the same constructor function or (by extension) the execution of identical code in different actors.  The **Relational Testing Protocol** consists of the methods ''isCloneOf'' and ''isRelatedTo'' which allow testing whether 2 objects are related through cloning or a combination of cloning and object extension. Note that these operators are not able to discern that objects were generated by the same constructor function or (by extension) the execution of identical code in different actors. 
  
-The **Type Testing Protocol** consists of the methods ''isTaggedAs'' and ''getTypeTags'', the former of which tests whether an object is transitively tagged with a particular type (i.e. this operation considers type tags of parent objects). ''getTypeTags'' on the other hand returns only the type tags which were explicitly attributed to this object (i.e. it does not return type tags attributed to the parent objects).+The **Type Tag Protocol** consists of the methods ''isTaggedAs'' and ''listTypeTags'', the former of which tests whether an object is transitively tagged with a particular type (i.e. this operation considers type tags of parent objects). ''listTypeTags'' on the other hand returns only the type tags which were explicitly attributed to this object (i.e. it does not return type tags attributed to the parent objects).
  
 The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the ''eval'' and ''quote'' meta-operations. The ''eval'' operation is called when evaluating a parsetree and typically returns the evaluated object itself, except for explicit parse-tree elements such as definition and method invocations.  The **Evaluation Protocol** ensures that any AmbientTalk object can be part of a parse tree, and therefore every object provides meaningful implementations of the ''eval'' and ''quote'' meta-operations. The ''eval'' operation is called when evaluating a parsetree and typically returns the evaluated object itself, except for explicit parse-tree elements such as definition and method invocations. 
at/tutorial/reflection.1225981503.txt.gz · Last modified: 2008/11/06 15:36 (external edit)