advanced reflection in pharo
DESCRIPTION
A Lecture held at Universitat Politècnica de Catalunya on Oct 29, 2013TRANSCRIPT
![Page 2: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/2.jpg)
What you know…
• Smalltalk is reflective
• e.g. Classes and Methods are Objects
• Reflective API on all Objects
![Page 3: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/3.jpg)
Instance Variables
![Page 4: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/4.jpg)
Object subclass: #Point!! instanceVariableNames: 'x y'!! classVariableNames: ''!! poolDictionaries: ''!! category: 'Kernel-BasicObjects'
2 instance variables
![Page 5: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/5.jpg)
• Ask the class:
Point instVarNames
• read:
3@4 instVarNamed: #x
• write:
3@4 instVarNamed: #x put: 5
![Page 6: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/6.jpg)
Great!
![Page 7: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/7.jpg)
But…
![Page 8: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/8.jpg)
Object subclass: #Point!! instanceVariableNames: 'x y'!! classVariableNames: ''!! poolDictionaries: ''!! category: 'Kernel-BasicObjects'
This is just a String!
![Page 9: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/9.jpg)
• returns an Array of Strings:
!
Point instVarNames
![Page 10: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/10.jpg)
Why not Objects?
![Page 11: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/11.jpg)
We can do better!
![Page 12: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/12.jpg)
• All classes have a Layout
!
• Describes the memory layout defined by a class
!
• Layout and all the description are Objects
![Page 13: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/13.jpg)
• Point layout
• a Normal Object
• Array layout
• an Array of Pointers
• ByteArray layout
• an Array of Bytes
![Page 14: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/14.jpg)
Point layout allSlots
!
==> an OrderedCollection(x => Slot y => Slot)
![Page 15: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/15.jpg)
• Slots know how to read values from Objects
!
mySlot := Point layout resolveSlot: #x.
mySlot read: 3@4.
![Page 16: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/16.jpg)
Why?
![Page 17: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/17.jpg)
Typed SlotsSlot subclass: #TypedSlot layout: PointerLayout slots: {#x => TypedSlot type: Integer}. !!TypedSlot >> write: aValue to: anInstance (aValue isNil or: [aValue isKindOf: type]) ifFalse: [ InvalidTypeError signal ]. super write: aValue to: anInstance.
![Page 18: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/18.jpg)
Property SlotsObject subclass: #PropertyObject layout: PointerLayout slots: { field => Slot property1 => PropertySlot. property2 => PropertySlot. ... propertyN => PropertySlot. }
![Page 19: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/19.jpg)
Others
• BitSlot
• BooleanSlot
• Alias
• Relationships (e.g. one-one, one-many)
• …. Your Domain level Slot!
![Page 20: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/20.jpg)
More in Paper from OOPSLA
![Page 21: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/21.jpg)
Status
• Slots are in Pharo3, but hidden
!
• In Pharo4: un-hide and introduce some Special Slots (e.g. Boolean, Property)
• e.g. for Morphic (user interface objects)
![Page 22: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/22.jpg)
Help Wanted!!!
![Page 23: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/23.jpg)
So this worked well…
![Page 24: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/24.jpg)
Lets do it again!
![Page 25: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/25.jpg)
Turn another String into
Objects
![Page 26: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/26.jpg)
:-)
![Page 27: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/27.jpg)
Methods
![Page 28: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/28.jpg)
Lets have a look
![Page 29: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/29.jpg)
• Method are Objects, but…
!
• No high-level model for sub-method elements
• Message sends
• Assignments
• Variable access
!
• Structural reflection stops at the granularity of methods!
![Page 30: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/30.jpg)
Can we do better?
![Page 31: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/31.jpg)
Compilers have ASTs
![Page 32: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/32.jpg)
Abstract Syntax Trees
![Page 33: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/33.jpg)
• Lets have a look at an example
(Object>>#halt) ast
![Page 34: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/34.jpg)
• Encodes the method as a tree of node-objects
• Visitor Pattern
• Transformations
• Refactoring tool uses this!
RBProgramNode RBDoItNode RBMethodNode RBReturnNode RBSequenceNode RBValueNode RBArrayNode RBAssignmentNode RBBlockNode RBCascadeNode RBLiteralNode RBMessageNode RBVariableNode
![Page 35: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/35.jpg)
In Pharo3
• AST based Navigation in the Editor
• “Suggestions”
• Debugger uses AST for pc->code mapping
• AST Interpreter for experiments
![Page 36: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/36.jpg)
Future
• AST everywhere!
• Do we need to store strings?
• Can we have an AST based editor?
• Sub-Method Reflection: The MetaLink
![Page 37: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/37.jpg)
Method
Can we modify the behaviour of code?
Meta
Link
> Annotate the AST with meta-links
![Page 38: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/38.jpg)
Why?
• Change behaviour for selected AST Nodes
• “All assignments”
• “this message send”
But without changing the program code!
![Page 39: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/39.jpg)
Breakpoints
![Page 40: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/40.jpg)
DEMO: Atoms
![Page 41: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/41.jpg)
© Marcus Denker
Behavioral Reflection
source code
(AST)
meta-object
activation
condition
links
![Page 42: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/42.jpg)
Uses…
• Debugger
• BreakPoints, WatchPoints
• Profilers
• Coverage Analysis
• AOP
![Page 43: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/43.jpg)
Will be in Pharo4
![Page 44: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/44.jpg)
Will be in Pharo4 !!help wanted!!
![Page 45: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/45.jpg)
What did we see?• Slots!
• Instance variables are just described with strings
• We can do better! Layout, Slots
• ASTs Everywhere!
• Methods are objects, but internal structure not modelled
• We can do better! AST, Sub-Method Reflection, Meta-Links
![Page 46: Advanced Reflection in Pharo](https://reader034.vdocuments.us/reader034/viewer/2022052618/554f79f3b4c9052a518b49a6/html5/thumbnails/46.jpg)
Questions???