reflection in pharo: beyond smalltak
TRANSCRIPT
![Page 1: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/1.jpg)
Reflection in Pharo: Beyond SmalltakMarcus Denker http://www.pharo.org
Talk held at SOFT, VUB, Brussels Jan 2016
![Page 2: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/2.jpg)
Everything is an Object
![Page 3: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/3.jpg)
Classes
![Page 4: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/4.jpg)
Methods
![Page 5: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/5.jpg)
The Stack
![Page 6: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/6.jpg)
Everything is an Object
![Page 7: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/7.jpg)
Code?
![Page 8: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/8.jpg)
AST: Abstract Syntax Tree
![Page 9: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/9.jpg)
AST in Pharo5• AST of the Refactoring browser
• Transformation
• Visitors
• Annotations (properties)
• Deeper integrated:
• Pretty Printing, Syntax Highlight, Suggestions
• Compiler uses RB AST
![Page 10: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/10.jpg)
AST in Pharo5
• Easy access
• #ast
• Demo: method and block
![Page 11: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/11.jpg)
DEMO
(OrderedCollection>>#do:) ast.
[ 1 + 2 ] sourceNode == thisContext method ast blockNodes first
• ASTCache: as twice, get the same answer (flushed on image save for now)
![Page 12: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/12.jpg)
AST + Tools
![Page 13: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/13.jpg)
Opal Compiler
• Uses RB AST
• Based on Visitors
Text AST AST+vars IR CM
Parser Semantic Analysis
AST Translator+ IRBuilder
BytecodeBuilder+ Encoder
![Page 14: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/14.jpg)
Opal: API
• All staged are Pluggable
• e.g Semantic Analyzer or Code Generator can be changed.
• compiler options
![Page 15: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/15.jpg)
Opal: Bytecode editing
• IR can be used to manipulate methods on a bytecode level
Text AST AST+vars IR CM
Parser Semantic Analysis
AST Translator+ IRBuilder
BytecodeBuilder+ Encoder
![Page 16: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/16.jpg)
Too complicated
![Page 17: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/17.jpg)
Too low level
![Page 18: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/18.jpg)
Can we do better?
![Page 19: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/19.jpg)
AST Meta Annotation
• We have an AST with properties
• We have Opal with Pluggable API
![Page 20: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/20.jpg)
Can’t we use that?
![Page 21: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/21.jpg)
Basis: the Evil Twin
CompiledMethod ReflectiveMethod
Know each other
Bytecode AST
![Page 22: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/22.jpg)
Basis: the Evil Twin
ReflectiveMethod
AST
run: aSelector with: anArray in: aReceiver self installCompiledMethod. self recompileAST. self installCompiledMethod. ^compiledMethod
valueWithReceiver: aReceiver arguments: anArray
![Page 23: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/23.jpg)
Demo: Morph
• Morph methods do: #createTwin
• Morph methods do: #invalidate
• inspect “Morph methods”
![Page 24: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/24.jpg)
Putting it together
• Annotate the AST
• Create Twin if needed
• Invalidate method
• Next call: generate code changed by annotation
![Page 25: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/25.jpg)
Annotations?
![Page 26: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/26.jpg)
MetaLink
![Page 27: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/27.jpg)
DEMO: Simple Linknode := (ReflectivityExamples>>#exampleMethod) ast. link := MetaLink new metaObject: (Object new); selector: #halt.
node link: link.
ReflectivityExamples new exampleMethod
![Page 28: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/28.jpg)
Meta Link
• When setting link:
• create twin if needed
• install reflective method
• On execution
• generate code and execute, install CM
![Page 29: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/29.jpg)
Twin Switch
CompiledMethod ReflectiveMethod
Know each other
Bytecode AST
![Page 30: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/30.jpg)
Link: metaobject
The object to send a message to
link := MetaLink new metaObject: [self halt]
![Page 31: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/31.jpg)
Link: selector
The selector to send
link := MetaLink new ….. selector: #value
![Page 32: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/32.jpg)
Link: control
before, after, instead
link := MetaLink new ….. control: #after
![Page 33: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/33.jpg)
Link: control
after: #ensure: wrap
link := MetaLink new ….. control: #after
![Page 34: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/34.jpg)
Link: control
instead: last link wins (for now no AOP around)
link := MetaLink new ….. control: #instead
![Page 35: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/35.jpg)
Link: condition
boolean or block
link := MetaLink new ….. condition: [self someCheck]
![Page 36: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/36.jpg)
Link: arguments
what to pass to the meta?
![Page 37: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/37.jpg)
Reifications
• Every operation has data that it works on
• Send: #arguments, #receiver, #selector
• Assignment: #newValue, #name
• All: #node, #object, #context
![Page 38: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/38.jpg)
Link: arguments
what to pass to the meta?
link := MetaLink new ….. arguments: #(name newValue)
![Page 39: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/39.jpg)
Reifications: condition
link := MetaLink new condition: [: object | object == 1];
![Page 40: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/40.jpg)
Virtual meta
• Reifications can be the meta object
link := MetaLink new metaObject: #receiver; selector: #perform:withArguments:; arguments: #(selector arguments).
![Page 41: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/41.jpg)
Statement Coverage
link := MetaLink new metaObject: #node; selector: #tagExecuted.
“set this link on all the AST nodes" (ReflectivityExamples>>#exampleMethod) ast
nodesDo: [:node | node link: link].
![Page 42: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/42.jpg)
Users
• BreakPoints Pharo5
• Coverage Kernel
• ….
![Page 43: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/43.jpg)
Everything is an Object
![Page 44: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/44.jpg)
Everything is an object?
SmalltalkImage classVarNamed: #CompilerClass ==> returns value
Object binding class ==> Association
![Page 45: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/45.jpg)
Why not an Object?
![Page 46: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/46.jpg)
Globals/ClassVariables
• We are close: bindings are associations
• Add sublass “LiteralVariable”
• Sublasses GlobalVariable, ClassVariable
• Enhance API
![Page 47: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/47.jpg)
Globals/ClassVariables
SmalltalkImage classVariableNamed: #CompilerClass
Object binding class
![Page 48: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/48.jpg)
Globals: Reflective APiglobal := SmalltalkImage classVariableNamed: #CompilerClass
global read global write: someObject
+ helper methods + compatibility methods
![Page 49: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/49.jpg)
Everything is an object?
• Point instanceVariables
• 5@3 instVarNamed: ‘x’
• 5@3 instVarNamed: ‘y’ put: 6
![Page 50: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/50.jpg)
Why not an Object?
![Page 51: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/51.jpg)
Slots
Point slots
(Point slotNamed: #x) read: (3@4)
(Point slotNamed: #x) write: 7 to: (3@4)
![Page 52: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/52.jpg)
Variables+MetaLink
• Helper methods
• But: can’t we annotate variables directly?
Point assignmentNodes
![Page 53: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/53.jpg)
Variables + Links
• Object binding link: myMetaLink
• (Point slotNamed: #x) link: myMetaLink
(not yet in Pharo5)
![Page 54: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/54.jpg)
Class Template
Object subclass: #MyClass slots: { #x => WeakSlot } classVariables: { } category: 'Example'
![Page 55: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/55.jpg)
Future
• Can’t we model bit patterns and bind them to named virtual slots?
• How to model Array-like layouts better?
![Page 56: Reflection in Pharo: Beyond Smalltak](https://reader031.vdocuments.site/reader031/viewer/2022030211/58a336621a28ab9b6d8b59b1/html5/thumbnails/56.jpg)
Questions ?