Metaprogramming & JavaScript Object Proxies Prof. Tom Austin - - PowerPoint PPT Presentation

metaprogramming javascript object proxies
SMART_READER_LITE
LIVE PREVIEW

Metaprogramming & JavaScript Object Proxies Prof. Tom Austin - - PowerPoint PPT Presentation

CS 152: Programming Language Paradigms Metaprogramming & JavaScript Object Proxies Prof. Tom Austin San Jos State University What is metaprogramming ? Writing programs that manipulate other programs. JavaScript Proxies


slide-1
SLIDE 1
slide-2
SLIDE 2

CS 152: Programming Language Paradigms

  • Prof. Tom Austin

San José State University

Metaprogramming & JavaScript Object Proxies

slide-3
SLIDE 3

Writing programs that manipulate

  • ther programs.

What is metaprogramming?

slide-4
SLIDE 4

JavaScript Proxies

Metaprogramming feature proposed for ECMAScript 6 (Harmony).

Mark Miller Tom Van Cutsem Proposed By:

slide-5
SLIDE 5

Proxies: Design Principles for Robust Object-oriented Intercession APIs

Abstract: Proxies are a powerful approach to implement meta-objects in

  • bject-oriented languages without

having to resort to metacircular

  • interpretation. We introduce such a

meta-level API based on proxies for Javascript…

slide-6
SLIDE 6

Metaprogramming terms

  • Reflection

– Introspection: examine a program – Self-modification: modify a program

  • Intercession: redefine the semantics
  • f operations.
  • Reflection is fairly common.

Intercession is more unusual.

slide-7
SLIDE 7

Introspection

Ability to examine the structure of a program. In JavaScript:

"x" in o; for (prop in o){ … }

Property lookup Property enumeration

slide-8
SLIDE 8

Self-modification

Ability to modify the structure of a program.

  • ["x"]; // computed property
  • .y = 42; // add new property

delete o.x; // remove property

  • ["m"].apply(o,[42]);

// reflected method call

slide-9
SLIDE 9

JavaScript proxies are intended to fix that.

Until recently, JavaScript did not support intercession.

But first a little history…

slide-10
SLIDE 10

Common Lisp

  • Developed before object-oriented

languages were popular.

  • Many libraries were created with

non-standard OO systems.

slide-11
SLIDE 11

Common Lisp Object System (CLOS)

  • Became standard object-oriented

system for Lisp

  • What could be done about pre-

existing object-oriented libraries?

slide-12
SLIDE 12

The Devil’s Choice

  • 1. Rewrite libraries for CLOS?

– huge # of libraries – infeasible to rewrite them all

  • 2. Make complex API?

– difficult API to understand. – Systems had conflicting features… – …But were essentially doing the same things.

slide-13
SLIDE 13
  • Keep API simple.
  • Modify object

behavior to fit different systems. Gregor Kiczales chose option 3:

Metaobject protocols were born…

slide-14
SLIDE 14

JavaScript Object Proxies Intercession API

slide-15
SLIDE 15

WARNING!

The Proxies API is in transition. We'll review Node's implementation, which lags behind the standard.

slide-16
SLIDE 16

Proxy and handler

The behavior of a proxy is determined by traps specified in its handler.

The metaobject

slide-17
SLIDE 17

Proxy design

  • Proxy: special object that has special

traps that define its behavior.

– Trap: method that intercepts an operation. – Available traps: https://developer.mozilla.org/en-US/docs/ Web/JavaScript/Old_Proxy_API

  • Handler: The meta-object that specifies

the details of the trap; the handler itself is usually a normal object.

slide-18
SLIDE 18

Using proxies in Node.js

$node --harmony-proxies prog.js

slide-19
SLIDE 19

Two kinds of proxies

  • Objects are defined with:

Proxy.create(handler,proto);

  • Functions (with extra traps) defined with:

Proxy.createFunction( handler, callTrap, constructTrap);

  • Do not exist for primitive values.
slide-20
SLIDE 20

What kind of things do we want to do to an object?

slide-21
SLIDE 21

Sample handler (incomplete)

var incompleteHandler = { get:function(myProxy, name){ console.log('Property ' + name + ' accessed.'); return 1; } };

slide-22
SLIDE 22

Creating a new proxy object

var p = Proxy.create( incompleteHandler); var q = p.hello; p.goodbye = "What happens?";

Prints "Property hello accessed" and returns 1. The set trap has not been specified, so this causes an error.

slide-23
SLIDE 23

No-op forwarding proxy

(in-class)

Wraps an object, intercepting all of its methods, and does nothing else.

slide-24
SLIDE 24

Another use case for proxies

  • Share a reference to an object, but do

not want it to be modified.

– Reference to the DOM, for instance

  • We can modify the forwarding

handler to provide this behavior:

slide-25
SLIDE 25

function roHandler(obj) { return { delete: function(name) { return obj[name]; }, set: function(rcvr,name,val){ return true; }, …

slide-26
SLIDE 26

Aspect-oriented programming (AOP)

  • Some code not well organized by objects

– Cross-cutting concern

  • Canonical example: logging statements

– littered throughout code – Swap out logger = massive code changes

slide-27
SLIDE 27

Lab: Tracing API

  • Use proxies to log all actions

taken on an object

  • Avoids having complexity of

logging framework