CS252 – Advanced Programming Language Principles Prof. Tom .

2y ago
43 Views
4 Downloads
1,022.60 KB
20 Pages
Last View : 1m ago
Last Download : 2m ago
Upload by : Brady Himes
Transcription

CS252 – Advanced ProgrammingLanguage PrinciplesProf. Tom Austin, Fall 2013Metaprogramming

What is metaprogramming?Writing programsthat manipulateother programs.

JavaScript ProxiesSpecial metaprogramming features are proposedfor ECMAScript 6, code-named ECMAScriptHarmony.Mark MillerProposedBy:Tom VanCutsem

Proxies: Design Principles for RobustObject-oriented Intercession APIsAbstract: Proxies are a powerfulapproach to implement meta-objects inobject-oriented languages withouthaving to resort to metacircularinterpretation. We introduce such ameta-level API based on proxies forJavascript

Metaprogramming terms Reflection– Introspection: examine a program– Self-modification: modify a program Intercession: redefine the semantics ofoperations. Reflection is fairly common. Intercession ismore unusual.

IntrospectionAbility to examine thestructure of a program.In JavaScript:Property lookup"x" in o;for (prop in o){ }Propertyenumeration

Self-modificationThe ability to modify the structure of a program.In JavaScript:o["x"]; // computed propertyo.y 42; // add a new propertydelete o.x; // remove a propertyo["m"].apply(o,[42]);// reflected method call

Until recently, JavaScript didnot support intercession.JavaScript proxies areintended to fix that.But first a little history

Metaobject Protocol History Common Lisp was developed before objectoriented languages were popular. Many libraries were created with non-standardobject-oriented systems. The Common Lisp Object System (CLOS) wasdeveloped, but what could be done about preexisting object-oriented libraries?

The Devil’s Choice1. Rewrite libraries for CLOS?– Huge # of libraries, so notfeasible to rewrite them all.2. Make complex API?– Might makes API difficult tounderstand.– Systems had conflicting features – But were essentially doing the same things.

Gregor Kiczales chose option 3:Keep a Simple API,but modify objectbehavior to fit withdifferent systems.Metaobject protocols were born

Proxy design Proxy: A special object that has special trapsthat define its behavior.– Trap: method that intercepts an operation.– Available /JavaScript/Old Proxy API Handler: The meta-object that specifies thedetails of the trap; the handler itself is usually anormal object.

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.

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

Creating a new proxy objectvar p Proxy.create(incompleteHandler);var q p.hello;Prints "Propertyhello accessed" andreturns 1.p.goodbye "What happens here?";The set trap has notbeen specified, sothis causes an error.

No-op forwarding proxyThis proxy wraps an object, intercepting allof it methods, and doing nothing else. Itserves as a useful base proxy.A simplified version is available athttp://cs.sjsu.edu/ austin/cs252-fall13/lab9/forwardingProxy.js. We'll review in class.

Another use case for proxies We want to share a reference to anobject, but you do not want it to bemodified.– Reference to the DOM, for instance We can modify the forwardinghandler to provide this behavior:

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

Aspect-oriented programming (AOP) Some code is not well organized by objects. For the canonical example, logging statementstend 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 andaddress this issue.

Lab 9 Modify the forwarding API to log all actionstaken on an object. With this proxy, you candebug an application by wrapping an object ina proxy and tracing its behavior. Usual drill: Work by yourself or with a partner. Details in Canvas.

Proxies: Design Principles for Robust Object-oriented Intercession APIs Abstract: Proxies are a powerful approach to implement meta-objects in object-oriented languages without having to resort to metacircular interpretation. We introduce such a meta-level API based on proxies for Javascript

Related Documents:

Programming is the key word here because you make the computer do what you want by programming it. Programming is like putting the soul inside a body. This book intends to teach you the basics of programming using GNU Smalltalk programming language. GNU Smalltalk is an implementation of the Smalltalk-80 programming language and

The C programming language was developed in the early 1980s. C was designed to add object-oriented programming techniques to the C language. Although C originally tended to be associated with systems programming, ithas evolved into a mature programming language that is well-suited for a wide variety of application programming.

The C programming language was developed in the early 1980s. C was designed to add object-oriented programming techniques to the C language. Although C originally tended to be associated with systems programming, it has evolved into a mature programming language that is well-suited for a wide variety of application programming.

which programming language or how to use HALCON with parallel programming. Programming With HALCON/C This part describes the HALCON's language interface to C . Programming With HALCON/.NET This part describes HALCON's language interface to .NET programming languages (C#, Visual Basic .NET, etc.). Programming With HALCON/C

Functional programming languages Early history The rst high-level programming language was Fortran (1957). Fortran is an imperative programming language. The second high-level programming language was Lisp (1958). Designed by people interested in AI ("the science and engineering of making intelligent machines");

Programming paradigms Structured programming: all programs are seen as composed of control structures Object-oriented programming (OOP): Java, C , C#, Python Functional programming: Clojure, Haskell Logic programming based on formal logic: Prolog, Answer set programming (ASP), Datalog

Object Oriented Programming 7 Purpose of the CoursePurpose of the Course To introduce several programming paradigms including Object-Oriented Programming, Generic Programming, Design Patterns To show how to use these programming schemes with the C programming language to build “good” programs.

Pile properties: The pile is modeled with structural beam elements and can be assigned either linear-elastic or elastic-perfectly plastic material properties. Up to ten different pile sections can be included in a single analysis. Soil p-y curves: The soil is modeled as a collection of independent (Winkler) springs. The load-