You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/02/28 23:02:27 UTC

svn commit: r632130 - in /incubator/shindig/trunk/features: core.io/io.js core/json.js rpc/rpc.js

Author: etnu
Date: Thu Feb 28 14:02:25 2008
New Revision: 632130

URL: http://svn.apache.org/viewvc?rev=632130&view=rev
Log:
Updated json.js to be based on json2.js rather than the older version. This gives huge performance improvements in IE and Safari.
Updated io.js to use eval on the initial response from the proxy (the proxy always ensures that this output is valid JSON). We aren't yet validating the BODY of the response, though, so JSON request types will still need to use json.parse().
Fixed a bug in rpc.js that was causing setRelayUrl to not work correctly.

Modified:
    incubator/shindig/trunk/features/core.io/io.js
    incubator/shindig/trunk/features/core/json.js
    incubator/shindig/trunk/features/rpc/rpc.js

Modified: incubator/shindig/trunk/features/core.io/io.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/core.io/io.js?rev=632130&r1=632129&r2=632130&view=diff
==============================================================================
--- incubator/shindig/trunk/features/core.io/io.js (original)
+++ incubator/shindig/trunk/features/core.io/io.js Thu Feb 28 14:02:25 2008
@@ -76,7 +76,9 @@
     //    to begin with, and we can solve this problem by using post requests
     //    and / or passing the url in the http headers.
     txt = txt.substr(UNPARSEABLE_CRUFT.length);
-    var data = gadgets.json.parse(txt);
+    // We are using eval directly here because the outer response comes from a
+    // trusted source, and json parsing is slow in IE.
+    var data = eval("(" + txt + ")");
     data = data[url];
     var resp = {
      text: data.body,

Modified: incubator/shindig/trunk/features/core/json.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/core/json.js?rev=632130&r1=632129&r2=632130&view=diff
==============================================================================
--- incubator/shindig/trunk/features/core/json.js (original)
+++ incubator/shindig/trunk/features/core/json.js Thu Feb 28 14:02:25 2008
@@ -39,134 +39,136 @@
  */
 
 /**
- * @scope gadgets.json
+ * Port of the public domain JSON library by Douglas Crockford.
+ * See: http://www.json.org/json2.js
  */
 gadgets.json = function () {
-    var m = {
-            '\b': '\\b',
-            '\t': '\\t',
-            '\n': '\\n',
-            '\f': '\\f',
-            '\r': '\\r',
-            '"' : '\\"',
-            '\\': '\\\\'
-        },
-        s = {
-            'boolean': function (x) {
-                return String(x);
-            },
-           /** @private */
-            number: function (x) {
-                return isFinite(x) ? String(x) : 'null';
-            },
-            /** @private */
-            string: function (x) {
-                if (/["\\\x00-\x1f]/.test(x)) {
-                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
-                        var c = m[b];
-                        if (c) {
-                            return c;
-                        }
-                        c = b.charCodeAt();
-                        return '\\u00' +
-                            Math.floor(c / 16).toString(16) +
-                            (c % 16).toString(16);
-                    });
-                }
-                return '"' + x + '"';
-            },
-            /** @private */
-            object: function (x) {
-                if (x) {
-                    var a = [], b, f, i, l, v;
-                    if (x instanceof Array) {
-                        a[0] = '[';
-                        l = x.length;
-                        for (i = 0; i < l; i += 1) {
-                            v = x[i];
-                            f = s[typeof v];
-                            if (f) {
-                                v = f(v);
-                                if (typeof v == 'string') {
-                                    if (b) {
-                                        a[a.length] = ',';
-                                    }
-                                    a[a.length] = v;
-                                    b = true;
-                                }
-                            }
-                        }
-                        a[a.length] = ']';
-                    } else if (typeof x.hasOwnProperty === 'function') {
-                        a[0] = '{';
-                        for (i in x) {
-                            if (x.hasOwnProperty(i)) {
-                                v = x[i];
-                                f = s[typeof v];
-                                if (f) {
-                                    v = f(v);
-                                    if (typeof v == 'string') {
-                                        if (b) {
-                                            a[a.length] = ',';
-                                        }
-                                        a.push(s.string(i), ':', v);
-                                        b = true;
-                                    }
-                                }
-                            }
-                        }
-                        a[a.length] = '}';
-                    } else {
-                        return;
-                    }
-                    return a.join('');
-                }
-                return 'null';
-            }
-        };
-    return {
-        copyright: '(c)2005 JSON.org',
-        license: 'http://www.JSON.org/license.html',
-
-        /**
-         * Converts a JavaScript value to a JSON string.
-         *
-         * @param {Object} v The object to convert
-         * @return {String} The JSON equivalent
-	 *
-         * @member gadgets.json
-         */
-        stringify: function (v) {
-            var f = s[typeof v];
-            if (f) {
-                v = f(v);
-                if (typeof v == 'string') {
-                    return v;
-                }
-            }
-            return null;
-        },
 
-        /**
-         * Parses a JSON string, producing a JavaScript value.
-         *
-         * @param {String} text The string to transform into an object &mdash;
-	 *     usually the result of a previous stringify call
-         * @return {Object} The object parsed from the passed in text; false if
-         *     an error occurred
-	 *
-         * @member gadgets.json
-         */
-        parse: function (text) {
-            try {
-                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
-                        text.replace(/("(\\.|[^"\\])*")|('(\\.|[^'\\])*')/g, ''))) &&
-                    eval('(' + text + ')');
-            } catch (e) {
-                return false;
+  /**
+   * Formats integers to 2 digits.
+   * @param {Number} n
+   */
+  function f(n) {
+    return n < 10 ? '0' + n : n;
+  }
+
+  Date.prototype.toJSON = function () {
+    return [this.getUTCFullYear(), '-',
+           f(this.getUTCMonth() + 1), '-',
+           f(this.getUTCDate()), 'T',
+           f(this.getUTCHours()), ':',
+           f(this.getUTCMinutes()), ':',
+           f(this.getUTCSeconds()), 'Z'].join("");
+  };
+
+  // table of character substitutions
+  var m = {
+    '\b': '\\b',
+    '\t': '\\t',
+    '\n': '\\n',
+    '\f': '\\f',
+    '\r': '\\r',
+    '"' : '\\"',
+    '\\': '\\\\'
+  };
+
+  /**
+   * Converts a json object into a string.
+   */
+  function stringify(value) {
+    var a,          // The array holding the partial texts.
+        i,          // The loop counter.
+        k,          // The member key.
+        l,          // Length.
+        r = /["\\\x00-\x1f\x7f-\x9f]/g,
+        v;          // The member value.
+
+    switch (typeof value) {
+    case 'string':
+    // If the string contains no control characters, no quote characters, and no
+    // backslash characters, then we can safely slap some quotes around it.
+    // Otherwise we must also replace the offending characters with safe ones.
+      return r.test(value) ?
+          '"' + value.replace(r, function (a) {
+            var c = m[a];
+            if (c) {
+              return c;
             }
+            c = a.charCodeAt();
+            return '\\u00' + Math.floor(c / 16).toString(16) +
+                (c % 16).toString(16);
+            }) + '"'
+          : '"' + value + '"';
+    case 'number':
+    // JSON numbers must be finite. Encode non-finite numbers as null.
+      return isFinite(value) ? String(value) : 'null';
+    case 'boolean':
+    case 'null':
+      return String(value);
+    case 'object':
+    // Due to a specification blunder in ECMAScript,
+    // typeof null is 'object', so watch out for that case.
+      if (!value) {
+        return 'null';
+      }
+    // If the object has a toJSON method, call it, and stringify the result.
+      if (typeof value.toJSON === 'function') {
+        return stringify(value.toJSON());
+      }
+      a = [];
+      if (typeof value.length === 'number' &&
+          !(value.propertyIsEnumerable('length'))) {
+        // The object is an array. Stringify every element. Use null as a
+        // placeholder for non-JSON values.
+        l = value.length;
+        for (i = 0; i < l; i += 1) {
+          a.push(stringify(value[i], whitelist) || 'null');
         }
-    };
+        // Join all of the elements together and wrap them in brackets.
+        return '[' + a.join(',') + ']';
+      }
+      // Otherwise, iterate through all of the keys in the object.
+      for (k in value) if (value.hasOwnProperty(k)) {
+        if (typeof k === 'string') {
+          v = stringify(value[k]);
+          if (v) {
+            a.push(stringify(k) + ':' + v);
+          }
+        }
+      }
+    }
+    // Join all of the member texts together and wrap them in braces.
+    return '{' + a.join(',') + '}';
+  }
+
+
+  return {
+    stringify: stringify,
+    parse: function (text) {
+// Parsing happens in three stages. In the first stage, we run the text against
+// regular expressions that look for non-JSON patterns. We are especially
+// concerned with '()' and 'new' because they can cause invocation, and '='
+// because it can cause mutation. But just to be safe, we want to reject all
+// unexpected forms.
+
+// We split the first stage into 4 regexp operations in order to work around
+// crippling inefficiencies in IE's and Safari's regexp engines. First we
+// replace all backslash pairs with '@' (a non-JSON character). Second, we
+// replace all simple value tokens with ']' characters. Third, we delete all
+// open brackets that follow a colon or comma or that begin the text. Finally,
+// we look to see that the remaining characters are only whitespace or ']' or
+// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
+
+      if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
+          replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
+          replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
+        return eval('(' + text + ')');
+      }
+      // If the text is not JSON parseable, then a SyntaxError is thrown.
+
+      throw new Error('parseJSON');
+    }
+  };
 }();
 
 var JSON = gadgets.json;

Modified: incubator/shindig/trunk/features/rpc/rpc.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/rpc/rpc.js?rev=632130&r1=632129&r2=632130&view=diff
==============================================================================
--- incubator/shindig/trunk/features/rpc/rpc.js (original)
+++ incubator/shindig/trunk/features/rpc/rpc.js Thu Feb 28 14:02:25 2008
@@ -288,14 +288,14 @@
     /**
      * Sets the relay URL of a target frame.
      * @param {String} targetId Name of the target frame.
-     * @param {String} relayUrl Full relay URL of the target frame.
+     * @param {String} url Full relay URL of the target frame.
      * @param {Boolean} opt_useLegacy True if this relay needs the legacy IFPC
      *     wire format.
      *
      * @member gadgets.rpc
      */
-    setRelayUrl: function(targetId, relayUrl, opt_useLegacy) {
-      relayUrl[targetId] = relayUrl;
+    setRelayUrl: function(targetId, url, opt_useLegacy) {
+      relayUrl[targetId] = url;
       useLegacyProtocol[targetId] = !!opt_useLegacy;
     },