Virtual Values for Language Extension Thomas H. Austin Tim - - PowerPoint PPT Presentation

virtual values for language extension
SMART_READER_LITE
LIVE PREVIEW

Virtual Values for Language Extension Thomas H. Austin Tim - - PowerPoint PPT Presentation

Virtual Values for Language Extension Thomas H. Austin Tim Disney Cormac Flanagan University of California Santa Cruz ICFP11 x + y Extensibility in Python is clean class Complex (object): x = Complex(2, 1) y = Complex(3, 1) def


slide-1
SLIDE 1

Virtual Values for Language Extension

Thomas H. Austin Tim Disney Cormac Flanagan

University of California Santa Cruz ICFP’11

slide-2
SLIDE 2

x + y

slide-3
SLIDE 3

class Complex(object): def __init__(self, real, imag): self.r = real self.i = imag def __add__(self, other): return Complex(self.r + other.r, self.i + other.i)

Extensibility in Python is clean

x = Complex(2, 1) y = Complex(3, 1) x + y

slide-4
SLIDE 4

Extensibility in JavaScript is ugly

var x = new Complex(2, 1); var y = new Complex(3, 1); x.plus(y); function Complex(real, imag) { this.r = real; this.i = imag; } Complex.prototype.plus(other) { return new Complex(this.r + other.r, this.i + other.i); }

slide-5
SLIDE 5

matrixMult([[new Complex(4,1), new Complex(2,1)], [new Complex(5,1), new Complex(8,1)]]) function matrixMult(a, b) { ... } matrixMult([[-3,-8,3], [-2,1,4]])

Even worse than ugly!

slide-6
SLIDE 6

x.plus(y) x + y vs.

slide-7
SLIDE 7

Virtualize the interface between code and data

Virtual Values:

slide-8
SLIDE 8

Code Data

x = 65 y = 1 x 65 y 1 z 66 z = x + y

+

Standard Addition

slide-9
SLIDE 9

handl handler

... ... plus λ... ... ...

handler = { ... plus: λr. 1 + r } z = p + 65 65 65 p = proxy(handler) p z 66

Virtualized Addition Code Data

+

slide-10
SLIDE 10

λproxy

{ f : v }

  • [f]
  • [f] = v

λx. e e1(e2) if b e1 e2 24 !true 24 + 42 true Idealized JavaScript-like language proxy(handler)

slide-11
SLIDE 11

handler = { get: λf... p[f] → h.get(f) p = proxy(h)

slide-12
SLIDE 12

Code Data

handl handler

... ... get λ... ... ...

handler = { get: λn. log(...)

  • bj[n]

... }

  • bj = {

“f”: 42 } p[“f”]

  • bj
  • bj

“f” 42

base meta p = proxy(handler) p “f” “f” “f”

slide-13
SLIDE 13

handler = { set: λf,v... p[f] = v → h.set(f,v) call: λv... p(v) → h.call(v) geti: λr... r[p] → h.geti(r) seti: λr,v... r[p] = v → h.seti(r,v) unary: λo... !p → h.unary(“!”) left: λo,r... p + x → h.left(“+”,x) right: λo,l... x + p → h.right(“+”,x) test: λ... } if p e e → if h.test() e e get: λf... p[f] → h.get(f) p = proxy(h)

slide-14
SLIDE 14

x = 4.0 + (1.0 * i) y = 3.0 + (1.0 * i) x + y

7

real imaginary

2

slide-15
SLIDE 15

meter = makeUnit(“meter”) second = makeUnit(“second”) g = 9.81 * meter / second / second meter g + 1 // Error: Units not compatible! print(g) // “9.81 meters seconds^-2” g + 1 * meter / second / second 10.81

“meter” “second”-2

9.81

“meter” “second”-2

1

“meter”

1

“second”

slide-16
SLIDE 16

isTainted(taint(4) + 5) == true taint = λx. ...

Tainting Extension

isTainted = λx. ...

4 4 5

+

9 true

slide-17
SLIDE 17

Security

Extensibility: Security: wants to extend behavior

  • f library extensions

wants to restrict behavior

  • f adversaries
slide-18
SLIDE 18

Security

isProxy(x) Always tells the truth

slide-19
SLIDE 19

critical = λx. if isProxy(x) then err() else ...

Stop proxies...

slide-20
SLIDE 20

...not quite

critical = λx. if isProxy(x) then err() else y = x() ...

slide-21
SLIDE 21

Trusted Module Untrusted Module nonProxy

The nonProxy proxy!

slide-22
SLIDE 22

critical = λx. x = nonProxy(x) y = x() ...

yes error! no

y y nonProxy x nonProxy

isProxy?

y

The nonProxy proxy!

slide-23
SLIDE 23

T.

  • V. Cutsem and M. S. Miller. Proxies: Design principles for robust object-oriented intercession APIs

JavaScript Proxies

handler = { test: ... } call: ... get: ... set: ... geti: ... seti: ... unary: ... left: ... right: ...

Virtual Values

contracts membranes complex units nonProxy taint tracking lazy evaluation