Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Reflection in Pharo5

2.810 Aufrufe

Veröffentlicht am

ESUG 2015, Brescia, Italy.

Behavioral Reflection in Pharo5

Veröffentlicht in: Technologie

Reflection in Pharo5

  1. 1. Reflection in Pharo5 ESUG 2015 Marcus Denker http://www.pharo.org
  2. 2. Everything is an Object
  3. 3. Everything?
  4. 4. Classes, yes.
  5. 5. Methods, yes
  6. 6. But Code?
  7. 7. Code is a String!
  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. recompileAST ast compilationContext semanticAnalyzerClass: RFSemanticAnalyzer; astTranslatorClass: RFASTTranslator. ast doSemanticAnalysis. "force semantic analysis" compiledMethod := ast generate: compiledMethod trailer. compiledMethod reflectiveMethod: self.
  26. 26. Annotations?
  27. 27. MetaLink
  28. 28. DEMO: Simple Link node := (ReflectivityExamples>>#exampleMethod) ast. link := MetaLink new metaObject: (Object new); selector: #halt. node link: link. ReflectivityExamples new exampleMethod
  29. 29. Meta Link • When setting link: • create twin if needed • install reflective method • On execution • generate code and execute, install CM
  30. 30. Twin Switch CompiledMethod ReflectiveMethod Know each other Bytecode AST
  31. 31. Link: metaobject The object to send a message to link := MetaLink new metaObject: [self halt]
  32. 32. Link: selector The selector to send link := MetaLink new ….. selector: #value
  33. 33. Link: control before, after, instead link := MetaLink new ….. control: #after
  34. 34. Link: control after: #ensure: wrap link := MetaLink new ….. control: #after
  35. 35. Link: control instead: last link wins (for now no AOP around) link := MetaLink new ….. control: #instead
  36. 36. Link: condition boolean or block link := MetaLink new ….. condition: [self someCheck]
  37. 37. Link: arguments what to pass to the meta?
  38. 38. Reifications • Every operation has data that it works on • Send: #arguments, #receiver, #selector • Assignment: #newValue, #name • All: #node, #object, #context
  39. 39. Link: arguments what to pass to the meta? link := MetaLink new ….. arguments: #(name newValue)
  40. 40. Reifications: condition link := MetaLink new condition: [: object | object == 1];
  41. 41. Virtual meta • Reifications can be the meta object link := MetaLink new metaObject: #receiver; selector: #perform:withArguments:; arguments: #(selector arguments).
  42. 42. 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].
  43. 43. Variables • Helper methods • But: can’t we annotate variables directly? Point assignmentNodes
  44. 44. Friday: Slots+Globals
  45. 45. RoadMap • Pharo4: Opal is default • Pharo5 • Remove old Compiler/AST • Reflectivity: First finished version • Pharo6: Object specific links
  46. 46. Users • Tools of ObjectProfile are being ported • BreakPoints Pharo5 • Coverage Kernel by Pavel • ….
  47. 47. Thanks! • Work of many people… • Too many to list here. (And I would forget for sure someone)
  48. 48. Questions ?

×