1 /* See license.txt for terms of usage */
  2 
  3 // ********************************************************************************************* //
  4 // Module
  5 
  6 var EXPORTED_SYMBOLS = ["CompilationUnit"];
  7 
  8 define([], function(){
  9 
 10 // ********************************************************************************************* //
 11 // Compilation Unit
 12 
 13 /**
 14  * Describes a compilation unit in a browser context. A compilation unit
 15  * may originate from a JavaScript source file or a script element in HTML.
 16  *
 17  * @constructor
 18  * @param url compilation unit URL - a {@link String} or <code>null</code> if none
 19  * @param context the {@link BrowserContext} this compilation unit is contained in
 20  * @type CompilationUnit
 21  * @return a new CompilationUnit
 22  * @version 1.0
 23  */
 24 function CompilationUnit(url, context)
 25 {
 26     this.url = url;
 27     this.context = context;
 28     this.breakpoints = [];
 29     this.numberOfLines = 0;
 30     this.kind = CompilationUnit.SCRIPT_TAG;
 31 
 32     // Compatibility with SourceLink. There are places where 'href' field is expected.
 33     // xxxHonza: should be investigated in 1.9
 34     this.href = url;
 35 }
 36 
 37 /**
 38  * Kinds of Compilation Units
 39  */
 40 CompilationUnit.SCRIPT_TAG = "script_tag";
 41 CompilationUnit.EVAL = "eval";
 42 CompilationUnit.BROWSER_GENERATED = "event";
 43 
 44 // ********************************************************************************************* //
 45 // API
 46 
 47 /**
 48  * Returns the Kind of Compilation Unit
 49  * <p>
 50  * This function does not require communication with
 51  * the browser.
 52  * </p>
 53  */
 54 CompilationUnit.prototype.getKind = function getKind()
 55 {
 56     return this.kind;
 57 }
 58 
 59 CompilationUnit.prototype.isExecutableLine = function isExecutableLine(lineNo)
 60 {
 61     // TODO no sourceFiles!
 62     return this.sourceFile.isExecutableLine(lineNo);
 63 }
 64 
 65 /**
 66  * Returns the URL of this compilation unit.
 67  * <p>
 68  * This function does not require communication with
 69  * the browser.
 70  * </p>
 71  *
 72  * @function
 73  * @returns compilation unit identifier as a {@link String}
 74  */
 75 CompilationUnit.prototype.getURL = function()
 76 {
 77     return this.url;
 78 };
 79 
 80 /**
 81  * Returns the browser context this compilation unit was compiled in.
 82  * <p>
 83  * This function does not require communication with
 84  * the browser.
 85  * </p>
 86  *
 87  * @function
 88  * @returns a {@link BrowserContext}
 89  */
 90 CompilationUnit.prototype.getBrowserContext = function()
 91 {
 92     return this.context;
 93 };
 94 
 95 /**
 96  * Returns the breakpoints that have been created in this compilation unit and
 97  * have not been cleared.
 98  * <p>
 99  * This function does not require communication with
100  * the browser.
101  * </p>
102  * @function
103  * @returns an array of {@link Breakpoint}'s
104  */
105 CompilationUnit.prototype.getBreakpoints = function()
106 {
107     // Return a copy of the breakpoints, so the master copy is not corrupted.
108     var bps = [];
109     for ( var i = 0; i < this.breakpoints.length; i++)
110         bps.push(this.breakpoints[i]);
111     return bps;
112 };
113 
114 CompilationUnit.prototype.eachBreakpoint = function( fnOfLineProps )
115 {
116      Firebug.Debugger.fbs.enumerateBreakpoints(this.getURL(), { call:
117          function(url, line, props, scripts)
118          {
119               fnOfLineProps(line, props);
120          }
121      });
122 };
123 
124 /**
125  * Requests the source of this compilation unit asynchronously. Source will be
126  * retrieved from the browser and reported back to the listener function when available.
127  * The handler may be called before or after this function returns.
128  * <p>
129  * TODO: what if the compilation unit no longer exists in the browser
130  * </p>
131  * @function
132  * @param firstLineNumber requested line number starting point; < 1 means from lowest line number
133  * @param lastLineNumber request last line number; < 1 means up to maximum line
134  * @param listener a listener (function) that accepts (compilationUnit, firstLineNumber,
135  *      lastLineNumber, array of source code lines)
136  */
137 CompilationUnit.prototype.getSourceLines = function(firstLine, lastLine, listener)
138 {
139     // xxxHonza: Do not cache the source lines in the compilation unit.
140     // The Script panel doesn't display the whole script if it's downloaded
141     // partially and the following caching happens sooner.
142     // Or tabCache.storeSplitLines should trigger an update.
143     //if (!this.lines)
144 
145     // TODO remove - a comment from xxxJJB.
146     this.lines = this.sourceFile.loadScriptLines(this.context);
147 
148     this.numberOfLines = (this.lines ? this.lines.length : 0);
149     listener(this, 1, this.numberOfLines, this.lines);
150 };
151 
152 /**
153  * Request the current estimated number of source lines in the entire compilationUnit
154  */
155 CompilationUnit.prototype.getNumberOfLines = function()
156 {
157     return this.numberOfLines;
158 };
159 
160 /**
161  * Requests to create a breakpoint in this compilation unit asynchronously. A breakpoint
162  * creation request will be sent to the browser and an <code>onToggleBreakpoint</code>
163  * event will be sent by the browser when the breakpoint is installed.
164  * <p>
165  * <ul>
166  * <li>TODO: onToggleBreakpoint event is not spec'd - is this the intended use?</li>
167  * <li>TODO: line number out of range</li>
168  * <li>TODO: compilation unit no longer exists in the browser</li>
169  * <li>TODO: breakpoint already set</li>
170  * <li>TODO: is line number 0 or 1 based</li>
171  * </ul>
172  * </p>
173  * @function
174  * @param lineNumber the source line number in this compilation unit to set the breakpoint on
175  * @return the {@link Breakpoint} that was created
176  */
177 
178 // ********************************************************************************************* //
179 // Private
180 
181 /**
182  * Adds the specified breakpoint to this compilation unit's collection of breakpoints.
183  * Implementation should call this method when a breakpoint is created in a compilation
184  * unit.
185  *
186  * @param breakpoint the breakpoint that was created
187  * @function
188  */
189 CompilationUnit.prototype._addBreakpoint = function(breakpoint)
190 {
191     this.breakpoints.push(breakpoint);
192 };
193 
194 /**
195  * Removes the specified breakpoint from this compilation unit's collection of breakpoints.
196  * Implementation should call this method when a breakpoint is cleared from a compilation
197  * unit.
198  *
199  * @param breakpoint the breakpoint that was removed
200  * @function
201  */
202 CompilationUnit.prototype._removeBreakpoint = function(breakpoint)
203 {
204     for ( var i = 0; i < this.breakpoints.length; i++)
205     {
206         if (this.breakpoints[i] === breakpoint)
207         {
208             this.breakpoints.splice(i, 1);
209             return;
210         }
211     }
212 };
213 
214 // ********************************************************************************************* //
215 // CommonJS
216 
217 exports = CompilationUnit;
218 return CompilationUnit;
219 
220 // ********************************************************************************************* //
221 });
222