enumerable.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // dump obj through for-in enumerator
  6. function enumObj(obj, lines) {
  7. for (var p in obj) {
  8. lines.push(" " + p + ": " + obj[p]);
  9. }
  10. }
  11. // dump obj through for-in enumerator + verify
  12. function enumAndVerifyObj(obj, lines) {
  13. var ctr = 0;
  14. for (var p in obj) {
  15. var thisl = " " + p + ": " + obj[p];
  16. if(lines[ctr] !== thisl) {
  17. telemetryLog(`Failed on ${lines[ctr]} !== ${thisl}`, true);
  18. }
  19. ctr++;
  20. }
  21. }
  22. // add a bunch of data/attribute properties with different attributes
  23. function addProp(o, prefix) {
  24. Object.defineProperty(o, prefix + "10", {
  25. value: "value 10"
  26. });
  27. Object.defineProperty(o, prefix + "11", {
  28. value: "value 11",
  29. enumerable: true
  30. });
  31. Object.defineProperty(o, prefix + "12", {
  32. value: "value 12",
  33. enumerable: true,
  34. configurable: true
  35. });
  36. Object.defineProperty(o, prefix + "13", {
  37. value: "value 13",
  38. enumerable: true,
  39. configurable: true,
  40. writable: true
  41. });
  42. Object.defineProperty(o, prefix + "20", {
  43. get: function() { return "get 20"; },
  44. });
  45. Object.defineProperty(o, prefix + "21", {
  46. get: function () { return "get 21"; },
  47. enumerable: true,
  48. });
  49. Object.defineProperty(o, prefix + "22", {
  50. get: function () { return "get 22"; },
  51. enumerable: true,
  52. configurable: true
  53. });
  54. Object.defineProperty(o, prefix + "25", {
  55. set: function() { echo("do not call 25"); },
  56. });
  57. Object.defineProperty(o, prefix + "26", {
  58. set: function() { echo("do not call 26"); },
  59. enumerable: true,
  60. });
  61. Object.defineProperty(o, prefix + "27", {
  62. set: function() { echo("do not call 27"); },
  63. enumerable: true,
  64. configurable: true
  65. });
  66. }
  67. function testWithObj(o, lines) {
  68. addProp(o, "xx");
  69. addProp(o, "1");
  70. enumObj(o, lines);
  71. }
  72. var s1 = {abc: -12, def: "hello", 1: undefined, 3: null};
  73. var l1 = [];
  74. testWithObj(s1, l1);
  75. var s2 = [-12, "hello", undefined, null];
  76. var l2 = [];
  77. testWithObj(s2, l2);
  78. // Test Object.defineProperties, Object.create
  79. function testPrototype(proto) {
  80. Object.defineProperties(proto, {
  81. name: { value: "SHOULD_NOT_enumerate_prototype" },
  82. 0: { get: function() { return "get 0"; } },
  83. 3: { value: 3 },
  84. 1: { get: function() { return "get 1"; }, enumerable: true },
  85. 5: { value: 5, enumerable: true },
  86. 2: { get: function() { return this.name; }, enumerable: true },
  87. });
  88. return Object.create(proto, {
  89. name: { value: "correct_original_instance" },
  90. 10: { get: function() { return "get 10"; } },
  91. 13: { value: 13 },
  92. 11: { get: function() { return "get 11"; }, enumerable: true },
  93. 15: { value: 15, enumerable: true },
  94. 12: { get: function() { return this.name; }, enumerable: true },
  95. });
  96. }
  97. var s3 = testPrototype({});
  98. var l3 = [];
  99. testWithObj(s3, l3);
  100. var s4 = testPrototype([]);
  101. var l4 = [];
  102. testWithObj(s4, l4);
  103. WScript.SetTimeout(testFunction, 50);
  104. /////////////////
  105. function testFunction()
  106. {
  107. telemetryLog('Testing obj enumeration', true);
  108. enumAndVerifyObj(s1, l1);
  109. telemetryLog('Testing array enumeration', true);
  110. enumAndVerifyObj(s2, l2);
  111. telemetryLog('Testing obj proto enumeration', true);
  112. enumAndVerifyObj(s3, l3);
  113. telemetryLog('Testing array proto enumeration', true);
  114. enumAndVerifyObj(s4, l4);
  115. emitTTDLog(ttdLogURI);
  116. }