Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reflection in Pharo: Beyond Smalltak

384 views

Published on

Talk: “Reflection in Pharo: Beyond Smalltak”, Talk held at SOFT research group VUB, Brussels, Belgium, January 25, 2016.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Reflection in Pharo: Beyond Smalltak

  1. 1. Reflection in Pharo: Beyond Smalltak Marcus Denker http://www.pharo.org Talk held at SOFT, VUB, Brussels Jan 2016
  2. 2. Everything is an Object
  3. 3. Classes
  4. 4. Methods
  5. 5. The Stack
  6. 6. Everything is an Object
  7. 7. Code?
  8. 8. AST: Abstract Syntax Tree
  9. 9. AST in Pharo5 • AST of the Refactoring browser • Transformation • Visitors • Annotations (properties) • Deeper integrated: • Pretty Printing, Syntax Highlight, Suggestions • Compiler uses RB AST
  10. 10. AST in Pharo5 • Easy access • #ast • Demo: method and block
  11. 11. DEMO (OrderedCollection>>#do:) ast. [ 1 + 2 ] sourceNode == thisContext method ast blockNodes first • ASTCache: as twice, get the same answer (flushed on image save for now)
  12. 12. AST + Tools
  13. 13. Opal Compiler • Uses RB AST • Based on Visitors Text AST AST +vars IR CM Parser Semantic Analysis AST Translator+ IRBuilder BytecodeBuilder+ Encoder
  14. 14. Opal: API • All staged are Pluggable • e.g Semantic Analyzer or Code Generator can be changed. • compiler options
  15. 15. Opal: Bytecode editing • IR can be used to manipulate methods on a bytecode level Text AST AST +vars IR CM Parser Semantic Analysis AST Translator+ IRBuilder BytecodeBuilder+ Encoder
  16. 16. Too complicated
  17. 17. Too low level
  18. 18. Can we do better?
  19. 19. AST Meta Annotation • We have an AST with properties • We have Opal with Pluggable API
  20. 20. Can’t we use that?
  21. 21. Basis: the Evil Twin CompiledMethod ReflectiveMethod Know each other Bytecode AST
  22. 22. Basis: the Evil Twin ReflectiveMethod AST run: aSelector with: anArray in: aReceiver self installCompiledMethod. self recompileAST. self installCompiledMethod. ^compiledMethod valueWithReceiver: aReceiver arguments: anArray
  23. 23. Demo: Morph • Morph methods do: #createTwin • Morph methods do: #invalidate • inspect “Morph methods”
  24. 24. Putting it together • Annotate the AST • Create Twin if needed • Invalidate method • Next call: generate code changed by annotation
  25. 25. Annotations?
  26. 26. MetaLink
  27. 27. DEMO: Simple Link node := (ReflectivityExamples>>#exampleMethod) ast. link := MetaLink new metaObject: (Object new); selector: #halt. node link: link. ReflectivityExamples new exampleMethod
  28. 28. Meta Link • When setting link: • create twin if needed • install reflective method • On execution • generate code and execute, install CM
  29. 29. Twin Switch CompiledMethod ReflectiveMethod Know each other Bytecode AST
  30. 30. Link: metaobject The object to send a message to link := MetaLink new metaObject: [self halt]
  31. 31. Link: selector The selector to send link := MetaLink new ….. selector: #value
  32. 32. Link: control before, after, instead link := MetaLink new ….. control: #after
  33. 33. Link: control after: #ensure: wrap link := MetaLink new ….. control: #after
  34. 34. Link: control instead: last link wins (for now no AOP around) link := MetaLink new ….. control: #instead
  35. 35. Link: condition boolean or block link := MetaLink new ….. condition: [self someCheck]
  36. 36. Link: arguments what to pass to the meta?
  37. 37. Reifications • Every operation has data that it works on • Send: #arguments, #receiver, #selector • Assignment: #newValue, #name • All: #node, #object, #context
  38. 38. Link: arguments what to pass to the meta? link := MetaLink new ….. arguments: #(name newValue)
  39. 39. Reifications: condition link := MetaLink new condition: [: object | object == 1];
  40. 40. Virtual meta • Reifications can be the meta object link := MetaLink new metaObject: #receiver; selector: #perform:withArguments:; arguments: #(selector arguments).
  41. 41. Statement Coverage link := MetaLink new metaObject: #node; selector: #tagExecuted. “set this link on all the AST nodes" (ReflectivityExamples>>#exampleMethod) ast nodesDo: [:node | node link: link].
  42. 42. Users • BreakPoints Pharo5 • Coverage Kernel • ….
  43. 43. Everything is an Object
  44. 44. Everything is an object? SmalltalkImage classVarNamed: #CompilerClass ==> returns value Object binding class ==> Association
  45. 45. Why not an Object?
  46. 46. Globals/ClassVariables • We are close: bindings are associations • Add sublass “LiteralVariable” • Sublasses GlobalVariable, ClassVariable • Enhance API
  47. 47. Globals/ClassVariables SmalltalkImage classVariableNamed: #CompilerClass Object binding class
  48. 48. Globals: Reflective APi global := SmalltalkImage classVariableNamed: #CompilerClass global read global write: someObject + helper methods + compatibility methods
  49. 49. Everything is an object? • Point instanceVariables • 5@3 instVarNamed: ‘x’ • 5@3 instVarNamed: ‘y’ put: 6
  50. 50. Why not an Object?
  51. 51. Slots Point slots (Point slotNamed: #x) read: (3@4) (Point slotNamed: #x) write: 7 to: (3@4)
  52. 52. Variables+MetaLink • Helper methods • But: can’t we annotate variables directly? Point assignmentNodes
  53. 53. Variables + Links • Object binding link: myMetaLink • (Point slotNamed: #x) link: myMetaLink (not yet in Pharo5)
  54. 54. Class Template Object subclass: #MyClass slots: { #x => WeakSlot } classVariables: { } category: 'Example'
  55. 55. Future • Can’t we model bit patterns and bind them to named virtual slots? • How to model Array-like layouts better?
  56. 56. Questions ?

×