Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reflective Programming in Smalltalk eXterme Metaprogramming

Similar presentations


Presentation on theme: "Reflective Programming in Smalltalk eXterme Metaprogramming"— Presentation transcript:

1 Reflective Programming in Smalltalk eXterme Metaprogramming
Brian Foote University of Illinois at Urbana-Champaign 7 May 1998 Smalltalk Solutions ‘98 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

2 Reflective Programming in Smalltalk
Definitions Building a Language Out of Objects Examples 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

3 Frequently Asked Questions
Q: What is reflection? A: It’s about building your language out of first-class, dynamic objects that you can look at and change at runtime. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

4 Frequently Asked Questions
Q: Haven’t people really been doing this for a long time? A: They certainly have. Smalltalk programmers have been doing this since the ‘70s. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

5 Reflection from the Eisenhower Era
Hence, [the machine] can, in particular, change the orders (since these are in memory!)--the very orders that control its actions --John Von Neumann 1958 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

6 Frequently Asked Questions
Q: What about efficiency? A: 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

7 Frequently Asked Questions
Q: What about efficiency? A: There are a variety of techniques that can be used to make reflective systems faster. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

8 Frequently Asked Questions
Q: Isn’t reflection dangerous? A: Yes! You bet it is! A: Yes, if you are not careful. A: Yes, but you can make it safer. A: Yes, but so is crossing the street. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

9 Frequently Asked Questions
Q: Isn’t reflection just a fancy name for a few clever hacks? A: Well, that and so much more... 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

10 Frequently Asked Questions
Q: Why is so much of the reflection literature so hard to read? A: This is due, in part, to the area’s AI heritage... 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

11 Reflective Terminology
introspection reflection reification 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

12 Reflective Terminology
infinite regress causal connection reflective tower 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

13 Introspection When a program can look at at the objects from which it is built Smalltalk has a rich, comprehensive, indeed, unrivaled collection of introspective facilities 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

14 Reflection When a program can change the objects from which it is built Smalltalk represents as much as it can as as first-class dynamic objects, and you can change them at runtime. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

15 Meta considered harmful “Meta” considered harmful
Consider these Meta considered harmful “Meta” considered harmful 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

16 Reflective Programming in Smalltalk
Neat Hack Hall of Fame Parentless Objects Does Not Understand Metaobjects Lightweight Classes Method Wrappers Byte Code Manipulation Compiled Method Copying Context Manipulation Source Generation Association Hacks Becomes Class Change Instance Variable At Methods On-Demand 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

17 Reflective Programming in Smalltalk
Objects themselves Object 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

18 Reflective Programming in Smalltalk
Object Introspection Object size == basicSize hash identityHash printOn: storeOn: dependents ... allOwners firstOwner nextInstance ownerAfter: instVarAt: isKindOf: class isMemberOf: respondsTo: 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

19 Example: AccessibleObjects
Demonstrates: doesNotUnderstand: instVarAt: instVarAt:put: at: at:put: 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

20 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject class methods for: examples example "AccessibleObject example" | temp | temp := AccessibleObject new. temp dog: 'Fido'. temp cat: 'Tabby'. Transcript print: temp dog; cr. Transcript print: temp items; cr. temp keysDo: [:key | Transcript print: key; cr]. Transcript print: (temp variableAt: #items); cr. Transcript endEntry 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

21 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject methods for: accessing at: key "Return the object associated with the given key..." ^self valueAt: key at: key put: value "Store the indicated value at the designated place in our item dictionary... " ^self valueAt: key put: value size "Let's say our size is the size of our item dictionary plus our number of instance variables..." ^self items size + self instVarNames size 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

22 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject methods for: accessing valueAt: key "Return the object associated with the given key..." ^self valueAt: key ifAbsent: [self errorKeyNotFound] valueAt: key put: value "Store the indicated value at the designated place in our item dictionary, unless there is an instance var by that name..." items isNil ifTrue: [items := IdentityDictionary new: 16]. (self hasVariableNamed: key) ifTrue: [^self variableAt: key put: value] ifFalse: [^items at: key put: value] 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

23 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject methods for: instance variable access allInstVarNames "Define a shorthand for this class method..." ^self class allInstVarNames hasVariableNamed: name "Say whether we have a variable by the given name..." ^(self variableIndex: name) ~= 0 instVarNames ^self class instVarNames 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

24 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject methods for: instance variable access variableAt: name "Return the named value..." | index | index := self variableIndex: name. index = 0 ifTrue: [self error: 'Bad instance variable name...']. ^self instVarAt: index variableAt: name put: value "Set the named instance variable to the indicated value..." ^self instVarAt: index put: value variableIndex: name "Return the instance variable index for this name, or zero..." ^self class allInstVarNames indexOf: name asString. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

25 Reflective Programming in Smalltalk
Accessible Objects AccessibleObject methods for: error interception doesNotUnderstand: aMessage "Refer messages we don't understand to our item dictionary..." | selector name args | selector := aMessage selector. name := (selector copyWithout: $:) asSymbol. args := aMessage arguments. (self hasVariableNamed: name) ifTrue: [args size = 0 ifTrue: [^self variableAt: name] ifFalse: [^self variableAt: name put: (args at: 1)]]. (items respondsTo: selector) ifTrue: [^items perform: selector withArguments: args]. args size = 1 ifTrue: [^self valueAt: name put: (args at: 1)] ifFalse: [^self valueAt: name ifAbsent: [^super doesNotUnderstand: aMessage]] 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

26 Behavior ClassDescription Class Metaclass
Classes and Behavior Behavior ClassDescription Class Metaclass 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

27 ClassBuilder SystemOrganizer ClassOrganizer ClassCategoryReader
Organizations ClassBuilder SystemOrganizer ClassOrganizer ClassCategoryReader 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

28 MethodDictionary CompiledMethod ByteArray BlockClosure
Code Representation MethodDictionary CompiledMethod ByteArray BlockClosure 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

29 Context MethodContext/BlockContext Message MessageSend
Runtime Enviroment Context MethodContext/BlockContext Message MessageSend 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

30 Event Exception Signal SignalHandler SignalCollection HandlerList
Exceptions and Events Event Exception Signal SignalHandler SignalCollection HandlerList 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

31 Process ProcessScheduler Semaphore SharedQueue
Process Scheduling Process ProcessScheduler Semaphore SharedQueue 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

32 Debugger Decompiler Inspector ChangeList <Browsers>
Viewing the Program Debugger Decompiler Inspector ChangeList <Browsers> 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

33 Reflective Programming in Smalltalk
Making a Promise Future methodsFor: ‘demonstration’ demo | f | f := Future promising: [2+2]. f printString '4.0' Future class methodsFor: 'instance creation' promising: aBlock | aFuture | aFuture := self new. ^aFuture promising: aBlock 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

34 Reflective Programming in Smalltalk
Creating an Orphan nil subclass: #Future instanceVariableNames: ‘semaphore ' classVariableNames: ‘ ' poolDictionaries: ' ' category: ‘Reflection-Examples’ In VisualWorks, ClassBuilder does the rest. Default implementations of doesNotUnderstand: and class are provided. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

35 Reflective Programming in Smalltalk
We’ll do it eventually... Future methodsFor: 'initialization/dispatching' promising: aBlock "Create a semaphore, and fork a block that will signal it. The result of this block is stored in result..." semaphore := Semaphore new. [result := aBlock value. semaphore signal] fork. ^self 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

36 Reflective Programming in Smalltalk
Keeping a Promise doesNotUndertand: aMessage "If this is our init message, let it by..." aMessage selector == #promising: ifTrue: [^super perform: aMessage selector withArguments: aMessage arguments]. "Wait until our result is available..." semaphore wait. "If our result is a SmallInteger, it has no oop.." (result isKindOf: SmallInteger) ifTrue: [result := result asFloat]. "Become the result and do the deferred message..." result become: self. ^self perform: aMessage selector withArguments: aMessage arguments 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

37 Metaobjects and Lightweight Classes
11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

38 Compiler Decompiler CodeRegenerator Scanner SmalltalkCompiler
Compiler Classes Compiler Decompiler CodeRegenerator Scanner SmalltalkCompiler 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

39 Compiler Support CodeStream DefineOpcodePool MethodNodeHolder
ProgramNodeEnumerator ScannerTable <more> 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

40 Reflective Programming in Smalltalk
Variables and Scopes ArgumentVariable InstanceVariable LocalScope LocalVariable NameScope NullScope PseudoVariable ReceiverVariable RemoteVariable StaticScope StaticVariable TemporaryVariable UndeclaredVariable VariableDefinition 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

41 Reflective Programming in Smalltalk
Parse Tree Nodes ProgramNode MethodNode ParameterNode StatementNode ReturnNode ValueNode ArithmeticLoopNode AssignmentNode CascadeNode ConditionalNode LeafNode BlockNode LiteralNode VariableNode LoopNode SequenceNode SimpleMessageNode MessageNode 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

42 Smalltalk SystemDictionary Association/VariableBinding
<class and pool variables> 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

43 ObjectMemory MemoryPolicy WeakArray WeakDictionary
Storage and Garbage ObjectMemory MemoryPolicy WeakArray WeakDictionary 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

44 Levels of Representation
Source Compiler Parse Node ProgramNode ProgramNode Byte Code VM Decompiler Native Code 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

45 Reflective Programming in Smalltalk
Ways to Wrap Source Code Modifications Byte Code Modifications New Selectors Dispatching Wrappers Class Wrappers Instance Wrappers Method Wrappers 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

46 Reflective Programming in Smalltalk
Compiled Methods 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

47 Reflective Programming in Smalltalk
Method Wrappers 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

48 Reflective Programming in Smalltalk
Method Wrappers valueWithReceiver: anObject arguments: args self beforeMethod. ^[clientMethod valueWithReceiver: anObject arguments: args] valueNowOrOnUnwindDo: [self afterMethod] originalMethodName: argument ^#() receiver: self value: argument 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

49 Reflective Programming in Smalltalk
Multimethods OptimizingVisitor>>visitWithNode: aNode <ParseNode> ^self value optimized OptimizingVisitor>> visitWithNode: aNode <VariableNode> ^aNode lookupIn: self symbolTable 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

50 Reflective Programming in Smalltalk
Parse Tree 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

51 Power Safety Efficiency Comprehensibility
Issues Power Safety Efficiency Comprehensibility 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

52 Reflection in Smalltalk
Astonishing Introspective Highly Reflective Intelligently Factored Uniquely Malleable Unappoligetically Dynamic 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

53 Frequently Asked Questions
Q: What can’t I get my hands on? A: In ‘89 we said: sending a message, receiving a message, returns, and instance variable access. Oh, and the ObjectMemory... Progress has been made. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

54 Metalevel Architecture
Building languages out of objects allows one to achieve the goals of reflection Loops, Lazy Reification, and Induction Where does the language end? 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

55 Reflective Programming in Smalltalk
Go Ye Forth and Hack The best way to learn how do reflective programming in Smalltalk is to read code and experiment. 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk

56 Contact Information Brian Foote Dept. of Computer Science 3253 DCL 1304 W. Springfield Urbana, IL 61801 (217) 11/18/2018 (C) Brian Foote 1998 Reflective Programming in Smalltalk


Download ppt "Reflective Programming in Smalltalk eXterme Metaprogramming"

Similar presentations


Ads by Google