SlideShare a Scribd company logo
1 of 80
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@inria.fr
http://stephane.ducasse.free.fr/
Reflective Programming
in Smalltalk
M. Denker and S. Ducasse - 2005
Introspection
Metaclasses
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
Why...
“As a programming language becomes higher and higher
level, its implementation in terms of underlying machine
involves more and more tradeoffs, on the part of the
implementor, about what cases to optimize at the
expense of what other cases.... the ability to cleanly
integrate something outside of the language’s scope
becomes more and more limited” [Kiczales’92a]
S.Ducasse 4
Definition
“Reflection is the ability of a program to manipulate as data
something representing the state of the program during its
own execution.There are two aspects of such manipulation:
introspection and intercession.
Introspection is the ability for a program to observe and
therefore reason about its own state.
Intercessory is the ability for a program to modify its own
execution state or alter its own interpretation or meaning.
Both aspects require a mechanism for encoding execution
state as data: providing such an encoding is called reification.”
[Bobrow, Gabriel and White in Paepke‘92]
S.Ducasse 5
• A system having itself as application domain and that is
causally connected with this domain can be qualified as a
reflective system [Pattie Maes]
• A reflective system has an internal representation of itself.
• A reflective system is able to act on itself with the
ensurance that its representation will be causally
connected (up to date).
• A reflective system has some static capacity of self-
representation and dynamic self-modification in constant
synchronization
Consequences
S.Ducasse 6
• The meta-language and the language can be different:
Scheme and an OO language
• The meta-language and the language can be same:
Meta Programming in Prog. Language
S.Ducasse 7
S.Ducasse 8
The Essence of a Class
• A format (number of instance variables and types)
• A superclass
• A method dictionary
S.Ducasse 9
Behavior >> new
In Squeak (3.8)
Behavior>>new
| classInstance |
classInstance := self basicNew.
classInstance methodDictionary: classInstance
emptyMethodDictionary.
classInstance superclass: Object.
classInstance setFormat: Object format.
^ classInstance
S.Ducasse 10
The Essence of an Object
• class pointer
• values
• Can be special:
• the pointer pointing to the object is the object itself
• character, smallInteger (compact classes)
S.Ducasse 11
Some MetaObjects
• Structure: Behavior, ClassDescription, Class, Metaclass,
ClassBuilder
• Semantics: Compiler, Decompiler, ProgramNode,
ProgramNodeBuilder, IRBuilder
• Behavior: CompiledMethod, CompiledBlock, Message,
Exception
• ControlState: Context, BlockContext, Process,
ProcessorScheduler
• Resources: ObjectMemory,WeakArray
• Naming: SystemDictionary, Namespace
• Libraries: MethodDictionary, ClassOrganizer
S.Ducasse 12
Meta-Operations
• MetaOperations are operations that provide
information about an object as opposed to
information directly contained by the object ...They
permit things to be done that are not normally
possible [Inside Smalltalk]”
S.Ducasse 13
Access
• Object>>instVarAt: aNumber
• Object>>instVarNamed: aString
• Object>>instVarAt: aNumber put: anObject
• Browser new instVarNamed: 'classOrganizer'
• | pt |
• pt := 10@3.
• pt instVarNamed: 'x' put: 33.
• pt
• > 33@3
S.Ducasse 14
Access
• Object>>class
• Object>>identityHash
S.Ducasse 15
Changes
• Object>>changeClassOfThat: anInstance
• inVW and Squeak both classes should have the same
format, i.e., the same physical structure of their
instances
• Object>>become: anotherObject
• Object>>becomeForward: anotherObject
S.Ducasse 16
Implementing Instance Specific Methods
In Squeak 3.8
| behavior browser |
behavior := Behavior new.
behavior superclass: Browser.
behavior setFormat: Browser format.
browser := Browser new.
browser primitiveChangeClassTo: behavior new.
behavior compile: 'thisIsATest ^ 2'.
self assert: browser thisIsATest = 2.
self should: [Browser new thisIsATest] raise:
MessageNotUnderstood
S.Ducasse 17
become: and oneWayBecome:
• become: is symmetric and swaps all the pointers
• oneWayBecome: (inVW) becomeForward: (Squeak)
changes pointers only in one way
S.Ducasse 18
become:
• Swap all the pointers from one object to the other
and back (symmetric)
• | pt1 pt2 pt3 |
• pt1 := 0@0.
• pt2 := pt1.
• pt3 := 100@100.
• pt1 become: pt3.
• self assert: pt2 = (100@100).
• self assert: pt3 = (0@0).
• self assert: pt1 = (100@100).
S.Ducasse 19
becomeForward:
• Swap all the pointers from one object to the other
one
• | pt1 pt2 pt3 |
• pt1 := 0@0.
• pt2 := pt1.
• pt3 := 100@100.
• pt1 becomeForward: pt3.
• self assert: (pt2 = (100@100)).
• self assert: pt3 = pt2.
• self assert: pt1 = (100@100)
S.Ducasse 20
Structure
• Objects represent classes
• Object root of inheritance
• default behavior
• minimal behavior
• Behavior: essence of class
• anymous class
• format, methodDict, superclass
• ClassDescription:
• human representation and organization
• Metaclass:
• sole instance
S.Ducasse 21
CompiledMethod Holders
S.Ducasse 22
ClassBuilder
• Manages class creation
• unique instance
• format with superclass checking
• changes of existing instance when class structure
changes
S.Ducasse 23
Some Selected Protocols
• Illustrated by the tools of the IDE
• Class>>selectors
• Class>>superclass
• Class>>compiledMethodAt: aSymbol
• Class>>instVarNames
• Class>>compiler
S.Ducasse 24
The Smalltalk Compiler
S.Ducasse 25
Compiler
• Fully reified compilation process:
• Scanner/Parser (build with SmaCC)
• builds AST (from Refactoring Browser)
• Semantic Analysis: ASTChecker
• annotates the AST (e.g., var bindings)
• Translation to IR: ASTTranslator
• uses IRBuilder to build IR (Intermediate Representation)
• Bytecode generation: IRTranslator
• uses BytecodeBuilder to emit bytecodes
S.Ducasse 26
Compiler: Overview
Scanner /
Parser
Semantic
Analysis
Code
Generation
Build IR
SmaCC Scanner
Parser
ASTChecker
ASTTranslator
IRBuilder
IRTranslator
BytecodeBuilder
Bytecode
Generation
AST AST BytecodeCode
IRAST Bytecode
Code generation in detail:
S.Ducasse 27
Compiler: Syntax
• SmaCC: Smalltalk Compiler Compiler
• Like Lex/Yacc
• Input:
• scanner definition: Regular Expressions
• parser: BNF Like Grammar
• code that build AST as annotation
• SmaCC can build LARL(1) or LR(1) parser
• Output:
• class for Scanner (subclass SmaCCScanner)
• class for Parser (subclass SmaCCParser)
S.Ducasse 28
Scanner
S.Ducasse 29
Parser
S.Ducasse 30
Calling Parser code
S.Ducasse 31
Compiler:AST
• AST:Abstract Syntax Tree
• Encodes the Syntax as a Tree
• No semantics yet!
• Uses the RB Tree:
• visitors
• backward pointers in ParseNodes
• transformation (replace/add/delete)
• pattern directed TreeRewriter
• PrettyPrinter
RBProgramNode
RBDoItNode
RBMethodNode
RBReturnNode
RBSequenceNode
RBValueNode
RBArrayNode
RBAssignmentNode
RBBlockNode
RBCascadeNode
RBLiteralNode
RBMessageNode
RBOptimizedNode
RBVariableNode
S.Ducasse 32
Compiler: Semantics
• We need to analyse the AST
• names need to be linked to theVariables according to
the scoping rules
• ASTChecker implemented as a visitor
• subclass of RBProgramNodeVisitor
• visits the nodes
• grows and shrinks Scope chain
• method/Blocks are linked with the Scope
• variable definitions and references are linked with
objects describing the variables
S.Ducasse 33
A Simple Tree
S.Ducasse 34
A SimpleVisitor
• RBProgramNodeVisitor new visitNode: tree.
• does nothing except walking throw the tree
S.Ducasse 35
LiteralGatherer
RBProgramNodeVisitor subclass: #LiteralGatherer
instanceVariableNames: 'literals'
classVariableNames: ''
poolDictionaries: ''
category: 'Compiler-AST-Visitors'
initialize
literals := Set new.
literals
^literals
acceptLiteralNode: aLiteralNode
literals add: aLiteralNode value.
(TestVisitor new visitNode: tree) literals
#(3 4)
S.Ducasse 36
Compiler III: IR
• IR: Intermediate Representation
• semantic like Bytecode, but more abstract
• independent of the bytecode set
• IR is a tree
• IR nodes allow easy transformation
• decompilation to RB AST
• IR build from AST using ASTTranslator:
• ASTVisitor
• uses IRBuilder
S.Ducasse 37
Compiler 4: Bytecode
• IR needs to be converted to Bytecode
• IRTranslator:Visitor for IR tree
• Uses BytecodeBuilder to generate Bytecode
• Builds a compiledMethod
testReturn1
| iRMethod aCompiledMethod |
iRMethod := IRBuilder new
numRargs: 1;
addTemps: #(self); "receiver and args declarations"
pushLiteral: 1;
returnTop;
ir.
aCompiledMethod := iRMethod compiledMethod.
self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 1].
S.Ducasse 38
Behavior
• Method Lookup
• Method Application
S.Ducasse 39
• Look on the receiver class (1)
• Follow inheritance link (2)
The Essence
Node
accept: aPacket
Workstation
originate: aPacket
aMac accept
(1)
(2)
S.Ducasse 40
doesNotUnderstand:
• When the lookup fails
• doesNotUnderstand: on the original message receiver
• reification of the message
• 2 zork
• leads to
• 2 doesNotUnderstand: aMessage
• aMessage selector -> #zork
S.Ducasse 41
Invoking a message from its name
• Object>>perform: aSymbol
• Object>>perform: aSymbol with: arg
• ...
• Asks an object to execute a message
• The method lookup is done!
• 5 factorial
• 5 perform: #factorial
S.Ducasse 42
Executing a compiled method
CompiledMethod>>valueWithReceiver:argument
s:
(Integer>>factorial)
valueWithReceiver: 5
arguments: #()
-> 120
No lookup is performed
S.Ducasse 43
Other Reflective Entities
• Execution stack can be reified and manipulated on
demand
• thisContext is a pseudo variable which gives access to
the stack
S.Ducasse 44
• We need a space for
• the temporary variables
• remembering where to return to
• Everything is an Object!
• So: we model this space as Objects
• Class MethodContext
What happens on Method
execution?
ContextPart variableSubclass: #MethodContext
instanceVariableNames: 'method receiverMap receiver'
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Methods'
S.Ducasse 45
MethodContext
• MethodContext holds all state associated with the
execution of a CompiledMethod
• Program Counter (pc, from ContextPart)
• the Method itself (method)
• Receiver (receiver) and the Sender (sender)
• The sender is the previous Context
• The chain of senders is a stack
• It grows and shrinks with activation/return
S.Ducasse 46
Contexts: Stack Reification
S.Ducasse 47
Example: #haltIf:
• You can’t put a halt in methods that are called often
(e.g. OrderedCollection>>add:)
• Idea: only halt if called from a method with a certain
name
haltIf: aSelector
| cntxt |
cntxt := thisContext.
[cntxt sender isNil] whileFalse: [
cntxt := cntxt sender.
(cntxt selector = aSelector) ifTrue: [
Halt signal
].
].
S.Ducasse 48
Controling Messages
S.Ducasse 49
Approaches to Control Message
• Error Handling Specialization
• Minimal Objects + doesNotUnderstand:
• Using Method Lookup
• anonymous classes between instances and their classes
• Method Substitution
• wrapping methods
• Control: instance-based/class/group
• Granularity: all/unknown/specific
S.Ducasse 50
Error Handling Specialization
• Minimal Object
• do not understand too much
• redefine doesNotUnderstand:
• wrap normal object in a minimal object
• nil superclass or ProtoObject
• use becomeForward: to substitute the object to
control
S.Ducasse 51
Minimal Object atWork
S.Ducasse 52
Control
• MinimalObject>>doesNotUnderstand: aMsg
• ...
• originalObject perform: aMsg selector
• withArguments: aMsg arguments
• ....
S.Ducasse 53
Minimal Behavior inVW
MinimalObject class>>initialize
superclass := nil.
#(doesNotUnderstand: error: isNil = ==̃ ̃
printString printOn: class inspect basicInspect
        basicAt: basicSize instVarAt: instVarAt:put:)
              do: [:selector |
self recompile: selector from: Object]
S.Ducasse 54
Limits
• self problem:
• messages sent by the object itself are not trapped
• messages sent to a reference on it passed by the
controlled object
• Class control is impossible
• Interpretation of minimal protocol:
• message sent to the minimal object or to controlled
object
S.Ducasse 55
Evaluation
• Simple
• In Squeak ProtoObject
• Some problems
• Instance-based
• All messages
S.Ducasse 56
Approaches to Control Message
• Error Handling Specialization
• Minimal Objects + doesNotUnderstand:
• Using Method Lookup
• anonymous classes between instances and their classes
• Method Substitution
• wrapping methods
S.Ducasse 57
UsingVM Lookup
• Creation of a controlling class that is interposed
between the instance and its class
• Definition of controlling methods
• Class change
• Hidding it from the developper/user using anonymous
class
S.Ducasse 58
1@1, 2@2 are controlled, but not 3@3
S.Ducasse 59
Anonymous class inVW
Object>>specialize
|nCl|
(1) nCl :=Behavior new
(2) setInstanceFormat: self class format;
(2) superclass: self class;
methodDictionary:MethodDictionary new.
(3) self changeClassToThatOf: nCl basicNew
S.Ducasse 60
Control
anAnonymousClass>>setX:t1setY:t2
...before
super setX:t1setY:t2
...after
S.Ducasse 61
The beauty inVisualWorks
AnonymousClass>>installEssentialMethods
self compile: ’class ˆ super class superclass’.
self compile: ’isControlled ˆ true’.
self compile: ’anonymousClass ˆ super class’
In Squeak class is not sent but optimized by the compiler
S.Ducasse 62
Evaluation
• instance-based or group-based
• selective control
• no identity problem
• good performance
• transparent to the user
• requires a bit of compilation (could be avoided using
clone as in Method Wrapper)
S.Ducasse 63
Approaches to Control Message
• Error Handling Specialization
• Minimal Objects + doesNotUnderstand:
• Using Method Lookup
• anonymous classes between instances and their classes
• Method Substitution
• wrapping methods
S.Ducasse 64
Method Substitution
• First approach: add methods with offucasted names
• but the user can see them
• Wrapping the methods without poluting the interface
S.Ducasse 65
MethodWrapper Definition
CompiledMethod variableSubclass: #MethodWrapper
instanceVariableNames: ’clientMethod selector’
classVariableNames: ’’
poolDictionaries:’’
category: ’Method Wrappers’
(MethodWrapper on: #color inClass: Point) install
S.Ducasse 66
Method Wrappers:The Idea
S.Ducasse 67
Mechanics
WrapperMethod>>valueWithReceiver: anObject arguments: args
self beforeMethod.
ˆ [clientMethod
valueWithReceiver: object
arguments: args]
valueNowOrOnUnwindDo:
[self afterMethod]
aClass>>originalSelector: t1
|t2|
(t2 := Array new: 1) at: 1 put: t1.
ˆself valueWithReceiver: self arguments: t2
S.Ducasse 68
Evaluation
• Class based: all instances are controlled
• Only known messages
• Single method can be controlled
• Smart implementation does not require compilation
for installation/removal
S.Ducasse 69
Scaffolding Patterns
• How to prototype applications even faster?
• Based on K.Auer Patterns
S.Ducasse 70
Patterns
• Extensible Attributes
• Artificial Delegation
• How do you prepare for additional delegated
operations?
• Cached Extensibility
• Selector Synthesis
S.Ducasse 71
Extensible Attributes
Context:
multi person project + heavy version control
other designers will want to add attributes to your class
How do you minimize the effort required to add
additional attributes to the class?
Solution:
Add a dictionary attribute to your class
+ a dictionary access
S.Ducasse 72
Extensible Attributes
anExtensibleObject attributes at: #attName put: value
value := anExtensibleObject attributes at: #attName
S.Ducasse 73
Artificial Accessors
Context: you applied Extensible Attributes
How do you make it easier for other classes to access
your extended attributes?
Solution: simulate the presence of accessor for the
attributes by specializing doesNotUnderstand:
S.Ducasse 74
Artificial Accessors Code
anExtensibleObject widgets: 4
is converted to
self attributes at: #widgets put: 4
anExtensibleObject widgets
is converted to
^ self attributes at: #widgets
S.Ducasse 75
Consequences
Accessors do not exist therefore
browsing can be a problem
tracing also
reflective queries (allSelectors, canUnderstand:....) will not
work as with plain methods
S.Ducasse 76
Artificial Delegation
How do you make
^ self delegate anOperation
easier?
Solution: Override doesNotUnderstand: of the delegator
to iterate through its attribute looking for an attribute
that supports the method selector that was not
understood
S.Ducasse 77
Cached Extensibility
Context: you used the previous patterns
How do you know which artificial accessors or artificial
delegate have been used?
Solution: Specialize doesNotUnderstand: to create
methods as soon as artificial ones are invoked
S.Ducasse 78
Selector Synthesis
How can you implement a state-dependent object with a
minimal effort?
Solution: define state and event as symbols and given a
pair synthesise a method selector
selector := ‘handle’, anEvent aString,‘In’, aState asString.
self perform: selector asSymbol.
S.Ducasse 79
References
• [Ducasse’99] S. Ducasse,“Message Passing Control Techniques in Smalltalk”,
JOOP, 1999
• [Rivard’96] F. Rivard, Smalltalk : a Reflective Language, REFLECTION'96,1996
• [Bran’98] Wrappers To The Rescue, ECOOP’98, 1998
• [Auer] Scaffolding patterns, PLOD 3,Addison-Wesley,
(http://www.rolemodelsoftware.com/moreAboutUs/publications/articles/scaffol
d.php)
• Smalltalk the Language, Golberg Robson,Addison-Wesley
S.Ducasse 80
Smalltalk Reflective Capabilities
Both introspection and reflection
Powerful
Based on everything is an object approach

More Related Content

What's hot

Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....AboutYouGmbH
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMax Kleiner
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max Kleiner
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt CommunicationAndreas Jakl
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals ThreadsNeera Mital
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?Kyle Oba
 
Inside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGInside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGSylvain Wallez
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java DevelopersMuhammad Abdullah
 
The dedexer disassembler
The dedexer disassemblerThe dedexer disassembler
The dedexer disassemblerGabor Paller
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrencyfeng lee
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConfJaroslaw Palka
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in PracticeAlina Dolgikh
 
Clockless design language - ilia greenblat
Clockless design language - ilia greenblatClockless design language - ilia greenblat
Clockless design language - ilia greenblatchiportal
 
Threading in iOS / Cocoa Touch
Threading in iOS / Cocoa TouchThreading in iOS / Cocoa Touch
Threading in iOS / Cocoa Touchmobiledeveloperpl
 

What's hot (20)

Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
Runtime
RuntimeRuntime
Runtime
 
Qt Application Programming with C++ - Part 2
Qt Application Programming with C++ - Part 2Qt Application Programming with C++ - Part 2
Qt Application Programming with C++ - Part 2
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
Cp7 rpc
Cp7 rpcCp7 rpc
Cp7 rpc
 
Inside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGInside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUG
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
The dedexer disassembler
The dedexer disassemblerThe dedexer disassembler
The dedexer disassembler
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Effective java - concurrency
Effective java - concurrencyEffective java - concurrency
Effective java - concurrency
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConf
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Clockless design language - ilia greenblat
Clockless design language - ilia greenblatClockless design language - ilia greenblat
Clockless design language - ilia greenblat
 
Threading in iOS / Cocoa Touch
Threading in iOS / Cocoa TouchThreading in iOS / Cocoa Touch
Threading in iOS / Cocoa Touch
 

Viewers also liked (20)

15 - Streams
15 - Streams15 - Streams
15 - Streams
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
10 reflection
10 reflection10 reflection
10 reflection
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
12 - Conditions and Loops
12 - Conditions and Loops12 - Conditions and Loops
12 - Conditions and Loops
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 

Similar to Stoop 305-reflective programming5

Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOLMicro Focus
 
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...Big Data Value Association
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)lennartkats
 
walkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventionswalkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventionswalkmod
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개Reagan Hwang
 
Introduction to Murasaki
Introduction to MurasakiIntroduction to Murasaki
Introduction to MurasakiSeiichi Horie
 
Introducción a Stream Processing utilizando Kafka Streams
Introducción a Stream Processing utilizando Kafka StreamsIntroducción a Stream Processing utilizando Kafka Streams
Introducción a Stream Processing utilizando Kafka Streamsconfluent
 
Development workflow guide for building docker apps
Development workflow guide for building docker appsDevelopment workflow guide for building docker apps
Development workflow guide for building docker appsAbdul Khan
 
Development workflow guide for building docker apps
Development workflow guide for building docker appsDevelopment workflow guide for building docker apps
Development workflow guide for building docker appsAbdul Khan
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NETDoommaker
 
Umbraco OktoberFest 2014
Umbraco OktoberFest 2014Umbraco OktoberFest 2014
Umbraco OktoberFest 2014Jeavon Leopold
 
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.Hakky St
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...AboutYouGmbH
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)The World of Smalltalk
 

Similar to Stoop 305-reflective programming5 (20)

Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOL
 
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...
BDVe Webinar Series - Toreador Intro - Designing Big Data pipelines (Paolo Ce...
 
C++ Boot Camp Part 2
C++ Boot Camp Part 2C++ Boot Camp Part 2
C++ Boot Camp Part 2
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
 
walkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventionswalkmod: An open source tool for coding conventions
walkmod: An open source tool for coding conventions
 
Containerization using docker and its applications
Containerization using docker and its applicationsContainerization using docker and its applications
Containerization using docker and its applications
 
Containerization using docker and its applications
Containerization using docker and its applicationsContainerization using docker and its applications
Containerization using docker and its applications
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개
 
Introduction to Murasaki
Introduction to MurasakiIntroduction to Murasaki
Introduction to Murasaki
 
Typesafe spark- Zalando meetup
Typesafe spark- Zalando meetupTypesafe spark- Zalando meetup
Typesafe spark- Zalando meetup
 
Introducción a Stream Processing utilizando Kafka Streams
Introducción a Stream Processing utilizando Kafka StreamsIntroducción a Stream Processing utilizando Kafka Streams
Introducción a Stream Processing utilizando Kafka Streams
 
Development workflow guide for building docker apps
Development workflow guide for building docker appsDevelopment workflow guide for building docker apps
Development workflow guide for building docker apps
 
Development workflow guide for building docker apps
Development workflow guide for building docker appsDevelopment workflow guide for building docker apps
Development workflow guide for building docker apps
 
What’s new in .NET
What’s new in .NETWhat’s new in .NET
What’s new in .NET
 
Refactoring
RefactoringRefactoring
Refactoring
 
Umbraco OktoberFest 2014
Umbraco OktoberFest 2014Umbraco OktoberFest 2014
Umbraco OktoberFest 2014
 
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.
Creating basic workflows as Jupyter Notebooks to use Cytoscape programmatically.
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 

More from The World of Smalltalk (19)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Recently uploaded

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 

Recently uploaded (20)

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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 

Stoop 305-reflective programming5

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@inria.fr http://stephane.ducasse.free.fr/ Reflective Programming in Smalltalk M. Denker and S. Ducasse - 2005 Introspection Metaclasses
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Why... “As a programming language becomes higher and higher level, its implementation in terms of underlying machine involves more and more tradeoffs, on the part of the implementor, about what cases to optimize at the expense of what other cases.... the ability to cleanly integrate something outside of the language’s scope becomes more and more limited” [Kiczales’92a]
  • 4. S.Ducasse 4 Definition “Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution.There are two aspects of such manipulation: introspection and intercession. Introspection is the ability for a program to observe and therefore reason about its own state. Intercessory is the ability for a program to modify its own execution state or alter its own interpretation or meaning. Both aspects require a mechanism for encoding execution state as data: providing such an encoding is called reification.” [Bobrow, Gabriel and White in Paepke‘92]
  • 5. S.Ducasse 5 • A system having itself as application domain and that is causally connected with this domain can be qualified as a reflective system [Pattie Maes] • A reflective system has an internal representation of itself. • A reflective system is able to act on itself with the ensurance that its representation will be causally connected (up to date). • A reflective system has some static capacity of self- representation and dynamic self-modification in constant synchronization Consequences
  • 6. S.Ducasse 6 • The meta-language and the language can be different: Scheme and an OO language • The meta-language and the language can be same: Meta Programming in Prog. Language
  • 8. S.Ducasse 8 The Essence of a Class • A format (number of instance variables and types) • A superclass • A method dictionary
  • 9. S.Ducasse 9 Behavior >> new In Squeak (3.8) Behavior>>new | classInstance | classInstance := self basicNew. classInstance methodDictionary: classInstance emptyMethodDictionary. classInstance superclass: Object. classInstance setFormat: Object format. ^ classInstance
  • 10. S.Ducasse 10 The Essence of an Object • class pointer • values • Can be special: • the pointer pointing to the object is the object itself • character, smallInteger (compact classes)
  • 11. S.Ducasse 11 Some MetaObjects • Structure: Behavior, ClassDescription, Class, Metaclass, ClassBuilder • Semantics: Compiler, Decompiler, ProgramNode, ProgramNodeBuilder, IRBuilder • Behavior: CompiledMethod, CompiledBlock, Message, Exception • ControlState: Context, BlockContext, Process, ProcessorScheduler • Resources: ObjectMemory,WeakArray • Naming: SystemDictionary, Namespace • Libraries: MethodDictionary, ClassOrganizer
  • 12. S.Ducasse 12 Meta-Operations • MetaOperations are operations that provide information about an object as opposed to information directly contained by the object ...They permit things to be done that are not normally possible [Inside Smalltalk]”
  • 13. S.Ducasse 13 Access • Object>>instVarAt: aNumber • Object>>instVarNamed: aString • Object>>instVarAt: aNumber put: anObject • Browser new instVarNamed: 'classOrganizer' • | pt | • pt := 10@3. • pt instVarNamed: 'x' put: 33. • pt • > 33@3
  • 15. S.Ducasse 15 Changes • Object>>changeClassOfThat: anInstance • inVW and Squeak both classes should have the same format, i.e., the same physical structure of their instances • Object>>become: anotherObject • Object>>becomeForward: anotherObject
  • 16. S.Ducasse 16 Implementing Instance Specific Methods In Squeak 3.8 | behavior browser | behavior := Behavior new. behavior superclass: Browser. behavior setFormat: Browser format. browser := Browser new. browser primitiveChangeClassTo: behavior new. behavior compile: 'thisIsATest ^ 2'. self assert: browser thisIsATest = 2. self should: [Browser new thisIsATest] raise: MessageNotUnderstood
  • 17. S.Ducasse 17 become: and oneWayBecome: • become: is symmetric and swaps all the pointers • oneWayBecome: (inVW) becomeForward: (Squeak) changes pointers only in one way
  • 18. S.Ducasse 18 become: • Swap all the pointers from one object to the other and back (symmetric) • | pt1 pt2 pt3 | • pt1 := 0@0. • pt2 := pt1. • pt3 := 100@100. • pt1 become: pt3. • self assert: pt2 = (100@100). • self assert: pt3 = (0@0). • self assert: pt1 = (100@100).
  • 19. S.Ducasse 19 becomeForward: • Swap all the pointers from one object to the other one • | pt1 pt2 pt3 | • pt1 := 0@0. • pt2 := pt1. • pt3 := 100@100. • pt1 becomeForward: pt3. • self assert: (pt2 = (100@100)). • self assert: pt3 = pt2. • self assert: pt1 = (100@100)
  • 20. S.Ducasse 20 Structure • Objects represent classes • Object root of inheritance • default behavior • minimal behavior • Behavior: essence of class • anymous class • format, methodDict, superclass • ClassDescription: • human representation and organization • Metaclass: • sole instance
  • 22. S.Ducasse 22 ClassBuilder • Manages class creation • unique instance • format with superclass checking • changes of existing instance when class structure changes
  • 23. S.Ducasse 23 Some Selected Protocols • Illustrated by the tools of the IDE • Class>>selectors • Class>>superclass • Class>>compiledMethodAt: aSymbol • Class>>instVarNames • Class>>compiler
  • 25. S.Ducasse 25 Compiler • Fully reified compilation process: • Scanner/Parser (build with SmaCC) • builds AST (from Refactoring Browser) • Semantic Analysis: ASTChecker • annotates the AST (e.g., var bindings) • Translation to IR: ASTTranslator • uses IRBuilder to build IR (Intermediate Representation) • Bytecode generation: IRTranslator • uses BytecodeBuilder to emit bytecodes
  • 26. S.Ducasse 26 Compiler: Overview Scanner / Parser Semantic Analysis Code Generation Build IR SmaCC Scanner Parser ASTChecker ASTTranslator IRBuilder IRTranslator BytecodeBuilder Bytecode Generation AST AST BytecodeCode IRAST Bytecode Code generation in detail:
  • 27. S.Ducasse 27 Compiler: Syntax • SmaCC: Smalltalk Compiler Compiler • Like Lex/Yacc • Input: • scanner definition: Regular Expressions • parser: BNF Like Grammar • code that build AST as annotation • SmaCC can build LARL(1) or LR(1) parser • Output: • class for Scanner (subclass SmaCCScanner) • class for Parser (subclass SmaCCParser)
  • 31. S.Ducasse 31 Compiler:AST • AST:Abstract Syntax Tree • Encodes the Syntax as a Tree • No semantics yet! • Uses the RB Tree: • visitors • backward pointers in ParseNodes • transformation (replace/add/delete) • pattern directed TreeRewriter • PrettyPrinter RBProgramNode RBDoItNode RBMethodNode RBReturnNode RBSequenceNode RBValueNode RBArrayNode RBAssignmentNode RBBlockNode RBCascadeNode RBLiteralNode RBMessageNode RBOptimizedNode RBVariableNode
  • 32. S.Ducasse 32 Compiler: Semantics • We need to analyse the AST • names need to be linked to theVariables according to the scoping rules • ASTChecker implemented as a visitor • subclass of RBProgramNodeVisitor • visits the nodes • grows and shrinks Scope chain • method/Blocks are linked with the Scope • variable definitions and references are linked with objects describing the variables
  • 34. S.Ducasse 34 A SimpleVisitor • RBProgramNodeVisitor new visitNode: tree. • does nothing except walking throw the tree
  • 35. S.Ducasse 35 LiteralGatherer RBProgramNodeVisitor subclass: #LiteralGatherer instanceVariableNames: 'literals' classVariableNames: '' poolDictionaries: '' category: 'Compiler-AST-Visitors' initialize literals := Set new. literals ^literals acceptLiteralNode: aLiteralNode literals add: aLiteralNode value. (TestVisitor new visitNode: tree) literals #(3 4)
  • 36. S.Ducasse 36 Compiler III: IR • IR: Intermediate Representation • semantic like Bytecode, but more abstract • independent of the bytecode set • IR is a tree • IR nodes allow easy transformation • decompilation to RB AST • IR build from AST using ASTTranslator: • ASTVisitor • uses IRBuilder
  • 37. S.Ducasse 37 Compiler 4: Bytecode • IR needs to be converted to Bytecode • IRTranslator:Visitor for IR tree • Uses BytecodeBuilder to generate Bytecode • Builds a compiledMethod testReturn1 | iRMethod aCompiledMethod | iRMethod := IRBuilder new numRargs: 1; addTemps: #(self); "receiver and args declarations" pushLiteral: 1; returnTop; ir. aCompiledMethod := iRMethod compiledMethod. self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 1].
  • 38. S.Ducasse 38 Behavior • Method Lookup • Method Application
  • 39. S.Ducasse 39 • Look on the receiver class (1) • Follow inheritance link (2) The Essence Node accept: aPacket Workstation originate: aPacket aMac accept (1) (2)
  • 40. S.Ducasse 40 doesNotUnderstand: • When the lookup fails • doesNotUnderstand: on the original message receiver • reification of the message • 2 zork • leads to • 2 doesNotUnderstand: aMessage • aMessage selector -> #zork
  • 41. S.Ducasse 41 Invoking a message from its name • Object>>perform: aSymbol • Object>>perform: aSymbol with: arg • ... • Asks an object to execute a message • The method lookup is done! • 5 factorial • 5 perform: #factorial
  • 42. S.Ducasse 42 Executing a compiled method CompiledMethod>>valueWithReceiver:argument s: (Integer>>factorial) valueWithReceiver: 5 arguments: #() -> 120 No lookup is performed
  • 43. S.Ducasse 43 Other Reflective Entities • Execution stack can be reified and manipulated on demand • thisContext is a pseudo variable which gives access to the stack
  • 44. S.Ducasse 44 • We need a space for • the temporary variables • remembering where to return to • Everything is an Object! • So: we model this space as Objects • Class MethodContext What happens on Method execution? ContextPart variableSubclass: #MethodContext instanceVariableNames: 'method receiverMap receiver' classVariableNames: '' poolDictionaries: '' category: 'Kernel-Methods'
  • 45. S.Ducasse 45 MethodContext • MethodContext holds all state associated with the execution of a CompiledMethod • Program Counter (pc, from ContextPart) • the Method itself (method) • Receiver (receiver) and the Sender (sender) • The sender is the previous Context • The chain of senders is a stack • It grows and shrinks with activation/return
  • 47. S.Ducasse 47 Example: #haltIf: • You can’t put a halt in methods that are called often (e.g. OrderedCollection>>add:) • Idea: only halt if called from a method with a certain name haltIf: aSelector | cntxt | cntxt := thisContext. [cntxt sender isNil] whileFalse: [ cntxt := cntxt sender. (cntxt selector = aSelector) ifTrue: [ Halt signal ]. ].
  • 49. S.Ducasse 49 Approaches to Control Message • Error Handling Specialization • Minimal Objects + doesNotUnderstand: • Using Method Lookup • anonymous classes between instances and their classes • Method Substitution • wrapping methods • Control: instance-based/class/group • Granularity: all/unknown/specific
  • 50. S.Ducasse 50 Error Handling Specialization • Minimal Object • do not understand too much • redefine doesNotUnderstand: • wrap normal object in a minimal object • nil superclass or ProtoObject • use becomeForward: to substitute the object to control
  • 52. S.Ducasse 52 Control • MinimalObject>>doesNotUnderstand: aMsg • ... • originalObject perform: aMsg selector • withArguments: aMsg arguments • ....
  • 53. S.Ducasse 53 Minimal Behavior inVW MinimalObject class>>initialize superclass := nil. #(doesNotUnderstand: error: isNil = ==̃ ̃ printString printOn: class inspect basicInspect         basicAt: basicSize instVarAt: instVarAt:put:)               do: [:selector | self recompile: selector from: Object]
  • 54. S.Ducasse 54 Limits • self problem: • messages sent by the object itself are not trapped • messages sent to a reference on it passed by the controlled object • Class control is impossible • Interpretation of minimal protocol: • message sent to the minimal object or to controlled object
  • 55. S.Ducasse 55 Evaluation • Simple • In Squeak ProtoObject • Some problems • Instance-based • All messages
  • 56. S.Ducasse 56 Approaches to Control Message • Error Handling Specialization • Minimal Objects + doesNotUnderstand: • Using Method Lookup • anonymous classes between instances and their classes • Method Substitution • wrapping methods
  • 57. S.Ducasse 57 UsingVM Lookup • Creation of a controlling class that is interposed between the instance and its class • Definition of controlling methods • Class change • Hidding it from the developper/user using anonymous class
  • 58. S.Ducasse 58 1@1, 2@2 are controlled, but not 3@3
  • 59. S.Ducasse 59 Anonymous class inVW Object>>specialize |nCl| (1) nCl :=Behavior new (2) setInstanceFormat: self class format; (2) superclass: self class; methodDictionary:MethodDictionary new. (3) self changeClassToThatOf: nCl basicNew
  • 61. S.Ducasse 61 The beauty inVisualWorks AnonymousClass>>installEssentialMethods self compile: ’class ˆ super class superclass’. self compile: ’isControlled ˆ true’. self compile: ’anonymousClass ˆ super class’ In Squeak class is not sent but optimized by the compiler
  • 62. S.Ducasse 62 Evaluation • instance-based or group-based • selective control • no identity problem • good performance • transparent to the user • requires a bit of compilation (could be avoided using clone as in Method Wrapper)
  • 63. S.Ducasse 63 Approaches to Control Message • Error Handling Specialization • Minimal Objects + doesNotUnderstand: • Using Method Lookup • anonymous classes between instances and their classes • Method Substitution • wrapping methods
  • 64. S.Ducasse 64 Method Substitution • First approach: add methods with offucasted names • but the user can see them • Wrapping the methods without poluting the interface
  • 65. S.Ducasse 65 MethodWrapper Definition CompiledMethod variableSubclass: #MethodWrapper instanceVariableNames: ’clientMethod selector’ classVariableNames: ’’ poolDictionaries:’’ category: ’Method Wrappers’ (MethodWrapper on: #color inClass: Point) install
  • 67. S.Ducasse 67 Mechanics WrapperMethod>>valueWithReceiver: anObject arguments: args self beforeMethod. ˆ [clientMethod valueWithReceiver: object arguments: args] valueNowOrOnUnwindDo: [self afterMethod] aClass>>originalSelector: t1 |t2| (t2 := Array new: 1) at: 1 put: t1. ˆself valueWithReceiver: self arguments: t2
  • 68. S.Ducasse 68 Evaluation • Class based: all instances are controlled • Only known messages • Single method can be controlled • Smart implementation does not require compilation for installation/removal
  • 69. S.Ducasse 69 Scaffolding Patterns • How to prototype applications even faster? • Based on K.Auer Patterns
  • 70. S.Ducasse 70 Patterns • Extensible Attributes • Artificial Delegation • How do you prepare for additional delegated operations? • Cached Extensibility • Selector Synthesis
  • 71. S.Ducasse 71 Extensible Attributes Context: multi person project + heavy version control other designers will want to add attributes to your class How do you minimize the effort required to add additional attributes to the class? Solution: Add a dictionary attribute to your class + a dictionary access
  • 72. S.Ducasse 72 Extensible Attributes anExtensibleObject attributes at: #attName put: value value := anExtensibleObject attributes at: #attName
  • 73. S.Ducasse 73 Artificial Accessors Context: you applied Extensible Attributes How do you make it easier for other classes to access your extended attributes? Solution: simulate the presence of accessor for the attributes by specializing doesNotUnderstand:
  • 74. S.Ducasse 74 Artificial Accessors Code anExtensibleObject widgets: 4 is converted to self attributes at: #widgets put: 4 anExtensibleObject widgets is converted to ^ self attributes at: #widgets
  • 75. S.Ducasse 75 Consequences Accessors do not exist therefore browsing can be a problem tracing also reflective queries (allSelectors, canUnderstand:....) will not work as with plain methods
  • 76. S.Ducasse 76 Artificial Delegation How do you make ^ self delegate anOperation easier? Solution: Override doesNotUnderstand: of the delegator to iterate through its attribute looking for an attribute that supports the method selector that was not understood
  • 77. S.Ducasse 77 Cached Extensibility Context: you used the previous patterns How do you know which artificial accessors or artificial delegate have been used? Solution: Specialize doesNotUnderstand: to create methods as soon as artificial ones are invoked
  • 78. S.Ducasse 78 Selector Synthesis How can you implement a state-dependent object with a minimal effort? Solution: define state and event as symbols and given a pair synthesise a method selector selector := ‘handle’, anEvent aString,‘In’, aState asString. self perform: selector asSymbol.
  • 79. S.Ducasse 79 References • [Ducasse’99] S. Ducasse,“Message Passing Control Techniques in Smalltalk”, JOOP, 1999 • [Rivard’96] F. Rivard, Smalltalk : a Reflective Language, REFLECTION'96,1996 • [Bran’98] Wrappers To The Rescue, ECOOP’98, 1998 • [Auer] Scaffolding patterns, PLOD 3,Addison-Wesley, (http://www.rolemodelsoftware.com/moreAboutUs/publications/articles/scaffol d.php) • Smalltalk the Language, Golberg Robson,Addison-Wesley
  • 80. S.Ducasse 80 Smalltalk Reflective Capabilities Both introspection and reflection Powerful Based on everything is an object approach