Bug 458959. this.JSON is enumerable. r+sr=mrbkap
authorRobert Sayre <sayrer@gmail.com>
Wed, 08 Oct 2008 19:33:26 -0400
changeset 20182 e18564ab198d0dc150cf904c23d6bcf9235fde5a
parent 20181 dd18104921a94fe82e6d2638980038c7330c86b1
child 20183 e7cad76c39521a71e4f7c575fb8d9552f9123f09
push idunknown
push userunknown
push dateunknown
bugs458959
milestone1.9.1b1pre
Bug 458959. this.JSON is enumerable. r+sr=mrbkap
browser/components/sessionstore/src/nsSessionStore.js
content/events/test/test_bug350471.xul
js/src/json.cpp
js/src/xpconnect/loader/JSON.jsm
js/src/xpconnect/tests/unit/test_json.js
toolkit/components/search/nsSearchSuggestions.js
--- a/browser/components/sessionstore/src/nsSessionStore.js
+++ b/browser/components/sessionstore/src/nsSessionStore.js
@@ -2317,20 +2317,20 @@ SessionStoreService.prototype = {
    *
    * The inverse operation consists of eval("(" + JSON_string + ")");
    * and should be provably safe.
    *
    * @param aJSObject is the object to be converted
    * @return the object's JSON representation
    */
   _toJSONString: function sss_toJSONString(aJSObject) {
-    let str = JSON.toString(aJSObject, ["_tab", "_hosts", "_formDataSaved"] /* keys to drop */);
+    let str = JSONModule.toString(aJSObject, ["_tab", "_hosts", "_formDataSaved"] /* keys to drop */);
     
     // sanity check - so that API consumers can just eval this string
-    if (!JSON.isMostlyHarmless(str))
+    if (!JSONModule.isMostlyHarmless(str))
       throw new Error("JSON conversion failed unexpectedly!");
     
     return str;
   },
 
 /* ........ Storage API .............. */
 
   /**
--- a/content/events/test/test_bug350471.xul
+++ b/content/events/test/test_bug350471.xul
@@ -110,17 +110,17 @@ function testEventDispatching() {
     // Send the event to the documentElement.
     synthesizeMouseScroll(document.documentElement, 10, 10, expectedEvents[0]);
   
     document.removeEventListener("DOMMouseScroll", listener, true);
     document.removeEventListener("MozMousePixelScroll", listener, true);
   
     // expectedEvents should be empty now. If it's not, print errors.
     expectedEvents.forEach(function(e) {
-      ok(false, "Didn't receive expected event: " + JSON.toString(e));
+      ok(false, "Didn't receive expected event: " + JSON.parse(e));
     });
   };
   let i = 0;
   [0, 1, 2].forEach(function(aKind) {
     ["horizontal", "vertical"].forEach(function(aAxis) {
       [false, true].forEach(function(aShift) {
         [false, true].forEach(function(aCtrl) {
           [false, true].forEach(function(aAlt) {
@@ -208,17 +208,16 @@ function testDefaultHandling() {
         });
       });
     });
   });
 }
 
 function runTests() {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-  Components.utils.import("resource://gre/modules/JSON.jsm");
 
   testEventDispatching();
 
   let scrollbox = document.getElementById("scrollbox");
   synthesizeMouse(scrollbox, 10, 10, { type: "mousemove" });
 
   setTimeout(runOtherTests, 1000);
 }
--- a/js/src/json.cpp
+++ b/js/src/json.cpp
@@ -908,16 +908,17 @@ JSObject *
 js_InitJSONClass(JSContext *cx, JSObject *obj)
 {
     JSObject *JSON;
 
     JSON = JS_NewObject(cx, &js_JSONClass, NULL, obj);
     if (!JSON)
         return NULL;
     if (!JS_DefineProperty(cx, obj, js_JSON_str, OBJECT_TO_JSVAL(JSON),
-                           JS_PropertyStub, JS_PropertyStub, JSPROP_ENUMERATE))
+                           JS_PropertyStub, JS_PropertyStub,
+                           JSPROP_READONLY | JSPROP_PERMANENT))
         return NULL;
 
     if (!JS_DefineFunctions(cx, JSON, json_static_methods))
         return NULL;
 
     return JSON;
 }
--- a/js/src/xpconnect/loader/JSON.jsm
+++ b/js/src/xpconnect/loader/JSON.jsm
@@ -39,34 +39,34 @@
  * See http://www.json.org/ for comprehensive information about JSON.
  *
  * Import this module through
  *
  * Components.utils.import("resource://gre/modules/JSON.jsm");
  *
  * Usage:
  *
- * var newJSONString = JSON.toString( GIVEN_JAVASCRIPT_OBJECT );
- * var newJavaScriptObject = JSON.fromString( GIVEN_JSON_STRING );
+ * var newJSONString = JSONModule.toString( GIVEN_JAVASCRIPT_OBJECT );
+ * var newJavaScriptObject = JSONModule.fromString( GIVEN_JSON_STRING );
  *
  * Note: For your own safety, Objects/Arrays returned by
- *       JSON.fromString aren't instanceof Object/Array.
+ *       JSONModule.fromString aren't instanceof Object/Array.
  */
 
-var EXPORTED_SYMBOLS = ["JSON"];
+var EXPORTED_SYMBOLS = ["JSONModule"];
 
 // The following code is a loose adaption of Douglas Crockford's code
 // from http://www.json.org/json.js (public domain'd)
 
 // Notable differences:
 // * Unserializable values such as |undefined| or functions aren't
 //   silently dropped but always lead to a TypeError.
 // * An optional key blacklist has been added to JSON.toString
 
-var JSON = {
+var JSONModule = {
   /**
    * Converts a JavaScript object into a JSON string.
    *
    * @param aJSObject is the object to be converted
    * @param aKeysToDrop is an optional array of keys which will be
    *                    ignored in all objects during the serialization
    * @return the object's JSON representation
    *
--- a/js/src/xpconnect/tests/unit/test_json.js
+++ b/js/src/xpconnect/tests/unit/test_json.js
@@ -32,43 +32,43 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 function run_test() {
   // converts an object to a JSON string and tests its integrity
   function toJSONString(a) {
-    var res = JSON.toString(a);
-    if (!JSON.isMostlyHarmless(res))
+    var res = JSONModule.toString(a);
+    if (!JSONModule.isMostlyHarmless(res))
       throw new SyntaxError("Invalid JSON string: " + res);
     return res;
   }
   
   // ensures that an object can't be converted to a JSON string
   function isInvalidType(a) {
     try {
-      JSON.toString(a);
+      JSONModule.toString(a);
       return false;
     } catch (ex) {
       return ex.name == "TypeError";
     }
   }
   // ensures that a string can't be converted back to a JavaScript object
   function isInvalidSyntax(a) {
     try {
-      JSON.fromString(a);
+      JSONModule.fromString(a);
       return false;
     } catch (ex) {
       return ex.name == "SyntaxError";
     }
   }
   
-  Components.utils.import("resource://gre/modules/JSON.jsm");
-  do_check_eq(typeof(JSON), "object");
+  Components.utils.import("resource://gre/modules/JSONModule.jsm");
+  do_check_eq(typeof(JSONModule), "object");
   
   // some of the tests are adapted from /testing/mochitest/tests/test_Base.js
   do_check_eq(toJSONString(true), "true");
   do_check_eq(toJSONString(false), "false");
   
   do_check_eq(toJSONString(1), "1");
   do_check_eq(toJSONString(1.23), "1.23");
   do_check_eq(toJSONString(1.23e-45), "1.23e-45");
@@ -84,55 +84,55 @@ function run_test() {
   do_check_true(isInvalidType(undefined));
   
   do_check_eq(toJSONString([1, "2", 3.3]), '[1,"2",3.3]');
   // duck-typed Array (since we'll never really get something instanceof Array)
   do_check_eq(toJSONString({ 0: 0, 1: "1", 2: -2.2, length: 3 }), '[0,"1",-2.2]');
   
   var obj = { a: 1, b: "2", c: [-3e+30] };
   do_check_eq(toJSONString(obj), '{"a":1,"b":"2","c":[-3e+30]}');
-  do_check_eq(JSON.toString(obj, ["b", "c"] /* keys to drop */), '{"a":1}');
+  do_check_eq(JSONModule.toString(obj, ["b", "c"] /* keys to drop */), '{"a":1}');
   
   do_check_true(isInvalidType(function() { }));
   
   // make sure that toJSONString actually works...
-  do_check_eq(toJSONString(obj), JSON.toString(obj));
+  do_check_eq(toJSONString(obj), JSONModule.toString(obj));
   
-  do_check_eq(JSON.fromString("true"), true);
-  do_check_eq(JSON.fromString("false"), false);
-  do_check_eq(JSON.fromString("1"), 1);
-  do_check_eq(JSON.fromString('"2.2"'), "2.2");
-  do_check_eq(JSON.fromString("1.23e-45"), 1.23e-45);
+  do_check_eq(JSONModule.fromString("true"), true);
+  do_check_eq(JSONModule.fromString("false"), false);
+  do_check_eq(JSONModule.fromString("1"), 1);
+  do_check_eq(JSONModule.fromString('"2.2"'), "2.2");
+  do_check_eq(JSONModule.fromString("1.23e-45"), 1.23e-45);
   do_check_true(isInvalidSyntax("NaN"));
   
-  do_check_eq(JSON.fromString('"Foo-Bar \\b\\t\\n\\f\\r\\"\\\\ \\u0001\\u20ac"'),
+  do_check_eq(JSONModule.fromString('"Foo-Bar \\b\\t\\n\\f\\r\\"\\\\ \\u0001\\u20ac"'),
                               "Foo-Bar \b\t\n\f\r\"\\ \x01\u20ac");
   do_check_true(isInvalidSyntax('"multi\nline"'));
-  do_check_eq(JSON.fromString("null"), null);
+  do_check_eq(JSONModule.fromString("null"), null);
   do_check_true(isInvalidSyntax("."));
   
-  var res = JSON.fromString('[1,"2",3.3]');
+  var res = JSONModule.fromString('[1,"2",3.3]');
   do_check_eq(res.length, 3);
   do_check_eq(res[2], 3.3);
   // res is an instance of the sandbox's array
   do_check_false(res instanceof Array);
   
-  res = JSON.fromString(toJSONString(obj));
+  res = JSONModule.fromString(toJSONString(obj));
   do_check_eq(res.a, obj.a);
   do_check_eq(res.b, obj.b);
   do_check_eq(res.c.length, obj.c.length);
   do_check_eq(res.c[0], obj.c[0]);
   
-  // those would throw on JSON.fromString if there's no object |a|
-  do_check_true(JSON.isMostlyHarmless("a"));
-  do_check_true(JSON.isMostlyHarmless("a[0]"));
-  do_check_true(JSON.isMostlyHarmless('a["alert(\\"P0wn3d!\\");"]'));
+  // those would throw on JSONModule.fromString if there's no object |a|
+  do_check_true(JSONModule.isMostlyHarmless("a"));
+  do_check_true(JSONModule.isMostlyHarmless("a[0]"));
+  do_check_true(JSONModule.isMostlyHarmless('a["alert(\\"P0wn3d!\\");"]'));
   
-  do_check_false(JSON.isMostlyHarmless('(function() { alert("P0wn3d!"); })()'));
-  do_check_false(JSON.isMostlyHarmless('{ get a() { return "P0wn3d!"; } }'));
+  do_check_false(JSONModule.isMostlyHarmless('(function() { alert("P0wn3d!"); })()'));
+  do_check_false(JSONModule.isMostlyHarmless('{ get a() { return "P0wn3d!"; } }'));
   
   // this string shouldn't cause a "script stack space quota is exhausted" error
   let bigString = " ";
   while (bigString.length < (1 << 22))
     bigString += bigString;
   
-  do_check_eq(JSON.fromString(toJSONString(bigString)), bigString);
+  do_check_eq(JSONModule.fromString(toJSONString(bigString)), bigString);
 }
--- a/toolkit/components/search/nsSearchSuggestions.js
+++ b/toolkit/components/search/nsSearchSuggestions.js
@@ -50,17 +50,16 @@ const Cr = Components.results;
 const Cu = Components.utils;
 
 const HTTP_OK                    = 200;
 const HTTP_INTERNAL_SERVER_ERROR = 500;
 const HTTP_BAD_GATEWAY           = 502;
 const HTTP_SERVICE_UNAVAILABLE   = 503;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/JSON.jsm");
 
 /**
  * SuggestAutoCompleteResult contains the results returned by the Suggest
  * service - it implements nsIAutoCompleteResult and is used by the auto-
  * complete controller to populate the front end.
  * @constructor
  */
 function SuggestAutoCompleteResult(searchString,
@@ -518,17 +517,17 @@ SuggestAutoComplete.prototype = {
     }
 
     var responseText = this._request.responseText;
     if (status != HTTP_OK || responseText == "")
       return;
 
     this._clearServerErrors();
 
-    var serverResults = JSON.fromString(responseText);
+    var serverResults = JSON.parse(responseText);
     var searchString = serverResults[0] || "";
     var results = serverResults[1] || [];
 
     var comments = [];  // "comments" column values for suggestions
     var historyResults = [];
     var historyComments = [];
 
     // If form history is enabled and has results, add them to the list.