reflection and context

54
© Marcus Denker Reflection and Context Marcus Denker [email protected] http://rmod.lille.inria.fr

Upload: marcus-denker

Post on 11-May-2015

1.425 views

Category:

Technology


0 download

DESCRIPTION

Lecture at Universite catholique de Louvain, 17.03.2011

TRANSCRIPT

Page 1: Reflection and Context

© Marcus Denker

Reflection and Context

Marcus [email protected]://rmod.lille.inria.fr

Page 2: Reflection and Context

© Marcus Denker

Roadmap

> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection> III. Meta Context

Page 3: Reflection and Context

© Marcus Denker

Smalltalk

> Smalltalk has support for reflection

> Structural reflection— Classes / methods are objects— Can be changed at runtime

> Behavioral reflection— Current execution reified (thisContext)— #doesNotUnderstand / MethodWrappers

Page 4: Reflection and Context

© Marcus Denker

Can we do better?

> Structural Reflection stops at method level— Bytecode in the CompiledMethod: Numbers— Text: Just a String, needs to be compiled

> Behavior hard coded in the Virtual Machine— Message Sending— Variable Access

> Both structural and behavioral reflection is limited— We should do better!

Page 5: Reflection and Context

© Marcus Denker

Structural Reflection

> Structure modeled as objects

— e.g. Classes, methods— Causally connected

> Uses:— Development environments— Language extensions and experiments

Page 6: Reflection and Context

© Marcus Denker

Methods and Reflection

> Method are Objects— e.g in Smalltalk

> No high-level model for sub-method elements— Message sends— Assignments— Variable access

> Structural reflection stops at the granularity of methods

Page 7: Reflection and Context

© Marcus Denker

Sub-Method Reflection

> Many tools work on sub method level— Profiler, Refactoring Tool, Debugger, Type Checker

> Communication between tools needed— Example: Code coverage

> All tools use different representations— Tools are harder to build— Communication not possible

Page 8: Reflection and Context

© Marcus Denker

Existing Method Representations

> Existing representations for Methods

— Text

— Bytecode

— AST

Page 9: Reflection and Context

© Marcus Denker

Requirements

> Causal Connection

> Abstraction Level

> Extensibility

> Persistency

> Size and Performance

Page 10: Reflection and Context

© Marcus Denker

Text

> Low level abstraction— String of characters

> Not causally connected— Need to call compiler

Page 11: Reflection and Context

© Marcus Denker

Bytecode

> Low level abstraction— Array of Integers

> Missing extensibility— e.g. for tools

> Mix of base- and meta-level code— Problems with synthesized code when changing code— Examples: AOP point-cut residues, reflection hooks

Page 12: Reflection and Context

© Marcus Denker

Abstract Syntax Tree

> Not causally connected— Need to call compiler

> Not extensible— Fixed set of codes, no way to store meta data

> Not persistent— Generated by compiler from text, never stored

Page 13: Reflection and Context

© Marcus Denker

Solution: Reflective Methods

> Annotated, persistent AST> Bytecode generated on demand and cached

:ReflectiveMethod

annotation

#(12 13 45 38 98 128 84 72 42 77 22 28 59

32 7 49 51 87 64)

:CompiledMethod

compiledMethod

reflectiveMethodannotation

Tools VM

Page 14: Reflection and Context

© Marcus Denker

Persephone

> Implementation of Reflective Methods for Squeak 3.9

> Smalltalk compiler generates Reflective Methods— Translated to bytecode on demand

> Open Compiler: Plugins— Called before code generation— Transform a copy of the AST

Page 15: Reflection and Context

© Marcus Denker

Requirements revisited

> Abstraction Level OK

> Causal Connection OK

> Extensibility OK

> Persistency OK

> Size and Performance OK

Page 16: Reflection and Context

© Marcus Denker

Annotations

> Source visible annotations— extended Smalltalk syntax

> Source invisible annotations— Reflective API— Can reference any object

> Every node can be annotated> Semantics: Compiler Plugins

(9 raisedTo: 10000) <:evaluateAtCompiletime:>

Page 17: Reflection and Context

© Marcus Denker

Example: Pluggable Type-System

> Example for textual annotations

bitFromBoolean: aBoolean <:type: Boolean :> ^ (aBoolean ifTrue: [1] ifFalse: [0]) <:type: Integer :>

> Optional, pluggable type-system> Types stored as annotations in the Reflective Methods

Page 18: Reflection and Context

© Marcus Denker

Memory

number of classes memory

Squeak 3.9

Persephoneno reflective methods

Persephonereflective methods

2040 15.7 MB

2224 20 MB

2224 123 MB

Page 19: Reflection and Context

© Marcus Denker

Roadmap

> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection> III. Meta Context

Page 20: Reflection and Context

© Marcus Denker

Behavioral Reflection

> Reflect on the execution— method execution— message sending, variable access

> In Smalltalk— No model of execution below method body— message sending / variable access hard coded by VM— #doesNotUnderstand / MethodWrappers

> Reflective capabilities of Smalltalk should be improved!

Page 21: Reflection and Context

© Marcus Denker

MetaclassTalk

> Extends the Smalltalk metaclass model— Similar to CLOS MOP

> Metaclass defines— message lookup— access to instance variables

> Problems:— Reflection only controllable at class boundaries— No fine-grained selection (e.g. single operations)— Protocol between base and meta level is fixed

Page 22: Reflection and Context

© Marcus Denker

Reflex: Partial Behavioral Reflection

> Hooksets: collection of operation occurrences> Links

— Bind hooksets to meta-objects— Define protocol between base and meta

> Goals— Highly selective reification— Flexible meta-level engineering

– Protocol specification– Cross-cutting hooksets

activation

condition

hookset

metaobject

links

Tanter, OOPSLA03

Page 23: Reflection and Context

© Marcus Denker

Example: Profiler

> Operation: — Method execution (around)

> Hookset: — All execution operations in a package

> Meta-object: — A profiling tool activation

condition

hookset

metaobject

links

Page 24: Reflection and Context

© Marcus Denker

Reflex for Squeak

> Partial Behavioral Reflection pioneered in Java— Code transformation at load time— Not unanticipated (it’s Java...)

> Geppetto: Partial Behavioral Reflection for Smalltalk— For Squeak 3.9 with Bytecode transformation

Page 25: Reflection and Context

© Marcus Denker

Problems

> Annotation performance— Decompile bytecode

> Execution performance— Preambles for stack manipulation

> Low-level representation— ifTrue:ifFalse:— Blocks— Global variables

Page 26: Reflection and Context

© Marcus Denker

Method

Links as Annotations

Meta

Link

> Links can be annotations on the AST

Page 27: Reflection and Context

© Marcus Denker

Behavioral Reflection: Flexible

source code

(AST)

meta-object

activation

condition

links

> Very Flexible

Page 28: Reflection and Context

© Marcus Denker

Behavioral Reflection: CLOS

source code

(AST)

meta-object

activation

condition

links

> Meta-class MOP (CLOS)

Page 29: Reflection and Context

© Marcus Denker

Behavioral Reflection: AOP

source code

(AST)

meta-object

activation

condition

links

> Aspects

Page 30: Reflection and Context

© Marcus Denker

Behavioral Reflection: Tracer

> Tracer

source code

(AST)

tracer metaobject

link

Page 31: Reflection and Context

© Marcus Denker

Properties

> Very fast annotations— No decompile!

> On-the-fly code generation— Only code executed gets generated

> Generated code is fast— Better then working on bytecode level

Page 32: Reflection and Context

© Marcus Denker

Demo

> Show Bounce Demo

Page 33: Reflection and Context

© Marcus Denker

Reflectivity

> Prototype implementation in Squeak

— Sub-Method Structure— Partial Behavioral Reflection

> Download:

http:/scg.unibe.ch/Research/Reflectivity

Page 34: Reflection and Context

© Marcus Denker

Reflectivity: Pharo

> Not yet.... but soon.

— Slowly revisiting all research stuff— Now we can do it for real!

– Engineering vs. Research...

Page 35: Reflection and Context

© Marcus Denker

Roadmap

> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection> III. Meta Context

Page 36: Reflection and Context

© Marcus Denker

Behavioral Reflection: Flexible

source code

(AST)

meta-object

activation

condition

links

> Letʼs use it!

Page 37: Reflection and Context

© Marcus Denker

Problem: Recursion

> Behavioral reflection cannot be applied to the whole system

— System classes— Meta-objects

Page 38: Reflection and Context

© Marcus Denker

Example: Beeper

> Call the Beeper from OrderedCollection>>#add

beepLink := Link new metaObject: Beeper.beepLink selector: #beep.

(OrderedCollection>>#add:) methodNode link: beepLink.

Page 39: Reflection and Context

© Marcus Denker

Meta-object Call Recursion

Base Level Meta Object Meta Object

Infinite recursion

#beep send#add: send

#add: send

#beep send#add: send

Page 40: Reflection and Context

© Marcus Denker

Ad-hoc Solutions

> Code duplication

> Adding special tests

Page 41: Reflection and Context

© Marcus Denker

Tower of Interpreters

> Smith, 1982

Page 42: Reflection and Context

© Marcus Denker

The Real Problem

Representing Meta-Level Execution

Page 43: Reflection and Context

© Marcus Denker

The Meta-Context

> Link enables MetaContext

Base Level Meta Level

MetaContext activation

MetaContext deactivation

Page 44: Reflection and Context

© Marcus Denker

Context-aware Links

> Disable call when already on the meta-level

Base Level Meta Level

Stop meta-level call

Page 45: Reflection and Context

© Marcus Denker

MetaContext

> Recursion problem solved

operation

meta-object

links

level 0

base

meta

Page 46: Reflection and Context

© Marcus Denker

MetaContext

> Meta-level analysis:— Trace the tracer

operation

meta-object

links

level 0

metameta-

object

link

level 1

base

meta

meta-2

Page 47: Reflection and Context

© Marcus Denker

MetaContext

> Recursion problem

> Missing representation of meta-level execution

> Meta-context— Solves the recursion problem— Enables meta-level analysis

Page 48: Reflection and Context

© Marcus Denker

Transactional Perspective 42

transaction

system

transactional o

bjects

original objec

ts

What?adapt reflective behaviour to transactional contexts

set-slot

insert-slot

remove-slot

define-method

remove-method

declare-delegation

set-slot

insert-slot

remove-slot

define-method

remove-method

declare-delegation

default MOP

adapted MOP

Page 49: Reflection and Context

© Marcus Denker

Transaction Transparency 47

non-destructive operations also need to see the objects belonging to the current transaction

(with-context @transaction(defmethod send (selector arguments)

(let ((log (log (host-context))))

(do-slots (arguments argument index)

(setf ($slot-value arguments index) (gethash argument log argument))))

(resend)))

How?

(empty? inventory ) (empty? inventory’ )

alter-object

inventory inventory’

case study is not implemented purely on top of the object model

- System must make sure that any method invoked in transaction context sees the transactional versions of modified objects, rather than the original versions.- $slot-value: sending a message within the send method leads to infinite recursion; hence this direct accessor.

Page 50: Reflection and Context

© Marcus Denker

Rethinking Reflection

> Meta change “shows through”— Introspection shows implementation— Recursion and confusion of meta levels

> Reflective change is always global— Any change is visible to the whole system— No way to batch multiple changes into one atomic operation

Page 51: Reflection and Context

© Marcus Denker

Next steps

> Generalize context model: — Beyond context as control flow.

> Virtual machine support... to make it practical

> What is the next reflective language kernel?

Page 52: Reflection and Context

© Marcus Denker

A lot of open questions...

thats why it is

Research...

?????

Page 53: Reflection and Context

© Marcus Denker

Questions

?

Page 54: Reflection and Context

© Marcus Denker

License

> http://creativecommons.org/licenses/by-sa/2.5/

Attribution-ShareAlike 2.5You are free:• to copy, distribute, display, and perform the work• to make derivative works• to make commercial use of the work

Under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor.

"Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

• For any reuse or distribution, you must make clear to others the license terms of this work.• Any of these conditions can be waived if you get permission from the copyright holder.

Your fair use and other rights are in no way affected by the above.