enumerable.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 echo = WScript.Echo;
  6. function guarded_call(func) {
  7. try {
  8. func();
  9. } catch (e) {
  10. echo(e.name + " : " + e.message);
  11. }
  12. }
  13. var testCount = 0;
  14. function scenario(title, func) {
  15. if (testCount > 0) {
  16. echo("\n");
  17. }
  18. echo((testCount++) + ".", title);
  19. guarded_call(func);
  20. }
  21. // If needed, echo lines sorted to remove dependency on order.
  22. function echoLines(lines, sort) {
  23. if (sort) {
  24. lines.sort();
  25. }
  26. for(var i = 0; i < lines.length; i++) {
  27. echo(lines[i]);
  28. }
  29. }
  30. // dump own property descriptors of obj
  31. function dumpProp(obj) {
  32. var lines = [];
  33. var names = Object.getOwnPropertyNames(obj);
  34. for (var p in names) {
  35. var name = names[p];
  36. var desc = Object.getOwnPropertyDescriptor(obj, name);
  37. var s = " " + name + ": " + obj[name];
  38. if (desc.enumerable) { s += " enumerable"; }
  39. if (desc.configurable) { s += " configurable"; }
  40. if (desc.writable) { s += " writable"; }
  41. if (desc.getter) { s += " getter"; }
  42. if (desc.setter) { s += " setter"; }
  43. lines.push(s);
  44. }
  45. echoLines(lines);
  46. }
  47. // dump obj through for-in enumerator
  48. function enumObj(obj) {
  49. var lines = [];
  50. for (var p in obj) {
  51. // add property during enumeration
  52. if (!obj[500]) {
  53. obj[500] = "Should not enumerate 500";
  54. obj[600] = "Should not enumerate 600";
  55. Object.defineProperty(obj, "700", {
  56. get: function () { return "Should not enumerate 700"; },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. }
  61. lines.push(" " + p + ": " + obj[p]);
  62. }
  63. echoLines(lines);
  64. }
  65. // add a bunch of data/attribute properties with different attributes
  66. function addProp(o, prefix) {
  67. Object.defineProperty(o, prefix + "10", {
  68. value: "value 10"
  69. });
  70. Object.defineProperty(o, prefix + "11", {
  71. value: "value 11",
  72. enumerable: true
  73. });
  74. Object.defineProperty(o, prefix + "12", {
  75. value: "value 12",
  76. enumerable: true,
  77. configurable: true
  78. });
  79. Object.defineProperty(o, prefix + "13", {
  80. value: "value 13",
  81. enumerable: true,
  82. configurable: true,
  83. writable: true
  84. });
  85. Object.defineProperty(o, prefix + "20", {
  86. get: function() { return "get 20"; },
  87. });
  88. Object.defineProperty(o, prefix + "21", {
  89. get: function () { return "get 21"; },
  90. enumerable: true,
  91. });
  92. Object.defineProperty(o, prefix + "22", {
  93. get: function () { return "get 22"; },
  94. enumerable: true,
  95. configurable: true
  96. });
  97. Object.defineProperty(o, prefix + "25", {
  98. set: function() { echo("do not call 25"); },
  99. });
  100. Object.defineProperty(o, prefix + "26", {
  101. set: function() { echo("do not call 26"); },
  102. enumerable: true,
  103. });
  104. Object.defineProperty(o, prefix + "27", {
  105. set: function() { echo("do not call 27"); },
  106. enumerable: true,
  107. configurable: true
  108. });
  109. }
  110. function testWithObj(o) {
  111. addProp(o, "xx");
  112. addProp(o, "1");
  113. echo(" --- properties ---");
  114. dumpProp(o);
  115. echo(" --- for-in enumerate ---");
  116. enumObj(o);
  117. }
  118. scenario("Test with object", function() {
  119. testWithObj({
  120. abc: -12,
  121. def: "hello",
  122. 1: undefined,
  123. 3: null
  124. });
  125. });
  126. scenario("Test with array", function() {
  127. testWithObj([
  128. -12, "hello", undefined, null
  129. ]);
  130. });
  131. // Test Object.defineProperties, Object.create
  132. function testPrototype(proto) {
  133. Object.defineProperties(proto, {
  134. name: { value: "SHOULD_NOT_enumerate_prototype" },
  135. 0: { get: function() { return "get 0"; } },
  136. 3: { value: 3 },
  137. 1: { get: function() { return "get 1"; }, enumerable: true },
  138. 5: { value: 5, enumerable: true },
  139. 2: { get: function() { return this.name; }, enumerable: true },
  140. });
  141. var o = Object.create(proto, {
  142. name: { value: "correct_original_instance" },
  143. 10: { get: function() { return "get 10"; } },
  144. 13: { value: 13 },
  145. 11: { get: function() { return "get 11"; }, enumerable: true },
  146. 15: { value: 15, enumerable: true },
  147. 12: { get: function() { return this.name; }, enumerable: true },
  148. });
  149. echo("*** Prototype ***");
  150. dumpProp(proto);
  151. echo("*** Object ***");
  152. dumpProp(o);
  153. echo("*** for in ***");
  154. enumObj(o);
  155. }
  156. scenario("Test prototype with object", function() {
  157. testPrototype({});
  158. });
  159. scenario("Test prototype with array", function() {
  160. testPrototype([]);
  161. });
  162. // Test String index property names
  163. function testStr(o) {
  164. // Set 0, 1, 2
  165. guarded_call(function () {
  166. o[0] = "x";
  167. echo(" ", 0, o[0]);
  168. });
  169. guarded_call(function () {
  170. Object.defineProperty(o, 1, { value: "y" });
  171. echo(" ", 1, o[1]);
  172. });
  173. guarded_call(function () {
  174. Object.defineProperty(o, 2, { get: function () { return "z"; } });
  175. echo(" ", 2, o[2]);
  176. });
  177. // Set 6, 7
  178. guarded_call(function () {
  179. o[6] = "6";
  180. echo(" ", 6, o[6]);
  181. });
  182. guarded_call(function () {
  183. Object.defineProperty(o, 7, { get: function () { return "7"; }, enumerable: true });
  184. echo(" ", 7, o[7]);
  185. });
  186. guarded_call(function () {
  187. echo(" --- Properties ---");
  188. dumpProp(o);
  189. });
  190. guarded_call(function () {
  191. echo(" --- Enumerate ---");
  192. enumObj(o);
  193. });
  194. }
  195. scenario("Test String with String value", function() {
  196. testStr("abcd");
  197. });
  198. scenario("Test String with String object", function() {
  199. testStr(new String("abcd"));
  200. });
  201. scenario("Testing forin caching when forin changes from array to Es5array", function() {
  202. var array = [4, 5, 6];
  203. for (var i in array) {
  204. WScript.Echo(i);
  205. }
  206. // Adding a numeric property with attributes should create an ES5 array.
  207. Object.defineProperty(array, "8", { get: function () { return 34; }, enumerable: true });
  208. for (var i in array) {
  209. WScript.Echo(i);
  210. }
  211. }
  212. );
  213. scenario("Testing RegExp Number String Boolean Object Constructor length property attributes", function() {
  214. function printAll()
  215. {
  216. for(var i in arguments[0])
  217. {
  218. WScript.Echo(i + ":" + arguments[0][i]);
  219. }
  220. }
  221. printAll(Object.getOwnPropertyDescriptor(RegExp, "length"));
  222. printAll(Object.getOwnPropertyDescriptor(String, "length"));
  223. printAll(Object.getOwnPropertyDescriptor(Boolean, "length"));
  224. printAll(Object.getOwnPropertyDescriptor(Number, "length"));
  225. printAll(Object.getOwnPropertyDescriptor(Object, "length"));
  226. });