letconst_global_shadowing.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. function print(x) { WScript.Echo(x + ''); }
  6. function filter(p) { return /^[a-z]$/.test(p); }
  7. function printfilteredprops(o) { printprops(o, filter); }
  8. function printprops(o, filter) {
  9. var s = "{",
  10. prependComma = false;
  11. for (var p in o)
  12. {
  13. if (!filter || filter(p))
  14. {
  15. if (prependComma) {
  16. s += "\n";
  17. } else {
  18. s += "\n";
  19. prependComma = true;
  20. }
  21. s += " " + p + ": " + o[p];
  22. }
  23. }
  24. if (prependComma) {
  25. s += "\n}";
  26. } else {
  27. s += " }";
  28. }
  29. print(s);
  30. }
  31. // ====================================================================================================================
  32. //
  33. print('\n==== Let/const globals should not show up in for-in enumeration ====\n');
  34. // Let/const globals should not show up in list of global
  35. // properties in for-in enumeration. They should also not
  36. // cause global properties to no longer show up in
  37. // enumeration (Blue bug 217657)
  38. print('\nBefore x, y, z, w declarations and globals\n');
  39. try { print(x); } catch (e) { print(e); }
  40. try { print(y); } catch (e) { print(e); }
  41. try { print(z); } catch (e) { print(e); }
  42. try { print(w); } catch (e) { print(e); }
  43. print(this.x);
  44. print(this.y);
  45. print(this.z);
  46. print(this.w);
  47. printfilteredprops(this);
  48. let x = "let x";
  49. this.y = "this.y";
  50. const z = "const z";
  51. this.w = "this.w";
  52. print('\nAfter let x, this.y, const z, this.w\n');
  53. try { print(x); } catch (e) { print(e); }
  54. try { print(y); } catch (e) { print(e); }
  55. try { print(z); } catch (e) { print(e); }
  56. try { print(w); } catch (e) { print(e); }
  57. print(this.x);
  58. print(this.y);
  59. print(this.z);
  60. print(this.w);
  61. printfilteredprops(this);
  62. this.x = "this.x";
  63. let y = "let y";
  64. this.z = "this.z";
  65. const w = "const w";
  66. print('\nAfter this.x, let y, this.z, const w\n');
  67. try { print(x); } catch (e) { print(e); }
  68. try { print(y); } catch (e) { print(e); }
  69. try { print(z); } catch (e) { print(e); }
  70. try { print(w); } catch (e) { print(e); }
  71. print(this.x);
  72. print(this.y);
  73. print(this.z);
  74. print(this.w);
  75. printfilteredprops(this);
  76. // ====================================================================================================================
  77. //
  78. print('\n==== Attributes on global properties should not be lost with let/const shadowing ====\n');
  79. Object.defineProperty(this, "p", { configurable: false, enumerable: false, writable: false, value: 'this.p' });
  80. try { print(p); } catch (e) { print(e); }
  81. print(this.p);
  82. printprops(Object.getOwnPropertyDescriptor(this, "p"));
  83. let p = 'let p';
  84. try { print(p); } catch (e) { print(e); }
  85. print(this.p);
  86. printprops(Object.getOwnPropertyDescriptor(this, "p"));
  87. // ====================================================================================================================
  88. //
  89. print('\n==== Global properties added after const should not destroy const-ness ====\n');
  90. const q = 'const q';
  91. this.q = 'this.q';
  92. try { eval("q = 'assigned to const??';"); } catch (e) { print(e); }
  93. print(q);
  94. print(this.q);
  95. // ====================================================================================================================
  96. //
  97. print('\n==== Attributes on shadowing let properties should not be lost with Object.defineProperty() ====\n');
  98. let r=0;
  99. print(r);
  100. print(this.r);
  101. r=1;
  102. print(r);
  103. print(this.r);
  104. Object.defineProperty(this, "r", {} );
  105. print(r);
  106. print(this.r);
  107. r=2; // bug289741 assertion failure at this point
  108. print(r);
  109. print(this.r);
  110. // test against bug 929017
  111. Object.defineProperty(this, "s", {} );
  112. let s=0;
  113. s=3; // bug 929017:assertion:cacheoperators.cpp:info->IsWritable()
  114. print(s);
  115. print(this.s);