stringify-replacer.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var o = new Array();
  6. var a = new Object();
  7. // Generate getter that will return a constructed string
  8. function propString(i)
  9. {
  10. return function() { var ret = "a" + i; return ret; };
  11. }
  12. function init(o, a)
  13. {
  14. for (var i = 0; i < 21; i++)
  15. {
  16. // Create a replacer array that doesn't hold the string reference by using a getter to create
  17. // the string.
  18. Object.defineProperty(o, i, { get: propString(i) } );
  19. // Initialize the object to be stringify
  20. a["a" + i] = i;
  21. }
  22. }
  23. init(o,a);
  24. WScript.Echo(JSON.stringify(a,o));
  25. // Bug 30349 - invalid replacer array element after valid element causes crash regardless of input
  26. WScript.Echo(JSON.stringify(true, [new Number(1.5), true])); // Original repro
  27. WScript.Echo(JSON.stringify(false, [new Number(1.5), true]));
  28. WScript.Echo(JSON.stringify(null, [new Number(1.5), true]));
  29. // Valid input should just ignore any bad replacer array elements
  30. WScript.Echo(JSON.stringify(a, [false, "a0", true, "a10", false]));