Top Banner
Reflection in Pharo5 ESUG 2015 Marcus Denker http://www.pharo.org
48

Reflection in Pharo5

Aug 07, 2015

Download

Technology

Marcus Denker
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Reflection in Pharo5

Reflection in Pharo5 ESUG 2015Marcus Denker http://www.pharo.org

Page 2: Reflection in Pharo5

Everything is an Object

Page 3: Reflection in Pharo5

Everything?

Page 4: Reflection in Pharo5

Classes, yes.

Page 5: Reflection in Pharo5

Methods, yes

Page 6: Reflection in Pharo5

But Code?

Page 7: Reflection in Pharo5

Code is a String!

Page 8: Reflection in Pharo5

AST: Abstract Syntax Tree

Page 9: Reflection in Pharo5

AST in Pharo5• AST of the Refactoring browser

• Transformation

• Visitors

• Annotations (properties)

• Deeper integrated:

• Pretty Printing, Syntax Highlight, Suggestions

• Compiler uses RB AST

Page 10: Reflection in Pharo5

AST in Pharo5

• Easy access

• #ast

• Demo: method and block

Page 11: Reflection in Pharo5

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)

Page 12: Reflection in Pharo5

AST + Tools

Page 13: Reflection in Pharo5

Opal Compiler

• Uses RB AST

• Based on Visitors

Text AST AST+vars IR CM

Parser Semantic Analysis

AST Translator+ IRBuilder

BytecodeBuilder+ Encoder

Page 14: Reflection in Pharo5

Opal: API

• All staged are Pluggable

• e.g Semantic Analyzer or Code Generator can be changed.

• compiler options

Page 15: Reflection in Pharo5

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

Page 16: Reflection in Pharo5

Too complicated

Page 17: Reflection in Pharo5

Too low level

Page 18: Reflection in Pharo5

Can we do better?

Page 19: Reflection in Pharo5

AST Meta Annotation

• We have an AST with properties

• We have Opal with Pluggable API

Page 20: Reflection in Pharo5

Can’t we use that?

Page 21: Reflection in Pharo5

Basis: the Evil Twin

CompiledMethod ReflectiveMethod

Know each other

Bytecode AST

Page 22: Reflection in Pharo5

Basis: the Evil Twin

ReflectiveMethod

AST

run: aSelector with: anArray in: aReceiver self installCompiledMethod. self recompileAST. self installCompiledMethod. ^compiledMethod

valueWithReceiver: aReceiver arguments: anArray

Page 23: Reflection in Pharo5

Demo: Morph

• Morph methods do: #createTwin

• Morph methods do: #invalidate

• inspect “Morph methods”

Page 24: Reflection in Pharo5

Putting it together

• Annotate the AST

• Create Twin if needed

• Invalidate method

• Next call: generate code changed by annotation

Page 25: Reflection in Pharo5

recompileAST ast compilationContext semanticAnalyzerClass: RFSemanticAnalyzer; astTranslatorClass: RFASTTranslator. ast doSemanticAnalysis. "force semantic analysis" compiledMethod := ast generate: compiledMethod trailer. compiledMethod reflectiveMethod: self.

Page 26: Reflection in Pharo5

Annotations?

Page 27: Reflection in Pharo5

MetaLink

Page 28: Reflection in Pharo5

DEMO: Simple Linknode := (ReflectivityExamples>>#exampleMethod) ast. link := MetaLink new metaObject: (Object new); selector: #halt.

node link: link.

ReflectivityExamples new exampleMethod

Page 29: Reflection in Pharo5

Meta Link

• When setting link:

• create twin if needed

• install reflective method

• On execution

• generate code and execute, install CM

Page 30: Reflection in Pharo5

Twin Switch

CompiledMethod ReflectiveMethod

Know each other

Bytecode AST

Page 31: Reflection in Pharo5

Link: metaobject

The object to send a message to

link := MetaLink new metaObject: [self halt]

Page 32: Reflection in Pharo5

Link: selector

The selector to send

link := MetaLink new ….. selector: #value

Page 33: Reflection in Pharo5

Link: control

before, after, instead

link := MetaLink new ….. control: #after

Page 34: Reflection in Pharo5

Link: control

after: #ensure: wrap

link := MetaLink new ….. control: #after

Page 35: Reflection in Pharo5

Link: control

instead: last link wins (for now no AOP around)

link := MetaLink new ….. control: #instead

Page 36: Reflection in Pharo5

Link: condition

boolean or block

link := MetaLink new ….. condition: [self someCheck]

Page 37: Reflection in Pharo5

Link: arguments

what to pass to the meta?

Page 38: Reflection in Pharo5

Reifications

• Every operation has data that it works on

• Send: #arguments, #receiver, #selector

• Assignment: #newValue, #name

• All: #node, #object, #context

Page 39: Reflection in Pharo5

Link: arguments

what to pass to the meta?

link := MetaLink new ….. arguments: #(name newValue)

Page 40: Reflection in Pharo5

Reifications: condition

link := MetaLink new condition: [: object | object == 1];

Page 41: Reflection in Pharo5

Virtual meta

• Reifications can be the meta object

link := MetaLink new metaObject: #receiver; selector: #perform:withArguments:; arguments: #(selector arguments).

Page 42: Reflection in Pharo5

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].

Page 43: Reflection in Pharo5

Variables

• Helper methods

• But: can’t we annotate variables directly?

Point assignmentNodes

Page 44: Reflection in Pharo5

Friday: Slots+Globals

Page 45: Reflection in Pharo5

RoadMap

• Pharo4: Opal is default

• Pharo5

• Remove old Compiler/AST

• Reflectivity: First finished version

• Pharo6: Object specific links

Page 46: Reflection in Pharo5

Users

• Tools of ObjectProfile are being ported

• BreakPoints Pharo5

• Coverage Kernel by Pavel

• ….

Page 47: Reflection in Pharo5

Thanks!

• Work of many people…

• Too many to list here. (And I would forget for sure someone)

Page 48: Reflection in Pharo5

Questions ?