Revision: 3848
Author: ihab.awad
Date: Tue Nov 17 11:07:08 2009
Log: Update debug format to JSON and add hooks for Closure Inspector.
http://codereview.appspot.com/152129

- Update debug data in the module format to be correct JSON.

- Add hooks in cajita.js to support debugging using the Closure
Inspector, which is soon to release support for Caja. Please see:

http://code.google.com/closure/compiler/docs/inspector.html

[email protected]

http://code.google.com/p/google-caja/source/detail?r=3848

Modified:
 /trunk/src/com/google/caja/cajita.js
 /trunk/src/com/google/caja/render/SourceSpansRenderer.java
 /trunk/tests/com/google/caja/parser/quasiliteral/testModule.out.js
 /trunk/tests/com/google/caja/render/ssp-golden.txt

=======================================
--- /trunk/src/com/google/caja/cajita.js        Fri Nov 13 13:37:15 2009
+++ /trunk/src/com/google/caja/cajita.js        Tue Nov 17 11:07:08 2009
@@ -3230,6 +3230,13 @@
handle: markFuncFreeze(function handleOnly(newModule){ return newModule; })
   });

+  function registerClosureInspector(module) {
+    if (this && this.CLOSURE_INSPECTOR___
+        && this.CLOSURE_INSPECTOR___.supportsCajaDebugging) {
+      this.CLOSURE_INSPECTOR___.registerCajaModule(module);
+    }
+  }
+
   /**
    * Makes and returns a fresh "normal" module handler whose imports
    * are initialized to a copy of the sharedImports.
@@ -3293,6 +3300,7 @@
        * the same manner.
        */
       handle: markFuncFreeze(function handle(newModule) {
+        registerClosureInspector(newModule);
         lastOutcome = void 0;
         try {
           var result = newModule.instantiate(___, imports);
@@ -3364,6 +3372,7 @@
    * Produces a function module given an object literal module
    */
   function prepareModule(module, load) {
+    registerClosureInspector(module);
     function theModule(imports) {
// The supplied 'imports' contain arguments supplied by the caller of the // module. We need to add the primordials (Array, Object, ...) to these
=======================================
--- /trunk/src/com/google/caja/render/SourceSpansRenderer.java Mon May 4 15:13:42 2009 +++ /trunk/src/com/google/caja/render/SourceSpansRenderer.java Tue Nov 17 11:07:08 2009
@@ -19,6 +19,8 @@
 import com.google.caja.lexer.TokenConsumer;
 import com.google.caja.reporting.MessageContext;
 import com.google.caja.util.Callback;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;

 import java.io.IOException;
 import java.text.MessageFormat;
@@ -183,13 +185,10 @@
       linePositionIndicesByLine.add(new ArrayList<Integer>());
inputSourcesByLine.add(new TreeSet<InputSource>(INPUT_SOURCE_COMPARATOR));

-      FilePosition lastPos = null;
-      FilePosition currentPos;
-
for (int charIdx = 0; charIdx < allPositionsByLine.get(lineIdx).size();
            charIdx++) {

-        currentPos = FilePosition.startOf(
+        FilePosition currentPos = FilePosition.startOf(
             allPositionsByLine.get(lineIdx).get(charIdx));

         inputSourcesByLine.get(lineIdx).add(currentPos.source());
@@ -197,11 +196,6 @@
         if (!currentPos.source().equals(InputSource.UNKNOWN)) {
           mc.addInputSource(currentPos.source());
         }
-
-        if (currentPos.equals(lastPos) && charIdx != 0) {
-          linePositionIndicesByLine.get(lineIdx).add(-1);
-          continue;
-        }

         Integer tableIndex = tableIndexByFilePosition.get(currentPos);
         if (tableIndex == null) {
@@ -211,8 +205,6 @@
         }

         linePositionIndicesByLine.get(lineIdx).add(tableIndex);
-
-        lastPos = currentPos;
       }
     }

@@ -226,48 +218,59 @@
       List<List<Integer>> linePositionIndicesByLine,
       List<Set<InputSource>> inputSourcesByLine,
       List<FilePosition> filePositionTable) {
-    sourceLocationMap.add(MessageFormat.format(
-        "/** Begin line maps. **/'{' file: {0}, count: {1} '}'",
-        renderInputSource(mc, cajoledOutputFilename),
-        linePositionIndicesByLine.size()));
+    {
+      JSONObject header = new JSONObject();
+      jsonObjectPut(header,
+          "file", renderInputSource(mc, cajoledOutputFilename));
+      jsonObjectPut(header,
+          "count", linePositionIndicesByLine.size());
+      sourceLocationMap.add(
+          "/** Begin line maps. **/" + header.toJSONString());
+    }

     for (int i = 0; i < linePositionIndicesByLine.size(); i++) {
-      StringBuilder sb = new StringBuilder();
-      sb.append("[");
+      JSONArray line = new JSONArray();
       for (int j = 0; j < linePositionIndicesByLine.get(i).size(); j++) {
-        if (j != 0) { sb.append(","); }
-        int idx = linePositionIndicesByLine.get(i).get(j);
-        if (idx >= 0) { sb.append(idx); }
-      }
-      sb.append("]");
-      sourceLocationMap.add(sb.toString());
+          jsonArrayAdd(line, linePositionIndicesByLine.get(i).get(j));
+      }
+      sourceLocationMap.add(line.toJSONString());
     }

     sourceLocationMap.add("/** Begin file information. **/");

     for (int i = 0; i < inputSourcesByLine.size(); i++) {
-      StringBuilder sb = new StringBuilder();
-      sb.append("[");
-      boolean first = true;
+      JSONArray line = new JSONArray();
       for (InputSource p : inputSourcesByLine.get(i)) {
-        if (first) { first = false; }
-        else { sb.append(","); }
-        sb.append(renderInputSource(mc, p));
-      }
-      sb.append("]");
-      sourceLocationMap.add(sb.toString());
+        jsonArrayAdd(line, renderInputSource(mc, p));
+      }
+      sourceLocationMap.add(line.toJSONString());
     }

     sourceLocationMap.add("/** Begin mapping definitions. **/");

     for (int i = 0; i < filePositionTable.size(); i++) {
-      sourceLocationMap.add(MessageFormat.format(
-          "[{0},{1},{2}]",
-          renderInputSource(mc, filePositionTable.get(i).source()),
-          filePositionTable.get(i).startLineNo(),
-          filePositionTable.get(i).startCharInLine()));
+      JSONArray line = new JSONArray();
+      jsonArrayAdd(line,
+          renderInputSource(mc, filePositionTable.get(i).source()));
+      jsonArrayAdd(line,
+          filePositionTable.get(i).startLineNo());
+      jsonArrayAdd(line,
+          filePositionTable.get(i).startCharInLine());
+      sourceLocationMap.add(line.toJSONString());
     }
   }
+
+  // Use instead of JSONObject.put to avoid unchecked warnings.
+  @SuppressWarnings("unchecked")
+ private static void jsonObjectPut(JSONObject o, Object key, Object value) {
+    o.put(key, value);
+  }
+
+  // Use instead of JSONArray.add to avoid unchecked warnings.
+  @SuppressWarnings("unchecked")
+  private static void jsonArrayAdd(JSONArray a, Object value) {
+    a.add(value);
+  }

   /**
    * Return the rendered text of the input program.
@@ -317,7 +320,6 @@

private static String renderInputSource(MessageContext mc, InputSource is) {
     return InputSource.UNKNOWN.equals(is)
-        ? "(void 0)"
-        : "'" + mc.abbreviate(is) + "'";
+        ? null : mc.abbreviate(is);
   }
 }
=======================================
--- /trunk/tests/com/google/caja/parser/quasiliteral/testModule.out.js Fri Nov 13 13:37:15 2009 +++ /trunk/tests/com/google/caja/parser/quasiliteral/testModule.out.js Tue Nov 17 11:07:08 2009
@@ -15,15 +15,17 @@
 'sourceLocationMap': {
   'type': 'content',
   'content': [
- '/** Begin line maps. **/{ file: \'file:///CAJOLED-OUTPUT\', count: 7 }',
-    '[0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]', '[0,,,,,,,,,,,,,,,,,,,,,]',
-    '[0,,,,,,,,,,,,,,,,,,]', '[0,,,,,,,,,,,,,,,,,,1,,,]', '[2,,3]',
-    '[0,,,,,,,,,,,,,,,,,,,,,,,]', '[0]', '/** Begin file information. **/',
- '[(void 0)]', '[(void 0)]', '[(void 0)]', '[\'testModule.js\',(void 0)]',
-    '[\'testModule.js\']', '[(void 0)]', '[(void 0)]',
-    '/** Begin mapping definitions. **/', '[(void 0),0,0]',
-    '[\'testModule.js\',1,1]', '[\'testModule.js\',1,5]',
-    '[\'testModule.js\',1,6]' ]
+ '/** Begin line maps. **/{\"count\":7,\"file\":\"file:\\/\\/\\/CAJOLED-OUTPUT\"}',
+    '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]',
+    '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]',
+    '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]',
+    '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1]', '[2,2,3]',
+    '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]', '[0]',
+    '/** Begin file information. **/', '[null]', '[null]', '[null]',
+    '[\"testModule.js\",null]', '[\"testModule.js\"]', '[null]', '[null]',
+    '/** Begin mapping definitions. **/', '[null,0,0]',
+    '[\"testModule.js\",1,1]', '[\"testModule.js\",1,5]',
+    '[\"testModule.js\",1,6]' ]
 },
 'originalSource': {
   'testModule.js': {
=======================================
--- /trunk/tests/com/google/caja/render/ssp-golden.txt Wed Sep 9 13:59:29 2009 +++ /trunk/tests/com/google/caja/render/ssp-golden.txt Tue Nov 17 11:07:08 2009
@@ -1,17 +1,17 @@
  xyz.invokeMeth(function (funcArgXyz) {
     1 + 1;
   })
-/** Begin line maps. **/{ file: 'file://foo/bar.js', count: 3 }
-[0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]
-[1,,,,,2,,3,,4]
-[0,,,]
+/** Begin line maps. **/{"count":3,"file":"file:\/\/foo\/bar.js"}
+[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
+[1,1,1,1,1,2,2,3,3,4]
+[0,0,0,0]
 /** Begin file information. **/
-[(void 0)]
-['ssp-test-input.js']
-[(void 0)]
+[null]
+["ssp-test-input.js"]
+[null]
 /** Begin mapping definitions. **/
-[(void 0),0,0]
-['ssp-test-input.js',1,1]
-['ssp-test-input.js',1,4]
-['ssp-test-input.js',1,6]
-['ssp-test-input.js',1,8]
+[null,0,0]
+["ssp-test-input.js",1,1]
+["ssp-test-input.js",1,4]
+["ssp-test-input.js",1,6]
+["ssp-test-input.js",1,8]

Reply via email to