SlideShare a Scribd company logo
1 of 47
Download to read offline
Advanced Reflection:
MetaLinks
Marcus Denker, Inria
http://marcusdenker.de
Lecture at VUB Brussels, October 30, 2018
What we know…
• Smalltalk is reflective

• Classes, Methods, Stack-Frames… are Objects

• Reflective API on all Objects
Take home message
• Reflection is based on the meta-class model, thus
inherently structural.
• Behavioural reflection limited to:
• Method lookup upon failure ( doesNotUnderstand:
message)
• Current execution reified (thisContext)
61
Can we do better?
• A more fine-grained reflective mechanism seems to be
missing

• Let’s look again at a Method in the Inspector
Inspector on a Method
The AST
• AST = Abstract Syntax Tree

• Tree Representation of the Method

• Produced by the Parser (part of the Compiler)

• Used by all tools (refactoring, syntax-highlighting,…)
Smalltalk compiler parse: 'test ^(1+2)'
AST
• RBMethodNode Root

• RBVariableNode Variable (read and write)

• RBAssignmentNode Assignment

• RBMessageNode A Message (most of them)

• RBReturnNode Return
Inspect a simple AST
• A very simple Example
Smalltalk compiler parse: 'test ^(1+2)'
AST: Navigation
• To make it easy to find and enumerate nodes, there are
some helper methods

• CompiledMethod has: #sendNodes,
#variableNodes, #assignmentNodes
• Every AST node has #nodesDo: and #allChildren
AST: Visitor
• RBProgramNodeVisitor: Visitor Pattern for the AST

• Make subclass, override visit… methods

• Let’s see it in action: Count Message sends
Demo: Visitor
Repeat:The AST
• AST = Abstract Syntax Tree

• Tree Representation of the Method

• Produced by the Parser (part of the Compiler)

• Used by all tools (refactoring, syntax-highlighting,…)
Smalltalk compiler parse: 'test ^(1+2)'
The Compiler
• Smalltalk compiler -> Compiler Facade 

• Classes define the compiler to use

• You can override method #compiler

• Behind: Compiler Chain
The Compiler
Source AST
Annotated
AST
IR
Annotated
AST
Bytecode
RBParser OCSemanticAnalyzer
OCASTTranslator/
IRBuilder
IRBytecodeGenerator
AST Integration
• Originally just internal to the compiler

• Pharo: 

• send #ast to a method to get the AST

• Cached for persistency.
(Point>>#x) ast == (Point>>#x) ast
—> true
AST Integration
• We can navigate from execution to AST

• Example:
[ 1 + 2 ] sourceNode ==
thisContext method sourceNode blockNodes first
Back to the topic…
• A more fine-grained reflective mechanism seems to be
missing

• Can’t we do something with the AST?
Wouldn’t it be nice..
• With the AST, wouldn’t it be nice if we could use this
structure for Behavioural Reflection?

• If we could somehow attach a “arrow to the code” that
points to a meta-object
test
^( 1 + 2 )
meta-object
for this Send
We have all pieces…
• We have the AST for each method

• It is quite simple

• We have a compiler in the system

• So this should be possible…
The MetaLink
• MetaLink points to metaObject

• Defines a selector to call

• And a control attribute: #before, #after, #instead

• Installed on a AST node:
link := MetaLink new
metaObject: Halt;
selector: #once;
control: #before.
(Number>>#sin) ast link: link
The MetaLink
• Can be installed on any AST Node

• Methods will be re-compiled on the fly just before next
execution

• Link installation is very fast

• Changing a method removes all links from this method

• Managing link re-installation has to be done by the user
MetaLink: MetaObject
• MetaObject can be any object

• Even a Block: [Transcript show ‘hello’]
• Install on any Node with #link:

• de-install a link with #uninstall
MetaLink: Selector
• MetaLink defines a message send to the MetaObject

• #selector defines which one

• Default is #value

• Yes, a selector with arguments is supported 

• We can pass information to the meta-object
MetaLink: Argument
• The arguments define which arguments to pass

• We support a number of reifications
Reifications
• Reifications define data to be passed as arguments

• Reify —> Make something into an object that is not one
normally

• Example: “All arguments of this message”
Reifications: examples
• All nodes: #object #context #class #node
#link
• Sends: #arguments #receiver #selector
• Method: #arguments #selector 

• Variable: #value

They are defined as subclasses of class RFReification
Reifications as MetaObject
• We support some special metaObjects:

• #node The AST Node we are installed on

• #object self at runtime

• #class The class the links is installed in
MetaLink: Condition
• We can specify a condition for the MetaLink

• Link is active if the condition evaluates to true
• We can pass reifications as arguments
link := MetaLink new
metaObject: Halt;
selector: #once;
condition: [:object | object == 5] arguments: #(object).
(Number>>#sin) ast link: link.
MetaLink: control
• We can specify when to call the meta-object

• We support #before, #after and #instead
• The instead is very simple: last one wins
Example: Log
• We want to just print something to the Transcript
link := MetaLink new
metaObject: [Transcript show: 'Reached Here'].
(Number>>#sin) ast link: link
Recursion Problem
• Before we see more examples: There is a problem

• Imagine we put a MetaLink on some method deep in the
System (e.g new, +, do: ).

• Our Meta-Object might use exactly that method, too
Endless Loop!!
Recursion Problem
• Solution: Meta-Level

• We encode the a level in the execution of the system

• Every Link Activation increases the level

• A meta-link is just active for one level. (e.g. 0)
link := MetaLink new
metaObject: [ Object new ];
level: 0.
(Behavior>>#new) ast link: link.
Example: Log
• Better use #level: 0

• Nevertheless: be careful! If you add this to method called
often it can be very slow.
link := MetaLink new
metaObject: [Transcript show: 'Reached Here’];
level: 0.
Example: Counter
• In the Browser you can add a “counter” to the AST

• See class ExecutionCounter
install
link := MetaLink new
metaObject: self;
selector: #increase.
node link: link.
Example: Breakpoint
• “Add Breakpoint” in AST (Suggestions) Menu

• See class Breakpoint

• Break Once 

• Conditional Break breakLink
^ MetaLink new
metaObject: Break;
selector: #break;
options: options
Example: WatchPoint
• Watchpoint: Record Value at a point in the AST

• Example: Watch event in WorldMorph>>#mouseDown:
Click on background
-> value recorded
Example: WatchPoint
• Implementation: class Watchpoint, method install

• example of a #after link with a condition
link := MetaLink new
metaObject: self;
selector: #addValue:;
arguments: #(value);
control: #after;
condition: [ recording ].
Example: Code Coverage
• Small Demo.

• Start with CoverageDemo new openWithSpec
Example: Code Coverage
• Example of a MetaLink with a #node MetObject

• Meta-Object is the node that the link is installed on
link := MetaLink new
metaObject: #node;
selector: #tagExecuted.
Interesting Properties
• Cross Cutting

• One Link can be installed multiple times

• Over multiple methods and even Classes

• And across operations (e.g., Send and Assignment) as
long as all reifications requested are compatible

• Fully Dynamic: Links can be added and removed at runtime

• Even by the meta-object of another meta-link!
Example: Accept for Test
• Imagine we want to edit a method that is called often by
the System.

• How do we test it?

• It would be nice if we could “Accept for Test”
Example: Accept for Test
• Menu in the browser. Quick hack, a Suggestions AST
menu shows for all nodes.
SugsSuggestion subclass: #SugsAcceptForTest
instanceVariableNames: ''
classVariableNames: ''
package: 'SmartSuggestions-Suggestion'
label
^'Accept for test'
• We implement our code in the #execute method
Example: Accept for Test
• How we know that we are in a test?
CurrentExecutionEnvironment value isTest
• We can compile the current text buffer
newMethod := context selectedClass compiler
source: context code;
options: #(+ optionParseErrors);
compile.
Example: Accept for Test
• Add this code to the beginning of the method:
[:aContext :args |
CurrentExecutionEnvironment value isTest ifTrue: [
aContext return: (newMethod
valueWithReceiver: aContext
receiver
arguments: args) ]]
• Let’s do that with a MetaLink!
Example: Accept for Test
execute
| newMethod metaLink |
newMethod := context selectedClass compiler
source: context code;
options: #(+ optionParseErrors);
compile.
"the link executes the method we just created and returns"
metaLink := MetaLink new
metaObject: [ :aContext :args |
CurrentExecutionEnvironment value isTest
ifTrue: [ aContext return: (newMethod
valueWithReceiver: aContext receiver
arguments: args) ] ];
selector: #value:value:;
arguments: #(context arguments).
context selectedMethod ast link: metaLink
Limitations
• Better use Pharo7 (we are improving it still)

• #instead needs more work (e.g to support conditions)

• Keep in mind: next metaLink taken into account for next
method activation 

• Take care with long running loops!
Help Wanted
• We are always interested in improvements!

• Pharo7 is under active development. 

• Pull Requests Welcome!

More Related Content

What's hot

Launching Python Cloud Services for AI/IoT Projects
Launching Python Cloud Services for AI/IoT ProjectsLaunching Python Cloud Services for AI/IoT Projects
Launching Python Cloud Services for AI/IoT ProjectsVincent Claes
 
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup Munich
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup MunichKotlin and Domain-Driven Design: A perfect match - Kotlin Meetup Munich
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup MunichFlorian Benz
 
Git and github - Verson Control for the Modern Developer
Git and github - Verson Control for the Modern DeveloperGit and github - Verson Control for the Modern Developer
Git and github - Verson Control for the Modern DeveloperJohn Stevenson
 
Our Best Practices Are Killing Us
Our Best Practices Are Killing UsOur Best Practices Are Killing Us
Our Best Practices Are Killing UsNicole Sullivan
 
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersShiju Varghese
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Saulius Skeirys
 
Clean architecture
Clean architectureClean architecture
Clean architectureLieven Doclo
 
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]RootedCON
 
Modernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptxModernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptxEd Charbeneau
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hookPiyush Jamwal
 
Bloc for Pharo: Current State and Future Perspective
Bloc for Pharo: Current State and Future PerspectiveBloc for Pharo: Current State and Future Perspective
Bloc for Pharo: Current State and Future PerspectiveESUG
 
Pymongo password change made easy
Pymongo password change made easyPymongo password change made easy
Pymongo password change made easyDarshan Jayarama
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events WebStackAcademy
 
Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Maksym Husar
 
Python/Flask Presentation
Python/Flask PresentationPython/Flask Presentation
Python/Flask PresentationParag Mujumdar
 

What's hot (20)

Launching Python Cloud Services for AI/IoT Projects
Launching Python Cloud Services for AI/IoT ProjectsLaunching Python Cloud Services for AI/IoT Projects
Launching Python Cloud Services for AI/IoT Projects
 
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup Munich
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup MunichKotlin and Domain-Driven Design: A perfect match - Kotlin Meetup Munich
Kotlin and Domain-Driven Design: A perfect match - Kotlin Meetup Munich
 
Git and github - Verson Control for the Modern Developer
Git and github - Verson Control for the Modern DeveloperGit and github - Verson Control for the Modern Developer
Git and github - Verson Control for the Modern Developer
 
Our Best Practices Are Killing Us
Our Best Practices Are Killing UsOur Best Practices Are Killing Us
Our Best Practices Are Killing Us
 
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol Buffers
 
Rust-lang
Rust-langRust-lang
Rust-lang
 
Javascript and DOM
Javascript and DOMJavascript and DOM
Javascript and DOM
 
Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?Typescript in React: HOW & WHY?
Typescript in React: HOW & WHY?
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]
Javier Marcos - Detección de amenazas a escala con osquery [rooted2019]
 
Modernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptxModernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptx
 
React js use contexts and useContext hook
React js use contexts and useContext hookReact js use contexts and useContext hook
React js use contexts and useContext hook
 
Bloc for Pharo: Current State and Future Perspective
Bloc for Pharo: Current State and Future PerspectiveBloc for Pharo: Current State and Future Perspective
Bloc for Pharo: Current State and Future Perspective
 
Pymongo password change made easy
Pymongo password change made easyPymongo password change made easy
Pymongo password change made easy
 
Tomcat Server
Tomcat ServerTomcat Server
Tomcat Server
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
 
Clean code: SOLID (iOS)
Clean code: SOLID (iOS)Clean code: SOLID (iOS)
Clean code: SOLID (iOS)
 
Id and class selector
Id and class selectorId and class selector
Id and class selector
 
Python/Flask Presentation
Python/Flask PresentationPython/Flask Presentation
Python/Flask Presentation
 

Similar to Lecture: Advanced Reflection. MetaLinks

VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksMarcus Denker
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Marcus Denker
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksMarcus Denker
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5Marcus Denker
 
Behavioral Reflection in Pharo
Behavioral Reflection in PharoBehavioral Reflection in Pharo
Behavioral Reflection in PharoESUG
 
#Pharo Days 2016 Reflectivity
#Pharo Days 2016 Reflectivity#Pharo Days 2016 Reflectivity
#Pharo Days 2016 ReflectivityPhilippe Back
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakMarcus Denker
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakMarcus Denker
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST AnnotationsESUG
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST AnnotationsMarcus Denker
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesAndrey Karpov
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Developer testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticDeveloper testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticLB Denker
 
Behavioral Reflection
Behavioral ReflectionBehavioral Reflection
Behavioral ReflectionMarcus Denker
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryGauravPatil318
 
Developer testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to IntegrateDeveloper testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to IntegrateLB Denker
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disquszeeg
 

Similar to Lecture: Advanced Reflection. MetaLinks (20)

VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinksVUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
VUB Brussels Lecture 2019: Advanced Reflection: MetaLinks
 
Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"Lecture: "Advanced Reflection: MetaLinks"
Lecture: "Advanced Reflection: MetaLinks"
 
Lecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinksLecture. Advanced Reflection: MetaLinks
Lecture. Advanced Reflection: MetaLinks
 
Lecture: MetaLinks
Lecture: MetaLinksLecture: MetaLinks
Lecture: MetaLinks
 
Reflection in Pharo5
Reflection in Pharo5Reflection in Pharo5
Reflection in Pharo5
 
Behavioral Reflection in Pharo
Behavioral Reflection in PharoBehavioral Reflection in Pharo
Behavioral Reflection in Pharo
 
#Pharo Days 2016 Reflectivity
#Pharo Days 2016 Reflectivity#Pharo Days 2016 Reflectivity
#Pharo Days 2016 Reflectivity
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
First Class Variables as AST Annotations
 First Class Variables as AST Annotations First Class Variables as AST Annotations
First Class Variables as AST Annotations
 
First Class Variables as AST Annotations
First Class Variables as AST AnnotationsFirst Class Variables as AST Annotations
First Class Variables as AST Annotations
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Developer testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticDeveloper testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing Fanatic
 
Behavioral Reflection
Behavioral ReflectionBehavioral Reflection
Behavioral Reflection
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Variables in Pharo
Variables in PharoVariables in Pharo
Variables in Pharo
 
Developer testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to IntegrateDeveloper testing 201: When to Mock and When to Integrate
Developer testing 201: When to Mock and When to Integrate
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disqus
 

More from Marcus Denker

ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11Marcus Denker
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportMarcus Denker
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the DebuggerMarcus Denker
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for PharoMarcus Denker
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite GameMarcus Denker
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoMarcus Denker
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in PracticeMarcus Denker
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterMarcus Denker
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesMarcus Denker
 
How to Contribute to Pharo
How to Contribute to PharoHow to Contribute to Pharo
How to Contribute to PharoMarcus Denker
 
Pharo Status (from PharoDays 2015)
Pharo Status (from PharoDays 2015)Pharo Status (from PharoDays 2015)
Pharo Status (from PharoDays 2015)Marcus Denker
 
Pharo Status Fosdem 2015
Pharo Status Fosdem 2015Pharo Status Fosdem 2015
Pharo Status Fosdem 2015Marcus Denker
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014Marcus Denker
 

More from Marcus Denker (20)

Soil And Pharo
Soil And PharoSoil And Pharo
Soil And Pharo
 
ConstantBlocks in Pharo11
ConstantBlocks in Pharo11ConstantBlocks in Pharo11
ConstantBlocks in Pharo11
 
Demo: Improved DoIt
Demo: Improved DoItDemo: Improved DoIt
Demo: Improved DoIt
 
Supporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo SupportSupporting Pharo / Getting Pharo Support
Supporting Pharo / Getting Pharo Support
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Improving code completion for Pharo
Improving code completion for PharoImproving code completion for Pharo
Improving code completion for Pharo
 
Slot Composition
Slot CompositionSlot Composition
Slot Composition
 
PHARO IOT
PHARO IOTPHARO IOT
PHARO IOT
 
Open-Source: An Infinite Game
Open-Source: An Infinite GameOpen-Source: An Infinite Game
Open-Source: An Infinite Game
 
PharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to PharoPharoTechTalk: Contributing to Pharo
PharoTechTalk: Contributing to Pharo
 
Feedback Loops in Practice
Feedback Loops in PracticeFeedback Loops in Practice
Feedback Loops in Practice
 
Pharo6 - ESUG17
Pharo6 - ESUG17Pharo6 - ESUG17
Pharo6 - ESUG17
 
Pharo6
Pharo6Pharo6
Pharo6
 
Perfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is betterPerfection & Feedback Loops or: why worse is better
Perfection & Feedback Loops or: why worse is better
 
Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
How to Contribute to Pharo
How to Contribute to PharoHow to Contribute to Pharo
How to Contribute to Pharo
 
Pharo Status (from PharoDays 2015)
Pharo Status (from PharoDays 2015)Pharo Status (from PharoDays 2015)
Pharo Status (from PharoDays 2015)
 
Pharo Status Fosdem 2015
Pharo Status Fosdem 2015Pharo Status Fosdem 2015
Pharo Status Fosdem 2015
 
Pharo Status ESUG 2014
Pharo Status ESUG 2014Pharo Status ESUG 2014
Pharo Status ESUG 2014
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Lecture: Advanced Reflection. MetaLinks

  • 1. Advanced Reflection: MetaLinks Marcus Denker, Inria http://marcusdenker.de Lecture at VUB Brussels, October 30, 2018
  • 2. What we know… • Smalltalk is reflective • Classes, Methods, Stack-Frames… are Objects • Reflective API on all Objects
  • 3. Take home message • Reflection is based on the meta-class model, thus inherently structural. • Behavioural reflection limited to: • Method lookup upon failure ( doesNotUnderstand: message) • Current execution reified (thisContext) 61
  • 4. Can we do better? • A more fine-grained reflective mechanism seems to be missing • Let’s look again at a Method in the Inspector
  • 5. Inspector on a Method
  • 6. The AST • AST = Abstract Syntax Tree • Tree Representation of the Method • Produced by the Parser (part of the Compiler) • Used by all tools (refactoring, syntax-highlighting,…) Smalltalk compiler parse: 'test ^(1+2)'
  • 7. AST • RBMethodNode Root • RBVariableNode Variable (read and write) • RBAssignmentNode Assignment • RBMessageNode A Message (most of them) • RBReturnNode Return
  • 8. Inspect a simple AST • A very simple Example Smalltalk compiler parse: 'test ^(1+2)'
  • 9. AST: Navigation • To make it easy to find and enumerate nodes, there are some helper methods • CompiledMethod has: #sendNodes, #variableNodes, #assignmentNodes • Every AST node has #nodesDo: and #allChildren
  • 10. AST: Visitor • RBProgramNodeVisitor: Visitor Pattern for the AST • Make subclass, override visit… methods • Let’s see it in action: Count Message sends
  • 12. Repeat:The AST • AST = Abstract Syntax Tree • Tree Representation of the Method • Produced by the Parser (part of the Compiler) • Used by all tools (refactoring, syntax-highlighting,…) Smalltalk compiler parse: 'test ^(1+2)'
  • 13. The Compiler • Smalltalk compiler -> Compiler Facade • Classes define the compiler to use • You can override method #compiler • Behind: Compiler Chain
  • 14. The Compiler Source AST Annotated AST IR Annotated AST Bytecode RBParser OCSemanticAnalyzer OCASTTranslator/ IRBuilder IRBytecodeGenerator
  • 15. AST Integration • Originally just internal to the compiler • Pharo: • send #ast to a method to get the AST • Cached for persistency. (Point>>#x) ast == (Point>>#x) ast —> true
  • 16. AST Integration • We can navigate from execution to AST • Example: [ 1 + 2 ] sourceNode == thisContext method sourceNode blockNodes first
  • 17. Back to the topic… • A more fine-grained reflective mechanism seems to be missing • Can’t we do something with the AST?
  • 18. Wouldn’t it be nice.. • With the AST, wouldn’t it be nice if we could use this structure for Behavioural Reflection? • If we could somehow attach a “arrow to the code” that points to a meta-object test ^( 1 + 2 ) meta-object for this Send
  • 19. We have all pieces… • We have the AST for each method • It is quite simple • We have a compiler in the system • So this should be possible…
  • 20. The MetaLink • MetaLink points to metaObject • Defines a selector to call • And a control attribute: #before, #after, #instead • Installed on a AST node: link := MetaLink new metaObject: Halt; selector: #once; control: #before. (Number>>#sin) ast link: link
  • 21. The MetaLink • Can be installed on any AST Node • Methods will be re-compiled on the fly just before next execution • Link installation is very fast • Changing a method removes all links from this method • Managing link re-installation has to be done by the user
  • 22. MetaLink: MetaObject • MetaObject can be any object • Even a Block: [Transcript show ‘hello’] • Install on any Node with #link: • de-install a link with #uninstall
  • 23. MetaLink: Selector • MetaLink defines a message send to the MetaObject • #selector defines which one • Default is #value • Yes, a selector with arguments is supported • We can pass information to the meta-object
  • 24. MetaLink: Argument • The arguments define which arguments to pass • We support a number of reifications
  • 25. Reifications • Reifications define data to be passed as arguments • Reify —> Make something into an object that is not one normally • Example: “All arguments of this message”
  • 26. Reifications: examples • All nodes: #object #context #class #node #link • Sends: #arguments #receiver #selector • Method: #arguments #selector • Variable: #value
 They are defined as subclasses of class RFReification
  • 27. Reifications as MetaObject • We support some special metaObjects: • #node The AST Node we are installed on • #object self at runtime • #class The class the links is installed in
  • 28. MetaLink: Condition • We can specify a condition for the MetaLink • Link is active if the condition evaluates to true • We can pass reifications as arguments link := MetaLink new metaObject: Halt; selector: #once; condition: [:object | object == 5] arguments: #(object). (Number>>#sin) ast link: link.
  • 29. MetaLink: control • We can specify when to call the meta-object • We support #before, #after and #instead • The instead is very simple: last one wins
  • 30. Example: Log • We want to just print something to the Transcript link := MetaLink new metaObject: [Transcript show: 'Reached Here']. (Number>>#sin) ast link: link
  • 31. Recursion Problem • Before we see more examples: There is a problem • Imagine we put a MetaLink on some method deep in the System (e.g new, +, do: ). • Our Meta-Object might use exactly that method, too Endless Loop!!
  • 32. Recursion Problem • Solution: Meta-Level • We encode the a level in the execution of the system • Every Link Activation increases the level • A meta-link is just active for one level. (e.g. 0) link := MetaLink new metaObject: [ Object new ]; level: 0. (Behavior>>#new) ast link: link.
  • 33. Example: Log • Better use #level: 0 • Nevertheless: be careful! If you add this to method called often it can be very slow. link := MetaLink new metaObject: [Transcript show: 'Reached Here’]; level: 0.
  • 34. Example: Counter • In the Browser you can add a “counter” to the AST • See class ExecutionCounter install link := MetaLink new metaObject: self; selector: #increase. node link: link.
  • 35. Example: Breakpoint • “Add Breakpoint” in AST (Suggestions) Menu • See class Breakpoint • Break Once • Conditional Break breakLink ^ MetaLink new metaObject: Break; selector: #break; options: options
  • 36. Example: WatchPoint • Watchpoint: Record Value at a point in the AST • Example: Watch event in WorldMorph>>#mouseDown: Click on background -> value recorded
  • 37. Example: WatchPoint • Implementation: class Watchpoint, method install • example of a #after link with a condition link := MetaLink new metaObject: self; selector: #addValue:; arguments: #(value); control: #after; condition: [ recording ].
  • 38. Example: Code Coverage • Small Demo. • Start with CoverageDemo new openWithSpec
  • 39. Example: Code Coverage • Example of a MetaLink with a #node MetObject • Meta-Object is the node that the link is installed on link := MetaLink new metaObject: #node; selector: #tagExecuted.
  • 40. Interesting Properties • Cross Cutting • One Link can be installed multiple times • Over multiple methods and even Classes • And across operations (e.g., Send and Assignment) as long as all reifications requested are compatible • Fully Dynamic: Links can be added and removed at runtime • Even by the meta-object of another meta-link!
  • 41. Example: Accept for Test • Imagine we want to edit a method that is called often by the System. • How do we test it? • It would be nice if we could “Accept for Test”
  • 42. Example: Accept for Test • Menu in the browser. Quick hack, a Suggestions AST menu shows for all nodes. SugsSuggestion subclass: #SugsAcceptForTest instanceVariableNames: '' classVariableNames: '' package: 'SmartSuggestions-Suggestion' label ^'Accept for test' • We implement our code in the #execute method
  • 43. Example: Accept for Test • How we know that we are in a test? CurrentExecutionEnvironment value isTest • We can compile the current text buffer newMethod := context selectedClass compiler source: context code; options: #(+ optionParseErrors); compile.
  • 44. Example: Accept for Test • Add this code to the beginning of the method: [:aContext :args | CurrentExecutionEnvironment value isTest ifTrue: [ aContext return: (newMethod valueWithReceiver: aContext receiver arguments: args) ]] • Let’s do that with a MetaLink!
  • 45. Example: Accept for Test execute | newMethod metaLink | newMethod := context selectedClass compiler source: context code; options: #(+ optionParseErrors); compile. "the link executes the method we just created and returns" metaLink := MetaLink new metaObject: [ :aContext :args | CurrentExecutionEnvironment value isTest ifTrue: [ aContext return: (newMethod valueWithReceiver: aContext receiver arguments: args) ] ]; selector: #value:value:; arguments: #(context arguments). context selectedMethod ast link: metaLink
  • 46. Limitations • Better use Pharo7 (we are improving it still) • #instead needs more work (e.g to support conditions) • Keep in mind: next metaLink taken into account for next method activation • Take care with long running loops!
  • 47. Help Wanted • We are always interested in improvements! • Pharo7 is under active development. • Pull Requests Welcome!