WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Authoring Languages and Web Authoring Software 4.01 Examine web page development and design.
GATEKEEPER MOSTLY STATIC ENFORCEMENT OF SECURITY AND RELIABILITY PROPERTIES FOR JAVASCRIPT CODE Salvatore Guarnieri & Benjamin Livshits Presented by Michael.
The Web Warrior Guide to Web Design Technologies
Detecting Conflicts Among Declarative UI Extensions Benjamin Lerner Brown University Dan Grossman UW Computer Science & Engineering.
Copyright 2007, Information Builders. Slide 1 Implementing On-Click Functionality With the HTML Layout Painter Larry J Braun Education Specialist June,
FULL DISCLOSURE: ADVICE FOR COMPOSING JAVASCRIPT Benjamin Lerner Affiliates Day 2009.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
JQuery CS 268. What is jQuery? From their web site:
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
Javascript and the Web Whys and Hows of Javascript.
4.1 JavaScript Introduction
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
Chapter 19: Adding JavaScript
Unobtrusive JavaScript
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Lesson 19: Site Development with FrontPage 2003 – Advanced Features.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
JavaScript Syntax, how to use it in a HTML document
Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
SUPPORTING DYNAMIC, THIRD-PARTY CODE CUSTOMIZATIONS IN JAVASCRIPT USING ASPECTS Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington,
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
Introduction to JavaScript Fort Collins, CO Copyright © XTR Systems, LLC Introduction to JavaScript Programming Instructor: Joseph DiVerdi, Ph.D., MBA.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
USING JAVASCRIPT TO SHOW AN ALERT Web Design Sec 6-2 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Understanding JavaScript and Coding Essentials Lesson 8.
Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
JavaScript Invented 1995 Steve, Tony & Sharon. A Scripting Language (A scripting language is a lightweight programming language that supports the writing.
Module 1 Introduction to JavaScript
Build in Objects In JavaScript, almost "everything" is an object.
Using JavaScript to Show an Alert
Presented by Brian Nicholson
JavaScript.
14 A Brief Look at JavaScript and jQuery.
JavaScript an introduction.
DHTML Javascript Internet Technology.
DHTML Javascript Internet Technology.
Functions, Regular expressions and Events
JavaScript What is JavaScript? What can JavaScript do?
Web Design and Development
JavaScript What is JavaScript? What can JavaScript do?
JavaScript CS 4640 Programming Languages for Web Applications
Introduction to JavaScript
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research

How do web pages run?  Web pages =  HTML (structure)  CSS (style)  JS (behavior)  Extensions =  New JS inserted into the page... function msg() { return "Salutations"; } body.onclick = "alert(msg());";... function msg() { return "hi"; }

Outline  Motivation  Userscripts  Browser extensions  Techniques and semantic flaws  Wrapping  Monkey-patching  Language approach: weaving mechanism  Functions

Outline  Motivation  Userscripts  Browser extensions  Techniques and semantic flaws  Wrapping  Monkey-patching  Language approach: weaving mechanism  Functions

Motivation: Userscripts  Lightweight extensions to individual web pages  Add or change features of the site in ways the site designer never anticipated

Key features of Userscripts  Execution  Userscripts are appended to the page  Once added, they behave identically to page scripts  Popularity  60K scripts  10M+ users

Motivation: Web-browser Extensions  Downloadable code that customizes a browser

How do these extensions work?  Can’t only append new code to the page  It won’t get called  Need to replace existing code too  Only two techniques available within JS:  Wrapping  Monkey patching

Wrapping  “This function doesn’t quite do what I want; let me replace it”  How? function P(iframe, data) { if (data[0] == "mb") data[1] = format(data[1]);... } function P(iframe, data) {... } var oldP = window.P; window.P = function(iframe, data) { if (data[0] == "mb") data[1] = format(data[1]); return oldP.apply(iframe, arguments); }

eval("foo = " + foo.toString().replace("some code", "modified code")); Monkey patching  “This function doesn’t quite do what I want; let me tweak it” A closure A closure’s toString () returns its source code String-level search & replace Create a new closure and bind to existing name

Monkey patching “idioms” eval("XULBrowserWindow.setOverLink = " + XULBrowserWindow.setOverLink.toString().replace(/{/, "$& link = Fission.setOverLink(link);")); function XULBrowserWindow.setOverLink(link) { link = Fission.setOverLink(link);... } Idiom: the first { is always the start of the function Idiom: $& inserts whatever was matched What is link ? When does this code run? function XULBrowserWindow.setOverLink(link) {... }

Drawbacks of these approaches  Incorrect for aliases  All other aliases are unmodified function foo(x) { return x*x; } var bar = foo; eval("foo = " + foo.toString().replace("x*x", "42")); > foo(5) == bar(5); > false function foo(x) { return x*x; } var bar = foo; eval("foo = " + foo.toString().replace("x*x", "42")); > foo(5) == bar(5); > false

So, don’t alias functions…?  Function aliases are everywhere in web JS code  Installing event handlers creates aliases  Needs a solution that works with existing web code function onLoad(evt) { window.alert("hello"); } window.addEventListener("load", onLoad,...); eval("onLoad = " + onLoad.toString.replace('hello', 'hi there')); >...loading the page... > Alert: “hello” function onLoad(evt) { window.alert("hello"); } window.addEventListener("load", onLoad,...); eval("onLoad = " + onLoad.toString.replace('hello', 'hi there')); >...loading the page... > Alert: “hello”

Drawbacks of these approaches  Incorrect for closures  They are new closures that have the wrong environment function makeAdder(x) { return function(y){ return x+y; }; } var addFive = makeAdder(5); eval("addFive = " + addFive.toString.replace('y', 'z')); > addFive(3) > error: ‘x’ is undefined function makeAdder(x) { return function(y){ return x+y; }; } var addFive = makeAdder(5); eval("addFive = " + addFive.toString.replace('y', 'z')); > addFive(3) > error: ‘x’ is undefined

Outline  Motivation  Userscripts  Browser extensions  Techniques and semantic flaws  Wrapping  Monkey-patching  Language approach: weaving mechanism  Functions

Goal: combine extensions & mainline  Extensions need to:  Define what new code to run  When it needs to run  How it interacts with existing code  Sounds a lot like dynamic aspect weaving!  …Unless you’d rather we not call it “aspects”  These aren’t traditional “cross-cutting concerns”  We use the same mechanism, not the same motivation

at pointcut(callee(square)) before (x) { print("x is ", x); } at pointcut(callee(square)) before (x) { print("x is ", x); } Aspects print("x is ", x); Advice callee(square) Pointcut (x) Arguments to function before Type of advice  Aspects = Advice + Pointcuts  Advice defines what new code to run  Pointcuts define when to trigger it

Key features of our aspects at pointcut(callee(square)) before (x) { print("x is ", x); } at pointcut(callee(square)) before (x) { print("x is ", x); } square envcode _ + advice print("x is ", x); callee(square) aliasToSquare This cannot be done in JS

Kinds of aspects  Function advice:  Before, around, after calls to functions  Before, around, after bodies of functions  Field advice:  Around getting, setting fields  Statement advice:  Before, after, around statements within functions  …others?

Implementation: function advice  Targeting a JIT compiler  Key idea: weaving = inlining advice + invalidating JITed closure  Inlining advice:  Avoids function-call overhead  Ensures advice has access to local variables  Invalidating JITed closure:  Ensures next calls to function get the advice  Amortizes weaving cost across all calls to function

Conclusions  Extensions have strange behavior  Existing techniques within JS are inadequate  But we can’t simply outlaw all extensions  Introduced dynamic aspect weaving as new JS language feature  Provides cleaner semantics  Provides better performance  Provides sufficient expressive power for real extensions  Win-win!