Title: [103627] trunk
Revision
103627
Author
apav...@chromium.org
Date
2011-12-23 07:40:47 -0800 (Fri, 23 Dec 2011)

Log Message

Web Inspector: Implement a worker for parsing out _javascript_ function data
https://bugs.webkit.org/show_bug.cgi?id=75166

Reviewed by Pavel Feldman.

Source/WebCore:

Test: inspector/debugger/script-extract-outline.html

* inspector/front-end/ScriptFormatter.js:
(WebInspector.ScriptFormatter.prototype.formatContent):
* inspector/front-end/ScriptFormatterWorker.js:
(onmessage):
(format):
(getChunkCount):
():
(Array.prototype.keySet):

LayoutTests:

* inspector/debugger/script-extract-outline-expected.txt: Added.
* inspector/debugger/script-extract-outline.html: Added.
* inspector/debugger/script-formatter.html:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (103626 => 103627)


--- trunk/LayoutTests/ChangeLog	2011-12-23 15:38:43 UTC (rev 103626)
+++ trunk/LayoutTests/ChangeLog	2011-12-23 15:40:47 UTC (rev 103627)
@@ -1,3 +1,14 @@
+2011-12-23  Alexander Pavlov  <apav...@chromium.org>
+
+        Web Inspector: Implement a worker for parsing out _javascript_ function data
+        https://bugs.webkit.org/show_bug.cgi?id=75166
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/debugger/script-extract-outline-expected.txt: Added.
+        * inspector/debugger/script-extract-outline.html: Added.
+        * inspector/debugger/script-formatter.html:
+
 2011-12-23  Ilya Tikhonovsky  <loi...@chromium.org>
 
         Unreviewed, rolling out r103624.

Added: trunk/LayoutTests/inspector/debugger/script-extract-outline-expected.txt (0 => 103627)


--- trunk/LayoutTests/inspector/debugger/script-extract-outline-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/debugger/script-extract-outline-expected.txt	2011-12-23 15:40:47 UTC (rev 103627)
@@ -0,0 +1,4 @@
+Tests the script outline extraction functionality.
+
+{"chunk":[{"line":0,"name":"first","arguments":"(x, y)"},{"line":1,"name":"second","arguments":"(y, z)"},{"line":4,"name":"Object","arguments":"(arg)"},{"line":6,"name":"functionOnPrototype","arguments":"(a, b, c, d, e, f)"},{"line":8,"name":"innerFunction1","arguments":"()"},{"line":9,"name":"innerFunction2","arguments":"(arg1, arg2)"}],"id":1,"total":1,"index":1}
+
Property changes on: trunk/LayoutTests/inspector/debugger/script-extract-outline-expected.txt
___________________________________________________________________

Added: svn:eol-style

Added: trunk/LayoutTests/inspector/debugger/script-extract-outline.html (0 => 103627)


--- trunk/LayoutTests/inspector/debugger/script-extract-outline.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/debugger/script-extract-outline.html	2011-12-23 15:40:47 UTC (rev 103627)
@@ -0,0 +1,37 @@
+<html>
+<head>
+<script src=""
+<script src=""
+
+<script>
+
+var test = function()
+{
+    var worker = new Worker("ScriptFormatterWorker.js");
+    var parsedScript = "function first(x,y) { }\n var second = function(y \n , \nz) { }\n Object = function(arg) {\n}\n Object.prototype.functionOnPrototype = function  (  a,  b  ,  \n  c,  d   ,  \n  e  ,  f )    { function innerFunction1() {\n var innerFunction2 = function(arg1,arg2) {} } }";
+
+    worker._onmessage_ = InspectorTest.safeWrap(function(event)
+    {
+        InspectorTest.addResult(JSON.stringify(event.data));
+        InspectorTest.completeTest();
+    });
+
+    worker._onerror_ = function(event)
+    {
+        InspectorTest.addResult("Error in worker: " + event.data);
+        InspectorTest.completeTest();
+    };
+
+    worker.postMessage({ method: "outline", params: { content: parsedScript, id: 1 } });
+}
+
+</script>
+
+</head>
+
+<body _onload_="runTest()">
+<p>Tests the script outline extraction functionality.
+</p>
+
+</body>
+</html>
Property changes on: trunk/LayoutTests/inspector/debugger/script-extract-outline.html
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/LayoutTests/inspector/debugger/script-formatter.html (103626 => 103627)


--- trunk/LayoutTests/inspector/debugger/script-formatter.html	2011-12-23 15:38:43 UTC (rev 103626)
+++ trunk/LayoutTests/inspector/debugger/script-formatter.html	2011-12-23 15:40:47 UTC (rev 103627)
@@ -26,7 +26,7 @@
                 next();
             };
 
-            worker.postMessage({ mimeType: "text/_javascript_", content: "var x=0" });
+            worker.postMessage({ method: "format", params: { mimeType: "text/_javascript_", content: "var x=0" } });
         },
 
         function testSourceMapping(next)
@@ -89,7 +89,7 @@
             };
 
             var content = "<html><body><script>function f(){}<" + "/script><script>function g(){var a;if (a) return;}<" + "/script></body></html>";
-            worker.postMessage({ mimeType: "text/html", content: content, indentString: "**" });
+            worker.postMessage({ method: "format", params: { mimeType: "text/html", content: content, indentString: "**" } });
         }
     ]);
 }

Modified: trunk/Source/WebCore/ChangeLog (103626 => 103627)


--- trunk/Source/WebCore/ChangeLog	2011-12-23 15:38:43 UTC (rev 103626)
+++ trunk/Source/WebCore/ChangeLog	2011-12-23 15:40:47 UTC (rev 103627)
@@ -1,3 +1,21 @@
+2011-12-23  Alexander Pavlov  <apav...@chromium.org>
+
+        Web Inspector: Implement a worker for parsing out _javascript_ function data
+        https://bugs.webkit.org/show_bug.cgi?id=75166
+
+        Reviewed by Pavel Feldman.
+
+        Test: inspector/debugger/script-extract-outline.html
+
+        * inspector/front-end/ScriptFormatter.js:
+        (WebInspector.ScriptFormatter.prototype.formatContent):
+        * inspector/front-end/ScriptFormatterWorker.js:
+        (onmessage):
+        (format):
+        (getChunkCount):
+        ():
+        (Array.prototype.keySet):
+
 2011-12-23  Ilya Tikhonovsky  <loi...@chromium.org>
 
         Unreviewed, rolling out r103624.

Modified: trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js (103626 => 103627)


--- trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js	2011-12-23 15:38:43 UTC (rev 103626)
+++ trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js	2011-12-23 15:40:47 UTC (rev 103627)
@@ -57,9 +57,10 @@
     formatContent: function(mimeType, content, callback)
     {
         content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
-        var data = { mimeType: mimeType, content: content, indentString: "    "  };
-        this._tasks.push({ data: data, callback: callback });
-        this._worker.postMessage(data);
+        const method = "format";
+        var parameters = { mimeType: mimeType, content: content, indentString: "    "  };
+        this._tasks.push({ data: parameters, callback: callback });
+        this._worker.postMessage({ method: method, params: parameters });
     },
 
     _didFormatContent: function(event)

Modified: trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js (103626 => 103627)


--- trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js	2011-12-23 15:38:43 UTC (rev 103626)
+++ trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js	2011-12-23 15:40:47 UTC (rev 103627)
@@ -29,20 +29,116 @@
  */
 
 _onmessage_ = function(event) {
+    if (!event.data.method)
+        return;
+
+    self[event.data.method](event.data.params);
+};
+
+function format(params)
+{
     // Default to a 4-space indent.
-    var indentString = event.data.indentString || "    ";
+    var indentString = params.indentString || "    ";
     var result = {};
 
-    if (event.data.mimeType === "text/html") {
+    if (params.mimeType === "text/html") {
         var formatter = new HTMLScriptFormatter(indentString);
-        result = formatter.format(event.data.content);
+        result = formatter.format(params.content);
     } else {
         result.mapping = { original: [0], formatted: [0] };
-        result.content = formatScript(event.data.content, result.mapping, 0, 0, indentString);
+        result.content = formatScript(params.content, result.mapping, 0, 0, indentString);
     }
     postMessage(result);
-};
+}
 
+function getChunkCount(totalLength, chunkSize)
+{
+    if (totalLength <= chunkSize)
+        return 1;
+
+    var remainder = totalLength % chunkSize;
+    var partialLength = totalLength - remainder;
+    return (partialLength / chunkSize) + (remainder ? 1 : 0);
+}
+
+function outline(params)
+{
+    const chunkSize = 100000; // characters per data chunk
+    const totalLength = params.content.length;
+    const lines = params.content.split("\n");
+    const chunkCount = getChunkCount(totalLength, chunkSize);
+    var outlineChunk = [];
+    var previousIdentifier = null;
+    var previousToken = null;
+    var previousTokenType = null;
+    var currentChunk = 1;
+    var processedChunkCharacters = 0;
+    var addedFunction = false;
+    var isReadingArguments = false;
+    var argumentsText = "";
+    var currentFunction = null;
+    var scriptTokenizer = new WebInspector.SourceJavaScriptTokenizer();
+    scriptTokenizer.condition = scriptTokenizer.createInitialCondition();
+
+    for (var i = 0; i < lines.length; ++i) {
+        var line = lines[i];
+        var column = 0;
+        scriptTokenizer.line = line;
+        do {
+            var newColumn = scriptTokenizer.nextToken(column);
+            var tokenType = scriptTokenizer.tokenType;
+            var tokenValue = line.substring(column, newColumn);
+            if (tokenType === "_javascript_-ident") {
+                previousIdentifier = tokenValue;
+                if (tokenValue && previousToken === "function") {
+                    // A named function: "function f...".
+                    currentFunction = { line: i, name: tokenValue };
+                    addedFunction = true;
+                    previousIdentifier = null;
+                }
+            } else if (tokenType === "_javascript_-keyword") {
+                if (tokenValue === "function") {
+                    if (previousIdentifier && (previousToken === "=" || previousToken === ":")) {
+                        // Anonymous function assigned to an identifier: "...f = function..."
+                        // or "funcName: function...".
+                        currentFunction = { line: i, name: previousIdentifier };
+                        addedFunction = true;
+                        previousIdentifier = null;
+                    }
+                }
+            } else if (tokenValue === "." && previousTokenType === "_javascript_-ident")
+                previousIdentifier += ".";
+            else if (tokenValue === "(" && addedFunction)
+                isReadingArguments = true;
+            if (isReadingArguments && tokenValue)
+                argumentsText += tokenValue;
+
+            if (tokenValue === ")" && isReadingArguments) {
+                addedFunction = false;
+                isReadingArguments = false;
+                currentFunction.arguments = argumentsText.replace(/,[\r\n\s]*/g, ", ").replace(/([^,])[\r\n\s]+/g, "$1");
+                argumentsText = "";
+                outlineChunk.push(currentFunction);
+            }
+
+            if (tokenValue.trim().length) {
+                // Skip whitespace tokens.
+                previousToken = tokenValue;
+                previousTokenType = tokenType;
+            }
+            processedChunkCharacters += newColumn - column;
+            column = newColumn;
+
+            if (processedChunkCharacters >= chunkSize) {
+                postMessage({ chunk: outlineChunk, id: params.id, total: chunkCount, index: currentChunk++ });
+                outlineChunk = [];
+                processedChunkCharacters = 0;
+            }
+        } while (column < line.length);
+    }
+    postMessage({ chunk: outlineChunk, id: params.id, total: chunkCount, index: chunkCount });
+}
+
 function formatScript(content, mapping, offset, formattedOffset, indentString)
 {
     var formattedContent;
@@ -59,8 +155,18 @@
 }
 
 WebInspector = {};
+
+Array.prototype.keySet = function()
+{
+    var keys = {};
+    for (var i = 0; i < this.length; ++i)
+        keys[this[i]] = true;
+    return keys;
+};
+
 importScripts("SourceTokenizer.js");
 importScripts("SourceHTMLTokenizer.js");
+importScripts("SourceJavaScriptTokenizer.js");
 
 HTMLScriptFormatter = function(indentString)
 {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to