Script Origin Tracking

From MozillaWiki
Revision as of 07:11, 10 May 2011 by Jimb (talk | contribs) (work in progress; just filed to get it off my laptop)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This draft is being discussed in bug 637572. The interface it describes is not stable, and perhaps not even implemented.

A debugger should help developers find any JavaScript code that has been introduced into a browsing context, regardess of how it got there; allow them to set breakpoints in any code; and be able to explain where any given piece of code came from.

In a browser, JavaScript code can initially enter the system through any number of channels:

  • A script may appear in an HTML <script> element (or be cited by its src attribute).
  • A script may appear in HTML as an event handler content attribute.

Once loaded, JavaScript code can then itself introduce new scripts:

  • It can call eval or the Function constructor.
  • It can assign a new script to a DOM element's event handler IDL attribute.
  • It can use DOM manipulation (assignments to innerHTML, calls to appendChild, and so on) to introduce new <script> elements and event handler content attributes.

Origin Values

... separate into things representing scripts (algebraic) and things representing specific locations in a script (line, [column,] script)

An origin value describes how a particular script was loaded into its browsing context. An origin value has one of the forms below.

{ url:url }
This script was loaded from the resource identified by the absolute URL url. Note that this covers:
  • <script> elements with a src attribute (here, url is that attribute's value);
  • <script> elements with script text in-line; and
  • event handler content attributes (here, url identifies the containing HTML file)
{ eval:function, origin:origin, line:line, column:column }
This script was produced by a call to function at line and column in the script given by origin, which is itself an origin value. Function is a string, naming the function called to evaluate or compile the script; typical values would be:
  • "eval", referring to the global object's eval property;
  • "Function", referring to the Function constructor;
  • "setTimeout", referring to the HTML5 setTimeout function;

and so on.

{ attributeAssignment:attribute, element:element, origin:origin, line:line, column:column }
The assignment at line and column in the script given by origin set the property of the DOM element element named attribute to this script. The value was interpreted as a script either because attribute is an event handler IDL attribute of element (like "onclick" or "onkeydown"), or element is a <script> node and attribute is text or textContent.
{ dynamicMarkup:function, element:element, origin:origin, line:line, column:column }
The call to the dynamic markup insertion function named function at line and column in origin created this script. (This is used for functions that supply the new markup as a string, not for functions that operate on DOM elements as JavaScript objects.) (What are line numbers relative to here?)
{ elementConstructor:constructor, element:element, attribute:attribute, origin:origin, line:line, column:column }
The call to constructor at line and column in origin created the DOM element element whose attribute attribute is this script. Attribute (We do not record the point at which element was inserted in the DOM, only where the element was constructed.)

any origin value can have a source property, too

For example:

 inline script
 inline event handler content attribute


The origin value prototype

The prototype of an origin value holds the following methods:

toString()
Format the origin value as a human-readable string.

The Debug.Script.prototype.origin accessor

The origin property of a Debug.Script is an origin value describing how the given script was loaded into its browsing context.

Open items

  • Do these get serialized by XDR? Hopefully, yes. DOM elements probably don't need to be preserved there.
  • Need to be able to pass an origin to eval explicitly (and thus, need the prototype to be public)