2. What is a proxy?
A proxy object is a surrogate or
placeholder that controls access to
another target object.
2
Monday, August 22, 2011
3. Glossary
Target: object to proxify.
Client: user of the proxy.
Interceptor: object that intercepts message
sending.
Handler: object that performs a desired
action as a consequence of an interception.
3
Monday, August 22, 2011
4. Forwarder and
Logging example
4
Monday, August 22, 2011
5. With or without
Object replacement?
In proxies with object replacement
(#become:), the target object is replaced by a
proxy.
Proxies without object replacement are a
kind of factory.
5
Monday, August 22, 2011
6. With or without
Object replacement?
In proxies with object replacement
(#become:), the target object is replaced by a
proxy.
Proxies without object replacement are a
kind of factory.
5
Monday, August 22, 2011
7. Traditional proxy
implementations
Usage of a minimal object together with an implementation of a
custom #doesNotUnderstand
ProtoObject
identityHash
pointersTo
nextObject
...
Proxy
target
doesNotUnderstand:
executeBeforeHandling
executeAfterHandling
6
Monday, August 22, 2011
8. We are going to play
a little game...
7
Monday, August 22, 2011
9. Are both prints in Transcript the same or not?
8
Monday, August 22, 2011
10. Are both prints in Transcript the same or not?
Conclusion: methods understood are NOT intercepted.
Is that bad?
8
Monday, August 22, 2011
11. Are both prints in Transcript the same or not?
Conclusion: methods understood are NOT intercepted.
Is that bad?
Different execution paths 8
Errors difficult to find
Monday, August 22, 2011
12. Do we want the regular #doesNotUnderstand
or to intercept the message?
9
Monday, August 22, 2011
13. Do we want the regular #doesNotUnderstand
or to intercept the message?
10
Monday, August 22, 2011
14. I wanted the normal #doesNotUnderstand!!!
11
Monday, August 22, 2011
15. I wanted the normal #doesNotUnderstand!!!
12
Monday, August 22, 2011
16. Problems
#doesNotUnderstand: cannot be trapped like a
regular message.
Mix of handling procedure and proxy
interception.
Only methods that are not understood are
intercepted.
No separation between proxies and handlers
This approach is not stratified
13
Monday, August 22, 2011
18. VM CRASH
This solution is not uniform
15
Monday, August 22, 2011
19. A Uniform, Light-weight and Stratified Proxy
Model and Implementation.
16
Monday, August 22, 2011
20. Used hooks
Object replacement (#become:)
Change an object’s class (#adoptInstance:)
Objects as methods (#run:with:in:)
Classes with no method dictionary
(#cannotInterpret:)
17
Monday, August 22, 2011
21. Object replacement
B B
A A
c become: d
D D
C C
18
Monday, August 22, 2011
22. Objects as methods
The VM sends #run: aSelector with: anArray in: aReceiver
19
Monday, August 22, 2011
23. Objects as methods
The VM sends #run: aSelector with: anArray in: aReceiver
So.....We can implement in Proxy:
19
Monday, August 22, 2011
24. CLasses with no
method dictionary
References Object
instance of
message send
lookup
subclass Proxy
cannotInterpret: aMessage
aProxy username 4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
20
Monday, August 22, 2011
26. How it works? Object
Proxy
handler
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
22
Monday, August 22, 2011
27. How it works? Object
Proxy
handler
target
cannotInterpret:
Proxy class >> proxyFor:
ProxyTrap
initialize
nilMethodDict
22
Monday, August 22, 2011
28. How it works? Object
Of
Proxy
ce
handler
ns tan target
Proxy class >> i cannotInterpret:
proxyFor:
aProxy
ProxyTrap
initialize
nilMethodDict
aHandler ‘Mariano’
22
Monday, August 22, 2011
29. How it works? Object
Of
Proxy
ce
handler
ns tan target
Proxy class >> i cannotInterpret:
proxyFor:
aProxy
ProxyTrap
initialize
nilMethodDict
aHandler ‘Mariano’
ProxyTrap class >>
22
Monday, August 22, 2011
30. How it works? Object
Proxy
handler
target
cannotInterpret:
Proxy class >>
aProxy ins proxyFor:
tan
ce
Of ProxyTrap
initialize
nilMethodDict
aHandler ‘Mariano’
ProxyTrap class >>
22
Monday, August 22, 2011
31. How it works? Object
Proxy
handler
target
cannotInterpret:
Proxy class >>
aProxy ins proxyFor:
tan
ce
Of ProxyTrap
initialize
nilMethodDict
aHandler ‘Mariano’
22
Monday, August 22, 2011
32. How it works?
References
instance of Object
message send
lookup
subclass
Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
33. How it works?
References
instance of Object
message send
lookup
subclass
Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
34. How it works?
References
instance of Object
message send
lookup
subclass
Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
35. How it works?
References
instance of Object
message send
lookup
subclass
Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
36. How it works?
References
instance of Object
message send
lookup
subclass
Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
37. How it works?
References
instance of Object
message send
lookup
subclass
Proxy >> Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
38. How it works?
References
instance of Object
message send
lookup
subclass
Proxy >> Proxy
cannotInterpret: aMessage
aProxy username
4: #cannotInterpret: lookup
1: #username send
ProxyTrap
methodDict := nil
aProxy
2: #aProxy lookup
SimpleForwarderHandler >>
3: Since the method dictionary was nil,
the VM sends #cannotInterpret to
the receiver but starting the lookup in the superclass
23
Monday, August 22, 2011
39. Traditional Ghost
#doesNotUnderstand: #cannotInterpret: is
cannot be trapped like a trapped like a regular
regular message. message.
Mix of handling No mix of handling
procedure and proxy procedure and proxy
interception. interception.
Only methods that are
“All” methods are
not understood are
intercepted.
intercepted.
No separation between Clear separation between
proxies and handlers. proxies and handlers.
24
Monday, August 22, 2011
41. Methods not intercepted
1) Optimizations done by the Compiler
2) Special shortcut bytecodes between Compiler and VM
2.1) Methods NEVER sent: #== and #class
2.2) Methods that may or may not be executed depending
on the receiver and arguments: e.g. in ‘1+1’ #+ is not
executed. But with ‘1+$C’ #+ is executed.
2.3)Always executed, they are just little optimizations.
Examples #new, #next, 26
#nextPut:, #size, etc.
Monday, August 22, 2011
42. Methods not intercepted
1) Optimizations done by the Compiler
2) Special shortcut bytecodes between Compiler and VM
2.1) Methods NEVER sent: #== and #class
2.2) Methods that may or may not be executed depending
on the receiver and arguments: e.g. in ‘1+1’ #+ is not
executed. But with ‘1+$C’ #+ is executed.
2.3)Always executed, they are just little optimizations.
Examples #new, #next, 26
#nextPut:, #size, etc.
Monday, August 22, 2011
43. Methods not intercepted
1) Optimizations done by the Compiler
2) Special shortcut bytecodes between Compiler and VM
2.1) Methods NEVER sent: #== and #class
2.2) Methods that may or may not be executed depending
on the receiver and arguments: e.g. in ‘1+1’ #+ is not
executed. But with ‘1+$C’ #+ is executed.
2.3)Always executed, they are just little optimizations.
Examples #new, #next, 26
#nextPut:, #size, etc.
Monday, August 22, 2011
44. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
validate:
27
Monday, August 22, 2011
45. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
validate:
27
Monday, August 22, 2011
46. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
validate:
27
Monday, August 22, 2011
47. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
validate:
27
Monday, August 22, 2011
48. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
ins
tan validate:
ce
aUser of
aClassProxy
methodDict = nil
superclass = ClassProxy
27
Monday, August 22, 2011
49. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
ins
tan validate:
ce
aUser of
aClassProxy
methodDict = nil
superclass = ClassProxy
27
User name
Monday, August 22, 2011
50. Proxy for classes
Object
ClassProxy Proxy
superclass handler
methodDict target
format cannotInterpret:
handler proxyFor:
target
cannotInterpret:
proxyFor:
ProxyTrap
initialize
nilMethodDict
ClassProxyTrap
initialize
nilMethodDict
User
username
age
logIn:
ins
tan validate:
ce
aUser of
aClassProxy
methodDict = nil
aUser username superclass = ClassProxy
27
User name
Monday, August 22, 2011
53. Proxy for methods
Regular message
Method execution
28
Monday, August 22, 2011
54. Proxy for methods
Regular message
Method execution
Just handling #run:with:in correctly is enough to also
intercept method execution.
28
Monday, August 22, 2011
57. More features
Low memory footprint.
Compact classes.
Store the minimal needed state.
Easy debugging.
Custom list of messages.
30
Monday, August 22, 2011
58. Conclusion
With a little bit of special support from
the VM (#cannotInterpret hook), we
can have an image-side proxy solution
much better than the classic
#doesNotUnderstand:
31
Monday, August 22, 2011
59. Future work
Experiment with immediate proxies
(memory address tag) in VM side.
Think how to correctly intercept non-
executed methods.
32
Monday, August 22, 2011
60. Links
http://rmod.lille.inria.fr/web/pier/software/
Marea/GhostProxies
http://www.squeaksource.com/Marea.html
33
Monday, August 22, 2011
61. A Uniform, Light-weight and Stratified Proxy Model and
Implementation
Mariano Martinez Peck
marianopeck@gmail.com
http://marianopeck.wordpress.com/
Monday, August 22, 2011