Reflective Programming in Smalltalk e Xterme Metaprogramming Brian
Reflective Programming in Smalltalk e. Xterme Metaprogramming Brian Foote University of Illinois at Urbana-Champaign 7 May 1998 Smalltalk Solutions ‘ 98 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 1
Reflective Programming in Smalltalk • Definitions • Building a Language Out of Objects • Examples 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 2
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. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 3
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 ‘ 70 s. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 4
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 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 5
Frequently Asked Questions Q: What about efficiency? A: 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 6
Frequently Asked Questions Q: What about efficiency? A: There a variety of techniques that can be used to make reflective systems faster. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 7
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. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 8
Frequently Asked Questions Q: Isn’t reflection just a fancy name for a few clever hacks? A: Well, that and so much more. . . 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 9
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. . . 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 10
Reflective Terminology • introspection • reflection • reification 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 11
Reflective Terminology • infinite regress • causal connection • reflective tower 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 12
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 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 13
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. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 14
Consider these Meta considered harmful “Meta” considered harmful 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 15
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 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 16
Objects themselves Object 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 17
Object Introspection Object size == basic. Size hash identity. Hash print. On: store. On: dependents. . . all. Owners first. Owner next. Instance owner. After: inst. Var. At: is. Kind. Of: class is. Member. Of: responds. To: 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 18
Example: Accessible. Objects Demonstrates: does. Not. Understand: inst. Var. At: put: at: put: 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 19
Accessible Objects Accessible. Object class methods for: examples example "Accessible. Object example" | temp : = Accessible. Object new. temp dog: 'Fido'. temp cat: 'Tabby'. Transcript print: temp dog; cr. Transcript print: temp items; cr. temp keys. Do: [: key | Transcript print: key; cr]. Transcript print: (temp variable. At: #items); cr. Transcript end. Entry 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 20
Accessible Objects Accessible. Object methods for: accessing at: key "Return the object associated with the given key. . . " ^self value. At: key at: key put: value "Store the indicated value at the designated place in our item dictionary. . . " ^self value. At: 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 inst. Var. Names size 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 21
Accessible Objects Accessible. Object methods for: accessing value. At: key "Return the object associated with the given key. . . " ^self value. At: key if. Absent: [self error. Key. Not. Found] value. At: 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 is. Nil if. True: [items : = Identity. Dictionary new: 16]. (self has. Variable. Named: key) if. True: [^self variable. At: key put: value] if. False: [^items at: key put: value] 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 22
Accessible Objects Accessible. Object methods for: instance variable access all. Inst. Var. Names "Define a shorthand for this class method. . . " ^self class all. Inst. Var. Names has. Variable. Named: name "Say whether we have a variable by the given name. . . " ^(self variable. Index: name) ~= 0 inst. Var. Names "Define a shorthand for this class method. . . " ^self class inst. Var. Names 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 23
Accessible Objects Accessible. Object methods for: instance variable access variable. At: name "Return the named value. . . " | index : = self variable. Index: name. index = 0 if. True: [self error: 'Bad instance variable name. . . ']. ^self inst. Var. At: index variable. At: name put: value "Set the named instance variable to the indicated value. . . " | index : = self variable. Index: name. index = 0 if. True: [self error: 'Bad instance variable name. . . ']. ^self inst. Var. At: index put: value variable. Index: name "Return the instance variable index for this name, or zero. . . " ^self class all. Inst. Var. Names index. Of: name as. String. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 24
Accessible Objects Accessible. Object methods for: error interception does. Not. Understand: a. Message "Refer messages we don't understand to our item dictionary. . . " | selector name args | selector : = a. Message selector. name : = (selector copy. Without: $: ) as. Symbol. args : = a. Message arguments. (self has. Variable. Named: name) if. True: [args size = 0 if. True: [^self variable. At: name] if. False: [^self variable. At: name put: (args at: 1)]]. (items responds. To: selector) if. True: [^items perform: selector with. Arguments: args]. args size = 1 if. True: [^self value. At: name put: (args at: 1)] if. False: [^self value. At: name if. Absent: [^super does. Not. Understand: a. Message]] 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 25
Classes and Behavior Class. Description Class Metaclass 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 26
Organizations Class. Builder System. Organizer Class. Category. Reader 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 27
Code Representation Method. Dictionary Compiled. Method Byte. Array Block. Closure 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 28
Runtime Enviroment Context Method. Context/Block. Context Message. Send 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 29
Exceptions and Events Event Exception Signal. Handler Signal. Collection Handler. List 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 30
Process Scheduling Process. Scheduler Semaphore Shared. Queue 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 31
Viewing the Program Debugger Decompiler Inspector Change. List <Browsers> 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 32
Making a Promise Future methods. For: ‘demonstration’ demo |f| f : = Future promising: [2+2]. f print. String '4. 0' Future class methods. For: 'instance creation' promising: a. Block | a. Future : = self new. ^a. Future promising: a. Block 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 33
Creating an Orphan nil subclass: #Future instance. Variable. Names: ‘semaphore ' class. Variable. Names: ‘ ' pool. Dictionaries: ' ' category: ‘Reflection-Examples’ In Visual. Works, Class. Builder does the rest. Default implementations of does. Not. Understand: and class are provided. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 34
We’ll do it eventually. . . Future methods. For: 'initialization/dispatching' promising: a. Block "Create a semaphore, and fork a block that will signal it. The result of this block is stored in result. . . " semaphore : = Semaphore new. [result : = a. Block value. semaphore signal] fork. ^self 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 35
Keeping a Promise does. Not. Undertand: a. Message "If this is our init message, let it by. . . " a. Message selector == #promising: if. True: [^super perform: a. Message selector with. Arguments: a. Message arguments]. "Wait until our result is available. . . " semaphore wait. "If our result is a Small. Integer, it has no oop. . " (result is. Kind. Of: Small. Integer) if. True: [result : = result as. Float]. "Become the result and do the deferred message. . . " result become: self. ^self perform: a. Message selector with. Arguments: a. Message arguments 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 36
Metaobjects and Lightweight Classes 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 37
Compiler Classes Compiler Decompiler Code. Regenerator Scanner Smalltalk. Compiler 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 38
Compiler Support Code. Stream Define. Opcode. Pool Method. Node. Holder Program. Node. Enumerator Scanner. Table <more> 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 39
Variables and Scopes Argument. Variable Instance. Variable Local. Scope Local. Variable Name. Scope Null. Scope Pseudo. Variable Receiver. Variable Remote. Variable Static. Scope Static. Variable Temporary. Variable Undeclared. Variable. Definition 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 40
Parse Tree Nodes Program. Node Method. Node Parameter. Node Statement. Node Return. Node Value. Node Arithmetic. Loop. Node Assignment. Node Cascade. Node Conditional. Node Leaf. Node Block. Node Literal. Node Variable. Node Loop. Node Sequence. Node Simple. Message. Node 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 41
Smalltalk System. Dictionary Association/Variable. Binding <class and pool variables> 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 42
Storage and Garbage Object. Memory. Policy Weak. Array Weak. Dictionary 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 43
Levels of Representation Source Compiler Parse Node Program. Node Byte Code VM Decompiler Native Code 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 44
Ways to Wrap • Source Code Modifications • Byte Code Modifications • New Selectors • Dispatching Wrappers • Class Wrappers • Instance Wrappers • Method Wrappers 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 45
Compiled Methods 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 46
Method Wrappers 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 47
Method Wrappers value. With. Receiver: an. Object arguments: args self before. Method. ^[client. Method value. With. Receiver: an. Object arguments: args] value. Now. Or. On. Unwind. Do: [self after. Method] original. Method. Name: argument ^#() receiver: self value: argument 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 48
Multimethods Optimizing. Visitor>>visit. With. Node: a. Node <Parse. Node> ^self value optimized Optimizing. Visitor>> visit. With. Node: a. Node <Variable. Node> ^a. Node lookup. In: self symbol. Table 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 49
Parse Tree 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 50
Issues Power Safety Efficiency Comprehensibility 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 51
Reflection in Smalltalk Astonishing Introspective Highly Reflective Intelligently Factored Uniquely Malleable Unappoligetically Dynamic 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 52
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 Object. Memory. . . Progress has been made. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 53
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? 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 54
Go Ye Forth and Hack The best way to learn how do reflective programming in Smalltalk is to read code and experiment. 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 55
Contact Information Brian Foote Dept. of Computer Science 3253 DCL 1304 W. Springfield Urbana, IL 61801 (217) 328 -3523 http: //www. laputan. org foote@cs. uiuc. edu 9/8/2021 (C) Brian Foote 1998 Reflective Programming in Smalltalk 56
- Slides: 56