Metaprogramming & JavaScript Object Proxies What is - - PowerPoint PPT Presentation

metaprogramming javascript object proxies what is
SMART_READER_LITE
LIVE PREVIEW

Metaprogramming & JavaScript Object Proxies What is - - PowerPoint PPT Presentation

C152 Programming Language Paradigms Prof. Tom Austin, Fall 2014 Metaprogramming & JavaScript Object Proxies What is metaprogramming ? Writing programs that manipulate other programs. JavaScript Proxies Special metaprogramming


slide-1
SLIDE 1

C152 – Programming Language Paradigms

  • Prof. Tom Austin, Fall 2014

Metaprogramming & JavaScript Object Proxies

slide-2
SLIDE 2

Writing programs that manipulate

  • ther programs.

What is metaprogramming?

slide-3
SLIDE 3

JavaScript Proxies

Special metaprogramming features are proposed for ECMAScript 6, code-named ECMAScript Harmony. Mark Miller Tom Van Cutsem Proposed By:

slide-4
SLIDE 4

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-5
SLIDE 5

Metaprogramming terms

  • Reflection

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

  • Intercession: redefine the semantics of
  • perations.
  • Reflection is fairly common. Intercession is

more unusual.

slide-6
SLIDE 6

Introspection

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

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

Property lookup Property enumeration

slide-7
SLIDE 7

Self-modification

The ability to modify the structure of a program. In JavaScript:

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

delete o.x; // remove a property

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

// reflected method call

slide-8
SLIDE 8

JavaScript proxies are intended to fix that.

Until recently, JavaScript did not support intercession.

But first a little history…

slide-9
SLIDE 9

Metaobject Protocol History

  • Common Lisp was developed before object-
  • riented languages were popular.
  • Many libraries were created with non-standard
  • bject-oriented systems.
  • The Common Lisp Object System (CLOS) was

developed, but what could be done about pre- existing object-oriented libraries?

slide-10
SLIDE 10

The Devil’s Choice

  • 1. Rewrite libraries for CLOS?

– Huge # of libraries, so not feasible to rewrite them all.

  • 2. Make complex API?

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

slide-11
SLIDE 11

Keep a Simple API, but modify object behavior to fit with different systems. Gregor Kiczales chose option 3:

Metaobject protocols were born…

slide-12
SLIDE 12

JavaScript Object Proxies Intercession API

slide-13
SLIDE 13

WARNING! The Proxies API is in transition. We will review the old API, since that is more widely available (including in Node.js)

slide-14
SLIDE 14

Proxy design

  • Proxy: A 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-15
SLIDE 15

Using proxies in Node.js

  • Node.js supports proxies, but has them

disabled by default.

  • To run node with proxies, type:

$ node --harmony-proxies prog.js

slide-16
SLIDE 16

Two kinds of proxies

  • Objects are defined with:

Proxy.create(handler, proto);

  • Functions (with extra traps) are defined with:

Proxy.createFunction(handler, callTrap, constructTrap);

  • Proxies do not exist for primitive values.
  • Note: This API is in transition.
slide-17
SLIDE 17

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

slide-18
SLIDE 18

Sample handler (incomplete)

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

slide-19
SLIDE 19

Creating a new proxy object

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

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

slide-20
SLIDE 20

No-op forwarding proxy

(in-class) This proxy wraps an object, intercepting all

  • f it methods, and doing nothing else.

It serves as a useful base proxy.

slide-21
SLIDE 21

Another use case for proxies

  • We want to share a reference to an
  • bject, 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-22
SLIDE 22

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

slide-23
SLIDE 23

Aspect-oriented programming (AOP)

  • Some code is not well organized by objects.
  • For the canonical example, logging statements

tend to be littered throughout code.

– If you want to change your logging framework, you have to change massive amounts of code. – In AOP terms, logging is a cross-cutting concern.

  • We can use proxies to build a tracing API and

address this issue.

slide-24
SLIDE 24

Proxies Lab

Modify the forwarding API to log all actions taken on an object. With this proxy, you can debug an application by wrapping an object in a proxy and tracing its behavior.