SlideShare a Scribd company logo
Reflection in Pharo5
ESUG 2015
Marcus Denker
http://www.pharo.org
Everything is an Object
Everything?
Classes, yes.
Methods, yes
But Code?
Code is a String!
AST: Abstract Syntax Tree
AST in Pharo5
• AST of the Refactoring browser
• Transformation
• Visitors
• Annotations (properties)
• Deeper integrated:
• Pretty Printing, Syntax Highlight, Suggestions
• Compiler uses RB AST
AST in Pharo5
• Easy access
• #ast
• Demo: method and block
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)
AST + Tools
Opal Compiler
• Uses RB AST
• Based on Visitors
Text AST
AST
+vars
IR CM
Parser Semantic
Analysis
AST Translator+
IRBuilder
BytecodeBuilder+
Encoder
Opal: API
• All staged are Pluggable
• e.g Semantic Analyzer or Code Generator can
be changed.
• compiler options
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
Too complicated
Too low level
Can we do better?
AST Meta Annotation
• We have an AST with properties
• We have Opal with Pluggable API
Can’t we use that?
Basis: the Evil Twin
CompiledMethod ReflectiveMethod
Know
each
other
Bytecode AST
Basis: the Evil Twin
ReflectiveMethod
AST
run: aSelector with: anArray in: aReceiver
self installCompiledMethod.
self recompileAST.
self installCompiledMethod.
^compiledMethod
valueWithReceiver: aReceiver
arguments: anArray
Demo: Morph
• Morph methods do: #createTwin
• Morph methods do: #invalidate
• inspect “Morph methods”
Putting it together
• Annotate the AST
• Create Twin if needed
• Invalidate method
• Next call: generate code changed by annotation
recompileAST
ast compilationContext
semanticAnalyzerClass: RFSemanticAnalyzer;
astTranslatorClass: RFASTTranslator.
ast doSemanticAnalysis. "force semantic analysis"
compiledMethod := ast generate: compiledMethod trailer.
compiledMethod reflectiveMethod: self.
Annotations?
MetaLink
DEMO: Simple Link
node := (ReflectivityExamples>>#exampleMethod) ast.
link := MetaLink
new metaObject: (Object new);
selector: #halt.
node link: link.
ReflectivityExamples new exampleMethod
Meta Link
• When setting link:
• create twin if needed
• install reflective method
• On execution
• generate code and execute, install CM
Twin Switch
CompiledMethod ReflectiveMethod
Know
each
other
Bytecode AST
Link: metaobject
The object to send
a message to
link := MetaLink new
metaObject: [self halt]
Link: selector
The selector to send
link := MetaLink new
…..
selector: #value
Link: control
before, after, instead
link := MetaLink new
…..
control: #after
Link: control
after: #ensure: wrap
link := MetaLink new
…..
control: #after
Link: control
instead: last link wins
(for now no AOP around)
link := MetaLink new
…..
control: #instead
Link: condition
boolean or block
link := MetaLink new
…..
condition: [self someCheck]
Link: arguments
what to pass to the meta?
Reifications
• Every operation has data that it works on
• Send: #arguments, #receiver, #selector
• Assignment: #newValue, #name
• All: #node, #object, #context
Link: arguments
what to pass to the meta?
link := MetaLink new
…..
arguments: #(name newValue)
Reifications: condition
link := MetaLink new
condition: [: object | object == 1];
Virtual meta
• Reifications can be the meta object
link := MetaLink new
metaObject: #receiver;
selector: #perform:withArguments:;
arguments: #(selector arguments).
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].
Variables
• Helper methods
• But: can’t we annotate variables directly?
Point assignmentNodes
Friday: Slots+Globals
RoadMap
• Pharo4: Opal is default
• Pharo5
• Remove old Compiler/AST
• Reflectivity: First finished version
• Pharo6: Object specific links
Users
• Tools of ObjectProfile are being ported
• BreakPoints Pharo5
• Coverage Kernel by Pavel
• ….
Thanks!
• Work of many people…
• Too many to list here. (And I would forget for
sure someone)
Questions ?

More Related Content

PDF
Variables in Pharo5
PDF
Reflection in Pharo: Beyond Smalltak
PDF
PharoDAYS 2015: Pharo Status - by Markus Denker
PDF
Dynamically Composing Collection Operations through Collection Promises
PDF
Advanced Reflection in Pharo
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Pharo: A Reflective System
PDF
#Pharo Days 2016 Reflectivity
Variables in Pharo5
Reflection in Pharo: Beyond Smalltak
PharoDAYS 2015: Pharo Status - by Markus Denker
Dynamically Composing Collection Operations through Collection Promises
Advanced Reflection in Pharo
Reflection in Pharo: Beyond Smalltak
Pharo: A Reflective System
#Pharo Days 2016 Reflectivity

What's hot (19)

PDF
#Pharo Days 2016 Data Formats and Protocols
PDF
Pharo Status Fosdem 2015
PDF
Gatling @ Scala.Io 2013
PDF
java8-patterns
PDF
Rethinking the debugger
PDF
Pharo Status ESUG 2014
PPTX
Java 8 concurrency abstractions
PDF
How we migrated Zalando app to Swift3?
PPTX
Async Programming in C# 5
PDF
Lecture: MetaLinks
PDF
Load test REST APIs using gatling
ODP
How to start using Scala
PPTX
End to-end async and await
PPTX
Async in .NET
PDF
Maccro Strikes Back
PPTX
Java 8 New features
PDF
Invitation to the dark side of Ruby
PDF
C# 8 in Libraries and Applications
PDF
SF Front End Developers - Ember + D3
#Pharo Days 2016 Data Formats and Protocols
Pharo Status Fosdem 2015
Gatling @ Scala.Io 2013
java8-patterns
Rethinking the debugger
Pharo Status ESUG 2014
Java 8 concurrency abstractions
How we migrated Zalando app to Swift3?
Async Programming in C# 5
Lecture: MetaLinks
Load test REST APIs using gatling
How to start using Scala
End to-end async and await
Async in .NET
Maccro Strikes Back
Java 8 New features
Invitation to the dark side of Ruby
C# 8 in Libraries and Applications
SF Front End Developers - Ember + D3
Ad

Viewers also liked (6)

PDF
Perfection & Feedback Loops or: why worse is better
PDF
Type Feedback for Bytecode Interpreters
KEY
Talk: The Present and Future of Pharo
PDF
Practical, Pluggable Types
PDF
Pharo Status (from PharoDays 2015)
PDF
How to Contribute to Pharo
Perfection & Feedback Loops or: why worse is better
Type Feedback for Bytecode Interpreters
Talk: The Present and Future of Pharo
Practical, Pluggable Types
Pharo Status (from PharoDays 2015)
How to Contribute to Pharo
Ad

Similar to Reflection in Pharo5 (16)

PDF
Lecture: Advanced Reflection. MetaLinks
PDF
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
PDF
Lecture. Advanced Reflection: MetaLinks
PDF
Lecture: "Advanced Reflection: MetaLinks"
PDF
Advanced Reflection in Pharo
PDF
Pharo: A Reflective System
PDF
Illicium: Compiling Pharo to C
PDF
Towards a flexible Pharo Compiler
PDF
Towards a flexible Pharo Compiler
PDF
Towards a flexible Pharo Compiler
PDF
Pharo: a reflective language A first systematic analysis of reflective APIs
PDF
First class Variables in Pharo
PDF
The Reflectivity
PDF
Demo: Reflectivity
PDF
Reflection and Context
KEY
Opal compiler
Lecture: Advanced Reflection. MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
Lecture: "Advanced Reflection: MetaLinks"
Advanced Reflection in Pharo
Pharo: A Reflective System
Illicium: Compiling Pharo to C
Towards a flexible Pharo Compiler
Towards a flexible Pharo Compiler
Towards a flexible Pharo Compiler
Pharo: a reflective language A first systematic analysis of reflective APIs
First class Variables in Pharo
The Reflectivity
Demo: Reflectivity
Reflection and Context
Opal compiler

More from Marcus Denker (16)

PDF
Soil And Pharo
PDF
ConstantBlocks in Pharo11
PDF
Demo: Improved DoIt
PDF
First Class Variables as AST Annotations
PDF
Supporting Pharo / Getting Pharo Support
PDF
thisContext in the Debugger
PDF
Variables in Pharo
PDF
Improving code completion for Pharo
PDF
Slot Composition
PDF
PHARO IOT
PDF
Open-Source: An Infinite Game
PDF
PharoTechTalk: Contributing to Pharo
PDF
Feedback Loops in Practice
PDF
Pharo6 - ESUG17
PDF
PDF
Nomads do not build Cathedrals
Soil And Pharo
ConstantBlocks in Pharo11
Demo: Improved DoIt
First Class Variables as AST Annotations
Supporting Pharo / Getting Pharo Support
thisContext in the Debugger
Variables in Pharo
Improving code completion for Pharo
Slot Composition
PHARO IOT
Open-Source: An Infinite Game
PharoTechTalk: Contributing to Pharo
Feedback Loops in Practice
Pharo6 - ESUG17
Nomads do not build Cathedrals

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf

Reflection in Pharo5