Advanced Reflection in Pharo

46
Advanced Reflection in Pharo Marcus Denker http://rmod.lille.inria.fr

description

A Lecture held at Universitat Politècnica de Catalunya on Oct 29, 2013

Transcript of Advanced Reflection in Pharo

Page 1: Advanced Reflection in Pharo

Advanced Reflection in Pharo

Marcus Denker

http://rmod.lille.inria.fr

Page 2: Advanced Reflection in Pharo

What you know…

• Smalltalk is reflective

• e.g. Classes and Methods are Objects

• Reflective API on all Objects

Page 3: Advanced Reflection in Pharo

Instance Variables

Page 4: Advanced Reflection in Pharo

Object subclass: #Point!! instanceVariableNames: 'x y'!! classVariableNames: ''!! poolDictionaries: ''!! category: 'Kernel-BasicObjects'

2 instance variables

Page 5: Advanced Reflection in Pharo

• Ask the class:

Point instVarNames

• read:

3@4 instVarNamed: #x

• write:

3@4 instVarNamed: #x put: 5

Page 6: Advanced Reflection in Pharo

Great!

Page 7: Advanced Reflection in Pharo

But…

Page 8: Advanced Reflection in Pharo

Object subclass: #Point!! instanceVariableNames: 'x y'!! classVariableNames: ''!! poolDictionaries: ''!! category: 'Kernel-BasicObjects'

This is just a String!

Page 9: Advanced Reflection in Pharo

• returns an Array of Strings:

!

Point instVarNames

Page 10: Advanced Reflection in Pharo

Why not Objects?

Page 11: Advanced Reflection in Pharo

We can do better!

Page 12: Advanced Reflection in Pharo

• All classes have a Layout

!

• Describes the memory layout defined by a class

!

• Layout and all the description are Objects

Page 13: Advanced Reflection in Pharo

• Point layout

• a Normal Object

• Array layout

• an Array of Pointers

• ByteArray layout

• an Array of Bytes

Page 14: Advanced Reflection in Pharo

Point layout allSlots

!

==> an OrderedCollection(x => Slot y => Slot)

Page 15: Advanced Reflection in Pharo

• Slots know how to read values from Objects

!

mySlot := Point layout resolveSlot: #x.

mySlot read: 3@4.

Page 16: Advanced Reflection in Pharo

Why?

Page 17: Advanced Reflection in Pharo

Typed SlotsSlot subclass: #TypedSlot layout: PointerLayout slots: {#x => TypedSlot type: Integer}. !!TypedSlot >> write: aValue to: anInstance (aValue isNil or: [aValue isKindOf: type]) ifFalse: [ InvalidTypeError signal ]. super write: aValue to: anInstance.

Page 18: Advanced Reflection in Pharo

Property SlotsObject subclass: #PropertyObject layout: PointerLayout slots: { field => Slot property1 => PropertySlot. property2 => PropertySlot. ... propertyN => PropertySlot. }

Page 19: Advanced Reflection in Pharo

Others

• BitSlot

• BooleanSlot

• Alias

• Relationships (e.g. one-one, one-many)

• …. Your Domain level Slot!

Page 20: Advanced Reflection in Pharo

More in Paper from OOPSLA

Page 21: Advanced Reflection in Pharo

Status

• Slots are in Pharo3, but hidden

!

• In Pharo4: un-hide and introduce some Special Slots (e.g. Boolean, Property)

• e.g. for Morphic (user interface objects)

Page 22: Advanced Reflection in Pharo

Help Wanted!!!

Page 23: Advanced Reflection in Pharo

So this worked well…

Page 24: Advanced Reflection in Pharo

Lets do it again!

Page 25: Advanced Reflection in Pharo

Turn another String into

Objects

Page 26: Advanced Reflection in Pharo

:-)

Page 27: Advanced Reflection in Pharo

Methods

Page 28: Advanced Reflection in Pharo

Lets have a look

Page 29: Advanced Reflection in Pharo

• Method are Objects, but…

!

• No high-level model for sub-method elements

• Message sends

• Assignments

• Variable access

!

• Structural reflection stops at the granularity of methods!

Page 30: Advanced Reflection in Pharo

Can we do better?

Page 31: Advanced Reflection in Pharo

Compilers have ASTs

Page 32: Advanced Reflection in Pharo

Abstract Syntax Trees

Page 33: Advanced Reflection in Pharo

• Lets have a look at an example

(Object>>#halt) ast

Page 34: Advanced Reflection in Pharo

• Encodes the method as a tree of node-objects

• Visitor Pattern

• Transformations

• Refactoring tool uses this!

RBProgramNode RBDoItNode RBMethodNode RBReturnNode RBSequenceNode RBValueNode RBArrayNode RBAssignmentNode RBBlockNode RBCascadeNode RBLiteralNode RBMessageNode RBVariableNode

Page 35: Advanced Reflection in Pharo

In Pharo3

• AST based Navigation in the Editor

• “Suggestions”

• Debugger uses AST for pc->code mapping

• AST Interpreter for experiments

Page 36: Advanced Reflection in Pharo

Future

• AST everywhere!

• Do we need to store strings?

• Can we have an AST based editor?

• Sub-Method Reflection: The MetaLink

Page 37: Advanced Reflection in Pharo

Method

Can we modify the behaviour of code?

Meta

Link

> Annotate the AST with meta-links

Page 38: Advanced Reflection in Pharo

Why?

• Change behaviour for selected AST Nodes

• “All assignments”

• “this message send”

But without changing the program code!

Page 39: Advanced Reflection in Pharo

Breakpoints

Page 40: Advanced Reflection in Pharo

DEMO: Atoms

Page 41: Advanced Reflection in Pharo

© Marcus Denker

Behavioral Reflection

source code

(AST)

meta-object

activation

condition

links

Page 42: Advanced Reflection in Pharo

Uses…

• Debugger

• BreakPoints, WatchPoints

• Profilers

• Coverage Analysis

• AOP

Page 43: Advanced Reflection in Pharo

Will be in Pharo4

Page 44: Advanced Reflection in Pharo

Will be in Pharo4 !!help wanted!!

Page 45: Advanced Reflection in Pharo

What did we see?• Slots!

• Instance variables are just described with strings

• We can do better! Layout, Slots

• ASTs Everywhere!

• Methods are objects, but internal structure not modelled

• We can do better! AST, Sub-Method Reflection, Meta-Links

Page 46: Advanced Reflection in Pharo

Questions???