Browse Source

Improving handling of internal properties in JSON.stringify

Jimmy Thomson 8 years ago
parent
commit
a88e1ac271
3 changed files with 15 additions and 21 deletions
  1. 1 1
      lib/Runtime/Library/JSON.cpp
  2. 0 20
      test/JSON/stringifyobjects.js
  3. 14 0
      test/JSON/toJSON.js

+ 1 - 1
lib/Runtime/Library/JSON.cpp

@@ -738,7 +738,7 @@ namespace JSON
                             for (uint32 i = 0; i < propertyCount; i++)
                             {
                                 id = typeHandler->GetPropertyId(scriptContext, (Js::PropertyId)i);
-                                if (id == Js::Constants::NoProperty)
+                                if (id == Js::Constants::NoProperty || id < Js::InternalPropertyIds::Count)
                                 {
                                     continue;
                                 }

+ 0 - 20
test/JSON/stringifyobjects.js

@@ -1,20 +0,0 @@
-//-------------------------------------------------------------------------------------------------------
-// Copyright (C) Microsoft. All rights reserved.
-// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
-//-------------------------------------------------------------------------------------------------------
-
-if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
-    this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
-}
-
-const tests = [
-    {
-        name: "Stringify error",
-        body: function () {
-            const err = new Error("message");
-            assert.areEqual('{"description":"message","message":"message"}', JSON.stringify(err));
-        }
-    }
-];
-
-testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

+ 14 - 0
test/JSON/toJSON.js

@@ -151,6 +151,20 @@ fnc.prototype.toJSON = function() {
   TEST(JSON.stringify(obj), '{"a":1}');
 }
 
+// test internal properties
+{
+  let obj = {foo: 1};
+  let wm = new WeakMap();
+  // Add internal WeakMapKeyMap property to obj
+  wm.set(obj,obj);
+  TEST('{"foo":1}', JSON.stringify(obj));
+  Object.defineProperty(obj, "getter", {get: function () { return 2}, enumerable: true, configurable: true});
+  TEST('{"foo":1,"getter":2}', JSON.stringify(obj));
+
+  const err = new Error("message");
+  TEST('{}', JSON.stringify(err));
+}
+
 // test - function prototype new instance
 TEST("\"1\"", JSON.stringify(new fnc(1)))