Land 598061(Process //@ scriptURL=url comment for scripts that don't have name set)
authoryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Fri, 12 Feb 2010 17:17:13 +0000 (17:17 +0000)
committeryurys@chromium.org <yurys@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Fri, 12 Feb 2010 17:17:13 +0000 (17:17 +0000)
Review URL: http://codereview.chromium.org/596088

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3850 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/mirror-delay.js
test/mjsunit/debug-compile-event.js

index 1487ce5..e1bedfd 100644 (file)
@@ -1733,7 +1733,8 @@ ScriptMirror.prototype.value = function() {
 
 
 ScriptMirror.prototype.name = function() {
-  return this.script_.name;
+  // If we have name, we trust it more than sourceURL from comments
+  return this.script_.name || this.sourceUrlFromComment_();
 };
 
 
@@ -1829,6 +1830,29 @@ ScriptMirror.prototype.toText = function() {
 
 
 /**
+ * Returns a suggested script URL from comments in script code (if found), 
+ * undefined otherwise. Used primarily by debuggers for identifying eval()'ed
+ * scripts. See 
+ * http://fbug.googlecode.com/svn/branches/firebug1.1/docs/ReleaseNotes_1.1.txt
+ * for details.
+ * 
+ * @return {?string} value for //@ sourceURL comment
+ */
+ScriptMirror.prototype.sourceUrlFromComment_ = function() {
+  if (!('sourceUrl_' in this) && this.source()) {
+    // TODO(608): the spaces in a regexp below had to be escaped as \040 
+    // because this file is being processed by js2c whose handling of spaces
+    // in regexps is broken.
+    // We're not using \s here to prevent \n from matching.
+    var sourceUrlPattern = /\/\/@[\040\t]sourceURL=[\040\t]*(\S+)[\040\t]*$/m;
+    var match = sourceUrlPattern.exec(this.source());
+    this.sourceUrl_ = match ? match[1] : undefined;
+  }
+  return this.sourceUrl_;
+};
+
+
+/**
  * Mirror object for context.
  * @param {Object} data The context data
  * @constructor
index 071183b..e7ecf47 100644 (file)
@@ -90,6 +90,11 @@ function listener(event, exec_state, event_data, data) {
       var json = event_data.toJSONProtocol();
       var msg = eval('(' + json + ')');
       assertTrue('context' in msg.body.script);
+
+      // Check that we pick script name from //@ sourceURL, iff present
+      assertEquals(current_source.indexOf('sourceURL') >= 0 ? 
+                     'myscript.js' : undefined,
+                   event_data.script().name());
     }
   } catch (e) {
     exception = e
@@ -109,6 +114,7 @@ compileSource('eval("eval(\'(function(){return a;})\')")');
 source_count += 2;  // Using eval causes additional compilation event.
 compileSource('JSON.parse(\'{"a":1,"b":2}\')');
 source_count++;  // Using JSON.parse causes additional compilation event.
+compileSource('x=1; //@ sourceURL=myscript.js');
 
 // Make sure that the debug event listener was invoked.
 assertFalse(exception, "exception in listener")